All files / src/generate kv.ts

13.33% Statements 2/15
0% Branches 0/11
0% Functions 0/1
13.33% Lines 2/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                                       1x  
import { KeyValue } from '../DataDriverTypes.js'
import { SearchTypes, HttpMethods } from 'declarapi-runtime'
export type KVInputGet = {
  method: 'GET',
  search: SearchTypes
}
export type KVInputBase = { method: HttpMethods, search? :SearchTypes}
export type KVInputType = KVInputBase & (KVInputGet | {
  method: 'POST' | 'PUT' | 'PATCH' | 'DELETE',
})
export const kvCodeGen = (driver: KeyValue, input: KVInputType):string => {
  const { index, backend } = driver
  switch (input.method) {
    case 'GET': {
      if (input.search === 'textSearch') {
        return `(input, auth, contract) => kv.get("${backend}", "${index}", contract, auth, input && input.id, input && input.search)`
      } else if (input.search === 'idOnly') {
        return `(input, auth, contract) => kv.get("${backend}", "${index}", contract, auth, input && input.id)`
      } else if (input.search === 'full') {
        throw new Error('Parametric get not implemented yet')
      }
      throw new Error(`Unsupported automatic key-value methods: ${JSON.stringify(input.search)}`)
    }
    case 'POST': return `(input, auth, contract) => kv.post("${backend}", "${index}", contract, auth, input)`
    case 'PATCH': return `(input, auth, contract) => kv.patch("${backend}", "${index}", contract, auth, input, input.id)`
    case 'PUT': return `(input, auth, contract) => kv.put("${backend}", "${index}", contract, auth, input, input.id)`
    case 'DELETE': return `(input, auth, contract) => kv.del("${backend}", "${index}", contract, auth, input.id)`
  }
}
 
export default kvCodeGen