codegen-openapi-ts 0.8.0-alpha.8 → 0.9.0-alpha.2

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2021 devteaa
3
+ Copyright (c) Ferdi Koomen
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -1,234 +1,85 @@
1
1
  # OpenAPI Typescript Codegen
2
2
 
3
3
  [![NPM][npm-image]][npm-url]
4
- [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
5
- ![Build](https://github.com/devTeaa/codegen-openapi-ts/actions/workflows/CI.yml/badge.svg)
4
+ [![License][license-image]][license-url]
5
+ [![Coverage][coverage-image]][coverage-url]
6
+ [![Coverage][coverage-image]][coverage-url]
7
+ [![Downloads][downloads-image]][downloads-url]
8
+ [![Build][build-image]][build-url]
6
9
 
7
10
  > Node.js library that generates Typescript clients based on the OpenAPI specification.
8
11
 
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
12
  ## Why?
12
13
  - Frontend ❤️ OpenAPI, but we do not want to use JAVA codegen in our builds
13
14
  - Quick, lightweight, robust and framework-agnostic 🚀
14
15
  - 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 generations of Fetch, Node-Fetch, Axios, Angular and XHR http clients
17
+ - Supports OpenAPI specification v2.0 and v3.0
16
18
  - Supports JSON and YAML files for input
17
- - Supports generation through Node.js
19
+ - Supports generation through CLI, Node.js and NPX
18
20
  - 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
- - Support defineConfig (v0.7.0)
29
- - New config model (v0.7.1)
21
+ - Supports aborting of requests (cancelable promise pattern)
22
+ - Supports external references using [json-schema-ref-parser](https://github.com/APIDevTools/json-schema-ref-parser/)
30
23
 
31
24
  ## Install
32
25
 
33
26
  ```
34
- npm install codegen-openapi-ts --save-dev
27
+ npm install openapi-typescript-codegen --save-dev
35
28
  ```
36
29
 
37
-
38
30
  ## Usage
39
31
 
40
32
  ```
41
- codegen-openapi-ts --help
42
- Usage: codegen-openapi-ts [options]
43
-
44
- Options:
45
- -V, --version output the version number
46
- --config <value> Path to config file (default: "codegen.config.js")
47
- -h, --help display help for command
48
- ```
49
-
50
- **codegen.config.js**
51
- ```
52
- export default defineConfig([
53
- {
54
- source: OpenAPI Swagger response (can check on the network response on the spec page),
55
- from: swagger_1, swagger_2, openapi_3, api_blueprint, io_docs, google, raml, wadl,
56
- output: output folder
57
- urlMethodMapping: {
58
- originalUrl: api path,
59
- method: http method (get/post/put/delete),
60
- methodName: output operation name,
61
- proxyUrl?: custom url api path
62
- }[]
63
- selectedOnly: this will make it so only generate services under urlMethodMapping, the default is false,
64
- modelNameMapping: {
65
- fromRegExp: regex model name on schema,
66
- newModelName: output model name
67
- }[]
68
- },
69
- ]);
70
- ```
71
-
72
- ## Example
73
- **codegen.config.js**
74
- ```javascript
75
- module.exports = [
76
- {
77
- source: 'http://pokemon-api/docs/api',
78
- from: 'openapi_3',
79
- output: 'src/api-types/pokemon-api', // pokemon-api
80
- urlMethodMapping: [
81
- {
82
- originalUrl: 'get-pokemon-list/gen1',
83
- method: 'get',
84
- methodName: 'GetPokemonListGen1'
85
- },
86
- {
87
- originalUrl: 'get-pokemon-list/gen2',
88
- method: 'get',
89
- methodName: 'GetPokemonListGen2',
90
- proxyUrl: 'gateway/get-pokemon-list/gen2'
91
- }
92
- ],
93
- selectedOnly: true,
94
- modelNameMapping: [
95
- {
96
- fromRegExp: /some\.custom\.model\.naming/,
97
- newModelName: 'CustomModelNaming'
98
- }
99
- ],
100
- },
101
- {
102
- // source: '<git repo> <branch name> <file to path>',
103
- source: 'ssh://git@github.com/pokemon/pokemon-api.git master -- docs/evolution-path.json',
104
- from: 'openapi_3',
105
- output: 'src/api-types/evolution-path', // evolution-path
106
- },
107
- ];
108
- ```
109
-
110
- **package.json**
111
- ```json
112
- {
113
- "scripts": {
114
- "codegen": "codegen-openapi-ts"
115
- }
116
- }
117
-
118
- // npm run generate
119
- ```
120
- ### Output folder
121
- .
122
- ├── ...
123
- ├── src # output value ('src/api-types/')
124
- │ ├── api-types
125
- │ | ├── pokemon-api # output
126
- │ | | ├── models # API schema models
127
- │ | | ├── services # API service level with methods/url/response/request types
128
- │ | | └── index.ts
129
- | | └── ...
130
- └── ...
131
-
132
-
133
- ## Features
134
- ### Nullable in OpenAPI v2
135
- In the OpenAPI v3 spec you can create properties that can be NULL, by providing a `nullable: true` in your schema.
136
- However, the v2 spec does not allow you to do this. You can use the unofficial `x-nullable` in your specification
137
- to generate nullable properties in OpenApi v2.
138
-
139
- ```json
140
- {
141
- "ModelWithNullableString": {
142
- "required": ["requiredProp"],
143
- "description": "This is a model with one string property",
144
- "type": "object",
145
- "properties": {
146
- "prop": {
147
- "description": "This is a simple string property",
148
- "type": "string",
149
- "x-nullable": true
150
- },
151
- "requiredProp": {
152
- "description": "This is a simple string property",
153
- "type": "string",
154
- "x-nullable": true
155
- }
156
- }
157
- }
158
- }
159
- ```
160
-
161
- Generated code:
162
- ```typescript
163
- interface ModelWithNullableString {
164
- prop?: string | null,
165
- requiredProp: string | null,
166
- }
33
+ $ openapi --help
34
+
35
+ Usage: openapi [options]
36
+
37
+ Options:
38
+ -V, --version output the version number
39
+ -i, --input <value> OpenAPI specification, can be a path, url or string content (required)
40
+ -o, --output <value> Output directory (required)
41
+ -c, --client <value> HTTP client to generate [fetch, xhr, node, axios, angular] (default: "fetch")
42
+ --name <value> Custom client class name
43
+ --useOptions Use options instead of arguments
44
+ --useUnionTypes Use union types instead of enums
45
+ --exportCore <value> Write core files to disk (default: true)
46
+ --exportServices <value> Write services to disk (default: true)
47
+ --exportModels <value> Write models to disk (default: true)
48
+ --exportSchemas <value> Write schemas to disk (default: false)
49
+ --indent <value> Indentation options [4, 2, tab] (default: "4")
50
+ --postfixServices Service name postfix (default: "Service")
51
+ --postfixModels Model name postfix
52
+ --request <value> Path to custom request file
53
+ -h, --help display help for command
54
+
55
+ Examples
56
+ $ openapi --input ./spec.json --output ./generated
57
+ $ openapi --input ./spec.json --output ./generated --client xhr
167
58
  ```
168
59
 
169
- ### References
170
-
171
- Local references to schema definitions (those beginning with `#/definitions/schemas/`)
172
- will be converted to type references to the equivalent, generated top-level type.
173
-
174
- The OpenAPI generator also supports external references, which allows you to break
175
- down your openapi.yml into multiple sub-files, or incorporate third-party schemas
176
- as part of your types to ensure everything is able to be TypeScript generated.
177
-
178
- External references may be:
179
- * *relative references* - references to other files at the same location e.g.
180
- `{ $ref: 'schemas/customer.yml' }`
181
- * *remote references* - fully qualified references to another remote location
182
- e.g. `{ $ref: 'https://myexampledomain.com/schemas/customer_schema.yml' }`
60
+ Documentation
61
+ ===
183
62
 
184
- For remote references, both files (when the file is on the current filesystem)
185
- and http(s) URLs are supported.
63
+ The main documentation can be found in the [openapi-typescript-codegen/wiki](https://github.com/ferdikoomen/openapi-typescript-codegen/wiki)
186
64
 
187
- External references may also contain internal paths in the external schema (e.g.
188
- `schemas/collection.yml#/definitions/schemas/Customer`) and back-references to
189
- the base openapi file or between files (so that you can reference another
190
- schema in the main file as a type of an object or array property, for example).
65
+ Sponsors
66
+ ===
191
67
 
192
- At start-up, an OpenAPI or Swagger file with external references will be "bundled",
193
- so that all external references and back-references will be resolved (but local
194
- references preserved).
68
+ If you or your company use the OpenAPI Typescript Codegen, please consider supporting me. By sponsoring I can free up time to give this project some love! Details can be found here: https://github.com/sponsors/ferdikoomen
195
69
 
70
+ If you're from an enterprise looking for a fully managed SDK generation, please consider our sponsor:
196
71
 
197
- FAQ
198
- ===
199
-
200
- ### Babel support
201
- If you use enums inside your models / definitions then those enums are by default inside a namespace with the same name
202
- as your model. This is called declaration merging. However, the [@babel/plugin-transform-typescript](https://babeljs.io/docs/en/babel-plugin-transform-typescript)
203
- does not support these namespaces, so if you are using babel in your project please use the `--useUnionTypes` flag
204
- to generate union types instead of traditional enums. More info can be found here: [Enums vs. Union Types](#enums-vs-union-types---useuniontypes).
205
-
206
- **Note:** If you are using Babel 7 and Typescript 3.8 (or higher) then you should enable the `onlyRemoveTypeImports` to
207
- ignore any 'type only' imports, see https://babeljs.io/docs/en/babel-preset-typescript#onlyremovetypeimports for more info
208
-
209
- ```javascript
210
- module.exports = {
211
- presets: [
212
- ['@babel/preset-typescript', {
213
- onlyRemoveTypeImports: true,
214
- }],
215
- ],
216
- };
217
- ```
72
+ <a href="https://speakeasyapi.dev/?utm_source=ferdi+repo&utm_medium=github+sponsorship">
73
+ <img alt="speakeasy" src="https://storage.googleapis.com/speakeasy-design-assets/ferdi-sponsorship.png" width="640"/>
74
+ </a>
218
75
 
219
- In order to compile the project and resolve the imports, you will need to enable the `allowSyntheticDefaultImports`
220
- in your `tsconfig.json` file.
221
-
222
-
223
- [npm-url]: https://npmjs.org/package/
224
- [npm-image]: https://img.shields.io/npm/v/codegen-openapi-ts.svg
225
- [coverage-url]: https://codecov.io/gh/ferdikoomen/codegen-openapi-ts
226
- [coverage-image]: https://img.shields.io/codecov/c/github/ferdikoomen/codegen-openapi-ts.svg
227
- [quality-url]: https://lgtm.com/projects/g/ferdikoomen/codegen-openapi-ts
228
- [quality-image]: https://img.shields.io/lgtm/grade/javascript/g/ferdikoomen/codegen-openapi-ts.svg
229
- [climate-url]: https://codeclimate.com/github/ferdikoomen/codegen-openapi-ts
230
- [climate-image]: https://img.shields.io/codeclimate/maintainability/ferdikoomen/codegen-openapi-ts.svg
231
- [downloads-url]: http://npm-stat.com/charts.html?package=codegen-openapi-ts
232
- [downloads-image]: http://img.shields.io/npm/dm/codegen-openapi-ts.svg
233
- [build-url]: https://circleci.com/gh/ferdikoomen/codegen-openapi-ts/tree/master
234
- [build-image]: https://circleci.com/gh/ferdikoomen/codegen-openapi-ts/tree/master.svg?style=svg
76
+ [npm-url]: https://npmjs.org/package/openapi-typescript-codegen
77
+ [npm-image]: https://img.shields.io/npm/v/openapi-typescript-codegen.svg
78
+ [license-url]: LICENSE
79
+ [license-image]: http://img.shields.io/npm/l/openapi-typescript-codegen.svg
80
+ [coverage-url]: https://codecov.io/gh/ferdikoomen/openapi-typescript-codegen
81
+ [coverage-image]: https://img.shields.io/codecov/c/github/ferdikoomen/openapi-typescript-codegen.svg
82
+ [downloads-url]: http://npm-stat.com/charts.html?package=openapi-typescript-codegen
83
+ [downloads-image]: http://img.shields.io/npm/dm/openapi-typescript-codegen.svg
84
+ [build-url]: https://circleci.com/gh/ferdikoomen/openapi-typescript-codegen/tree/master
85
+ [build-image]: https://circleci.com/gh/ferdikoomen/openapi-typescript-codegen/tree/master.svg?style=svg
package/bin/index.js CHANGED
@@ -3,7 +3,7 @@
3
3
  'use strict';
4
4
 
5
5
  const path = require('path');
6
- const program = require('commander');
6
+ const { program } = require('commander');
7
7
  const esmConfig = require('esm-config');
8
8
  const pkg = require('../package.json');
9
9
  const OpenAPI = require(path.resolve(__dirname, '../dist/index.js'));
@@ -38,7 +38,7 @@ async function generateOnConfig () {
38
38
  },
39
39
  configService.urlMethodMapping || [],
40
40
  configService.selectedOnly || false,
41
- configService.modelNameMapping || [],
41
+ configService.modelNameMapping,
42
42
  configFile.appendTemplate,
43
43
  configService.proxyConfig
44
44
  )