/
index.ts
142 lines (129 loc) · 3.89 KB
/
index.ts
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env node
import "source-map-support/register"
import path from "path"
import {Command, Option} from "@commander-js/extra-typings"
import {Input, OperationGroupStrategy} from "./core/input"
import {loadTsConfigCompilerOptions} from "./core/loaders/tsconfig.loader"
import {logger} from "./core/logger"
import {OpenapiLoader} from "./core/openapi-loader"
import {OpenapiValidator} from "./core/openapi-validator"
import {templates} from "./templates"
const program = new Command()
.addOption(
new Option("-i --input <value>", "input file to generate from")
.env("OPENAPI_INPUT")
.makeOptionMandatory(),
)
.addOption(
new Option(
"--input-type <value>",
"type of input file. this can be openapi3 or typespec",
)
.env("OPENAPI_INPUT_TYPE")
.choices(["openapi3", "typespec"] as const)
.default("openapi3" as const)
.makeOptionMandatory(),
)
.addOption(
new Option("-o --output <value>", "directory to output generated code")
.env("OPENAPI_OUTPUT")
.makeOptionMandatory(),
)
.addOption(
new Option("-t --template <value>", "template to use")
.env("OPENAPI_TEMPLATE")
.choices([
"typescript-koa",
"typescript-axios",
"typescript-fetch",
"typescript-angular",
] as const satisfies Readonly<Array<keyof typeof templates>>)
.makeOptionMandatory(),
)
.addOption(
new Option(
"-s --schema-builder <value>",
"runtime schema parsing library to use",
)
.env("OPENAPI_SCHEMA_BUILDER")
.choices(["zod", "joi"] as const)
.default("zod" as const)
.makeOptionMandatory(),
)
.addOption(
new Option(
"--enable-runtime-response-validation",
"(experimental) whether to validate response bodies using the chosen runtime schema library",
)
.env("OPENAPI_ENABLE_RUNTIME_RESPONSE_VALIDATION")
.default(false),
)
.addOption(
new Option(
"--extract-inline-schemas",
"(experimental) Generate names and extract types/schemas for inline schemas",
)
.env("OPENAPI_EXTRACT_INLINE_SCHEMAS")
.default(false),
)
.addOption(
new Option(
"--allow-unused-imports",
"Keep unused imports. Especially useful if there is a bug in the unused-import elimination.",
)
.env("OPENAPI_ALLOW_UNUSED_IMPORTS")
.default(false),
)
.addOption(
new Option(
"--grouping-strategy <value>",
"(experimental) Strategy to use for splitting output into separate files. Set to none for a single generated.ts",
)
.env("OPENAPI_GROUPING_STRATEGY")
.choices([
"none",
"first-tag",
"first-slug",
] as const satisfies OperationGroupStrategy[])
.default("none" as const)
.makeOptionMandatory(),
)
.showHelpAfterError()
.parse()
const config = program.opts()
async function main() {
logger.time("program starting")
logger.info(`running on input file '${config.input}'`)
logger.time("load files")
const compilerOptions = loadTsConfigCompilerOptions(
path.join(process.cwd(), config.output),
)
const validator = await OpenapiValidator.create()
const loader = await OpenapiLoader.create(
{entryPoint: config.input, fileType: config.inputType},
validator,
)
const input = new Input(loader, {
extractInlineSchemas: config.extractInlineSchemas,
})
const generator = templates[config.template]
await generator({
input,
dest: config.output,
schemaBuilder: config.schemaBuilder,
enableRuntimeResponseValidation: config.enableRuntimeResponseValidation,
compilerOptions,
allowUnusedImports: config.allowUnusedImports,
groupingStrategy: config.groupingStrategy,
})
}
main()
.then(() => {
logger.info("generation complete!")
logger.info("elapsed", logger.toJSON())
process.exit(0)
})
.catch((err) => {
logger.error("unhandled error", err)
process.exit(1)
})