All files / src/generate server.ts

41.67% Statements 10/24
17.86% Branches 5/28
40% Functions 2/5
45.45% Lines 10/22

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  1x 1x 1x   1x 2x 2x 2x 2x       2x                                                                                   1x  
import { OutputSuccess } from '../transform/types.js'
import elasticCodeGen, { ElasticInputType } from './elastic.js'
import { name, typeDef } from './common.js'
import { kvCodeGen } from './kv.js'
 
export const server = (contracts: OutputSuccess[]): string => {
  const valueDef = contracts.map(x => {
    let handle = 'undefined'
    Eif (x.preferredImplementation && x.preferredImplementation.type === 'elasticsearch') {
      const codeGenInput:ElasticInputType = x.method === 'GET'
        ? { method: x.method, search: x.search || 'idOnly' }
        : { method: x.method }
 
      handle = elasticCodeGen(x.preferredImplementation, codeGenInput)
    } else if (x.preferredImplementation && x.preferredImplementation.type === 'key-value') {
      const codeGenInput:ElasticInputType = x.method === 'GET'
        ? { method: x.method, search: x.search || 'idOnly' }
        : { method: x.method }
 
      handle = kvCodeGen(x.preferredImplementation, codeGenInput)
    }
 
    return `${name(x)}: {
          name: "${x.name}",
          manageFields: ${JSON.stringify(x.manageFields, undefined, 2)},
          authentication: ${JSON.stringify(x.authentication, undefined, 2)},
          type: "${x.method}",
          handle: ${handle},
          arguments: ${JSON.stringify(x.arguments)} ,
          returns: ${JSON.stringify(x.returns)}}`
  }).join(',\n')
 
  const contractTypeList = contracts.map(x =>
    `${name(x)}: ContractType<${name(x)}Argument, ${name(x)}Returns>`).join('\n')
 
  const elastic = contracts.find(x => x.preferredImplementation?.type === 'elasticsearch')
 
  const kv = contracts.find(x => x.preferredImplementation?.type === 'key-value')
 
  const elasticImport = elastic ? 'import * as elastic from "declarapi-runtime/elastic.js"\n' : ''
  const kvImport = kv ? 'import * as kv from "declarapi-runtime/kv.js"\n' : ''
 
  const result =
  `/**********************************************
   DO NOT EDIT THIS FILE, IT WILL BE OVERRIDDEN
***********************************************/
  import { ContractType } from "declarapi-runtime"
  ${elasticImport}${kvImport}
  ${typeDef(contracts)}
  export type ContractListType = {\n${contractTypeList}\n}\n
  export const contracts: ContractListType = {\n${valueDef}\n}\n`
 
  return result
}
 
export default server