codegen-openapi-ts 0.5.9 → 0.7.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 +216 -273
- package/bin/cli.js +35 -35
- package/bin/index.js +49 -48
- package/dist/index.d.ts +72 -0
- package/dist/index.js +1 -1
- package/package.json +113 -111
- package/types/helper.d.ts +58 -58
- package/types/index.d.ts +22 -22
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2021 devteaa
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 devteaa
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,273 +1,216 @@
|
|
|
1
|
-
# OpenAPI Typescript Codegen
|
|
2
|
-
|
|
3
|
-
[![NPM][npm-image]][npm-url]
|
|
4
|
-
[](https://opensource.org/licenses/MIT)
|
|
5
|
-

|
|
6
|
-
|
|
7
|
-
> Node.js library that generates Typescript clients based on the OpenAPI specification.
|
|
8
|
-
|
|
9
|
-
> This project is a fork from [Openapi Typescript Codegen](https://github.com/ferdikoomen/openapi-typescript-codegen) by [Ferdi Koomen](https://github.com/ferdikoomen), the reason is because I need some changes and use some of the lower level generated code that I can use on my projects.
|
|
10
|
-
|
|
11
|
-
## Why?
|
|
12
|
-
- Frontend ❤️ OpenAPI, but we do not want to use JAVA codegen in our builds
|
|
13
|
-
- Quick, lightweight, robust and framework-agnostic 🚀
|
|
14
|
-
- Supports generation of TypeScript clients
|
|
15
|
-
- Supports conversion from Swagger 1.x/2.x to OpenAPI 2.x/3.x with [`api-spec-converter`](https://github.com/LucyBot-Inc/api-spec-converter)
|
|
16
|
-
- Supports JSON and YAML files for input
|
|
17
|
-
- Supports generation through Node.js
|
|
18
|
-
- Supports tsc and @babel/plugin-transform-typescript
|
|
19
|
-
- Supports external references using [`json-schema-ref-parser`](https://github.com/APIDevTools/json-schema-ref-parser/)
|
|
20
|
-
- Supports generate multiple api based on config file
|
|
21
|
-
- Supports only generate specified api based on the url and http method (v0.4.1)
|
|
22
|
-
- Supports custom map model naming (v0.4.2)
|
|
23
|
-
- Supports fetching single file and generate type from that (v0.4.3)
|
|
24
|
-
- Supports custom url request mapping (ex: backend gateway) (v0.5.3)
|
|
25
|
-
- Supports config autocomplete wrapper (v0.5.8)
|
|
26
|
-
- Supports generating relevant models if selectedOnly (v0.5.8)
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
```
|
|
39
|
-
codegen-openapi-ts --help
|
|
40
|
-
Usage: codegen-openapi-ts [options]
|
|
41
|
-
|
|
42
|
-
Options:
|
|
43
|
-
-V, --version output the version number
|
|
44
|
-
--config <value> Path to config file (default: "codegen.config.js")
|
|
45
|
-
-h, --help display help for command
|
|
46
|
-
```
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
{
|
|
52
|
-
source: OpenAPI Swagger response (can check on the network response on the spec page),
|
|
53
|
-
from: swagger_1, swagger_2, openapi_3, api_blueprint, io_docs, google, raml, wadl,
|
|
54
|
-
output: output folder
|
|
55
|
-
urlMethodMapping: [
|
|
56
|
-
[api path, http method (get/post/put/delete), output operation name, custom url api path]
|
|
57
|
-
],
|
|
58
|
-
selectedOnly: this will make it so only generate services under urlMethodMapping, the default is false,
|
|
59
|
-
modelNameMapping: [
|
|
60
|
-
[regex model name on schema, output model name]
|
|
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
|
-
output
|
|
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
|
-
External references may be:
|
|
218
|
-
* *relative references* - references to other files at the same location e.g.
|
|
219
|
-
`{ $ref: 'schemas/customer.yml' }`
|
|
220
|
-
* *remote references* - fully qualified references to another remote location
|
|
221
|
-
e.g. `{ $ref: 'https://myexampledomain.com/schemas/customer_schema.yml' }`
|
|
222
|
-
|
|
223
|
-
For remote references, both files (when the file is on the current filesystem)
|
|
224
|
-
and http(s) URLs are supported.
|
|
225
|
-
|
|
226
|
-
External references may also contain internal paths in the external schema (e.g.
|
|
227
|
-
`schemas/collection.yml#/definitions/schemas/Customer`) and back-references to
|
|
228
|
-
the base openapi file or between files (so that you can reference another
|
|
229
|
-
schema in the main file as a type of an object or array property, for example).
|
|
230
|
-
|
|
231
|
-
At start-up, an OpenAPI or Swagger file with external references will be "bundled",
|
|
232
|
-
so that all external references and back-references will be resolved (but local
|
|
233
|
-
references preserved).
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
FAQ
|
|
237
|
-
===
|
|
238
|
-
|
|
239
|
-
### Babel support
|
|
240
|
-
If you use enums inside your models / definitions then those enums are by default inside a namespace with the same name
|
|
241
|
-
as your model. This is called declaration merging. However, the [@babel/plugin-transform-typescript](https://babeljs.io/docs/en/babel-plugin-transform-typescript)
|
|
242
|
-
does not support these namespaces, so if you are using babel in your project please use the `--useUnionTypes` flag
|
|
243
|
-
to generate union types instead of traditional enums. More info can be found here: [Enums vs. Union Types](#enums-vs-union-types---useuniontypes).
|
|
244
|
-
|
|
245
|
-
**Note:** If you are using Babel 7 and Typescript 3.8 (or higher) then you should enable the `onlyRemoveTypeImports` to
|
|
246
|
-
ignore any 'type only' imports, see https://babeljs.io/docs/en/babel-preset-typescript#onlyremovetypeimports for more info
|
|
247
|
-
|
|
248
|
-
```javascript
|
|
249
|
-
module.exports = {
|
|
250
|
-
presets: [
|
|
251
|
-
['@babel/preset-typescript', {
|
|
252
|
-
onlyRemoveTypeImports: true,
|
|
253
|
-
}],
|
|
254
|
-
],
|
|
255
|
-
};
|
|
256
|
-
```
|
|
257
|
-
|
|
258
|
-
In order to compile the project and resolve the imports, you will need to enable the `allowSyntheticDefaultImports`
|
|
259
|
-
in your `tsconfig.json` file.
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
[npm-url]: https://npmjs.org/package/
|
|
263
|
-
[npm-image]: https://img.shields.io/npm/v/codegen-openapi-ts.svg
|
|
264
|
-
[coverage-url]: https://codecov.io/gh/ferdikoomen/codegen-openapi-ts
|
|
265
|
-
[coverage-image]: https://img.shields.io/codecov/c/github/ferdikoomen/codegen-openapi-ts.svg
|
|
266
|
-
[quality-url]: https://lgtm.com/projects/g/ferdikoomen/codegen-openapi-ts
|
|
267
|
-
[quality-image]: https://img.shields.io/lgtm/grade/javascript/g/ferdikoomen/codegen-openapi-ts.svg
|
|
268
|
-
[climate-url]: https://codeclimate.com/github/ferdikoomen/codegen-openapi-ts
|
|
269
|
-
[climate-image]: https://img.shields.io/codeclimate/maintainability/ferdikoomen/codegen-openapi-ts.svg
|
|
270
|
-
[downloads-url]: http://npm-stat.com/charts.html?package=codegen-openapi-ts
|
|
271
|
-
[downloads-image]: http://img.shields.io/npm/dm/codegen-openapi-ts.svg
|
|
272
|
-
[build-url]: https://circleci.com/gh/ferdikoomen/codegen-openapi-ts/tree/master
|
|
273
|
-
[build-image]: https://circleci.com/gh/ferdikoomen/codegen-openapi-ts/tree/master.svg?style=svg
|
|
1
|
+
# OpenAPI Typescript Codegen
|
|
2
|
+
|
|
3
|
+
[![NPM][npm-image]][npm-url]
|
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
|
5
|
+

|
|
6
|
+
|
|
7
|
+
> Node.js library that generates Typescript clients based on the OpenAPI specification.
|
|
8
|
+
|
|
9
|
+
> This project is a fork from [Openapi Typescript Codegen](https://github.com/ferdikoomen/openapi-typescript-codegen) by [Ferdi Koomen](https://github.com/ferdikoomen), the reason is because I need some changes and use some of the lower level generated code that I can use on my projects.
|
|
10
|
+
|
|
11
|
+
## Why?
|
|
12
|
+
- Frontend ❤️ OpenAPI, but we do not want to use JAVA codegen in our builds
|
|
13
|
+
- Quick, lightweight, robust and framework-agnostic 🚀
|
|
14
|
+
- Supports generation of TypeScript clients
|
|
15
|
+
- Supports conversion from Swagger 1.x/2.x to OpenAPI 2.x/3.x with [`api-spec-converter`](https://github.com/LucyBot-Inc/api-spec-converter)
|
|
16
|
+
- Supports JSON and YAML files for input
|
|
17
|
+
- Supports generation through Node.js
|
|
18
|
+
- Supports tsc and @babel/plugin-transform-typescript
|
|
19
|
+
- Supports external references using [`json-schema-ref-parser`](https://github.com/APIDevTools/json-schema-ref-parser/)
|
|
20
|
+
- Supports generate multiple api based on config file
|
|
21
|
+
- Supports only generate specified api based on the url and http method (v0.4.1)
|
|
22
|
+
- Supports custom map model naming (v0.4.2)
|
|
23
|
+
- Supports fetching single file and generate type from that (v0.4.3)
|
|
24
|
+
- Supports custom url request mapping (ex: backend gateway) (v0.5.3)
|
|
25
|
+
- Supports config autocomplete wrapper (v0.5.8)
|
|
26
|
+
- Supports generating relevant models if selectedOnly (v0.5.8)
|
|
27
|
+
- Supports esm config (v0.6.0)
|
|
28
|
+
|
|
29
|
+
## Install
|
|
30
|
+
|
|
31
|
+
```
|
|
32
|
+
npm install codegen-openapi-ts --save-dev
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
## Usage
|
|
37
|
+
|
|
38
|
+
```
|
|
39
|
+
codegen-openapi-ts --help
|
|
40
|
+
Usage: codegen-openapi-ts [options]
|
|
41
|
+
|
|
42
|
+
Options:
|
|
43
|
+
-V, --version output the version number
|
|
44
|
+
--config <value> Path to config file (default: "codegen.config.js")
|
|
45
|
+
-h, --help display help for command
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
**codegen.config.js**
|
|
49
|
+
```
|
|
50
|
+
export default defineConfig([
|
|
51
|
+
{
|
|
52
|
+
source: OpenAPI Swagger response (can check on the network response on the spec page),
|
|
53
|
+
from: swagger_1, swagger_2, openapi_3, api_blueprint, io_docs, google, raml, wadl,
|
|
54
|
+
output: output folder
|
|
55
|
+
urlMethodMapping: [
|
|
56
|
+
[api path, http method (get/post/put/delete), output operation name, custom url api path]
|
|
57
|
+
],
|
|
58
|
+
selectedOnly: this will make it so only generate services under urlMethodMapping, the default is false,
|
|
59
|
+
modelNameMapping: [
|
|
60
|
+
[regex model name on schema, output model name]
|
|
61
|
+
]
|
|
62
|
+
},
|
|
63
|
+
]);
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Example
|
|
67
|
+
**codegen.config.js**
|
|
68
|
+
```javascript
|
|
69
|
+
module.exports = [
|
|
70
|
+
{
|
|
71
|
+
source: 'http://pokemon-api/docs/api',
|
|
72
|
+
from: 'openapi_3',
|
|
73
|
+
output: 'src/api-types/pokemon-api', // pokemon-api
|
|
74
|
+
urlMethodMapping: [
|
|
75
|
+
['get-pokemon-list/gen1', 'get', 'GetPokemonListGen1'],
|
|
76
|
+
['get-pokemon-list/gen2', 'get', 'GetPokemonListGen2', 'gateway/get-pokemon-list/gen2'],
|
|
77
|
+
],
|
|
78
|
+
selectedOnly: true,
|
|
79
|
+
modelNameMapping: [
|
|
80
|
+
['some.custom.model.naming', 'CustomModelNaming']
|
|
81
|
+
],
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
// source: '<git repo> <branch name> <file to path>',
|
|
85
|
+
source: 'ssh://git@github.com/pokemon/pokemon-api.git master -- docs/evolution-path.json',
|
|
86
|
+
from: 'openapi_3',
|
|
87
|
+
output: 'src/api-types/evolution-path', // evolution-path
|
|
88
|
+
},
|
|
89
|
+
];
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
**package.json**
|
|
93
|
+
```json
|
|
94
|
+
{
|
|
95
|
+
"scripts": {
|
|
96
|
+
"codegen": "codegen-openapi-ts"
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// npm run generate
|
|
101
|
+
```
|
|
102
|
+
### Output folder
|
|
103
|
+
.
|
|
104
|
+
├── ...
|
|
105
|
+
├── src # output value ('src/api-types/')
|
|
106
|
+
│ ├── api-types
|
|
107
|
+
│ | ├── pokemon-api # output
|
|
108
|
+
│ | | ├── models # API schema models
|
|
109
|
+
│ | | ├── services # API service level with methods/url/response/request types
|
|
110
|
+
│ | | └── index.ts
|
|
111
|
+
| | └── ...
|
|
112
|
+
└── ...
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
## Features
|
|
116
|
+
### Nullable in OpenAPI v2
|
|
117
|
+
In the OpenAPI v3 spec you can create properties that can be NULL, by providing a `nullable: true` in your schema.
|
|
118
|
+
However, the v2 spec does not allow you to do this. You can use the unofficial `x-nullable` in your specification
|
|
119
|
+
to generate nullable properties in OpenApi v2.
|
|
120
|
+
|
|
121
|
+
```json
|
|
122
|
+
{
|
|
123
|
+
"ModelWithNullableString": {
|
|
124
|
+
"required": ["requiredProp"],
|
|
125
|
+
"description": "This is a model with one string property",
|
|
126
|
+
"type": "object",
|
|
127
|
+
"properties": {
|
|
128
|
+
"prop": {
|
|
129
|
+
"description": "This is a simple string property",
|
|
130
|
+
"type": "string",
|
|
131
|
+
"x-nullable": true
|
|
132
|
+
},
|
|
133
|
+
"requiredProp": {
|
|
134
|
+
"description": "This is a simple string property",
|
|
135
|
+
"type": "string",
|
|
136
|
+
"x-nullable": true
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
Generated code:
|
|
144
|
+
```typescript
|
|
145
|
+
interface ModelWithNullableString {
|
|
146
|
+
prop?: string | null,
|
|
147
|
+
requiredProp: string | null,
|
|
148
|
+
}
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
### References
|
|
152
|
+
|
|
153
|
+
Local references to schema definitions (those beginning with `#/definitions/schemas/`)
|
|
154
|
+
will be converted to type references to the equivalent, generated top-level type.
|
|
155
|
+
|
|
156
|
+
The OpenAPI generator also supports external references, which allows you to break
|
|
157
|
+
down your openapi.yml into multiple sub-files, or incorporate third-party schemas
|
|
158
|
+
as part of your types to ensure everything is able to be TypeScript generated.
|
|
159
|
+
|
|
160
|
+
External references may be:
|
|
161
|
+
* *relative references* - references to other files at the same location e.g.
|
|
162
|
+
`{ $ref: 'schemas/customer.yml' }`
|
|
163
|
+
* *remote references* - fully qualified references to another remote location
|
|
164
|
+
e.g. `{ $ref: 'https://myexampledomain.com/schemas/customer_schema.yml' }`
|
|
165
|
+
|
|
166
|
+
For remote references, both files (when the file is on the current filesystem)
|
|
167
|
+
and http(s) URLs are supported.
|
|
168
|
+
|
|
169
|
+
External references may also contain internal paths in the external schema (e.g.
|
|
170
|
+
`schemas/collection.yml#/definitions/schemas/Customer`) and back-references to
|
|
171
|
+
the base openapi file or between files (so that you can reference another
|
|
172
|
+
schema in the main file as a type of an object or array property, for example).
|
|
173
|
+
|
|
174
|
+
At start-up, an OpenAPI or Swagger file with external references will be "bundled",
|
|
175
|
+
so that all external references and back-references will be resolved (but local
|
|
176
|
+
references preserved).
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
FAQ
|
|
180
|
+
===
|
|
181
|
+
|
|
182
|
+
### Babel support
|
|
183
|
+
If you use enums inside your models / definitions then those enums are by default inside a namespace with the same name
|
|
184
|
+
as your model. This is called declaration merging. However, the [@babel/plugin-transform-typescript](https://babeljs.io/docs/en/babel-plugin-transform-typescript)
|
|
185
|
+
does not support these namespaces, so if you are using babel in your project please use the `--useUnionTypes` flag
|
|
186
|
+
to generate union types instead of traditional enums. More info can be found here: [Enums vs. Union Types](#enums-vs-union-types---useuniontypes).
|
|
187
|
+
|
|
188
|
+
**Note:** If you are using Babel 7 and Typescript 3.8 (or higher) then you should enable the `onlyRemoveTypeImports` to
|
|
189
|
+
ignore any 'type only' imports, see https://babeljs.io/docs/en/babel-preset-typescript#onlyremovetypeimports for more info
|
|
190
|
+
|
|
191
|
+
```javascript
|
|
192
|
+
module.exports = {
|
|
193
|
+
presets: [
|
|
194
|
+
['@babel/preset-typescript', {
|
|
195
|
+
onlyRemoveTypeImports: true,
|
|
196
|
+
}],
|
|
197
|
+
],
|
|
198
|
+
};
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
In order to compile the project and resolve the imports, you will need to enable the `allowSyntheticDefaultImports`
|
|
202
|
+
in your `tsconfig.json` file.
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
[npm-url]: https://npmjs.org/package/
|
|
206
|
+
[npm-image]: https://img.shields.io/npm/v/codegen-openapi-ts.svg
|
|
207
|
+
[coverage-url]: https://codecov.io/gh/ferdikoomen/codegen-openapi-ts
|
|
208
|
+
[coverage-image]: https://img.shields.io/codecov/c/github/ferdikoomen/codegen-openapi-ts.svg
|
|
209
|
+
[quality-url]: https://lgtm.com/projects/g/ferdikoomen/codegen-openapi-ts
|
|
210
|
+
[quality-image]: https://img.shields.io/lgtm/grade/javascript/g/ferdikoomen/codegen-openapi-ts.svg
|
|
211
|
+
[climate-url]: https://codeclimate.com/github/ferdikoomen/codegen-openapi-ts
|
|
212
|
+
[climate-image]: https://img.shields.io/codeclimate/maintainability/ferdikoomen/codegen-openapi-ts.svg
|
|
213
|
+
[downloads-url]: http://npm-stat.com/charts.html?package=codegen-openapi-ts
|
|
214
|
+
[downloads-image]: http://img.shields.io/npm/dm/codegen-openapi-ts.svg
|
|
215
|
+
[build-url]: https://circleci.com/gh/ferdikoomen/codegen-openapi-ts/tree/master
|
|
216
|
+
[build-image]: https://circleci.com/gh/ferdikoomen/codegen-openapi-ts/tree/master.svg?style=svg
|
package/bin/cli.js
CHANGED
|
@@ -1,35 +1,35 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
'use strict';
|
|
4
|
-
|
|
5
|
-
const path = require('path');
|
|
6
|
-
const program = require('commander');
|
|
7
|
-
const pkg = require('../package.json');
|
|
8
|
-
|
|
9
|
-
const params = program
|
|
10
|
-
.name('codegen-openapi-ts-cli')
|
|
11
|
-
.usage('[options]')
|
|
12
|
-
.version(pkg.version)
|
|
13
|
-
.argument('<from>', 'Original response specification version')
|
|
14
|
-
.argument('<source>', 'Swagger/OpenAPI response url')
|
|
15
|
-
.argument('[output]', 'Output folder name', 'output')
|
|
16
|
-
.parse(process.argv)
|
|
17
|
-
.processedArgs;
|
|
18
|
-
|
|
19
|
-
const OpenAPI = require(path.resolve(__dirname, '../dist/index.js'));
|
|
20
|
-
|
|
21
|
-
if (OpenAPI) {
|
|
22
|
-
OpenAPI.convertAndGenerate(
|
|
23
|
-
{
|
|
24
|
-
from: params[0],
|
|
25
|
-
to: 'openapi_3',
|
|
26
|
-
source: params[1]
|
|
27
|
-
},
|
|
28
|
-
{
|
|
29
|
-
input: 'api-schema.json',
|
|
30
|
-
output: params[2],
|
|
31
|
-
useOptions: true,
|
|
32
|
-
useUnionTypes: true
|
|
33
|
-
},
|
|
34
|
-
)
|
|
35
|
-
}
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
const path = require('path');
|
|
6
|
+
const program = require('commander');
|
|
7
|
+
const pkg = require('../package.json');
|
|
8
|
+
|
|
9
|
+
const params = program
|
|
10
|
+
.name('codegen-openapi-ts-cli')
|
|
11
|
+
.usage('[options]')
|
|
12
|
+
.version(pkg.version)
|
|
13
|
+
.argument('<from>', 'Original response specification version')
|
|
14
|
+
.argument('<source>', 'Swagger/OpenAPI response url')
|
|
15
|
+
.argument('[output]', 'Output folder name', 'output')
|
|
16
|
+
.parse(process.argv)
|
|
17
|
+
.processedArgs;
|
|
18
|
+
|
|
19
|
+
const OpenAPI = require(path.resolve(__dirname, '../dist/index.js'));
|
|
20
|
+
|
|
21
|
+
if (OpenAPI) {
|
|
22
|
+
OpenAPI.convertAndGenerate(
|
|
23
|
+
{
|
|
24
|
+
from: params[0],
|
|
25
|
+
to: 'openapi_3',
|
|
26
|
+
source: params[1]
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
input: 'api-schema.json',
|
|
30
|
+
output: params[2],
|
|
31
|
+
useOptions: true,
|
|
32
|
+
useUnionTypes: true
|
|
33
|
+
},
|
|
34
|
+
)
|
|
35
|
+
}
|