| import fs from 'node:fs' |
| import openapiTS, { astToString } from 'openapi-typescript' |
| import { factory, SyntaxKind } from 'typescript' |
|
|
| const DATE = factory.createTypeReferenceNode(factory.createIdentifier('Date')) |
| const NULL = factory.createLiteralTypeNode(factory.createNull()) |
| const STRING = factory.createKeywordTypeNode(SyntaxKind.StringKeyword) |
|
|
| const schema = new URL('../../../openapi.cloud.yaml', import.meta.url) |
|
|
| const ast = await openapiTS(schema, { |
| defaultNonNullable: false, |
| rootTypes: true, |
| rootTypesNoSchemaPrefix: true, |
| transform(schemaObject, metadata) { |
| if (metadata.path === '#/components/schemas/Event') { |
| if ( |
| schemaObject.type === 'string' && |
| !schemaObject.nullable && |
| !['customer-id', 'com.example.someevent'].includes(schemaObject.example) |
| ) { |
| return { |
| questionToken: true, |
| schema: STRING, |
| } |
| } |
| } |
| if (schemaObject.format === 'date-time') { |
| const allowString = |
| (metadata.schema && |
| 'in' in metadata.schema && |
| metadata.schema.in === 'query') || |
| metadata.path?.includes('/parameters/query') |
|
|
| |
| if (allowString) { |
| return schemaObject.nullable |
| ? factory.createUnionTypeNode([DATE, NULL, STRING]) |
| : factory.createUnionTypeNode([DATE, STRING]) |
| } |
|
|
| return schemaObject.nullable |
| ? factory.createUnionTypeNode([DATE, NULL]) |
| : DATE |
| } |
| }, |
| }) |
|
|
| const contents = astToString(ast) |
|
|
| fs.writeFileSync('./src/client/schemas.ts', contents) |
|
|