Skip to content

Commit

Permalink
fix(opapi): slight fixes in new client generator (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
franklevasseur committed Apr 18, 2024
1 parent 2437093 commit a6aeeb9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
2 changes: 1 addition & 1 deletion opapi/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bpinternal/opapi",
"version": "0.10.2",
"version": "0.10.5",
"description": "",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down
36 changes: 30 additions & 6 deletions opapi/src/generators/client-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import pathlib from 'path'
import _ from 'lodash'
import { JSONSchema7 } from 'json-schema'
import { Operation, State } from '../state'
import { generateErrors } from '../generators/errors'
import { toRequestShape, toResponseShape } from '../handler-generator/map-operation'

type ObjectBuilder = utils.JsonSchemaBuilder['object']
Expand All @@ -25,10 +24,34 @@ const HEADER = `// this file was automatically generated, do not edit
/* eslint-disable */
`

const toTs = async (schema: JSONSchema7, name: string): Promise<string> => {
const { title, ...rest } = schema
// replace all occurences of { type: T, nullable: true } with { anyOf: [{ type: T }, { type: 'null' }] }
type NullableJsonSchema = JSONSchema7 & { nullable?: boolean }
const fixJsonSchema = (nullableSchema: NullableJsonSchema): JSONSchema7 => {
const { nullable, ...schema } = nullableSchema
if (nullable) {
const { title, description, ...rest } = schema
return { title, description, anyOf: [rest, { type: 'null' }] }
}

if (schema.type === 'object') {
const properties = schema.properties ? _.mapValues(schema.properties, fixJsonSchema) : schema.properties
const additionalProperties =
typeof schema.additionalProperties === 'object'
? fixJsonSchema(schema.additionalProperties)
: schema.additionalProperties
return { ...schema, properties, additionalProperties }
}

return schema
}

const toTs = async (originalSchema: JSONSchema7, name: string): Promise<string> => {
let { title, ...schema } = originalSchema
schema = fixJsonSchema(schema as NullableJsonSchema)

type jsonSchemaToTsInput = Parameters<typeof compile>[0]
const typeCode = await compile(rest as jsonSchemaToTsInput, name, { unknownAny: false, bannerComment: '' })
const typeCode = await compile(schema as jsonSchemaToTsInput, name, { unknownAny: false, bannerComment: '' })

return `${typeCode}\n`
}

Expand Down Expand Up @@ -95,7 +118,8 @@ export const generateOperations = async (state: State<string, string, string>, o
requestCode += await toTs(query, queryName)
requestCode += await toTs(params, paramsName)
if (reqBody) {
requestCode += await toTs(reqBody, reqBodyName)
const tsCode = await toTs(reqBody, reqBodyName)
requestCode += tsCode
} else {
requestCode += `export interface ${reqBodyName} {}\n\n`
}
Expand All @@ -110,7 +134,7 @@ export const generateOperations = async (state: State<string, string, string>, o
].join('\n')

const toObject = (keys: string[]) => '{ ' + keys.map((k) => `${k}: input.${k}`).join(', ') + ' }'
const path = op.path.replace(/{([^}]+)}/g, (_, p) => `\${input.${p}}`)
const path = op.path.replace(/{([^}]+)}/g, (_, p) => `\${encodeURIComponent(input.${p})}`)

const allParams = [...headerKeys, ...queryKeys, ...paramsKeys, ...reqBodyKeys]

Expand Down

0 comments on commit a6aeeb9

Please sign in to comment.