|
| 1 | +import type {Config} from "./config" |
| 2 | +import type {IFsAdaptor} from "./core/file-system/fs-adaptor" |
| 3 | +import {Input} from "./core/input" |
| 4 | +import type {IFormatter} from "./core/interfaces" |
| 5 | +import {GenericLoader} from "./core/loaders/generic.loader" |
| 6 | +import type {TypespecLoader} from "./core/loaders/typespec.loader" |
| 7 | +import {logger} from "./core/logger" |
| 8 | +import {OpenapiLoader} from "./core/openapi-loader" |
| 9 | +import type {OpenapiValidator} from "./core/openapi-validator" |
| 10 | +import {templates} from "./templates" |
| 11 | +import type {OpenapiGenerator} from "./templates.types" |
| 12 | +import {TypescriptEmitter} from "./typescript/common/typescript-emitter" |
| 13 | + |
| 14 | +function enumExtensibility( |
| 15 | + config: Config, |
| 16 | + generator: OpenapiGenerator, |
| 17 | +): "open" | "closed" { |
| 18 | + if (config.enumExtensibility) { |
| 19 | + return config.enumExtensibility |
| 20 | + } |
| 21 | + |
| 22 | + if (generator.type === "client") { |
| 23 | + return "open" |
| 24 | + } |
| 25 | + |
| 26 | + if (generator.type === "server") { |
| 27 | + return "closed" |
| 28 | + } |
| 29 | + |
| 30 | + throw new Error(`Unsupported generator type '${generator.type}'`) |
| 31 | +} |
| 32 | + |
| 33 | +export async function generate( |
| 34 | + config: Config, |
| 35 | + fsAdaptor: IFsAdaptor, |
| 36 | + formatter: IFormatter, |
| 37 | + validator: OpenapiValidator, |
| 38 | + typespecLoader: TypespecLoader, |
| 39 | +) { |
| 40 | + logger.time("program starting") |
| 41 | + logger.info(`running on input file '${config.input}'`) |
| 42 | + logger.time("load files") |
| 43 | + |
| 44 | + const genericLoader = new GenericLoader( |
| 45 | + fsAdaptor, |
| 46 | + config.remoteSpecRequestHeaders, |
| 47 | + ) |
| 48 | + const loader = await OpenapiLoader.create( |
| 49 | + { |
| 50 | + entryPoint: config.input, |
| 51 | + fileType: config.inputType, |
| 52 | + titleOverride: config.overrideSpecificationTitle, |
| 53 | + }, |
| 54 | + validator, |
| 55 | + genericLoader, |
| 56 | + typespecLoader, |
| 57 | + ) |
| 58 | + |
| 59 | + const generator = templates[config.template] |
| 60 | + |
| 61 | + const input = new Input( |
| 62 | + loader, |
| 63 | + { |
| 64 | + extractInlineSchemas: config.extractInlineSchemas, |
| 65 | + enumExtensibility: enumExtensibility(config, generator), |
| 66 | + }, |
| 67 | + generator.syntheticNameGenerator, |
| 68 | + ) |
| 69 | + |
| 70 | + const emitter = new TypescriptEmitter(fsAdaptor, formatter, { |
| 71 | + destinationDirectory: config.output, |
| 72 | + allowUnusedImports: config.allowUnusedImports, |
| 73 | + }) |
| 74 | + |
| 75 | + if ( |
| 76 | + config.tsIsEsmProject && |
| 77 | + !config.tsCompilerOptions.rewriteRelativeImportExtensions |
| 78 | + ) { |
| 79 | + logger.warn( |
| 80 | + `Generating in ESM mode, but typescript compiler option "rewriteRelativeImportExtensions" is not set to true. This may result in broken imports.`, |
| 81 | + ) |
| 82 | + } |
| 83 | + |
| 84 | + await generator.run({ |
| 85 | + input, |
| 86 | + emitter, |
| 87 | + schemaBuilder: config.schemaBuilder, |
| 88 | + enableRuntimeResponseValidation: config.enableRuntimeResponseValidation, |
| 89 | + enableTypedBasePaths: config.enableTypedBasePaths, |
| 90 | + compilerOptions: config.tsCompilerOptions, |
| 91 | + groupingStrategy: config.groupingStrategy, |
| 92 | + filenameConvention: config.filenameConvention, |
| 93 | + isEsmProject: config.tsIsEsmProject, |
| 94 | + allowAny: config.tsAllowAny, |
| 95 | + serverImplementationMethod: config.tsServerImplementationMethod, |
| 96 | + }) |
| 97 | +} |
0 commit comments