All files / src/generate elastic.ts

60% Statements 9/15
45.45% Branches 5/11
100% Functions 1/1
60% Lines 9/15

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                    1x 2x 2x   2x   2x   2x 1x   1x                 1x  
import { Elastic } from '../DataDriverTypes.js'
import { SearchTypes, HttpMethods } from 'declarapi-runtime'
export type ElasticInputGet = {
  method: 'GET',
  search: SearchTypes
}
export type ElasticInputBase = { method: HttpMethods, search? :SearchTypes}
export type ElasticInputType = ElasticInputBase & (ElasticInputGet | {
  method: 'POST' | 'PUT' | 'PATCH' | 'DELETE',
})
export const elasticCodeGen = (driver: Elastic, input: ElasticInputType):string => {
  const { index } = driver
  switch (input.method) {
    case 'GET': {
      Iif (input.search === 'textSearch') {
        return `(input, auth, contract) => elastic.get("${index}", contract, auth, input && input.id, input && input.search)`
      } else Iif (input.search === 'idOnly') {
        return `(input, auth, contract) => elastic.get("${index}", contract, auth, input && input.id)`
      } else if (input.search === 'full') {
        throw new Error('Parametric get not implemented yet')
      }
      throw new Error(`Unsupported automatic elasticsearch methods: ${JSON.stringify(input.search)}`)
    }
    case 'POST': return `(input, auth, contract) => elastic.post("${index}", contract, auth, input)`
    case 'PATCH': return `(input, auth, contract) => elastic.patch("${index}", contract, auth, input, input.id)`
    case 'PUT': return `(input, auth, contract) => elastic.put("${index}", contract, auth, input, input.id)`
    case 'DELETE': return `(input, auth, contract) => elastic.del("${index}", contract, auth, input.id)`
  }
}
 
export default elasticCodeGen