@hey-api/openapi-ts 0.34.0 → 0.34.1

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.
Files changed (3) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +244 -0
  3. package/package.json +111 -112
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Hey API
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 ADDED
@@ -0,0 +1,244 @@
1
+ # OpenAPI TypeScript 👋
2
+
3
+ > ✨ Turn your OpenAPI specification into a beautiful TypeScript client
4
+
5
+ ## Table of Contents
6
+ - [Table of Contents](#table-of-contents)
7
+ - [About](#about)
8
+ - [Features](#features)
9
+ - [Quick Start](#quick-start)
10
+ - [Installation](#installation)
11
+ - [Configuration](#configuration)
12
+ - [Clients](#clients)
13
+ - [Formatting](#formatting)
14
+ - [Linting](#linting)
15
+ - [Enums](#enums)
16
+ - [Config API](#config-api)
17
+ - [Interceptors](#interceptors)
18
+ - [Migrating](#migrating)
19
+ - [Contributing](#contributing)
20
+
21
+ ## About
22
+
23
+ `openapi-ts` started as a fork of [openapi-typescript-codegen](https://github.com/ferdikoomen/openapi-typescript-codegen). We created it after the original project became [unmaintained](https://github.com/ferdikoomen/openapi-typescript-codegen/issues/1276#issuecomment-1302392146) to add support for OpenAPI v3.1. We plan to resolve the most pressing issues in the original project – open an issue if you'd like to prioritise your use case!
24
+
25
+ ## Features
26
+
27
+ - generate TypeScript clients from OpenAPI v2.0, v3.0, and v3.1 specifications
28
+ - support JSON or YAML input files
29
+ - handle external references using [JSON Schema $Ref Parser](https://github.com/APIDevTools/json-schema-ref-parser/)
30
+ - generate Fetch, Node-Fetch, Axios, Angular, or XHR HTTP clients
31
+ - can be used with CLI, Node.js, or npx
32
+ - abortable requests through cancellable promise pattern
33
+
34
+ ## Quick Start
35
+
36
+ The fastest way to use `openapi-ts` is via npx
37
+
38
+ ```sh
39
+ npx @hey-api/openapi-ts -i path/to/openapi.json -o src/client
40
+ ```
41
+
42
+ Congratulations on creating your first client! 🎉
43
+
44
+ ## Installation
45
+
46
+ ```sh
47
+ npm install @hey-api/openapi-ts --save-dev
48
+ ```
49
+
50
+ or
51
+
52
+ ```sh
53
+ yarn add @hey-api/openapi-ts -D
54
+ ```
55
+
56
+ or
57
+
58
+ ```sh
59
+ pnpm add @hey-api/openapi-ts -D
60
+ ```
61
+
62
+ If you want to use `openapi-ts` with CLI, add a script to your `package.json` file
63
+
64
+ ```json
65
+ "scripts": {
66
+ "openapi-ts": "openapi-ts"
67
+ }
68
+ ```
69
+
70
+ You can also generate your client programmatically by importing `openapi-ts` in a `.ts` file.
71
+
72
+ ```ts
73
+ import { createClient } from '@hey-api/openapi-ts'
74
+
75
+ createClient({
76
+ input: 'path/to/openapi.json',
77
+ output: 'src/client',
78
+ })
79
+ ```
80
+
81
+ > ⚠️ You need to be running Node.js v18 or newer
82
+
83
+ ## Configuration
84
+
85
+ `openapi-ts` supports loading configuration from a file inside your project root directory. You can either create a `openapi-ts.config.cjs` file
86
+
87
+ ```cjs
88
+ /** @type {import('@hey-api/openapi-ts').UserConfig} */
89
+ module.exports = {
90
+ input: 'path/to/openapi.json',
91
+ output: 'src/client',
92
+ }
93
+ ```
94
+
95
+ or `openapi-ts.config.mjs`
96
+
97
+ ```mjs
98
+ /** @type {import('@hey-api/openapi-ts').UserConfig} */
99
+ export default {
100
+ input: 'path/to/openapi.json',
101
+ output: 'src/client',
102
+ }
103
+ ```
104
+
105
+ Alternatively, you can use `openapi-ts.config.js` and configure the export statement depending on your project setup.
106
+
107
+ ### Clients
108
+
109
+ By default, `openapi-ts` will try to guess your client based on your project dependencies. If we don't get it right, you can specify the desired client
110
+
111
+ ```mjs
112
+ /** @type {import('@hey-api/openapi-ts').UserConfig} */
113
+ export default {
114
+ client: 'fetch',
115
+ input: 'path/to/openapi.json',
116
+ output: 'src/client',
117
+ }
118
+ ```
119
+
120
+ We support these clients:
121
+
122
+ - [angular](https://angular.io/) (using [RxJS](https://rxjs.dev/))
123
+ - [axios](https://axios-http.com/)
124
+ - [fetch](https://developer.mozilla.org/docs/Web/API/Fetch_API)
125
+
126
+ We also support the legacy Node.js and XHR clients:
127
+
128
+ - [node](https://nodejs.org/) (using [node-fetch](https://www.npmjs.com/package/node-fetch))
129
+ - [xhr](https://developer.mozilla.org/docs/Web/API/XMLHttpRequest)
130
+
131
+ > ⚠️ You might not need a `node` client. Fetch API is [experimental](https://nodejs.org/docs/latest-v18.x/api/globals.html#fetch) in Node.js v18 and [stable](https://nodejs.org/docs/latest-v21.x/api/globals.html#fetch) in Node.js v21. We recommend upgrading to the latest Node.js version.
132
+
133
+ ### Formatting
134
+
135
+ By default, `openapi-ts` will automatically format your client according to your project configuration. To disable automatic formatting, set `format` to false
136
+
137
+ ```mjs
138
+ /** @type {import('@hey-api/openapi-ts').UserConfig} */
139
+ export default {
140
+ format: false,
141
+ input: 'path/to/openapi.json',
142
+ output: 'src/client',
143
+ }
144
+ ```
145
+
146
+ You can also prevent your client from being processed by formatters by adding your output path to the tool's ignore file (e.g. `.prettierignore`).
147
+
148
+ ### Linting
149
+
150
+ For performance reasons, `openapi-ts` does not automatically lint your client. To enable this feature, set `lint` to true
151
+
152
+ ```mjs
153
+ /** @type {import('@hey-api/openapi-ts').UserConfig} */
154
+ export default {
155
+ input: 'path/to/openapi.json',
156
+ lint: true,
157
+ output: 'src/client',
158
+ }
159
+ ```
160
+
161
+ You can also prevent your client from being processed by linters by adding your output path to the tool's ignore file (e.g. `.eslintignore`).
162
+
163
+ ### Enums
164
+
165
+ If you need to iterate through possible field values without manually typing arrays, you can export enums with
166
+
167
+ ```mjs
168
+ /** @type {import('@hey-api/openapi-ts').UserConfig} */
169
+ export default {
170
+ enums: 'javascript',
171
+ input: 'path/to/openapi.json',
172
+ output: 'src/client',
173
+ }
174
+ ```
175
+
176
+ This will export enums as plain JavaScript objects. For example, `Foo` would become
177
+
178
+ ```ts
179
+ export const FooEnum = {
180
+ FOO: 'foo',
181
+ BAR: 'bar',
182
+ } as const;
183
+ ```
184
+
185
+ We discourage generating [TypeScript enums](https://www.typescriptlang.org/docs/handbook/enums.html) because they are not standard JavaScript and pose [typing challenges](https://dev.to/ivanzm123/dont-use-enums-in-typescript-they-are-very-dangerous-57bh). If you really need TypeScript enums, you can export them with
186
+
187
+ ```mjs
188
+ /** @type {import('@hey-api/openapi-ts').UserConfig} */
189
+ export default {
190
+ enums: 'typescript',
191
+ input: 'path/to/openapi.json',
192
+ output: 'src/client',
193
+ }
194
+ ```
195
+
196
+ ### Config API
197
+
198
+ You can view the complete list of options in the [UserConfig](./src/types/config.ts) interface.
199
+
200
+ ## Interceptors
201
+
202
+ Interceptors (middleware) can be used to modify requests before they're sent or responses before they're returned to the rest of your application. Below is an example request interceptor
203
+
204
+ ```ts
205
+ OpenAPI.interceptors.request.use((request) => {
206
+ doSomethingWithRequest(request)
207
+ return request // <-- must return request
208
+ })
209
+ ```
210
+
211
+ and an example response interceptor
212
+
213
+ ```ts
214
+ OpenAPI.interceptors.response.use(async (response) => {
215
+ await doSomethingWithResponse(response) // async
216
+ return response // <-- must return response
217
+ })
218
+ ```
219
+
220
+ If you need to remove an interceptor, pass the same function to `OpenAPI.interceptors.request.eject()` or `OpenAPI.interceptors.response.eject()`.
221
+
222
+ > ⚠️ Angular client does not currently support request interceptors.
223
+
224
+ ## Migrating
225
+
226
+ While we try to avoid breaking changes, sometimes it's unavoidable in order to offer you the latest features.
227
+
228
+ ### v0.27.38
229
+
230
+ ### `useOptions: true`
231
+
232
+ By default, generated clients will use a single object argument to pass values to API calls. This is a significant change from the previous default of unspecified array of arguments. If migrating your application in one go isn't feasible, we recommend deprecating your old client and generating a new client.
233
+
234
+ ```ts
235
+ import { DefaultService } from 'client' // <-- old client with array arguments
236
+
237
+ import { DefaultService } from 'client_v2' // <-- new client with options argument
238
+ ```
239
+
240
+ This way, you can gradually switch over to the new syntax as you update parts of your code. Once you've removed all instances of `client` imports, you can safely delete the old `client` folder and find and replace all `client_v2` calls to `client`.
241
+
242
+ ## Contributing
243
+
244
+ Please refer to the [contributing guide](CONTRIBUTING.md) for how to install the project for development purposes.
package/package.json CHANGED
@@ -1,113 +1,112 @@
1
1
  {
2
- "name": "@hey-api/openapi-ts",
3
- "version": "0.34.0",
4
- "type": "module",
5
- "description": "Turn your OpenAPI specification into a beautiful TypeScript client",
6
- "homepage": "https://github.com/hey-api/openapi-ts/",
7
- "repository": {
8
- "type": "git",
9
- "url": "git+https://github.com/hey-api/openapi-ts.git"
10
- },
11
- "bugs": {
12
- "url": "https://github.com/hey-api/openapi-ts/issues"
13
- },
14
- "license": "MIT",
15
- "keywords": [
16
- "openapi",
17
- "swagger",
18
- "generator",
19
- "typescript",
20
- "javascript",
21
- "codegen",
22
- "yaml",
23
- "json",
24
- "fetch",
25
- "xhr",
26
- "axios",
27
- "angular",
28
- "node"
29
- ],
30
- "main": "./dist/node/index.js",
31
- "types": "./dist/node/index.d.ts",
32
- "bin": {
33
- "openapi-ts": "bin/index.js"
34
- },
35
- "files": [
36
- "bin",
37
- "dist"
38
- ],
39
- "scripts": {
40
- "build-bundle": "rollup --config rollup.config.ts --configPlugin typescript",
41
- "build-types-check": "tsc --project tsconfig.check.json",
42
- "build-types-roll": "rollup --config rollup.dts.config.ts --configPlugin typescript && rimraf temp",
43
- "build-types-temp": "tsc --emitDeclarationOnly --outDir temp -p src/node",
44
- "build-types": "npm run build-types-temp && npm run build-types-roll && npm run build-types-check",
45
- "build": "npm run clean && npm run build-bundle && npm run build-types",
46
- "clean": "rimraf dist test/generated test/e2e/generated coverage node_modules/.cache",
47
- "dev": "rimraf dist && npm run build-bundle -- --watch",
48
- "lint:fix": "eslint . --fix",
49
- "lint": "eslint .",
50
- "prepublishOnly": "npm run build",
51
- "test:coverage": "vitest run --config vitest.config.unit.ts --coverage",
52
- "test:e2e": "vitest run --config vitest.config.e2e.ts",
53
- "test:sample": "node test/sample.cjs",
54
- "test:update": "vitest watch --config vitest.config.unit.ts --update",
55
- "test:watch": "vitest watch --config vitest.config.unit.ts",
56
- "test": "vitest run --config vitest.config.unit.ts",
57
- "typecheck": "tsc --noEmit",
58
- "changeset": "changeset"
59
- },
60
- "engines": {
61
- "node": "^18.0.0 || >=20.0.0"
62
- },
63
- "dependencies": {
64
- "@apidevtools/json-schema-ref-parser": "11.5.4",
65
- "camelcase": "8.0.0",
66
- "commander": "12.0.0",
67
- "handlebars": "4.7.8"
68
- },
69
- "devDependencies": {
70
- "@angular-devkit/build-angular": "17.3.2",
71
- "@angular/animations": "17.3.2",
72
- "@angular/cli": "17.3.2",
73
- "@angular/common": "17.3.2",
74
- "@angular/compiler": "17.3.2",
75
- "@angular/compiler-cli": "17.3.2",
76
- "@angular/core": "17.3.2",
77
- "@angular/forms": "17.3.2",
78
- "@angular/platform-browser": "17.3.2",
79
- "@angular/platform-browser-dynamic": "17.3.2",
80
- "@angular/router": "17.3.2",
81
- "@rollup/plugin-commonjs": "25.0.7",
82
- "@rollup/plugin-json": "6.1.0",
83
- "@rollup/plugin-node-resolve": "15.2.3",
84
- "@rollup/plugin-terser": "0.4.4",
85
- "@rollup/plugin-typescript": "11.1.6",
86
- "@types/cross-spawn": "6.0.6",
87
- "@types/express": "4.17.21",
88
- "@types/node": "20.12.2",
89
- "@typescript-eslint/eslint-plugin": "7.5.0",
90
- "@typescript-eslint/parser": "7.5.0",
91
- "@vitest/coverage-v8": "1.4.0",
92
- "axios": "1.6.8",
93
- "cross-spawn": "7.0.3",
94
- "eslint": "8.57.0",
95
- "eslint-config-prettier": "9.1.0",
96
- "eslint-plugin-prettier": "5.1.3",
97
- "eslint-plugin-simple-import-sort": "12.0.0",
98
- "eslint-plugin-sort-keys-fix": "1.1.2",
99
- "express": "4.19.2",
100
- "glob": "10.3.12",
101
- "node-fetch": "3.3.2",
102
- "prettier": "3.2.5",
103
- "puppeteer": "22.6.1",
104
- "rimraf": "5.0.5",
105
- "rollup": "4.13.2",
106
- "rollup-plugin-dts": "6.1.0",
107
- "rxjs": "7.8.1",
108
- "ts-node": "10.9.2",
109
- "tslib": "2.6.2",
110
- "typescript": "5.4.3",
111
- "vitest": "1.4.0"
112
- }
113
- }
2
+ "name": "@hey-api/openapi-ts",
3
+ "version": "0.34.1",
4
+ "type": "module",
5
+ "description": "Turn your OpenAPI specification into a beautiful TypeScript client",
6
+ "homepage": "https://github.com/hey-api/openapi-ts/",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/hey-api/openapi-ts.git"
10
+ },
11
+ "bugs": {
12
+ "url": "https://github.com/hey-api/openapi-ts/issues"
13
+ },
14
+ "license": "MIT",
15
+ "keywords": [
16
+ "openapi",
17
+ "swagger",
18
+ "generator",
19
+ "typescript",
20
+ "javascript",
21
+ "codegen",
22
+ "yaml",
23
+ "json",
24
+ "fetch",
25
+ "xhr",
26
+ "axios",
27
+ "angular",
28
+ "node"
29
+ ],
30
+ "main": "./dist/node/index.js",
31
+ "types": "./dist/node/index.d.ts",
32
+ "bin": {
33
+ "openapi-ts": "bin/index.js"
34
+ },
35
+ "files": [
36
+ "bin",
37
+ "dist"
38
+ ],
39
+ "engines": {
40
+ "node": "^18.0.0 || >=20.0.0"
41
+ },
42
+ "dependencies": {
43
+ "@apidevtools/json-schema-ref-parser": "11.5.4",
44
+ "camelcase": "8.0.0",
45
+ "commander": "12.0.0",
46
+ "handlebars": "4.7.8"
47
+ },
48
+ "devDependencies": {
49
+ "@angular-devkit/build-angular": "17.3.3",
50
+ "@angular/animations": "17.3.2",
51
+ "@angular/cli": "17.3.3",
52
+ "@angular/common": "17.3.2",
53
+ "@angular/compiler": "17.3.2",
54
+ "@angular/compiler-cli": "17.3.2",
55
+ "@angular/core": "17.3.2",
56
+ "@angular/forms": "17.3.2",
57
+ "@angular/platform-browser": "17.3.2",
58
+ "@angular/platform-browser-dynamic": "17.3.2",
59
+ "@angular/router": "17.3.2",
60
+ "@rollup/plugin-commonjs": "25.0.7",
61
+ "@rollup/plugin-json": "6.1.0",
62
+ "@rollup/plugin-node-resolve": "15.2.3",
63
+ "@rollup/plugin-terser": "0.4.4",
64
+ "@rollup/plugin-typescript": "11.1.6",
65
+ "@types/cross-spawn": "6.0.6",
66
+ "@types/express": "4.17.21",
67
+ "@types/node": "20.12.3",
68
+ "@typescript-eslint/eslint-plugin": "7.5.0",
69
+ "@typescript-eslint/parser": "7.5.0",
70
+ "@vitest/coverage-v8": "1.4.0",
71
+ "axios": "1.6.8",
72
+ "cross-spawn": "7.0.3",
73
+ "eslint": "8.57.0",
74
+ "eslint-config-prettier": "9.1.0",
75
+ "eslint-plugin-prettier": "5.1.3",
76
+ "eslint-plugin-simple-import-sort": "12.0.0",
77
+ "eslint-plugin-sort-keys-fix": "1.1.2",
78
+ "express": "4.19.2",
79
+ "glob": "10.3.12",
80
+ "node-fetch": "3.3.2",
81
+ "prettier": "3.2.5",
82
+ "puppeteer": "22.6.1",
83
+ "rimraf": "5.0.5",
84
+ "rollup": "4.13.2",
85
+ "rollup-plugin-dts": "6.1.0",
86
+ "rxjs": "7.8.1",
87
+ "ts-node": "10.9.2",
88
+ "tslib": "2.6.2",
89
+ "typescript": "5.4.3",
90
+ "vitest": "1.4.0"
91
+ },
92
+ "scripts": {
93
+ "build-bundle": "rollup --config rollup.config.ts --configPlugin typescript",
94
+ "build-types-check": "tsc --project tsconfig.check.json",
95
+ "build-types-roll": "rollup --config rollup.dts.config.ts --configPlugin typescript && rimraf temp",
96
+ "build-types-temp": "tsc --emitDeclarationOnly --outDir temp -p src/node",
97
+ "build-types": "pnpm build-types-temp && pnpm build-types-roll && pnpm build-types-check",
98
+ "build": "pnpm clean && pnpm build-bundle && pnpm build-types",
99
+ "clean": "rimraf dist test/generated test/e2e/generated coverage node_modules/.cache",
100
+ "dev": "rimraf dist && pnpm build-bundle -- --watch",
101
+ "lint:fix": "eslint . --fix",
102
+ "lint": "eslint .",
103
+ "test:coverage": "vitest run --config vitest.config.unit.ts --coverage",
104
+ "test:e2e": "vitest run --config vitest.config.e2e.ts",
105
+ "test:sample": "node test/sample.cjs",
106
+ "test:update": "vitest watch --config vitest.config.unit.ts --update",
107
+ "test:watch": "vitest watch --config vitest.config.unit.ts",
108
+ "test": "vitest run --config vitest.config.unit.ts",
109
+ "typecheck": "tsc --noEmit",
110
+ "changeset": "changeset"
111
+ }
112
+ }