-
-
Notifications
You must be signed in to change notification settings - Fork 364
/
index.ts
355 lines (345 loc) · 11.3 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import * as path from "node:path";
import * as url from "node:url";
import { defineCommand, runMain } from "citty";
import { consola } from "consola";
import packageJson from "./package.json" with { type: "json" };
import { TemplatesGenConfig } from "./src/commands/generate-templates/configuration.js";
import { CodeGenConfig } from "./src/configuration.js";
import { HTTP_CLIENT } from "./src/constants.js";
import { generateApi, generateTemplates } from "./src/index.js";
const templateGenBaseConfig = new TemplatesGenConfig({});
const generateTemplatesCommand = defineCommand({
meta: {
name: "generate-templates",
description: 'Generate ".ejs" templates needed for generate api',
},
args: {
"clean-output": {
type: "boolean",
description:
"clean output folder before generate template. WARNING: May cause data loss",
default: templateGenBaseConfig.cleanOutput,
},
"http-client": {
type: "string",
description: `http client type (possible values: ${Object.values(
HTTP_CLIENT,
)})`,
default: templateGenBaseConfig.httpClientType,
},
modular: {
type: "boolean",
description:
"generate templates needed to separate files for http client, data contracts, and routes",
default: templateGenBaseConfig.modular,
},
output: {
type: "string",
alias: "o",
description: "output path of generated templates",
default: templateGenBaseConfig.output,
},
rewrite: {
type: "boolean",
alias: "r",
description: "rewrite content in existing templates",
default: templateGenBaseConfig.rewrite,
},
silent: {
type: "boolean",
description: "Output only errors to console",
default: templateGenBaseConfig.silent,
},
},
run: async ({ args }) => {
if (args.debug) consola.level = Number.MAX_SAFE_INTEGER;
if (args.silent) consola.level = 0;
await generateTemplates({
cleanOutput: args["clean-output"],
httpClientType: args["http-client"],
modular: args.modular,
output: args.output,
rewrite: args.rewrite,
silent: args.silent,
});
},
});
const codeGenBaseConfig = new CodeGenConfig({});
const generateCommand = defineCommand({
meta: {
name: "generate",
description: packageJson.description,
},
args: {
path: {
type: "string",
alias: "p",
description: "path/url to swagger scheme",
required: true,
},
output: {
type: "string",
alias: "o",
description: "output path of typescript api file",
default: "./",
},
name: {
type: "string",
alias: "n",
description: "name of output typescript api file",
default: codeGenBaseConfig.fileName,
},
templates: {
type: "string",
alias: "t",
description: "path to folder containing templates",
},
"default-as-success": {
type: "boolean",
alias: "d",
description:
'use "default" response status code as success response too. some swagger schemas use "default" response status code as success response type by default.',
default: codeGenBaseConfig.defaultResponseAsSuccess,
},
responses: {
type: "boolean",
alias: "r",
description:
"generate additional information about request responses also add typings for bad responses",
default: codeGenBaseConfig.generateResponses,
},
"union-enums": {
type: "boolean",
description: 'generate all "enum" types as union types (T1 | T2 | TN)',
default: codeGenBaseConfig.generateUnionEnums,
},
"add-readonly": {
type: "boolean",
description: "generate readonly properties",
default: codeGenBaseConfig.addReadonly,
},
"route-types": {
type: "boolean",
description: "generate type definitions for API routes",
default: codeGenBaseConfig.generateRouteTypes,
},
"no-client": {
type: "boolean",
description: "do not generate an API class",
default: codeGenBaseConfig.generateClient,
},
"enum-names-as-values": {
type: "boolean",
description:
"use values in 'x-enumNames' as enum values (not only as keys)",
default: codeGenBaseConfig.enumNamesAsValues,
},
"extract-request-params": {
description:
"extract request params to data contract (Also combine path params and query params into one object)",
default: codeGenBaseConfig.extractRequestParams,
},
"extract-request-body": {
type: "boolean",
description: "extract request body type to data contract",
default: codeGenBaseConfig.extractRequestBody,
},
"extract-response-body": {
type: "boolean",
description: "extract response body type to data contract",
default: codeGenBaseConfig.extractResponseBody,
},
"extract-response-error": {
type: "boolean",
description: "extract response error type to data contract",
default: codeGenBaseConfig.extractResponseError,
},
"extract-responses": {
type: "boolean",
description: "extract all responses described in /components/responses",
default: codeGenBaseConfig.extractResponses,
},
modular: {
type: "boolean",
description:
"generate separated files for http client, data contracts, and routes",
default: codeGenBaseConfig.modular,
},
js: {
type: "boolean",
description: "generate js api module with declaration file",
default: codeGenBaseConfig.toJS,
},
"module-name-index": {
type: "string",
description:
"determines which path index should be used for routes separation (example: GET:/fruits/getFruit -> index:0 -> moduleName -> fruits)",
default: codeGenBaseConfig.moduleNameIndex.toString(),
},
"module-name-first-tag": {
type: "boolean",
description: "splits routes based on the first tag",
default: codeGenBaseConfig.moduleNameFirstTag,
},
axios: {
type: "boolean",
description: "generate axios http client",
default: false,
},
"unwrap-response-data": {
type: "boolean",
description: "unwrap the data item from the response",
default: codeGenBaseConfig.unwrapResponseData,
},
"disable-throw-on-error": {
type: "boolean",
description: "Do not throw an error when response.ok is not true",
default: codeGenBaseConfig.disableThrowOnError,
},
"single-http-client": {
type: "boolean",
description: "Ability to send HttpClient instance to Api constructor",
default: codeGenBaseConfig.singleHttpClient,
},
silent: {
type: "boolean",
description: "Output only errors to console",
default: codeGenBaseConfig.silent,
},
"default-response": {
type: "string",
description: "default type for empty response schema",
default: codeGenBaseConfig.defaultResponseType,
},
"type-prefix": {
type: "string",
description: "data contract name prefix",
default: codeGenBaseConfig.typePrefix,
},
"type-suffix": {
type: "string",
description: "data contract name suffix",
default: codeGenBaseConfig.typeSuffix,
},
"clean-output": {
type: "boolean",
description:
"clean output folder before generate api. WARNING: May cause data loss",
default: codeGenBaseConfig.cleanOutput,
},
"api-class-name": {
type: "string",
description: "name of the api class",
default: codeGenBaseConfig.apiClassName,
},
patch: {
type: "boolean",
description: "fix up small errors in the swagger source definition",
default: codeGenBaseConfig.patch,
},
debug: {
type: "boolean",
description: "additional information about processes inside this tool",
default: codeGenBaseConfig.debug,
},
"another-array-type": {
type: "boolean",
description: "generate array types as Array<Type> (by default Type[])",
default: codeGenBaseConfig.anotherArrayType,
},
"sort-types": {
type: "boolean",
description: "sort fields and types",
default: codeGenBaseConfig.sortTypes,
},
"extract-enums": {
type: "boolean",
description:
"extract all enums from inline interface/type content to typescript enum construction",
default: codeGenBaseConfig.extractEnums,
},
"sort-routes": {
type: "boolean",
description: "sort routes in alphabetical order",
default: codeGenBaseConfig.sortRoutes,
},
"custom-config": {
type: "string",
description: "custom config: primitiveTypeConstructs, hooks, ... ",
},
},
run: async ({ args }) => {
if (args.debug) consola.level = Number.MAX_SAFE_INTEGER;
if (args.silent) consola.level = 0;
let customConfig;
if (args["custom-config"]) {
try {
const customConfigPath = url
.pathToFileURL(path.resolve(process.cwd(), args["custom-config"]))
.toString();
customConfig = await import(customConfigPath);
customConfig = customConfig.default || customConfig;
if (customConfig) {
consola.info(`Found custom config at: ${customConfigPath}`);
}
} catch (error) {
consola.error("Error loading custom config:", error);
}
}
await generateApi({
addReadonly: args["add-readonly"],
anotherArrayType: args["another-array-type"],
apiClassName: args["api-class-name"],
cleanOutput: args["clean-output"],
debug: args.debug,
defaultResponseAsSuccess: args["default-as-success"],
defaultResponseType: args["default-response"],
disableThrowOnError: args["disable-throw-on-error"],
enumNamesAsValues: args["enum-names-as-values"],
extractEnums: args["extract-enums"],
extractRequestBody: args["extract-request-body"],
extractRequestParams: args["extract-request-params"],
extractResponseBody: args["extract-response-body"],
extractResponseError: args["extract-response-error"],
extractResponses: args["extract-responses"],
fileName: args.name,
generateClient: !(args.axios || args["no-client"]),
generateResponses: args.responses,
generateRouteTypes: args["route-types"],
generateUnionEnums: args["union-enums"],
httpClientType:
args["http-client"] || args.axios
? HTTP_CLIENT.AXIOS
: HTTP_CLIENT.FETCH,
input: path.resolve(process.cwd(), args.path as string),
modular: args.modular,
moduleNameFirstTag: args["module-name-first-tag"],
moduleNameIndex: +args["module-name-index"] || 0,
output: path.resolve(process.cwd(), (args.output as string) || "."),
patch: args.patch,
silent: args.silent,
singleHttpClient: args["single-http-client"],
sortRoutes: args["sort-routes"],
sortTypes: args["sort-types"],
templates: args.templates,
toJS: args.js,
typePrefix: args["type-prefix"],
typeSuffix: args["type-suffix"],
unwrapResponseData: args["unwrap-response-data"],
url: args.path,
...customConfig,
});
},
});
const main = defineCommand({
meta: {
name: packageJson.name,
description: packageJson.description,
version: packageJson.version,
},
subCommands: {
generate: generateCommand,
"generate-templates": generateTemplatesCommand,
},
});
runMain(main);