All files / src shaking.ts

55.88% Statements 19/34
30% Branches 3/10
75% Functions 3/4
62.06% Lines 18/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 641x     1x 1x     1x 1x   1x   2x   2x     2x   2x 1x         2x 2x   1x             1x   1x 1x     1x                                            
import * as R from 'ramda'
import { OpenAPIV3 } from 'openapi-types'
import { ShakingFilter, ShakingFilterSync } from './types/shaking-filter'
import { isValidMethod } from './utils/is-valid-method'
import { openapiShakingOrphanedComponents } from './shaking-orphaned-components'
 
 
export async function openapiShaking(document: Readonly<OpenAPIV3.Document>, filter: ShakingFilter): Promise<OpenAPIV3.Document> {
  let doc = document
 
  const pairs = await Promise.all(
    Object.entries(document.paths).map(async ([path, methods]): Promise<[string, unknown] | undefined> => {
      Iif (!methods) return [path, methods]
 
      const pairs = await Promise.all(
        Object.entries(methods || {})
          .map(async ([method, operation]): Promise<[string, unknown] | undefined> => {
            Iif (!isValidMethod(method)) return [method, operation]
 
            if (await filter(path, method, operation as OpenAPIV3.OperationObject)) {
              return [method, operation]
            }
          }),
      )
 
      const pairsWithoutNil = pairs.filter(R.isNotNil) as [string, unknown][]
      if (!pairsWithoutNil.length) return undefined
 
      return [
        path,
        R.fromPairs(pairsWithoutNil) as OpenAPIV3.PathItemObject,
      ]
    }),
  )
 
  const paths = R.fromPairs(pairs.filter(R.isNotNil) as [string, unknown][])
 
  doc = R.assoc('paths', paths, doc)
  return openapiShakingOrphanedComponents(doc)
}
 
export function openapiShakingSync(document: Readonly<OpenAPIV3.Document>, filter: ShakingFilterSync): OpenAPIV3.Document {
  let doc = document
 
  for (const path in document.paths) {
    for (const method in document.paths[path]) {
      Iif (!isValidMethod(method)) continue
 
      const operation = R.path(['paths', path, method], document) as (OpenAPIV3.OperationObject | undefined)
      Iif (!operation) continue
 
      Iif (!filter(path, method, operation)) {
        doc = R.dissocPath(['paths', path, method], doc)
      }
    }
 
    Iif (R.isEmpty(R.path(['paths', path], doc))) {
      doc = R.dissocPath(['paths', path], doc)
    }
  }
 
  return openapiShakingOrphanedComponents(doc)
}