swagger-typescript-api 12.0.4 → 13.0.0
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/LICENSE +21 -21
- package/README.md +1 -1
- package/cli/constants.js +3 -3
- package/cli/execute.js +52 -31
- package/cli/index.d.ts +1 -2
- package/cli/index.js +18 -17
- package/cli/operations/display-help.js +51 -29
- package/cli/parse-args.js +3 -3
- package/cli/process-option.js +28 -20
- package/index.d.ts +99 -7
- package/index.js +158 -136
- package/package.json +35 -30
- package/src/code-formatter.js +28 -13
- package/src/code-gen-process.js +367 -264
- package/src/commands/generate-templates/configuration.js +2 -2
- package/src/commands/generate-templates/index.js +1 -2
- package/src/commands/generate-templates/templates-gen-process.js +62 -35
- package/src/component-type-name-resolver.js +44 -0
- package/src/configuration.js +166 -98
- package/src/constants.js +28 -22
- package/src/index.js +3 -4
- package/src/schema-components-map.js +39 -23
- package/src/schema-parser/base-schema-parsers/array.js +43 -0
- package/src/schema-parser/base-schema-parsers/complex.js +51 -0
- package/src/schema-parser/base-schema-parsers/discriminator.js +301 -0
- package/src/schema-parser/base-schema-parsers/enum.js +158 -0
- package/src/schema-parser/base-schema-parsers/object.js +105 -0
- package/src/schema-parser/base-schema-parsers/primitive.js +63 -0
- package/src/schema-parser/complex-schema-parsers/all-of.js +26 -0
- package/src/schema-parser/complex-schema-parsers/any-of.js +34 -0
- package/src/schema-parser/complex-schema-parsers/not.js +9 -0
- package/src/schema-parser/complex-schema-parsers/one-of.js +27 -0
- package/src/schema-parser/mono-schema-parser.js +48 -0
- package/src/schema-parser/schema-formatters.js +58 -44
- package/src/schema-parser/schema-parser-fabric.js +131 -0
- package/src/schema-parser/schema-parser.js +212 -361
- package/src/schema-parser/schema-utils.js +158 -33
- package/src/schema-parser/util/enum-key-resolver.js +26 -0
- package/src/{schema-parser → schema-routes}/schema-routes.js +472 -203
- package/src/schema-routes/util/specific-arg-name-resolver.js +26 -0
- package/src/schema-walker.js +93 -0
- package/src/swagger-schema-resolver.js +61 -28
- package/src/templates-worker.js +240 -0
- package/src/translators/javascript.js +83 -0
- package/src/translators/translator.js +35 -0
- package/src/{type-name.js → type-name-formatter.js} +35 -20
- package/src/util/file-system.js +30 -14
- package/src/util/id.js +2 -2
- package/src/util/internal-case.js +1 -1
- package/src/util/logger.js +46 -20
- package/src/util/name-resolver.js +50 -58
- package/src/util/object-assign.js +7 -3
- package/src/util/pascal-case.js +1 -1
- package/src/util/request.js +5 -5
- package/src/util/sort-by-property.js +17 -0
- package/templates/base/data-contract-jsdoc.ejs +37 -37
- package/templates/base/data-contracts.ejs +40 -28
- package/templates/base/enum-data-contract.ejs +12 -12
- package/templates/base/http-client.ejs +2 -2
- package/templates/base/http-clients/axios-http-client.ejs +139 -138
- package/templates/base/http-clients/fetch-http-client.ejs +224 -224
- package/templates/base/interface-data-contract.ejs +10 -10
- package/templates/base/object-field-jsdoc.ejs +28 -28
- package/templates/base/route-docs.ejs +30 -30
- package/templates/base/route-name.ejs +42 -42
- package/templates/base/route-type.ejs +22 -21
- package/templates/base/type-data-contract.ejs +15 -15
- package/templates/default/api.ejs +69 -65
- package/templates/default/procedure-call.ejs +100 -100
- package/templates/default/route-types.ejs +32 -28
- package/templates/modular/api.ejs +28 -28
- package/templates/modular/procedure-call.ejs +100 -100
- package/templates/modular/route-types.ejs +18 -18
- package/src/templates.js +0 -177
- package/src/translators/JavaScript.js +0 -60
package/index.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import {MonoSchemaParser} from "./src/schema-parser/mono-schema-parser";
|
|
2
|
+
|
|
1
3
|
type HttpClientType = "axios" | "fetch";
|
|
2
4
|
|
|
3
5
|
interface GenerateApiParamsBase {
|
|
@@ -7,7 +9,7 @@ interface GenerateApiParamsBase {
|
|
|
7
9
|
name?: string;
|
|
8
10
|
|
|
9
11
|
/**
|
|
10
|
-
* path to folder where will
|
|
12
|
+
* path to folder where will be located the created api module.
|
|
11
13
|
*
|
|
12
14
|
* may set to `false` to skip writing content to disk. in this case,
|
|
13
15
|
* you may access the `files` on the return value.
|
|
@@ -59,6 +61,11 @@ interface GenerateApiParamsBase {
|
|
|
59
61
|
*/
|
|
60
62
|
sortTypes?: boolean;
|
|
61
63
|
|
|
64
|
+
/**
|
|
65
|
+
* sort routes in alphabetical order
|
|
66
|
+
*/
|
|
67
|
+
sortRoutes?: boolean;
|
|
68
|
+
|
|
62
69
|
/**
|
|
63
70
|
* generate js api module with declaration file (default: false)
|
|
64
71
|
*/
|
|
@@ -168,6 +175,55 @@ interface GenerateApiParamsBase {
|
|
|
168
175
|
|
|
169
176
|
/** configuration for fetching swagger schema requests */
|
|
170
177
|
requestOptions?: null | Partial<import("node-fetch").RequestInit>;
|
|
178
|
+
|
|
179
|
+
/** ts compiler configuration object (for --to-js option) */
|
|
180
|
+
compilerTsConfig?: Record<string, any>;
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* custom ts->* translator
|
|
184
|
+
* do not use constructor args, it can break functionality of this property, just send class reference
|
|
185
|
+
*
|
|
186
|
+
* @example
|
|
187
|
+
* ```ts
|
|
188
|
+
* const { Translator } = require("swagger-typescript-api/src/translators/translator");
|
|
189
|
+
*
|
|
190
|
+
* class MyTranslator extends Translator {
|
|
191
|
+
*
|
|
192
|
+
* translate({ fileName, fileExtension, fileContent }) {
|
|
193
|
+
* this.codeFormatter.format()
|
|
194
|
+
* this.config.
|
|
195
|
+
* this.logger.
|
|
196
|
+
*
|
|
197
|
+
* return [
|
|
198
|
+
* {
|
|
199
|
+
* fileName,
|
|
200
|
+
* fileExtension,
|
|
201
|
+
* fileContent,
|
|
202
|
+
* }
|
|
203
|
+
* ]
|
|
204
|
+
* }
|
|
205
|
+
* }
|
|
206
|
+
* ```
|
|
207
|
+
*/
|
|
208
|
+
customTranslator?: new () => typeof import("./src/translators/translator").Translator;
|
|
209
|
+
/** fallback name for enum key resolver */
|
|
210
|
+
enumKeyResolverName?: string;
|
|
211
|
+
/** fallback name for type name resolver */
|
|
212
|
+
typeNameResolverName?: string;
|
|
213
|
+
/** fallback name for specific arg name resolver */
|
|
214
|
+
specificArgNameResolverName?: string;
|
|
215
|
+
schemaParsers?: {
|
|
216
|
+
complexOneOf?:MonoSchemaParser;
|
|
217
|
+
complexAllOf?:MonoSchemaParser;
|
|
218
|
+
complexAnyOf?:MonoSchemaParser;
|
|
219
|
+
complexNot?:MonoSchemaParser;
|
|
220
|
+
enum?:MonoSchemaParser;
|
|
221
|
+
object?:MonoSchemaParser;
|
|
222
|
+
complex?:MonoSchemaParser;
|
|
223
|
+
primitive?:MonoSchemaParser;
|
|
224
|
+
discriminator?:MonoSchemaParser;
|
|
225
|
+
array?: MonoSchemaParser;
|
|
226
|
+
}
|
|
171
227
|
}
|
|
172
228
|
|
|
173
229
|
type CodeGenConstruct = {
|
|
@@ -282,7 +338,10 @@ export interface Hooks {
|
|
|
282
338
|
/** calls after parse route (return type: customized route (ParsedRoute), nothing change (void), false (ignore this route)) */
|
|
283
339
|
onCreateRoute: (routeData: ParsedRoute) => ParsedRoute | void | false;
|
|
284
340
|
/** Start point of work this tool (after fetching schema) */
|
|
285
|
-
onInit?: <C extends GenerateApiConfiguration["config"]>(
|
|
341
|
+
onInit?: <C extends GenerateApiConfiguration["config"]>(
|
|
342
|
+
configuration: C,
|
|
343
|
+
codeGenProcess: import("./src/code-gen-process").CodeGenProcess,
|
|
344
|
+
) => C | void;
|
|
286
345
|
/** customize configuration object before sending it to ETA templates */
|
|
287
346
|
onPrepareConfig?: <C extends GenerateApiConfiguration>(currentConfiguration: C) => C | void;
|
|
288
347
|
/** customize route name as you need */
|
|
@@ -367,7 +426,7 @@ export interface SchemaComponent {
|
|
|
367
426
|
};
|
|
368
427
|
$parsed: ParsedSchema<SchemaTypeObjectContent | SchemaTypeEnumContent | SchemaTypePrimitiveContent>;
|
|
369
428
|
};
|
|
370
|
-
componentName:
|
|
429
|
+
componentName: "schemas" | "paths";
|
|
371
430
|
typeData: ParsedSchema<SchemaTypeObjectContent | SchemaTypeEnumContent | SchemaTypePrimitiveContent> | null;
|
|
372
431
|
}
|
|
373
432
|
|
|
@@ -443,10 +502,16 @@ type ExtractingOptions = {
|
|
|
443
502
|
responseBodySuffix: string[];
|
|
444
503
|
responseErrorSuffix: string[];
|
|
445
504
|
requestParamsSuffix: string[];
|
|
505
|
+
enumSuffix: string[];
|
|
506
|
+
discriminatorMappingSuffix: string[];
|
|
507
|
+
discriminatorAbstractPrefix: string[];
|
|
446
508
|
requestBodyNameResolver: (name: string, reservedNames: string) => string | undefined;
|
|
447
509
|
responseBodyNameResolver: (name: string, reservedNames: string) => string | undefined;
|
|
448
510
|
responseErrorNameResolver: (name: string, reservedNames: string) => string | undefined;
|
|
449
511
|
requestParamsNameResolver: (name: string, reservedNames: string) => string | undefined;
|
|
512
|
+
enumNameResolver: (name: string, reservedNames: string) => string | undefined;
|
|
513
|
+
discriminatorMappingNameResolver: (name: string, reservedNames: string) => string | undefined;
|
|
514
|
+
discriminatorAbstractResolver: (name: string, reservedNames: string) => string | undefined;
|
|
450
515
|
};
|
|
451
516
|
|
|
452
517
|
export interface GenerateApiConfiguration {
|
|
@@ -463,6 +528,18 @@ export interface GenerateApiConfiguration {
|
|
|
463
528
|
url: string;
|
|
464
529
|
spec: any;
|
|
465
530
|
fileName: string;
|
|
531
|
+
templatePaths: {
|
|
532
|
+
/** `templates/base` */
|
|
533
|
+
base: string;
|
|
534
|
+
/** `templates/default` */
|
|
535
|
+
default: string;
|
|
536
|
+
/** `templates/modular` */
|
|
537
|
+
modular: string;
|
|
538
|
+
/** usage path if `--templates` option is not set */
|
|
539
|
+
original: string;
|
|
540
|
+
/** custom path to templates (`--templates`) */
|
|
541
|
+
custom: string | null;
|
|
542
|
+
};
|
|
466
543
|
authorizationToken?: string;
|
|
467
544
|
generateResponses: boolean;
|
|
468
545
|
defaultResponseAsSuccess: boolean;
|
|
@@ -481,6 +558,7 @@ export interface GenerateApiConfiguration {
|
|
|
481
558
|
extractRequestParams: boolean;
|
|
482
559
|
unwrapResponseData: boolean;
|
|
483
560
|
sortTypes: boolean;
|
|
561
|
+
sortRoutes: boolean;
|
|
484
562
|
singleHttpClient: boolean;
|
|
485
563
|
typePrefix: string;
|
|
486
564
|
typeSuffix: string;
|
|
@@ -505,10 +583,16 @@ export interface GenerateApiConfiguration {
|
|
|
505
583
|
hooks: Hooks;
|
|
506
584
|
enumNamesAsValues: boolean;
|
|
507
585
|
version: string;
|
|
586
|
+
compilerTsConfig: Record<string, any>;
|
|
587
|
+
enumKeyResolverName: string;
|
|
588
|
+
typeNameResolverName: string;
|
|
589
|
+
specificArgNameResolverName: string;
|
|
590
|
+
/** do not use constructor args, it can break functionality of this property, just send class reference */
|
|
591
|
+
customTranslator?: new (...args: never[]) => typeof import("./src/translators/translator").Translator;
|
|
508
592
|
internalTemplateOptions: {
|
|
509
593
|
addUtilRequiredKeysType: boolean;
|
|
510
594
|
};
|
|
511
|
-
componentTypeNameResolver: typeof import("./src/
|
|
595
|
+
componentTypeNameResolver: typeof import("./src/component-type-name-resolver").ComponentTypeNameResolver;
|
|
512
596
|
fileNames: {
|
|
513
597
|
dataContracts: string;
|
|
514
598
|
routeTypes: string;
|
|
@@ -533,7 +617,6 @@ export interface GenerateApiConfiguration {
|
|
|
533
617
|
extractingOptions: ExtractingOptions;
|
|
534
618
|
};
|
|
535
619
|
modelTypes: ModelType[];
|
|
536
|
-
rawModelTypes: SchemaComponent[];
|
|
537
620
|
hasFormDataRoutes: boolean;
|
|
538
621
|
hasSecurityRoutes: boolean;
|
|
539
622
|
hasQueryRoutes: boolean;
|
|
@@ -569,9 +652,18 @@ export interface GenerateApiConfiguration {
|
|
|
569
652
|
};
|
|
570
653
|
}
|
|
571
654
|
|
|
655
|
+
type FileInfo = {
|
|
656
|
+
/** @example myFilename */
|
|
657
|
+
fileName: string;
|
|
658
|
+
/** @example .d.ts */
|
|
659
|
+
fileExtension: string;
|
|
660
|
+
/** content of the file */
|
|
661
|
+
fileContent: string;
|
|
662
|
+
};
|
|
663
|
+
|
|
572
664
|
export interface GenerateApiOutput {
|
|
573
665
|
configuration: GenerateApiConfiguration;
|
|
574
|
-
files:
|
|
666
|
+
files: FileInfo[];
|
|
575
667
|
createFile: (params: { path: string; fileName: string; content: string; withPrefix?: boolean }) => void;
|
|
576
668
|
renderTemplate: (
|
|
577
669
|
templateContent: string,
|
|
@@ -579,7 +671,7 @@ export interface GenerateApiOutput {
|
|
|
579
671
|
etaOptions?: import("eta/dist/types/config").PartialConfig,
|
|
580
672
|
) => string;
|
|
581
673
|
getTemplate: (params: { fileName?: string; name?: string; path?: string }) => string;
|
|
582
|
-
formatTSContent: (content: string) => string
|
|
674
|
+
formatTSContent: (content: string) => Promise<string>;
|
|
583
675
|
}
|
|
584
676
|
|
|
585
677
|
export declare function generateApi(params: GenerateApiParams): Promise<GenerateApiOutput>;
|
package/index.js
CHANGED
|
@@ -6,245 +6,290 @@
|
|
|
6
6
|
// License text available at https://opensource.org/licenses/MIT
|
|
7
7
|
// Repository https://github.com/acacode/swagger-typescript-api
|
|
8
8
|
|
|
9
|
-
const
|
|
10
|
-
const {
|
|
11
|
-
const {
|
|
12
|
-
const {
|
|
13
|
-
const {
|
|
14
|
-
const {
|
|
15
|
-
const {
|
|
16
|
-
|
|
9
|
+
const { version, name } = require('./package.json');
|
|
10
|
+
const { cli } = require('./cli');
|
|
11
|
+
const { generateApi, generateTemplates } = require('./src');
|
|
12
|
+
const { HTTP_CLIENT } = require('./src/constants');
|
|
13
|
+
const { resolve } = require('path');
|
|
14
|
+
const { CodeGenConfig } = require('./src/configuration');
|
|
15
|
+
const {
|
|
16
|
+
TemplatesGenConfig,
|
|
17
|
+
} = require('./src/commands/generate-templates/configuration');
|
|
17
18
|
|
|
18
19
|
const codeGenBaseConfig = new CodeGenConfig({});
|
|
19
20
|
const templateGenBaseConfig = new TemplatesGenConfig({});
|
|
20
21
|
|
|
21
22
|
const program = cli({
|
|
22
23
|
name: name,
|
|
23
|
-
alias:
|
|
24
|
+
alias: 'sta',
|
|
24
25
|
version: version,
|
|
25
|
-
description:
|
|
26
|
+
description:
|
|
27
|
+
'Generate api via swagger scheme.\nSupports OA 3.0, 2.0, JSON, yaml.',
|
|
26
28
|
options: [
|
|
27
29
|
{
|
|
28
|
-
flags:
|
|
29
|
-
description:
|
|
30
|
+
flags: '-p, --path <string>',
|
|
31
|
+
description: 'path/url to swagger scheme',
|
|
30
32
|
required: true,
|
|
31
33
|
},
|
|
32
34
|
{
|
|
33
|
-
flags:
|
|
34
|
-
description:
|
|
35
|
-
default:
|
|
35
|
+
flags: '-o, --output <string>',
|
|
36
|
+
description: 'output path of typescript api file',
|
|
37
|
+
default: './',
|
|
36
38
|
},
|
|
37
39
|
{
|
|
38
|
-
flags:
|
|
39
|
-
description:
|
|
40
|
+
flags: '-n, --name <string>',
|
|
41
|
+
description: 'name of output typescript api file',
|
|
40
42
|
default: codeGenBaseConfig.fileName,
|
|
41
43
|
},
|
|
42
44
|
{
|
|
43
|
-
flags:
|
|
44
|
-
description:
|
|
45
|
+
flags: '-t, --templates <string>',
|
|
46
|
+
description: 'path to folder containing templates',
|
|
45
47
|
},
|
|
46
48
|
{
|
|
47
|
-
flags:
|
|
49
|
+
flags: '-d, --default-as-success',
|
|
48
50
|
description:
|
|
49
51
|
'use "default" response status code as success response too.\n' +
|
|
50
52
|
'some swagger schemas use "default" response status code as success response type by default.',
|
|
51
53
|
default: codeGenBaseConfig.defaultResponseAsSuccess,
|
|
54
|
+
internal: { name: 'defaultResponseAsSuccess' },
|
|
52
55
|
},
|
|
53
56
|
{
|
|
54
|
-
flags:
|
|
55
|
-
description:
|
|
57
|
+
flags: '-r, --responses',
|
|
58
|
+
description:
|
|
59
|
+
'generate additional information about request responses\n' +
|
|
60
|
+
'also add typings for bad responses',
|
|
56
61
|
default: codeGenBaseConfig.generateResponses,
|
|
62
|
+
internal: { name: 'generateResponses' },
|
|
57
63
|
},
|
|
58
64
|
{
|
|
59
|
-
flags:
|
|
65
|
+
flags: '--union-enums',
|
|
60
66
|
description: 'generate all "enum" types as union types (T1 | T2 | TN)',
|
|
61
67
|
default: codeGenBaseConfig.generateUnionEnums,
|
|
68
|
+
internal: { name: 'generateUnionEnums' },
|
|
62
69
|
},
|
|
63
70
|
{
|
|
64
|
-
flags:
|
|
65
|
-
description:
|
|
71
|
+
flags: '--add-readonly',
|
|
72
|
+
description: 'generate readonly properties',
|
|
66
73
|
default: codeGenBaseConfig.addReadonly,
|
|
67
74
|
},
|
|
68
75
|
{
|
|
69
|
-
flags:
|
|
70
|
-
description:
|
|
76
|
+
flags: '--route-types',
|
|
77
|
+
description: 'generate type definitions for API routes',
|
|
71
78
|
default: codeGenBaseConfig.generateRouteTypes,
|
|
79
|
+
internal: { name: 'generateRouteTypes' },
|
|
72
80
|
},
|
|
73
81
|
{
|
|
74
|
-
flags:
|
|
75
|
-
description:
|
|
82
|
+
flags: '--no-client',
|
|
83
|
+
description: 'do not generate an API class',
|
|
76
84
|
default: codeGenBaseConfig.generateClient,
|
|
77
85
|
},
|
|
78
86
|
{
|
|
79
|
-
flags:
|
|
80
|
-
description:
|
|
87
|
+
flags: '--enum-names-as-values',
|
|
88
|
+
description:
|
|
89
|
+
"use values in 'x-enumNames' as enum values (not only as keys)",
|
|
81
90
|
default: codeGenBaseConfig.enumNamesAsValues,
|
|
82
91
|
},
|
|
83
92
|
{
|
|
84
|
-
flags:
|
|
93
|
+
flags: '--extract-request-params',
|
|
85
94
|
description:
|
|
86
|
-
|
|
95
|
+
'extract request params to data contract (Also combine path params and query params into one object)',
|
|
87
96
|
default: codeGenBaseConfig.extractRequestParams,
|
|
97
|
+
internal: { formatter: Boolean },
|
|
88
98
|
},
|
|
89
99
|
{
|
|
90
|
-
flags:
|
|
91
|
-
description:
|
|
100
|
+
flags: '--extract-request-body',
|
|
101
|
+
description: 'extract request body type to data contract',
|
|
92
102
|
default: codeGenBaseConfig.extractRequestBody,
|
|
103
|
+
internal: { formatter: Boolean },
|
|
93
104
|
},
|
|
94
105
|
{
|
|
95
|
-
flags:
|
|
96
|
-
description:
|
|
106
|
+
flags: '--extract-response-body',
|
|
107
|
+
description: 'extract response body type to data contract',
|
|
97
108
|
default: codeGenBaseConfig.extractResponseBody,
|
|
109
|
+
internal: { formatter: Boolean },
|
|
98
110
|
},
|
|
99
111
|
{
|
|
100
|
-
flags:
|
|
101
|
-
description:
|
|
112
|
+
flags: '--extract-response-error',
|
|
113
|
+
description: 'extract response error type to data contract',
|
|
102
114
|
default: codeGenBaseConfig.extractResponseError,
|
|
115
|
+
internal: { formatter: Boolean },
|
|
103
116
|
},
|
|
104
117
|
{
|
|
105
|
-
flags:
|
|
106
|
-
description:
|
|
118
|
+
flags: '--modular',
|
|
119
|
+
description:
|
|
120
|
+
'generate separated files for http client, data contracts, and routes',
|
|
107
121
|
default: codeGenBaseConfig.modular,
|
|
122
|
+
internal: { formatter: Boolean },
|
|
108
123
|
},
|
|
109
124
|
{
|
|
110
|
-
flags:
|
|
111
|
-
description:
|
|
125
|
+
flags: '--js',
|
|
126
|
+
description: 'generate js api module with declaration file',
|
|
112
127
|
default: codeGenBaseConfig.toJS,
|
|
128
|
+
internal: { formatter: Boolean, name: 'toJS' },
|
|
113
129
|
},
|
|
114
130
|
{
|
|
115
|
-
flags:
|
|
131
|
+
flags: '--module-name-index <number>',
|
|
116
132
|
description:
|
|
117
|
-
|
|
133
|
+
'determines which path index should be used for routes separation (example: GET:/fruites/getFruit -> index:0 -> moduleName -> fruites)',
|
|
118
134
|
default: codeGenBaseConfig.moduleNameIndex,
|
|
135
|
+
internal: { formatter: (moduleNameIndex) => +moduleNameIndex || 0 },
|
|
119
136
|
},
|
|
120
137
|
{
|
|
121
|
-
flags:
|
|
122
|
-
description:
|
|
138
|
+
flags: '--module-name-first-tag',
|
|
139
|
+
description: 'splits routes based on the first tag',
|
|
123
140
|
default: codeGenBaseConfig.moduleNameFirstTag,
|
|
124
141
|
},
|
|
125
142
|
{
|
|
126
|
-
flags:
|
|
127
|
-
description:
|
|
143
|
+
flags: '--disableStrictSSL',
|
|
144
|
+
description: 'disabled strict SSL',
|
|
128
145
|
default: codeGenBaseConfig.disableStrictSSL,
|
|
146
|
+
internal: { formatter: Boolean },
|
|
129
147
|
},
|
|
130
148
|
{
|
|
131
|
-
flags:
|
|
132
|
-
description:
|
|
149
|
+
flags: '--disableProxy',
|
|
150
|
+
description: 'disabled proxy',
|
|
133
151
|
default: codeGenBaseConfig.disableProxy,
|
|
152
|
+
internal: { formatter: Boolean },
|
|
134
153
|
},
|
|
135
154
|
{
|
|
136
|
-
flags:
|
|
137
|
-
description:
|
|
138
|
-
default: codeGenBaseConfig.httpClientType ===
|
|
155
|
+
flags: '--axios',
|
|
156
|
+
description: 'generate axios http client',
|
|
157
|
+
default: codeGenBaseConfig.httpClientType === HTTP_CLIENT.AXIOS,
|
|
139
158
|
},
|
|
140
159
|
{
|
|
141
|
-
flags:
|
|
142
|
-
description:
|
|
160
|
+
flags: '--unwrap-response-data',
|
|
161
|
+
description: 'unwrap the data item from the response',
|
|
143
162
|
default: codeGenBaseConfig.unwrapResponseData,
|
|
144
163
|
},
|
|
145
164
|
{
|
|
146
|
-
flags:
|
|
147
|
-
description:
|
|
165
|
+
flags: '--disable-throw-on-error',
|
|
166
|
+
description: 'Do not throw an error when response.ok is not true',
|
|
148
167
|
default: codeGenBaseConfig.disableThrowOnError,
|
|
149
168
|
},
|
|
150
169
|
{
|
|
151
|
-
flags:
|
|
152
|
-
description:
|
|
170
|
+
flags: '--single-http-client',
|
|
171
|
+
description: 'Ability to send HttpClient instance to Api constructor',
|
|
153
172
|
default: codeGenBaseConfig.singleHttpClient,
|
|
173
|
+
internal: { formatter: Boolean },
|
|
154
174
|
},
|
|
155
175
|
{
|
|
156
|
-
flags:
|
|
157
|
-
description:
|
|
176
|
+
flags: '--silent',
|
|
177
|
+
description: 'Output only errors to console',
|
|
158
178
|
default: codeGenBaseConfig.silent,
|
|
179
|
+
internal: { formatter: Boolean },
|
|
159
180
|
},
|
|
160
181
|
{
|
|
161
|
-
flags:
|
|
162
|
-
description:
|
|
182
|
+
flags: '--default-response <type>',
|
|
183
|
+
description: 'default type for empty response schema',
|
|
163
184
|
default: codeGenBaseConfig.defaultResponseType,
|
|
185
|
+
internal: { name: 'defaultResponseType' },
|
|
164
186
|
},
|
|
165
187
|
{
|
|
166
|
-
flags:
|
|
167
|
-
description:
|
|
188
|
+
flags: '--type-prefix <string>',
|
|
189
|
+
description: 'data contract name prefix',
|
|
168
190
|
default: codeGenBaseConfig.typePrefix,
|
|
169
191
|
},
|
|
170
192
|
{
|
|
171
|
-
flags:
|
|
172
|
-
description:
|
|
193
|
+
flags: '--type-suffix <string>',
|
|
194
|
+
description: 'data contract name suffix',
|
|
173
195
|
default: codeGenBaseConfig.typeSuffix,
|
|
174
196
|
},
|
|
175
197
|
{
|
|
176
|
-
flags:
|
|
177
|
-
description:
|
|
198
|
+
flags: '--clean-output',
|
|
199
|
+
description:
|
|
200
|
+
'clean output folder before generate api. WARNING: May cause data loss',
|
|
178
201
|
default: codeGenBaseConfig.cleanOutput,
|
|
202
|
+
internal: { formatter: Boolean },
|
|
179
203
|
},
|
|
180
204
|
{
|
|
181
|
-
flags:
|
|
182
|
-
description:
|
|
205
|
+
flags: '--api-class-name <string>',
|
|
206
|
+
description: 'name of the api class',
|
|
183
207
|
default: codeGenBaseConfig.apiClassName,
|
|
184
208
|
},
|
|
185
209
|
{
|
|
186
|
-
flags:
|
|
187
|
-
description:
|
|
210
|
+
flags: '--patch',
|
|
211
|
+
description: 'fix up small errors in the swagger source definition',
|
|
188
212
|
default: codeGenBaseConfig.patch,
|
|
213
|
+
internal: { formatter: Boolean },
|
|
189
214
|
},
|
|
190
215
|
{
|
|
191
|
-
flags:
|
|
192
|
-
description:
|
|
216
|
+
flags: '--debug',
|
|
217
|
+
description: 'additional information about processes inside this tool',
|
|
193
218
|
default: codeGenBaseConfig.debug,
|
|
194
219
|
},
|
|
195
220
|
{
|
|
196
|
-
flags:
|
|
197
|
-
description:
|
|
221
|
+
flags: '--another-array-type',
|
|
222
|
+
description: 'generate array types as Array<Type> (by default Type[])',
|
|
198
223
|
default: codeGenBaseConfig.anotherArrayType,
|
|
199
224
|
},
|
|
200
225
|
{
|
|
201
|
-
flags:
|
|
202
|
-
description:
|
|
226
|
+
flags: '--sort-types',
|
|
227
|
+
description: 'sort fields and types',
|
|
203
228
|
default: codeGenBaseConfig.sortTypes,
|
|
204
229
|
},
|
|
205
230
|
{
|
|
206
|
-
flags:
|
|
207
|
-
description:
|
|
231
|
+
flags: '--extract-enums',
|
|
232
|
+
description:
|
|
233
|
+
'extract all enums from inline interface\\type content to typescript enum construction',
|
|
208
234
|
default: codeGenBaseConfig.extractEnums,
|
|
209
235
|
},
|
|
236
|
+
{
|
|
237
|
+
flags: '--sort-routes',
|
|
238
|
+
description: 'sort routes in alphabetical order',
|
|
239
|
+
default: codeGenBaseConfig.sortRoutes,
|
|
240
|
+
},
|
|
241
|
+
{
|
|
242
|
+
flags: '--custom-config <string>',
|
|
243
|
+
description: 'custom config: primitiveTypeConstructs, hooks, ... ',
|
|
244
|
+
default: '',
|
|
245
|
+
},
|
|
210
246
|
],
|
|
211
247
|
});
|
|
212
248
|
|
|
213
249
|
program.addCommand({
|
|
214
|
-
name:
|
|
250
|
+
name: 'generate-templates',
|
|
215
251
|
description: `Generate ".ejs" templates needed for generate api`,
|
|
216
252
|
options: [
|
|
217
253
|
{
|
|
218
|
-
flags:
|
|
219
|
-
description:
|
|
254
|
+
flags: '-o, --output <string>',
|
|
255
|
+
description: 'output path of generated templates',
|
|
220
256
|
default: templateGenBaseConfig.output,
|
|
221
257
|
},
|
|
222
258
|
{
|
|
223
|
-
flags:
|
|
224
|
-
description:
|
|
259
|
+
flags: '-m, --modular',
|
|
260
|
+
description:
|
|
261
|
+
'generate templates needed to separate files for http client, data contracts, and routes',
|
|
225
262
|
default: templateGenBaseConfig.modular,
|
|
263
|
+
internal: { formatter: Boolean },
|
|
226
264
|
},
|
|
227
265
|
{
|
|
228
|
-
flags:
|
|
229
|
-
description: `http client type (possible values: ${Object.values(
|
|
266
|
+
flags: '--http-client <string>',
|
|
267
|
+
description: `http client type (possible values: ${Object.values(
|
|
268
|
+
HTTP_CLIENT,
|
|
269
|
+
)
|
|
230
270
|
.map((v) => `"${v}"`)
|
|
231
|
-
.join(
|
|
271
|
+
.join(', ')})`,
|
|
232
272
|
default: templateGenBaseConfig.httpClientType,
|
|
273
|
+
internal: { name: 'httpClientType' },
|
|
233
274
|
},
|
|
234
275
|
{
|
|
235
|
-
flags:
|
|
236
|
-
description:
|
|
276
|
+
flags: '-c, --clean-output',
|
|
277
|
+
description:
|
|
278
|
+
'clean output folder before generate template. WARNING: May cause data loss',
|
|
237
279
|
default: templateGenBaseConfig.cleanOutput,
|
|
280
|
+
internal: { formatter: Boolean },
|
|
238
281
|
},
|
|
239
282
|
{
|
|
240
|
-
flags:
|
|
241
|
-
description:
|
|
283
|
+
flags: '-r, --rewrite',
|
|
284
|
+
description: 'rewrite content in existing templates',
|
|
242
285
|
default: templateGenBaseConfig.rewrite,
|
|
286
|
+
internal: { formatter: Boolean },
|
|
243
287
|
},
|
|
244
288
|
{
|
|
245
|
-
flags:
|
|
246
|
-
description:
|
|
289
|
+
flags: '--silent',
|
|
290
|
+
description: 'Output only errors to console',
|
|
247
291
|
default: templateGenBaseConfig.silent,
|
|
292
|
+
internal: { formatter: Boolean },
|
|
248
293
|
},
|
|
249
294
|
],
|
|
250
295
|
});
|
|
@@ -252,59 +297,36 @@ program.addCommand({
|
|
|
252
297
|
const main = async () => {
|
|
253
298
|
const { command, options } = await program.execute({ args: process.argv });
|
|
254
299
|
|
|
300
|
+
let customConfig = null;
|
|
301
|
+
|
|
302
|
+
if (options.customConfig) {
|
|
303
|
+
try {
|
|
304
|
+
const customConfigPath = resolve(process.cwd(), options.customConfig);
|
|
305
|
+
console.log(`✨ found custom config at: ${customConfigPath}`);
|
|
306
|
+
customConfig = require(customConfigPath);
|
|
307
|
+
} catch (e) {
|
|
308
|
+
/* empty */
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
255
312
|
try {
|
|
256
313
|
switch (command) {
|
|
257
314
|
case null: {
|
|
258
315
|
await generateApi({
|
|
316
|
+
...options,
|
|
259
317
|
name: options.name,
|
|
260
318
|
url: options.path,
|
|
261
319
|
generateRouteTypes: options.routeTypes,
|
|
262
320
|
generateClient: !!(options.axios || options.client),
|
|
263
321
|
httpClientType: options.axios ? HTTP_CLIENT.AXIOS : HTTP_CLIENT.FETCH,
|
|
264
|
-
defaultResponseAsSuccess: options.defaultAsSuccess,
|
|
265
|
-
defaultResponseType: options.defaultResponse,
|
|
266
|
-
unwrapResponseData: options.unwrapResponseData,
|
|
267
|
-
disableThrowOnError: options.disableThrowOnError,
|
|
268
|
-
sortTypes: options.sortTypes,
|
|
269
|
-
generateUnionEnums: options.unionEnums,
|
|
270
|
-
addReadonly: options.addReadonly,
|
|
271
|
-
generateResponses: options.responses,
|
|
272
|
-
extractRequestParams: !!options.extractRequestParams,
|
|
273
|
-
extractRequestBody: !!options.extractRequestBody,
|
|
274
|
-
extractResponseBody: !!options.extractResponseBody,
|
|
275
|
-
extractResponseError: !!options.extractResponseError,
|
|
276
322
|
input: resolve(process.cwd(), options.path),
|
|
277
|
-
output: resolve(process.cwd(), options.output ||
|
|
278
|
-
|
|
279
|
-
modular: !!options.modular,
|
|
280
|
-
toJS: !!options.js,
|
|
281
|
-
enumNamesAsValues: options.enumNamesAsValues,
|
|
282
|
-
moduleNameIndex: +(options.moduleNameIndex || 0),
|
|
283
|
-
moduleNameFirstTag: options.moduleNameFirstTag,
|
|
284
|
-
disableStrictSSL: !!options.disableStrictSSL,
|
|
285
|
-
disableProxy: !!options.disableProxy,
|
|
286
|
-
singleHttpClient: !!options.singleHttpClient,
|
|
287
|
-
cleanOutput: !!options.cleanOutput,
|
|
288
|
-
silent: !!options.silent,
|
|
289
|
-
typePrefix: options.typePrefix,
|
|
290
|
-
typeSuffix: options.typeSuffix,
|
|
291
|
-
patch: !!options.patch,
|
|
292
|
-
apiClassName: options.apiClassName,
|
|
293
|
-
debug: options.debug,
|
|
294
|
-
anotherArrayType: options.anotherArrayType,
|
|
295
|
-
extractEnums: options.extractEnums,
|
|
323
|
+
output: resolve(process.cwd(), options.output || '.'),
|
|
324
|
+
...customConfig,
|
|
296
325
|
});
|
|
297
326
|
break;
|
|
298
327
|
}
|
|
299
|
-
case
|
|
300
|
-
await generateTemplates(
|
|
301
|
-
cleanOutput: options.cleanOutput,
|
|
302
|
-
output: options.output,
|
|
303
|
-
httpClientType: options.httpClient,
|
|
304
|
-
modular: options.modular,
|
|
305
|
-
silent: options.silent,
|
|
306
|
-
rewrite: options.rewrite,
|
|
307
|
-
});
|
|
328
|
+
case 'generate-templates': {
|
|
329
|
+
await generateTemplates(options);
|
|
308
330
|
break;
|
|
309
331
|
}
|
|
310
332
|
default: {
|