UNPKG

@quinck/fastify-openapi-typescript-generator

Version:

Contains utilities to generate fastify types from openapi definition for the fastify framework.

23 lines (22 loc) 968 B
import openapiTS from 'openapi-typescript'; import { disableLinter } from '../utils/consts.js'; const schemaStartPath = '#/components/schemas/'; export const generateOpenapiTypes = (openapiFilePath, options) => { const localPath = new URL(openapiFilePath, import.meta.url); const { externalTypesImportFrom } = options ?? {}; const openAPITSOptions = { inject: disableLinter, }; if (externalTypesImportFrom) { const externalTypesImport = 'openapiTypes'; openAPITSOptions.transform = (_, options) => { if (options.path.startsWith(schemaStartPath)) { const typeName = options.path.replace(schemaStartPath, ''); return `${externalTypesImport}.${typeName}`; } return undefined; }; openAPITSOptions.inject += `\nimport * as ${externalTypesImport} from "${externalTypesImportFrom}";\n`; } return openapiTS(localPath, openAPITSOptions); };