swagger-typescript-api 11.0.0--alpha-2 → 11.0.0--alpha-5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cli/execute.js +5 -2
- package/index.js +55 -40
- package/package.json +1 -1
- package/src/code-gen-process.js +4 -0
- package/src/configuration.js +10 -83
- package/src/index.js +6 -2
- package/src/util/object-assign.js +6 -2
- package/templates/base/interface-data-contract.ejs +0 -1
package/cli/execute.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
const _ = require("lodash");
|
|
2
2
|
const { root_command, skip_command } = require("./constants");
|
|
3
3
|
const { parseArgs } = require("./parse-args");
|
|
4
|
-
const
|
|
4
|
+
const didYouMean = require("didyoumean");
|
|
5
|
+
|
|
6
|
+
didYouMean.threshold = 0.5;
|
|
5
7
|
|
|
6
8
|
const execute = (params, commands, instance) => {
|
|
7
9
|
const args = parseArgs(params.args, params.from);
|
|
@@ -96,7 +98,8 @@ const processArgs = (commands, args) => {
|
|
|
96
98
|
const option = command.options.find((option) => option.flags.keys.includes(arg));
|
|
97
99
|
|
|
98
100
|
if (!option) {
|
|
99
|
-
|
|
101
|
+
const tip = didYouMean(arg, allFlagKeys);
|
|
102
|
+
error = `unknown option ${arg}${tip ? `\n(Did you mean ${tip} ?)` : ""}`;
|
|
100
103
|
}
|
|
101
104
|
|
|
102
105
|
if (option) {
|
package/index.js
CHANGED
|
@@ -191,51 +191,66 @@ const program = cli({
|
|
|
191
191
|
description: "generate array types as Array<Type> (by default Type[])",
|
|
192
192
|
default: false,
|
|
193
193
|
},
|
|
194
|
+
{
|
|
195
|
+
flags: "--sort-types",
|
|
196
|
+
description: "sort fields and types",
|
|
197
|
+
default: false,
|
|
198
|
+
},
|
|
194
199
|
],
|
|
195
200
|
});
|
|
196
201
|
|
|
197
202
|
const main = async () => {
|
|
198
203
|
const { command, options } = await program.execute({ args: process.argv });
|
|
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
|
-
|
|
204
|
+
|
|
205
|
+
switch (command) {
|
|
206
|
+
case null: {
|
|
207
|
+
await generateApi({
|
|
208
|
+
name: options.name,
|
|
209
|
+
url: options.path,
|
|
210
|
+
generateRouteTypes: options.routeTypes,
|
|
211
|
+
generateClient: !!(options.axios || options.client),
|
|
212
|
+
httpClientType: options.axios ? HTTP_CLIENT.AXIOS : HTTP_CLIENT.FETCH,
|
|
213
|
+
defaultResponseAsSuccess: options.defaultAsSuccess,
|
|
214
|
+
defaultResponseType: options.defaultResponse,
|
|
215
|
+
unwrapResponseData: options.unwrapResponseData,
|
|
216
|
+
disableThrowOnError: options.disableThrowOnError,
|
|
217
|
+
sortTypes: options.sortTypes,
|
|
218
|
+
generateUnionEnums: options.unionEnums,
|
|
219
|
+
addReadonly: options.addReadonly,
|
|
220
|
+
generateResponses: options.responses,
|
|
221
|
+
extractRequestParams: !!options.extractRequestParams,
|
|
222
|
+
extractRequestBody: !!options.extractRequestBody,
|
|
223
|
+
extractResponseBody: !!options.extractResponseBody,
|
|
224
|
+
extractResponseError: !!options.extractResponseError,
|
|
225
|
+
input: resolve(process.cwd(), options.path),
|
|
226
|
+
output: resolve(process.cwd(), options.output || "."),
|
|
227
|
+
templates: options.templates,
|
|
228
|
+
modular: !!options.modular,
|
|
229
|
+
toJS: !!options.js,
|
|
230
|
+
enumNamesAsValues: options.enumNamesAsValues,
|
|
231
|
+
moduleNameIndex: +(options.moduleNameIndex || 0),
|
|
232
|
+
moduleNameFirstTag: options.moduleNameFirstTag,
|
|
233
|
+
disableStrictSSL: !!options.disableStrictSSL,
|
|
234
|
+
disableProxy: !!options.disableProxy,
|
|
235
|
+
singleHttpClient: !!options.singleHttpClient,
|
|
236
|
+
cleanOutput: !!options.cleanOutput,
|
|
237
|
+
silent: !!options.silent,
|
|
238
|
+
typePrefix: options.typePrefix,
|
|
239
|
+
typeSuffix: options.typeSuffix,
|
|
240
|
+
patch: !!options.patch,
|
|
241
|
+
apiClassName: options.apiClassName,
|
|
242
|
+
debug: options.debug,
|
|
243
|
+
anotherArrayType: options.anotherArrayType,
|
|
244
|
+
});
|
|
245
|
+
break;
|
|
246
|
+
}
|
|
247
|
+
case "generate-templates": {
|
|
248
|
+
console.info("todo");
|
|
249
|
+
break;
|
|
250
|
+
}
|
|
251
|
+
default: {
|
|
252
|
+
break;
|
|
253
|
+
}
|
|
239
254
|
}
|
|
240
255
|
};
|
|
241
256
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "swagger-typescript-api",
|
|
3
|
-
"version": "11.0.0--alpha-
|
|
3
|
+
"version": "11.0.0--alpha-5",
|
|
4
4
|
"description": "Generate typescript/javascript api from swagger schema",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"cli:json": "node index.js -r -d -p ./swagger-test-cli.json -n swagger-test-cli.ts",
|
package/src/code-gen-process.js
CHANGED
|
@@ -53,6 +53,10 @@ class CodeGenProcess {
|
|
|
53
53
|
*/
|
|
54
54
|
codeFormatter;
|
|
55
55
|
|
|
56
|
+
/**
|
|
57
|
+
*
|
|
58
|
+
* @param config {Partial<import("../index.d.ts").GenerateApiConfiguration['config']>}
|
|
59
|
+
*/
|
|
56
60
|
constructor(config) {
|
|
57
61
|
this.config = new Configuration(config);
|
|
58
62
|
this.logger = new Logger(this.config);
|
package/src/configuration.js
CHANGED
|
@@ -293,52 +293,17 @@ class Configuration {
|
|
|
293
293
|
|
|
294
294
|
templateExtensions = [".eta", ".ejs"];
|
|
295
295
|
|
|
296
|
+
/**
|
|
297
|
+
* @param config {Partial<GenerateApiConfiguration['config']>}
|
|
298
|
+
*/
|
|
296
299
|
constructor({
|
|
297
|
-
|
|
298
|
-
output,
|
|
299
|
-
url,
|
|
300
|
-
spec,
|
|
301
|
-
name: fileName,
|
|
302
|
-
toJS,
|
|
303
|
-
modular,
|
|
304
|
-
templates,
|
|
305
|
-
generateResponses,
|
|
306
|
-
defaultResponseAsSuccess,
|
|
307
|
-
generateRouteTypes,
|
|
308
|
-
generateClient,
|
|
309
|
-
httpClientType,
|
|
310
|
-
generateUnionEnums,
|
|
311
|
-
addReadonly,
|
|
312
|
-
moduleNameIndex,
|
|
313
|
-
moduleNameFirstTag,
|
|
314
|
-
extractRequestParams,
|
|
315
|
-
extractRequestBody,
|
|
316
|
-
extractResponseBody,
|
|
317
|
-
extractResponseError,
|
|
318
|
-
defaultResponseType,
|
|
319
|
-
unwrapResponseData,
|
|
320
|
-
disableThrowOnError,
|
|
321
|
-
sortTypes,
|
|
322
|
-
singleHttpClient,
|
|
323
|
-
prettier: prettierOptions = getDefaultPrettierOptions(),
|
|
324
|
-
hooks: rawHooks,
|
|
325
|
-
extraTemplates,
|
|
326
|
-
enumNamesAsValues,
|
|
327
|
-
disableStrictSSL,
|
|
328
|
-
disableProxy,
|
|
329
|
-
cleanOutput,
|
|
330
|
-
silent,
|
|
331
|
-
typePrefix,
|
|
332
|
-
typeSuffix,
|
|
333
|
-
patch,
|
|
334
|
-
authorizationToken,
|
|
335
|
-
apiClassName,
|
|
336
|
-
debug,
|
|
337
|
-
anotherArrayType,
|
|
300
|
+
prettierOptions = getDefaultPrettierOptions(),
|
|
338
301
|
codeGenConstructs,
|
|
339
302
|
primitiveTypeConstructs,
|
|
340
303
|
constants,
|
|
341
304
|
templateInfos,
|
|
305
|
+
hooks,
|
|
306
|
+
...params
|
|
342
307
|
}) {
|
|
343
308
|
objectAssign(this.Ts, codeGenConstructs);
|
|
344
309
|
objectAssign(this.primitiveTypes, primitiveTypeConstructs);
|
|
@@ -346,52 +311,14 @@ class Configuration {
|
|
|
346
311
|
this.defaultResponseType = this.Ts.Keyword.Void;
|
|
347
312
|
|
|
348
313
|
this.update({
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
spec,
|
|
353
|
-
fileName,
|
|
354
|
-
extraTemplates,
|
|
355
|
-
defaultResponseAsSuccess,
|
|
356
|
-
generateRouteTypes,
|
|
357
|
-
generateClient,
|
|
358
|
-
httpClientType,
|
|
359
|
-
generateResponses,
|
|
360
|
-
templates,
|
|
361
|
-
generateUnionEnums,
|
|
362
|
-
addReadonly,
|
|
363
|
-
moduleNameIndex,
|
|
364
|
-
moduleNameFirstTag,
|
|
365
|
-
prettierOptions,
|
|
366
|
-
modular,
|
|
367
|
-
extractRequestParams,
|
|
368
|
-
extractRequestBody,
|
|
369
|
-
extractResponseBody,
|
|
370
|
-
extractResponseError,
|
|
371
|
-
hooks: _.merge(this.hooks, rawHooks || {}),
|
|
372
|
-
enumNamesAsValues,
|
|
373
|
-
disableStrictSSL,
|
|
374
|
-
disableProxy,
|
|
375
|
-
cleanOutput,
|
|
376
|
-
defaultResponseType,
|
|
377
|
-
unwrapResponseData,
|
|
378
|
-
disableThrowOnError,
|
|
379
|
-
sortTypes,
|
|
380
|
-
singleHttpClient,
|
|
314
|
+
...params,
|
|
315
|
+
prettierOptions: prettierOptions === undefined ? getDefaultPrettierOptions() : prettierOptions,
|
|
316
|
+
hooks: _.merge(this.hooks, hooks || {}),
|
|
381
317
|
constants: {
|
|
382
318
|
...constantsBase,
|
|
383
319
|
...constants,
|
|
384
320
|
},
|
|
385
|
-
|
|
386
|
-
silent,
|
|
387
|
-
toJS,
|
|
388
|
-
typePrefix,
|
|
389
|
-
typeSuffix,
|
|
390
|
-
patch,
|
|
391
|
-
apiClassName,
|
|
392
|
-
debug,
|
|
393
|
-
anotherArrayType,
|
|
394
|
-
templateInfos,
|
|
321
|
+
templateInfos: templateInfos || this.templateInfos,
|
|
395
322
|
});
|
|
396
323
|
|
|
397
324
|
this.jsPrimitiveTypes = [this.Ts.Keyword.Number, this.Ts.Keyword.String, this.Ts.Keyword.Boolean];
|
package/src/index.js
CHANGED
|
@@ -12,8 +12,12 @@ const { CodeGenProcess } = require("./code-gen-process.js");
|
|
|
12
12
|
|
|
13
13
|
module.exports = {
|
|
14
14
|
constants: constants,
|
|
15
|
-
generateApi: async (config) => {
|
|
16
|
-
const codeGenProcess = new CodeGenProcess(
|
|
15
|
+
generateApi: async ({ name, prettier, ...config }) => {
|
|
16
|
+
const codeGenProcess = new CodeGenProcess({
|
|
17
|
+
...config,
|
|
18
|
+
fileName: name,
|
|
19
|
+
prettierOptions: prettier,
|
|
20
|
+
});
|
|
17
21
|
return await codeGenProcess.start();
|
|
18
22
|
},
|
|
19
23
|
generateTemplates: (config) => {},
|
|
@@ -2,8 +2,12 @@ const _ = require("lodash");
|
|
|
2
2
|
|
|
3
3
|
const objectAssign = (target, updaterFn) => {
|
|
4
4
|
if (!updaterFn) return;
|
|
5
|
-
const
|
|
6
|
-
|
|
5
|
+
const update = typeof updaterFn === "function" ? updaterFn(target) : updaterFn;
|
|
6
|
+
const undefinedKeys = _.map(update, (value, key) => value === undefined && key).filter(Boolean);
|
|
7
|
+
Object.assign(target, _.merge(target, update));
|
|
8
|
+
undefinedKeys.forEach((key) => {
|
|
9
|
+
target[key] = undefined;
|
|
10
|
+
});
|
|
7
11
|
};
|
|
8
12
|
|
|
9
13
|
module.exports = {
|