This repository has been archived by the owner on Oct 25, 2023. It is now read-only.
forked from acacode/swagger-typescript-api
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
executable file
·140 lines (130 loc) · 4.98 KB
/
index.js
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
#!/usr/bin/env node
// Copyright (c) 2019-present acacode
// Node module: swagger-typescript-api
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
// Repository https://github.com/acacode/swagger-typescript-api
const { Command } = require("commander");
const { resolve } = require("path");
const { generateApi } = require("./src");
const { version, name: packageName } = require("./package.json");
const { TS_KEYWORDS, HTTP_CLIENT } = require("./src/constants");
const program = new Command(packageName);
program.storeOptionsAsProperties(true);
program
.version(version, "-v, --version", "output the current version")
.description("Generate api via swagger scheme.\nSupports OA 3.0, 2.0, JSON, yaml.");
program
.requiredOption("-p, --path <path>", "path/url to swagger scheme")
.option("-o, --output <output>", "output path of typescript api file", "./")
.option("-n, --name <name>", "name of output typescript api file", "Api.ts")
.option("-t, --templates <path>", "path to folder containing templates")
.option(
"-d, --default-as-success",
'use "default" response status code as success response too.\n' +
'some swagger schemas use "default" response status code as success response type by default.',
false,
)
.option(
"-r, --responses",
"generate additional information about request responses\n" + "also add typings for bad responses",
false,
)
.option("--union-enums", 'generate all "enum" types as union types (T1 | T2 | TN)', false)
.option("--route-types", "generate type definitions for API routes", false)
.option("--no-client", "do not generate an API class", false)
.option("--enum-names-as-values", "use values in 'x-enumNames' as enum values (not only as keys)", false)
.option(
"--extract-request-params",
"extract request params to data contract (Also combine path params and query params into one object)",
false,
)
.option("--extract-request-body", "extract request body type to data contract", false)
.option("--modular", "generate separated files for http client, data contracts, and routes", false)
.option("--js", "generate js api module with declaration file", false)
.option(
"--module-name-index <number>",
"determines which path index should be used for routes separation (example: GET:/fruites/getFruit -> index:0 -> moduleName -> fruites)",
0,
)
.option("--module-name-first-tag", "splits routes based on the first tag", false)
.option("--disableStrictSSL", "disabled strict SSL", false)
.option("--disableProxy", "disabled proxy", false)
.option("--axios", "generate axios http client", false)
.option("--unwrap-response-data", "unwrap the data item from the response", false)
.option("--disable-throw-on-error", "Do not throw an error when response.ok is not true", false)
.option("--single-http-client", "Ability to send HttpClient instance to Api constructor", false)
.option("--silent", "Output only errors to console", false)
.option("--default-response <type>", "default type for empty response schema", TS_KEYWORDS.VOID)
.option("--type-prefix <string>", "data contract name prefix", "")
.option("--type-suffix <string>", "data contract name suffix", "")
.option("--clean-output", "clean output folder before generate api. WARNING: May cause data loss", false)
.option("--patch", "fix up small errors in the swagger source definition", false);
program.parse(process.argv);
const {
path,
output,
name,
templates,
unionEnums,
routeTypes,
client,
defaultAsSuccess,
responses,
modular,
js,
moduleNameIndex,
moduleNameFirstTag,
extractRequestParams,
extractRequestBody,
enumNamesAsValues,
disableStrictSSL,
disableProxy,
cleanOutput,
defaultResponse,
unwrapResponseData,
disableThrowOnError,
sortTypes,
singleHttpClient,
axios,
silent,
typePrefix,
typeSuffix,
patch,
} = program;
generateApi({
name,
url: path,
generateRouteTypes: routeTypes,
generateClient: !!(axios || client),
httpClientType: axios ? HTTP_CLIENT.AXIOS : HTTP_CLIENT.FETCH,
defaultResponseAsSuccess: defaultAsSuccess,
defaultResponseType: defaultResponse,
unwrapResponseData: unwrapResponseData,
disableThrowOnError: disableThrowOnError,
sortTypes: sortTypes,
generateUnionEnums: unionEnums,
generateResponses: responses,
extractRequestParams: !!extractRequestParams,
extractRequestBody: !!extractRequestBody,
input: resolve(process.cwd(), path),
output: resolve(process.cwd(), output || "."),
templates,
modular: !!modular,
toJS: !!js,
enumNamesAsValues: enumNamesAsValues,
moduleNameIndex: +(moduleNameIndex || 0),
moduleNameFirstTag: moduleNameFirstTag,
disableStrictSSL: !!disableStrictSSL,
disableProxy: !!disableProxy,
singleHttpClient: !!singleHttpClient,
cleanOutput: !!cleanOutput,
silent: !!silent,
typePrefix,
typeSuffix,
patch: !!patch,
}).catch((err) => {
// NOTE collect all errors on top level and shows to users in any case
console.error(err);
process.exit(1);
});