swagger-typescript-api 10.0.2 β 10.0.3
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/README.md +45 -41
- package/index.d.ts +4 -0
- package/index.js +115 -115
- package/package.json +4 -1
- package/src/apiConfig.js +30 -30
- package/src/common.js +28 -28
- package/src/config.js +2 -0
- package/src/filePrefix.js +14 -14
- package/src/index.js +271 -271
- package/src/logger.js +68 -59
- package/src/modelNames.js +74 -78
- package/src/modelTypes.js +31 -31
- package/src/output.js +165 -165
- package/src/prettierOptions.js +23 -23
- package/src/render/utils/fmtToJSDocLine.js +10 -10
- package/src/render/utils/index.js +31 -31
- package/src/render/utils/templateRequire.js +17 -17
- package/src/routeNames.js +46 -46
- package/src/templates.js +155 -155
- package/src/translators/JavaScript.js +49 -60
- package/src/typeFormatters.js +121 -121
- package/src/utils/id.js +9 -9
- package/src/utils/random.js +14 -14
- package/src/utils/resolveName.js +94 -97
- package/templates/base/route-docs.ejs +1 -2
- package/templates/default/procedure-call.ejs +2 -2
- package/templates/modular/procedure-call.ejs +2 -2
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/swagger-typescript-api)
|
|
4
4
|
[](https://github.com/acacode/swagger-typescript-api/actions/workflows/main.yml) <!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
|
|
5
|
-
[](#contributors)
|
|
6
6
|
<!-- ALL-CONTRIBUTORS-BADGE:END -->
|
|
7
7
|
|
|
8
8
|
<img src="https://raw.githubusercontent.com/acacode/swagger-typescript-api/master/assets/swagger-typescript-api-logo.png" align="left"
|
|
@@ -120,6 +120,7 @@ generateApi({
|
|
|
120
120
|
enumNamesAsValues: false,
|
|
121
121
|
moduleNameFirstTag: false,
|
|
122
122
|
generateUnionEnums: false,
|
|
123
|
+
addReadonly: false,
|
|
123
124
|
extraTemplates: [],
|
|
124
125
|
hooks: {
|
|
125
126
|
onCreateComponent: (component) => {},
|
|
@@ -222,46 +223,49 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
|
|
|
222
223
|
<!-- prettier-ignore-start -->
|
|
223
224
|
<!-- markdownlint-disable -->
|
|
224
225
|
<table>
|
|
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
|
-
|
|
226
|
+
<tbody>
|
|
227
|
+
<tr>
|
|
228
|
+
<td align="center"><a href="https://github.com/js2me"><img src="https://avatars1.githubusercontent.com/u/16340911?v=4?s=100" width="100px;" alt="Sergey S. Volkov"/><br /><sub><b>Sergey S. Volkov</b></sub></a><br /><a href="https://github.com/acacode/swagger-typescript-api/commits?author=js2me" title="Code">π»</a> <a href="https://github.com/acacode/swagger-typescript-api/commits?author=js2me" title="Documentation">π</a> <a href="#design-js2me" title="Design">π¨</a> <a href="#example-js2me" title="Examples">π‘</a> <a href="#maintenance-js2me" title="Maintenance">π§</a> <a href="#ideas-js2me" title="Ideas, Planning, & Feedback">π€</a> <a href="https://github.com/acacode/swagger-typescript-api/issues?q=author%3Ajs2me" title="Bug reports">π</a></td>
|
|
229
|
+
<td align="center"><a href="https://github.com/andrefilimono"><img src="https://avatars0.githubusercontent.com/u/7794526?v=4?s=100" width="100px;" alt="Filimonov Andrey"/><br /><sub><b>Filimonov Andrey</b></sub></a><br /><a href="https://github.com/acacode/swagger-typescript-api/commits?author=andrefilimono" title="Code">π»</a> <a href="#ideas-andrefilimono" title="Ideas, Planning, & Feedback">π€</a> <a href="#design-andrefilimono" title="Design">π¨</a></td>
|
|
230
|
+
<td align="center"><a href="https://github.com/Fl0pZz"><img src="https://avatars2.githubusercontent.com/u/9510124?v=4?s=100" width="100px;" alt="Rafael Fakhreev"/><br /><sub><b>Rafael Fakhreev</b></sub></a><br /><a href="https://github.com/acacode/swagger-typescript-api/commits?author=Fl0pZz" title="Code">π»</a> <a href="#ideas-Fl0pZz" title="Ideas, Planning, & Feedback">π€</a></td>
|
|
231
|
+
<td align="center"><a href="https://azzola.dev"><img src="https://avatars3.githubusercontent.com/u/1297597?v=4?s=100" width="100px;" alt="Lucas Azzola"/><br /><sub><b>Lucas Azzola</b></sub></a><br /><a href="https://github.com/acacode/swagger-typescript-api/commits?author=azz" title="Code">π»</a> <a href="#ideas-azz" title="Ideas, Planning, & Feedback">π€</a> <a href="#design-azz" title="Design">π¨</a></td>
|
|
232
|
+
<td align="center"><a href="https://github.com/JennieJi"><img src="https://avatars3.githubusercontent.com/u/1913045?v=4?s=100" width="100px;" alt="Jennie"/><br /><sub><b>Jennie</b></sub></a><br /><a href="https://github.com/acacode/swagger-typescript-api/commits?author=JennieJi" title="Code">π»</a> <a href="#ideas-JennieJi" title="Ideas, Planning, & Feedback">π€</a></td>
|
|
233
|
+
<td align="center"><a href="https://github.com/jomarquez21"><img src="https://avatars1.githubusercontent.com/u/16705169?v=4?s=100" width="100px;" alt="Jose Enrique Marquez"/><br /><sub><b>Jose Enrique Marquez</b></sub></a><br /><a href="https://github.com/acacode/swagger-typescript-api/commits?author=jomarquez21" title="Code">π»</a> <a href="https://github.com/acacode/swagger-typescript-api/issues?q=author%3Ajomarquez21" title="Bug reports">π</a></td>
|
|
234
|
+
<td align="center"><a href="https://glassechidna.com.au"><img src="https://avatars1.githubusercontent.com/u/482276?v=4?s=100" width="100px;" alt="Benjamin Dobell"/><br /><sub><b>Benjamin Dobell</b></sub></a><br /><a href="https://github.com/acacode/swagger-typescript-api/commits?author=Benjamin-Dobell" title="Code">π»</a> <a href="https://github.com/acacode/swagger-typescript-api/issues?q=author%3ABenjamin-Dobell" title="Bug reports">π</a></td>
|
|
235
|
+
</tr>
|
|
236
|
+
<tr>
|
|
237
|
+
<td align="center"><a href="http://fixate.it"><img src="https://avatars0.githubusercontent.com/u/1510520?v=4?s=100" width="100px;" alt="Larry Botha"/><br /><sub><b>Larry Botha</b></sub></a><br /><a href="https://github.com/acacode/swagger-typescript-api/commits?author=larrybotha" title="Code">π»</a> <a href="https://github.com/acacode/swagger-typescript-api/issues?q=author%3Alarrybotha" title="Bug reports">π</a></td>
|
|
238
|
+
<td align="center"><a href="https://github.com/nikalun"><img src="https://avatars3.githubusercontent.com/u/13102962?v=4?s=100" width="100px;" alt="Nikolay Lukinykh"/><br /><sub><b>Nikolay Lukinykh</b></sub></a><br /><a href="https://github.com/acacode/swagger-typescript-api/commits?author=nikalun" title="Code">π»</a> <a href="#ideas-nikalun" title="Ideas, Planning, & Feedback">π€</a> <a href="https://github.com/acacode/swagger-typescript-api/issues?q=author%3Anikalun" title="Bug reports">π</a></td>
|
|
239
|
+
<td align="center"><a href="https://github.com/Mvbraathen"><img src="https://avatars0.githubusercontent.com/u/16756739?v=4?s=100" width="100px;" alt="Marius BrΓ₯then"/><br /><sub><b>Marius BrΓ₯then</b></sub></a><br /><a href="#security-Mvbraathen" title="Security">π‘οΈ</a></td>
|
|
240
|
+
<td align="center"><a href="https://github.com/xesjkeee"><img src="https://avatars2.githubusercontent.com/u/17751886?v=4?s=100" width="100px;" alt="Evgeny Vlasov"/><br /><sub><b>Evgeny Vlasov</b></sub></a><br /><a href="#ideas-xesjkeee" title="Ideas, Planning, & Feedback">π€</a></td>
|
|
241
|
+
<td align="center"><a href="https://github.com/kel666"><img src="https://avatars1.githubusercontent.com/u/2040661?v=4?s=100" width="100px;" alt="Fabio"/><br /><sub><b>Fabio</b></sub></a><br /><a href="https://github.com/acacode/swagger-typescript-api/issues?q=author%3Akel666" title="Bug reports">π</a> <a href="https://github.com/acacode/swagger-typescript-api/commits?author=kel666" title="Code">π»</a></td>
|
|
242
|
+
<td align="center"><a href="https://github.com/Fabiencdp"><img src="https://avatars.githubusercontent.com/u/6182473?v=4?s=100" width="100px;" alt="Fabien"/><br /><sub><b>Fabien</b></sub></a><br /><a href="https://github.com/acacode/swagger-typescript-api/issues?q=author%3AFabiencdp" title="Bug reports">π</a></td>
|
|
243
|
+
<td align="center"><a href="https://about.me/julienrousseau"><img src="https://avatars.githubusercontent.com/u/3296671?v=4?s=100" width="100px;" alt="Rousseau Julien"/><br /><sub><b>Rousseau Julien</b></sub></a><br /><a href="https://github.com/acacode/swagger-typescript-api/issues?q=author%3ARoXuS" title="Bug reports">π</a></td>
|
|
244
|
+
</tr>
|
|
245
|
+
<tr>
|
|
246
|
+
<td align="center"><a href="http://sebastianarias.dev"><img src="https://avatars.githubusercontent.com/u/9751266?v=4?s=100" width="100px;" alt="SebastiΓ‘n Arias"/><br /><sub><b>SebastiΓ‘n Arias</b></sub></a><br /><a href="https://github.com/acacode/swagger-typescript-api/issues?q=author%3ALarox" title="Bug reports">π</a></td>
|
|
247
|
+
<td align="center"><a href="https://github.com/Styn"><img src="https://avatars.githubusercontent.com/u/6705137?v=4?s=100" width="100px;" alt="Stijn Lammens"/><br /><sub><b>Stijn Lammens</b></sub></a><br /><a href="https://github.com/acacode/swagger-typescript-api/issues?q=author%3AStyn" title="Bug reports">π</a> <a href="https://github.com/acacode/swagger-typescript-api/commits?author=Styn" title="Code">π»</a></td>
|
|
248
|
+
<td align="center"><a href="http://emilecantin.com"><img src="https://avatars.githubusercontent.com/u/885486?v=4?s=100" width="100px;" alt="Emile Cantin"/><br /><sub><b>Emile Cantin</b></sub></a><br /><a href="https://github.com/acacode/swagger-typescript-api/issues?q=author%3Aemilecantin" title="Bug reports">π</a> <a href="https://github.com/acacode/swagger-typescript-api/commits?author=emilecantin" title="Code">π»</a></td>
|
|
249
|
+
<td align="center"><a href="https://github.com/armsnyder"><img src="https://avatars.githubusercontent.com/u/9969202?v=4?s=100" width="100px;" alt="Adam Snyder"/><br /><sub><b>Adam Snyder</b></sub></a><br /><a href="https://github.com/acacode/swagger-typescript-api/commits?author=armsnyder" title="Code">π»</a> <a href="https://github.com/acacode/swagger-typescript-api/issues?q=author%3Aarmsnyder" title="Bug reports">π</a></td>
|
|
250
|
+
<td align="center"><a href="https://github.com/jnpoyser"><img src="https://avatars.githubusercontent.com/u/7920428?v=4?s=100" width="100px;" alt="James Poyser"/><br /><sub><b>James Poyser</b></sub></a><br /><a href="https://github.com/acacode/swagger-typescript-api/commits?author=jnpoyser" title="Code">π»</a> <a href="#ideas-jnpoyser" title="Ideas, Planning, & Feedback">π€</a></td>
|
|
251
|
+
<td align="center"><a href="http://ru.linkedin.com/in/lisikhin"><img src="https://avatars.githubusercontent.com/u/475367?v=4?s=100" width="100px;" alt="Alexey"/><br /><sub><b>Alexey</b></sub></a><br /><a href="https://github.com/acacode/swagger-typescript-api/issues?q=author%3ANihisil" title="Bug reports">π</a></td>
|
|
252
|
+
<td align="center"><a href="http://imaniu.com"><img src="https://avatars.githubusercontent.com/u/50100681?v=4?s=100" width="100px;" alt="ζ±ιΊ»ε¦"/><br /><sub><b>ζ±ιΊ»ε¦</b></sub></a><br /><a href="https://github.com/acacode/swagger-typescript-api/commits?author=jiangmaniu" title="Code">π»</a></td>
|
|
253
|
+
</tr>
|
|
254
|
+
<tr>
|
|
255
|
+
<td align="center"><a href="https://kspr.dev"><img src="https://avatars.githubusercontent.com/u/5294519?v=4?s=100" width="100px;" alt="Kasper Moskwiak"/><br /><sub><b>Kasper Moskwiak</b></sub></a><br /><a href="https://github.com/acacode/swagger-typescript-api/commits?author=kmoskwiak" title="Code">π»</a> <a href="#ideas-kmoskwiak" title="Ideas, Planning, & Feedback">π€</a></td>
|
|
256
|
+
<td align="center"><a href="https://github.com/baggoedw"><img src="https://avatars.githubusercontent.com/u/92381702?v=4?s=100" width="100px;" alt="baggoedw"/><br /><sub><b>baggoedw</b></sub></a><br /><a href="https://github.com/acacode/swagger-typescript-api/commits?author=baggoedw" title="Code">π»</a></td>
|
|
257
|
+
<td align="center"><a href="https://marcusdunn.github.io"><img src="https://avatars.githubusercontent.com/u/51931484?v=4?s=100" width="100px;" alt="Marcus Dunn"/><br /><sub><b>Marcus Dunn</b></sub></a><br /><a href="https://github.com/acacode/swagger-typescript-api/commits?author=MarcusDunn" title="Code">π»</a> <a href="https://github.com/acacode/swagger-typescript-api/issues?q=author%3AMarcusDunn" title="Bug reports">π</a></td>
|
|
258
|
+
<td align="center"><a href="https://www.danielplayfaircal.com/"><img src="https://avatars.githubusercontent.com/u/1217649?v=4?s=100" width="100px;" alt="Daniel Playfair Cal"/><br /><sub><b>Daniel Playfair Cal</b></sub></a><br /><a href="https://github.com/acacode/swagger-typescript-api/commits?author=hedgepigdaniel" title="Code">π»</a> <a href="https://github.com/acacode/swagger-typescript-api/issues?q=author%3Ahedgepigdaniel" title="Bug reports">π</a></td>
|
|
259
|
+
<td align="center"><a href="https://www.linkedin.com/in/patrick-shaw/"><img src="https://avatars.githubusercontent.com/u/5153619?v=4?s=100" width="100px;" alt="Patrick Shaw"/><br /><sub><b>Patrick Shaw</b></sub></a><br /><a href="https://github.com/acacode/swagger-typescript-api/pulls?q=is%3Apr+reviewed-by%3APatrickShaw" title="Reviewed Pull Requests">π</a></td>
|
|
260
|
+
<td align="center"><a href="https://www.linkedin.com/in/jinkwon-lee"><img src="https://avatars.githubusercontent.com/u/1798916?v=4?s=100" width="100px;" alt="JK"/><br /><sub><b>JK</b></sub></a><br /><a href="https://github.com/acacode/swagger-typescript-api/commits?author=jinkwon" title="Code">π»</a></td>
|
|
261
|
+
<td align="center"><a href="https://github.com/RoCat"><img src="https://avatars.githubusercontent.com/u/3562317?v=4?s=100" width="100px;" alt="RoCat"/><br /><sub><b>RoCat</b></sub></a><br /><a href="https://github.com/acacode/swagger-typescript-api/commits?author=RoCat" title="Code">π»</a> <a href="#ideas-RoCat" title="Ideas, Planning, & Feedback">π€</a> <a href="#design-RoCat" title="Design">π¨</a></td>
|
|
262
|
+
</tr>
|
|
263
|
+
<tr>
|
|
264
|
+
<td align="center"><a href="https://github.com/ApacheEx"><img src="https://avatars.githubusercontent.com/u/1918108?v=4?s=100" width="100px;" alt="Oleg Kuzava"/><br /><sub><b>Oleg Kuzava</b></sub></a><br /><a href="https://github.com/acacode/swagger-typescript-api/commits?author=ApacheEx" title="Code">π»</a> <a href="https://github.com/acacode/swagger-typescript-api/issues?q=author%3AApacheEx" title="Bug reports">π</a></td>
|
|
265
|
+
<td align="center"><a href="http://nikz.se"><img src="https://avatars.githubusercontent.com/u/7352072?v=4?s=100" width="100px;" alt="Niklas Frank"/><br /><sub><b>Niklas Frank</b></sub></a><br /><a href="https://github.com/acacode/swagger-typescript-api/commits?author=nksfrank" title="Code">π»</a> <a href="https://github.com/acacode/swagger-typescript-api/issues?q=author%3Anksfrank" title="Bug reports">π</a></td>
|
|
266
|
+
<td align="center"><a href="https://quentinbrunet.com"><img src="https://avatars.githubusercontent.com/u/20137632?v=4?s=100" width="100px;" alt="Quentin Brunet"/><br /><sub><b>Quentin Brunet</b></sub></a><br /><a href="https://github.com/acacode/swagger-typescript-api/commits?author=qboot" title="Code">π»</a></td>
|
|
267
|
+
</tr>
|
|
268
|
+
</tbody>
|
|
265
269
|
</table>
|
|
266
270
|
|
|
267
271
|
<!-- markdownlint-restore -->
|
package/index.d.ts
CHANGED
|
@@ -123,6 +123,10 @@ interface GenerateApiParamsBase {
|
|
|
123
123
|
* authorization token
|
|
124
124
|
*/
|
|
125
125
|
authorizationToken?: string;
|
|
126
|
+
/**
|
|
127
|
+
* generate readonly properties (default: false)
|
|
128
|
+
*/
|
|
129
|
+
addReadonly?: boolean;
|
|
126
130
|
}
|
|
127
131
|
|
|
128
132
|
interface GenerateApiParamsFromPath extends GenerateApiParamsBase {
|
package/index.js
CHANGED
|
@@ -1,115 +1,115 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
// Copyright (c) 2019-present acacode
|
|
4
|
-
// Node module: swagger-typescript-api
|
|
5
|
-
// This file is licensed under the MIT License.
|
|
6
|
-
// License text available at https://opensource.org/licenses/MIT
|
|
7
|
-
// Repository https://github.com/acacode/swagger-typescript-api
|
|
8
|
-
|
|
9
|
-
const { Command } = require("commander");
|
|
10
|
-
const { resolve } = require("path");
|
|
11
|
-
const { generateApi } = require("./src");
|
|
12
|
-
const { version, name: packageName } = require("./package.json");
|
|
13
|
-
const { TS_KEYWORDS, HTTP_CLIENT } = require("./src/constants");
|
|
14
|
-
|
|
15
|
-
const program = new Command(packageName);
|
|
16
|
-
|
|
17
|
-
const options = program
|
|
18
|
-
.alias("sta")
|
|
19
|
-
.version(version, "-v, --version", "output the current version")
|
|
20
|
-
.description("Generate api via swagger scheme.\nSupports OA 3.0, 2.0, JSON, yaml.")
|
|
21
|
-
.requiredOption("-p, --path <string>", "path/url to swagger scheme")
|
|
22
|
-
.option("-o, --output <string>", "output path of typescript api file", "./")
|
|
23
|
-
.option("-n, --name <string>", "name of output typescript api file", "Api.ts")
|
|
24
|
-
.option("-t, --templates <string>", "path to folder containing templates")
|
|
25
|
-
.option(
|
|
26
|
-
"-d, --default-as-success",
|
|
27
|
-
'use "default" response status code as success response too.\n' +
|
|
28
|
-
'some swagger schemas use "default" response status code as success response type by default.',
|
|
29
|
-
false,
|
|
30
|
-
)
|
|
31
|
-
.option(
|
|
32
|
-
"-r, --responses",
|
|
33
|
-
"generate additional information about request responses\n" + "also add typings for bad responses",
|
|
34
|
-
false,
|
|
35
|
-
)
|
|
36
|
-
.option("--union-enums", 'generate all "enum" types as union types (T1 | T2 | TN)', false)
|
|
37
|
-
.option("--add-readonly", "generate readonly properties", false)
|
|
38
|
-
.option("--route-types", "generate type definitions for API routes", false)
|
|
39
|
-
.option("--no-client", "do not generate an API class", true)
|
|
40
|
-
.option("--enum-names-as-values", "use values in 'x-enumNames' as enum values (not only as keys)", false)
|
|
41
|
-
.option(
|
|
42
|
-
"--extract-request-params",
|
|
43
|
-
"extract request params to data contract (Also combine path params and query params into one object)",
|
|
44
|
-
false,
|
|
45
|
-
)
|
|
46
|
-
.option("--extract-request-body", "extract request body type to data contract", false)
|
|
47
|
-
.option("--extract-response-body", "extract response body type to data contract", false)
|
|
48
|
-
.option("--extract-response-error", "extract response error type to data contract", false)
|
|
49
|
-
.option("--modular", "generate separated files for http client, data contracts, and routes", false)
|
|
50
|
-
.option("--js", "generate js api module with declaration file", false)
|
|
51
|
-
.option(
|
|
52
|
-
"--module-name-index <number>",
|
|
53
|
-
"determines which path index should be used for routes separation (example: GET:/fruites/getFruit -> index:0 -> moduleName -> fruites)",
|
|
54
|
-
0,
|
|
55
|
-
)
|
|
56
|
-
.option("--module-name-first-tag", "splits routes based on the first tag", false)
|
|
57
|
-
.option("--disableStrictSSL", "disabled strict SSL", false)
|
|
58
|
-
.option("--disableProxy", "disabled proxy", false)
|
|
59
|
-
.option("--axios", "generate axios http client", false)
|
|
60
|
-
.option("--unwrap-response-data", "unwrap the data item from the response", false)
|
|
61
|
-
.option("--disable-throw-on-error", "Do not throw an error when response.ok is not true", false)
|
|
62
|
-
.option("--single-http-client", "Ability to send HttpClient instance to Api constructor", false)
|
|
63
|
-
.option("--silent", "Output only errors to console", false)
|
|
64
|
-
.option("--default-response <type>", "default type for empty response schema", TS_KEYWORDS.VOID)
|
|
65
|
-
.option("--type-prefix <string>", "data contract name prefix", "")
|
|
66
|
-
.option("--type-suffix <string>", "data contract name suffix", "")
|
|
67
|
-
.option("--clean-output", "clean output folder before generate api. WARNING: May cause data loss", false)
|
|
68
|
-
.option("--api-class-name <string>", "name of the api class")
|
|
69
|
-
.option("--patch", "fix up small errors in the swagger source definition", false)
|
|
70
|
-
.option("--debug", "additional information about processes inside this tool", false)
|
|
71
|
-
.parse(process.argv)
|
|
72
|
-
.opts();
|
|
73
|
-
|
|
74
|
-
generateApi({
|
|
75
|
-
name: options.name,
|
|
76
|
-
url: options.path,
|
|
77
|
-
generateRouteTypes: options.routeTypes,
|
|
78
|
-
generateClient: !!(options.axios || options.client),
|
|
79
|
-
httpClientType: options.axios ? HTTP_CLIENT.AXIOS : HTTP_CLIENT.FETCH,
|
|
80
|
-
defaultResponseAsSuccess: options.defaultAsSuccess,
|
|
81
|
-
defaultResponseType: options.defaultResponse,
|
|
82
|
-
unwrapResponseData: options.unwrapResponseData,
|
|
83
|
-
disableThrowOnError: options.disableThrowOnError,
|
|
84
|
-
sortTypes: options.sortTypes,
|
|
85
|
-
generateUnionEnums: options.unionEnums,
|
|
86
|
-
addReadonly: options.addReadonly,
|
|
87
|
-
generateResponses: options.responses,
|
|
88
|
-
extractRequestParams: !!options.extractRequestParams,
|
|
89
|
-
extractRequestBody: !!options.extractRequestBody,
|
|
90
|
-
extractResponseBody: !!options.extractResponseBody,
|
|
91
|
-
extractResponseError: !!options.extractResponseError,
|
|
92
|
-
input: resolve(process.cwd(), options.path),
|
|
93
|
-
output: resolve(process.cwd(), options.output || "."),
|
|
94
|
-
templates: options.templates,
|
|
95
|
-
modular: !!options.modular,
|
|
96
|
-
toJS: !!options.js,
|
|
97
|
-
enumNamesAsValues: options.enumNamesAsValues,
|
|
98
|
-
moduleNameIndex: +(options.moduleNameIndex || 0),
|
|
99
|
-
moduleNameFirstTag: options.moduleNameFirstTag,
|
|
100
|
-
disableStrictSSL: !!options.disableStrictSSL,
|
|
101
|
-
disableProxy: !!options.disableProxy,
|
|
102
|
-
singleHttpClient: !!options.singleHttpClient,
|
|
103
|
-
cleanOutput: !!options.cleanOutput,
|
|
104
|
-
silent: !!options.silent,
|
|
105
|
-
typePrefix: options.typePrefix,
|
|
106
|
-
typeSuffix: options.typeSuffix,
|
|
107
|
-
patch: !!options.patch,
|
|
108
|
-
apiClassName: options.apiClassName,
|
|
109
|
-
debug: options.debug,
|
|
110
|
-
}).catch((err) => {
|
|
111
|
-
// NOTE collect all errors on top level and shows to users in any case
|
|
112
|
-
console.error(err);
|
|
113
|
-
|
|
114
|
-
process.exit(1);
|
|
115
|
-
});
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// Copyright (c) 2019-present acacode
|
|
4
|
+
// Node module: swagger-typescript-api
|
|
5
|
+
// This file is licensed under the MIT License.
|
|
6
|
+
// License text available at https://opensource.org/licenses/MIT
|
|
7
|
+
// Repository https://github.com/acacode/swagger-typescript-api
|
|
8
|
+
|
|
9
|
+
const { Command } = require("commander");
|
|
10
|
+
const { resolve } = require("path");
|
|
11
|
+
const { generateApi } = require("./src");
|
|
12
|
+
const { version, name: packageName } = require("./package.json");
|
|
13
|
+
const { TS_KEYWORDS, HTTP_CLIENT } = require("./src/constants");
|
|
14
|
+
|
|
15
|
+
const program = new Command(packageName);
|
|
16
|
+
|
|
17
|
+
const options = program
|
|
18
|
+
.alias("sta")
|
|
19
|
+
.version(version, "-v, --version", "output the current version")
|
|
20
|
+
.description("Generate api via swagger scheme.\nSupports OA 3.0, 2.0, JSON, yaml.")
|
|
21
|
+
.requiredOption("-p, --path <string>", "path/url to swagger scheme")
|
|
22
|
+
.option("-o, --output <string>", "output path of typescript api file", "./")
|
|
23
|
+
.option("-n, --name <string>", "name of output typescript api file", "Api.ts")
|
|
24
|
+
.option("-t, --templates <string>", "path to folder containing templates")
|
|
25
|
+
.option(
|
|
26
|
+
"-d, --default-as-success",
|
|
27
|
+
'use "default" response status code as success response too.\n' +
|
|
28
|
+
'some swagger schemas use "default" response status code as success response type by default.',
|
|
29
|
+
false,
|
|
30
|
+
)
|
|
31
|
+
.option(
|
|
32
|
+
"-r, --responses",
|
|
33
|
+
"generate additional information about request responses\n" + "also add typings for bad responses",
|
|
34
|
+
false,
|
|
35
|
+
)
|
|
36
|
+
.option("--union-enums", 'generate all "enum" types as union types (T1 | T2 | TN)', false)
|
|
37
|
+
.option("--add-readonly", "generate readonly properties", false)
|
|
38
|
+
.option("--route-types", "generate type definitions for API routes", false)
|
|
39
|
+
.option("--no-client", "do not generate an API class", true)
|
|
40
|
+
.option("--enum-names-as-values", "use values in 'x-enumNames' as enum values (not only as keys)", false)
|
|
41
|
+
.option(
|
|
42
|
+
"--extract-request-params",
|
|
43
|
+
"extract request params to data contract (Also combine path params and query params into one object)",
|
|
44
|
+
false,
|
|
45
|
+
)
|
|
46
|
+
.option("--extract-request-body", "extract request body type to data contract", false)
|
|
47
|
+
.option("--extract-response-body", "extract response body type to data contract", false)
|
|
48
|
+
.option("--extract-response-error", "extract response error type to data contract", false)
|
|
49
|
+
.option("--modular", "generate separated files for http client, data contracts, and routes", false)
|
|
50
|
+
.option("--js", "generate js api module with declaration file", false)
|
|
51
|
+
.option(
|
|
52
|
+
"--module-name-index <number>",
|
|
53
|
+
"determines which path index should be used for routes separation (example: GET:/fruites/getFruit -> index:0 -> moduleName -> fruites)",
|
|
54
|
+
0,
|
|
55
|
+
)
|
|
56
|
+
.option("--module-name-first-tag", "splits routes based on the first tag", false)
|
|
57
|
+
.option("--disableStrictSSL", "disabled strict SSL", false)
|
|
58
|
+
.option("--disableProxy", "disabled proxy", false)
|
|
59
|
+
.option("--axios", "generate axios http client", false)
|
|
60
|
+
.option("--unwrap-response-data", "unwrap the data item from the response", false)
|
|
61
|
+
.option("--disable-throw-on-error", "Do not throw an error when response.ok is not true", false)
|
|
62
|
+
.option("--single-http-client", "Ability to send HttpClient instance to Api constructor", false)
|
|
63
|
+
.option("--silent", "Output only errors to console", false)
|
|
64
|
+
.option("--default-response <type>", "default type for empty response schema", TS_KEYWORDS.VOID)
|
|
65
|
+
.option("--type-prefix <string>", "data contract name prefix", "")
|
|
66
|
+
.option("--type-suffix <string>", "data contract name suffix", "")
|
|
67
|
+
.option("--clean-output", "clean output folder before generate api. WARNING: May cause data loss", false)
|
|
68
|
+
.option("--api-class-name <string>", "name of the api class")
|
|
69
|
+
.option("--patch", "fix up small errors in the swagger source definition", false)
|
|
70
|
+
.option("--debug", "additional information about processes inside this tool", false)
|
|
71
|
+
.parse(process.argv)
|
|
72
|
+
.opts();
|
|
73
|
+
|
|
74
|
+
generateApi({
|
|
75
|
+
name: options.name,
|
|
76
|
+
url: options.path,
|
|
77
|
+
generateRouteTypes: options.routeTypes,
|
|
78
|
+
generateClient: !!(options.axios || options.client),
|
|
79
|
+
httpClientType: options.axios ? HTTP_CLIENT.AXIOS : HTTP_CLIENT.FETCH,
|
|
80
|
+
defaultResponseAsSuccess: options.defaultAsSuccess,
|
|
81
|
+
defaultResponseType: options.defaultResponse,
|
|
82
|
+
unwrapResponseData: options.unwrapResponseData,
|
|
83
|
+
disableThrowOnError: options.disableThrowOnError,
|
|
84
|
+
sortTypes: options.sortTypes,
|
|
85
|
+
generateUnionEnums: options.unionEnums,
|
|
86
|
+
addReadonly: options.addReadonly,
|
|
87
|
+
generateResponses: options.responses,
|
|
88
|
+
extractRequestParams: !!options.extractRequestParams,
|
|
89
|
+
extractRequestBody: !!options.extractRequestBody,
|
|
90
|
+
extractResponseBody: !!options.extractResponseBody,
|
|
91
|
+
extractResponseError: !!options.extractResponseError,
|
|
92
|
+
input: resolve(process.cwd(), options.path),
|
|
93
|
+
output: resolve(process.cwd(), options.output || "."),
|
|
94
|
+
templates: options.templates,
|
|
95
|
+
modular: !!options.modular,
|
|
96
|
+
toJS: !!options.js,
|
|
97
|
+
enumNamesAsValues: options.enumNamesAsValues,
|
|
98
|
+
moduleNameIndex: +(options.moduleNameIndex || 0),
|
|
99
|
+
moduleNameFirstTag: options.moduleNameFirstTag,
|
|
100
|
+
disableStrictSSL: !!options.disableStrictSSL,
|
|
101
|
+
disableProxy: !!options.disableProxy,
|
|
102
|
+
singleHttpClient: !!options.singleHttpClient,
|
|
103
|
+
cleanOutput: !!options.cleanOutput,
|
|
104
|
+
silent: !!options.silent,
|
|
105
|
+
typePrefix: options.typePrefix,
|
|
106
|
+
typeSuffix: options.typeSuffix,
|
|
107
|
+
patch: !!options.patch,
|
|
108
|
+
apiClassName: options.apiClassName,
|
|
109
|
+
debug: options.debug,
|
|
110
|
+
}).catch((err) => {
|
|
111
|
+
// NOTE collect all errors on top level and shows to users in any case
|
|
112
|
+
console.error(err);
|
|
113
|
+
|
|
114
|
+
process.exit(1);
|
|
115
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "swagger-typescript-api",
|
|
3
|
-
"version": "10.0.
|
|
3
|
+
"version": "10.0.3",
|
|
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",
|
|
@@ -10,8 +10,10 @@
|
|
|
10
10
|
"contributors": "all-contributors generate",
|
|
11
11
|
"cli:help": "node index.js -h",
|
|
12
12
|
"test-all": "node --unhandled-rejections=strict ./scriptsRunner.js generate validate test:*",
|
|
13
|
+
"test-all(update-snapshots)": "cross-env UPDATE_SNAPSHOTS=true node --unhandled-rejections=strict ./scriptsRunner.js generate validate test:*",
|
|
13
14
|
"test-all-extended": "node --unhandled-rejections=strict ./scriptsRunner.js generate-extended validate generate validate test:*",
|
|
14
15
|
"test-specific": "node ./scriptsRunner.js generate validate test:*",
|
|
16
|
+
"test-specific-only": "node ./scriptsRunner.js test:*",
|
|
15
17
|
"prepare": "npm run test-all-extended",
|
|
16
18
|
"generate": "node tests/generate.js",
|
|
17
19
|
"generate-extended": "node tests/generate-extended.js",
|
|
@@ -52,6 +54,7 @@
|
|
|
52
54
|
"typings": "./index.d.ts",
|
|
53
55
|
"main": "src/index.js",
|
|
54
56
|
"devDependencies": {
|
|
57
|
+
"cross-env": "^7.0.3",
|
|
55
58
|
"@types/axios": "^0.14.0",
|
|
56
59
|
"@types/lodash": "^4.14.182",
|
|
57
60
|
"@types/node": "^15.0.2",
|
package/src/apiConfig.js
CHANGED
|
@@ -1,30 +1,30 @@
|
|
|
1
|
-
const _ = require("lodash");
|
|
2
|
-
|
|
3
|
-
const createApiConfig = (swaggerSchema) => {
|
|
4
|
-
const { info, servers, host, basePath, externalDocs, tags } = swaggerSchema;
|
|
5
|
-
const server = (servers && servers[0]) || { url: "" };
|
|
6
|
-
const { title = "No title", version, description: schemaDescription = "" } = info || {};
|
|
7
|
-
const { url: serverUrl } = server;
|
|
8
|
-
|
|
9
|
-
return {
|
|
10
|
-
info: info || {},
|
|
11
|
-
servers: servers || [],
|
|
12
|
-
basePath,
|
|
13
|
-
host,
|
|
14
|
-
externalDocs: _.merge(
|
|
15
|
-
{
|
|
16
|
-
url: "",
|
|
17
|
-
description: "",
|
|
18
|
-
},
|
|
19
|
-
externalDocs,
|
|
20
|
-
),
|
|
21
|
-
tags: _.compact(tags),
|
|
22
|
-
baseUrl: serverUrl,
|
|
23
|
-
title,
|
|
24
|
-
version,
|
|
25
|
-
};
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
module.exports = {
|
|
29
|
-
createApiConfig,
|
|
30
|
-
};
|
|
1
|
+
const _ = require("lodash");
|
|
2
|
+
|
|
3
|
+
const createApiConfig = (swaggerSchema) => {
|
|
4
|
+
const { info, servers, host, basePath, externalDocs, tags } = swaggerSchema;
|
|
5
|
+
const server = (servers && servers[0]) || { url: "" };
|
|
6
|
+
const { title = "No title", version, description: schemaDescription = "" } = info || {};
|
|
7
|
+
const { url: serverUrl } = server;
|
|
8
|
+
|
|
9
|
+
return {
|
|
10
|
+
info: info || {},
|
|
11
|
+
servers: servers || [],
|
|
12
|
+
basePath,
|
|
13
|
+
host,
|
|
14
|
+
externalDocs: _.merge(
|
|
15
|
+
{
|
|
16
|
+
url: "",
|
|
17
|
+
description: "",
|
|
18
|
+
},
|
|
19
|
+
externalDocs,
|
|
20
|
+
),
|
|
21
|
+
tags: _.compact(tags),
|
|
22
|
+
baseUrl: serverUrl,
|
|
23
|
+
title,
|
|
24
|
+
version,
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
module.exports = {
|
|
29
|
+
createApiConfig,
|
|
30
|
+
};
|
package/src/common.js
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
const _ = require("lodash");
|
|
2
|
-
|
|
3
|
-
module.exports = {
|
|
4
|
-
formatDescription: (description, inline) => {
|
|
5
|
-
if (!description) return "";
|
|
6
|
-
|
|
7
|
-
let prettified = description;
|
|
8
|
-
|
|
9
|
-
prettified = _.replace(prettified, /\*\//g, "*/");
|
|
10
|
-
|
|
11
|
-
const hasMultipleLines = _.includes(prettified, "\n");
|
|
12
|
-
|
|
13
|
-
if (!hasMultipleLines) return prettified;
|
|
14
|
-
|
|
15
|
-
if (inline) {
|
|
16
|
-
return _(prettified)
|
|
17
|
-
.split(/\n/g)
|
|
18
|
-
.map((part) => _.trim(part))
|
|
19
|
-
.compact()
|
|
20
|
-
.join(" ")
|
|
21
|
-
.valueOf();
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
return _.replace(prettified, /\n$/g, "");
|
|
25
|
-
},
|
|
26
|
-
internalCase: (value) => _.camelCase(_.lowerCase(value)),
|
|
27
|
-
classNameCase: (value) => _.upperFirst(_.camelCase(value)),
|
|
28
|
-
};
|
|
1
|
+
const _ = require("lodash");
|
|
2
|
+
|
|
3
|
+
module.exports = {
|
|
4
|
+
formatDescription: (description, inline) => {
|
|
5
|
+
if (!description) return "";
|
|
6
|
+
|
|
7
|
+
let prettified = description;
|
|
8
|
+
|
|
9
|
+
prettified = _.replace(prettified, /\*\//g, "*/");
|
|
10
|
+
|
|
11
|
+
const hasMultipleLines = _.includes(prettified, "\n");
|
|
12
|
+
|
|
13
|
+
if (!hasMultipleLines) return prettified;
|
|
14
|
+
|
|
15
|
+
if (inline) {
|
|
16
|
+
return _(prettified)
|
|
17
|
+
.split(/\n/g)
|
|
18
|
+
.map((part) => _.trim(part))
|
|
19
|
+
.compact()
|
|
20
|
+
.join(" ")
|
|
21
|
+
.valueOf();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return _.replace(prettified, /\n$/g, "");
|
|
25
|
+
},
|
|
26
|
+
internalCase: (value) => _.camelCase(_.lowerCase(value)),
|
|
27
|
+
classNameCase: (value) => _.upperFirst(_.camelCase(value)),
|
|
28
|
+
};
|
package/src/config.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
const { HTTP_CLIENT, TS_KEYWORDS, PRETTIER_OPTIONS } = require("./constants");
|
|
2
2
|
const { NameResolver } = require("./utils/resolveName");
|
|
3
|
+
const packageJson = require("../package.json");
|
|
3
4
|
|
|
4
5
|
const config = {
|
|
6
|
+
version: packageJson.version,
|
|
5
7
|
/** CLI flag */
|
|
6
8
|
templates: "../templates/default",
|
|
7
9
|
/** CLI flag */
|
package/src/filePrefix.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
module.exports = {
|
|
2
|
-
filePrefix: `/* eslint-disable */
|
|
3
|
-
/* tslint:disable */
|
|
4
|
-
/*
|
|
5
|
-
* ---------------------------------------------------------------
|
|
6
|
-
* ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ##
|
|
7
|
-
* ## ##
|
|
8
|
-
* ## AUTHOR: acacode ##
|
|
9
|
-
* ## SOURCE: https://github.com/acacode/swagger-typescript-api ##
|
|
10
|
-
* ---------------------------------------------------------------
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
`,
|
|
14
|
-
};
|
|
1
|
+
module.exports = {
|
|
2
|
+
filePrefix: `/* eslint-disable */
|
|
3
|
+
/* tslint:disable */
|
|
4
|
+
/*
|
|
5
|
+
* ---------------------------------------------------------------
|
|
6
|
+
* ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ##
|
|
7
|
+
* ## ##
|
|
8
|
+
* ## AUTHOR: acacode ##
|
|
9
|
+
* ## SOURCE: https://github.com/acacode/swagger-typescript-api ##
|
|
10
|
+
* ---------------------------------------------------------------
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
`,
|
|
14
|
+
};
|