@hey-api/openapi-ts 0.31.0 → 0.32.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/README.md +37 -40
- package/bin/index.js +51 -20
- package/dist/node/index.d.ts +12 -7
- package/dist/node/index.js +1 -1
- package/package.json +13 -13
package/README.md
CHANGED
|
@@ -100,14 +100,29 @@ Alternatively, you can use `openapi-ts.config.js` and configure the export state
|
|
|
100
100
|
|
|
101
101
|
### Clients
|
|
102
102
|
|
|
103
|
-
|
|
103
|
+
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
|
|
104
104
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
105
|
+
```mjs
|
|
106
|
+
/** @type {import('@hey-api/openapi-ts').UserConfig} */
|
|
107
|
+
export default {
|
|
108
|
+
client: 'fetch',
|
|
109
|
+
input: 'path/to/openapi.json',
|
|
110
|
+
output: 'src/client',
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
We support these clients:
|
|
115
|
+
|
|
116
|
+
- [angular](https://angular.io/) (using [RxJS](https://rxjs.dev/))
|
|
117
|
+
- [axios](https://axios-http.com/)
|
|
118
|
+
- [fetch](https://developer.mozilla.org/docs/Web/API/Fetch_API)
|
|
119
|
+
|
|
120
|
+
We also support the legacy Node.js and XHR clients:
|
|
121
|
+
|
|
122
|
+
- [node](https://nodejs.org/) (using [node-fetch](https://www.npmjs.com/package/node-fetch))
|
|
123
|
+
- [xhr](https://developer.mozilla.org/docs/Web/API/XMLHttpRequest)
|
|
124
|
+
|
|
125
|
+
> ⚠️ 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.
|
|
111
126
|
|
|
112
127
|
### Formatting
|
|
113
128
|
|
|
@@ -141,18 +156,18 @@ You can also prevent your client from being processed by linters by adding your
|
|
|
141
156
|
|
|
142
157
|
### Enums
|
|
143
158
|
|
|
144
|
-
|
|
159
|
+
If you need to iterate through possible field values without manually typing arrays, you can export enums with
|
|
145
160
|
|
|
146
161
|
```mjs
|
|
147
162
|
/** @type {import('@hey-api/openapi-ts').UserConfig} */
|
|
148
163
|
export default {
|
|
149
|
-
enums:
|
|
164
|
+
enums: 'javascript',
|
|
150
165
|
input: 'path/to/openapi.json',
|
|
151
166
|
output: 'src/client',
|
|
152
167
|
}
|
|
153
168
|
```
|
|
154
169
|
|
|
155
|
-
This will export
|
|
170
|
+
This will export enums as plain JavaScript objects. For example, `Foo` would become
|
|
156
171
|
|
|
157
172
|
```ts
|
|
158
173
|
export const FooEnum = {
|
|
@@ -161,39 +176,21 @@ export const FooEnum = {
|
|
|
161
176
|
} as const;
|
|
162
177
|
```
|
|
163
178
|
|
|
164
|
-
|
|
179
|
+
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
|
|
165
180
|
|
|
166
|
-
```
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
-i, --input <value> OpenAPI specification, can be a path, url or string content (required)
|
|
174
|
-
-o, --output <value> Output directory (required)
|
|
175
|
-
-c, --client <value> HTTP client to generate [fetch, xhr, node, axios, angular] (default: "fetch")
|
|
176
|
-
--name <value> Custom client class name
|
|
177
|
-
--useOptions <value> Use options instead of arguments (default: true)
|
|
178
|
-
--base <value> Manually set base in OpenAPI config instead of inferring from server value
|
|
179
|
-
--enums Generate JavaScript objects from enum definitions (default: false)
|
|
180
|
-
--exportCore <value> Write core files to disk (default: true)
|
|
181
|
-
--exportServices <value> Write services to disk [true, false, regexp] (default: true)
|
|
182
|
-
--exportModels <value> Write models to disk [true, false, regexp] (default: true)
|
|
183
|
-
--exportSchemas <value> Write schemas to disk (default: true)
|
|
184
|
-
--format Process output folder with formatter?
|
|
185
|
-
--no-format Disable processing output folder with formatter
|
|
186
|
-
--lint Process output folder with linter?
|
|
187
|
-
--no-lint Disable processing output folder with linter
|
|
188
|
-
--no-operationId Use path URL to generate operation ID
|
|
189
|
-
--postfixServices Service name postfix (default: "Service")
|
|
190
|
-
--postfixModels Model name postfix
|
|
191
|
-
--request <value> Path to custom request file
|
|
192
|
-
--useDateType <value> Output Date instead of string for the format "date-time" in the models (default: false)
|
|
193
|
-
--useLegacyEnums Generate Typescript enum definitions (default: false)
|
|
194
|
-
-h, --help display help for command
|
|
181
|
+
```mjs
|
|
182
|
+
/** @type {import('@hey-api/openapi-ts').UserConfig} */
|
|
183
|
+
export default {
|
|
184
|
+
enums: 'typescript',
|
|
185
|
+
input: 'path/to/openapi.json',
|
|
186
|
+
output: 'src/client',
|
|
187
|
+
}
|
|
195
188
|
```
|
|
196
189
|
|
|
190
|
+
### Config API
|
|
191
|
+
|
|
192
|
+
You can view the complete list of options in the [UserConfig](./src/types/config.ts) interface.
|
|
193
|
+
|
|
197
194
|
## Interceptors
|
|
198
195
|
|
|
199
196
|
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
|
package/bin/index.js
CHANGED
|
@@ -15,35 +15,66 @@ const params = program
|
|
|
15
15
|
.option('-i, --input <value>', 'OpenAPI specification (path, url, or string content)')
|
|
16
16
|
.option('-o, --output <value>', 'Output directory')
|
|
17
17
|
.option('-c, --client <value>', 'HTTP client to generate [angular, axios, fetch, node, xhr]')
|
|
18
|
-
.option('--
|
|
19
|
-
.option('--useOptions [value]', 'Use options instead of arguments')
|
|
18
|
+
.option('-d, --debug', 'Run in debug mode?')
|
|
20
19
|
.option('--base [value]', 'Manually set base in OpenAPI config instead of inferring from server value')
|
|
21
|
-
.option('--enums', '
|
|
22
|
-
.option('--exportCore
|
|
23
|
-
.option('--
|
|
24
|
-
.option('--
|
|
25
|
-
.option('--
|
|
26
|
-
.option('--format', 'Process output folder with formatter?')
|
|
27
|
-
.option('--
|
|
28
|
-
.option('--
|
|
29
|
-
.option('--
|
|
30
|
-
.option('--operationId', 'Use operationd ID?')
|
|
31
|
-
.option('--no-operationId', 'Use path URL to generate operation ID')
|
|
32
|
-
.option('--postfixServices <value>', 'Service name postfix')
|
|
33
|
-
.option('--serviceResponse [value]', 'Define shape of returned value from service calls')
|
|
34
|
-
.option('--useDateType <value>', 'Output Date instead of string for the format "date-time" in the models')
|
|
20
|
+
.option('--enums <value>', 'Export enum definitions (javascript, typescript)')
|
|
21
|
+
.option('--exportCore [value]', 'Write core files to disk')
|
|
22
|
+
.option('--exportModels [value]', 'Write models to disk')
|
|
23
|
+
.option('--exportSchemas [value]', 'Write schemas to disk')
|
|
24
|
+
.option('--exportServices [value]', 'Write services to disk')
|
|
25
|
+
.option('--format [value]', 'Process output folder with formatter?')
|
|
26
|
+
.option('--lint [value]', 'Process output folder with linter?')
|
|
27
|
+
.option('--name <value>', 'Custom client class name')
|
|
28
|
+
.option('--operationId [value]', 'Use operationd ID?')
|
|
35
29
|
.option('--postfixModels <value>', 'Model name postfix')
|
|
30
|
+
.option('--postfixServices <value>', 'Service name postfix')
|
|
36
31
|
.option('--request <value>', 'Path to custom request file')
|
|
37
|
-
.option('--
|
|
38
|
-
.option('--
|
|
39
|
-
.option('--
|
|
32
|
+
.option('--serviceResponse [value]', 'Define shape of returned value from service calls')
|
|
33
|
+
.option('--useDateType [value]', 'Output Date instead of string for the format "date-time" in the models')
|
|
34
|
+
.option('--useOptions [value]', 'Use options instead of arguments')
|
|
35
|
+
.option('--write [value]', 'Write files to disk? (used for testing)')
|
|
40
36
|
.parse(process.argv)
|
|
41
37
|
.opts();
|
|
42
38
|
|
|
39
|
+
const stringToBoolean = value => {
|
|
40
|
+
if (value === 'true') {
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
if (value === 'false') {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
return value;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const processParams = (obj, keys) => {
|
|
50
|
+
const result = {};
|
|
51
|
+
for (const key of keys) {
|
|
52
|
+
const value = obj[key];
|
|
53
|
+
if (typeof value === 'string') {
|
|
54
|
+
result[key] = stringToBoolean(value);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return result;
|
|
58
|
+
};
|
|
59
|
+
|
|
43
60
|
async function start() {
|
|
44
61
|
try {
|
|
45
62
|
const { createClient } = await import(new URL('../dist/node/index.js', import.meta.url));
|
|
46
|
-
await createClient(
|
|
63
|
+
await createClient({
|
|
64
|
+
...params,
|
|
65
|
+
...processParams(params, [
|
|
66
|
+
'exportCore',
|
|
67
|
+
'exportModels',
|
|
68
|
+
'exportSchemas',
|
|
69
|
+
'exportServices',
|
|
70
|
+
'format',
|
|
71
|
+
'lint',
|
|
72
|
+
'operationId',
|
|
73
|
+
'useDateType',
|
|
74
|
+
'useOptions',
|
|
75
|
+
'write',
|
|
76
|
+
]),
|
|
77
|
+
});
|
|
47
78
|
process.exit(0);
|
|
48
79
|
} catch (error) {
|
|
49
80
|
console.error(error);
|
package/dist/node/index.d.ts
CHANGED
|
@@ -9,10 +9,20 @@ interface UserConfig {
|
|
|
9
9
|
*/
|
|
10
10
|
client?: 'angular' | 'axios' | 'fetch' | 'node' | 'xhr';
|
|
11
11
|
/**
|
|
12
|
-
*
|
|
12
|
+
* Run in debug mode?
|
|
13
13
|
* @default false
|
|
14
14
|
*/
|
|
15
|
-
|
|
15
|
+
debug?: boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Export enum definitions?
|
|
18
|
+
* @default false
|
|
19
|
+
*/
|
|
20
|
+
enums?: 'javascript' | 'typescript' | false;
|
|
21
|
+
/**
|
|
22
|
+
* Generate an experimental build?
|
|
23
|
+
* @default false
|
|
24
|
+
*/
|
|
25
|
+
experimental?: boolean;
|
|
16
26
|
/**
|
|
17
27
|
* Generate core client classes?
|
|
18
28
|
* @default true
|
|
@@ -89,11 +99,6 @@ interface UserConfig {
|
|
|
89
99
|
* @default true
|
|
90
100
|
*/
|
|
91
101
|
useOptions?: boolean;
|
|
92
|
-
/**
|
|
93
|
-
* Generate Typescript enum definitions
|
|
94
|
-
* @default false
|
|
95
|
-
*/
|
|
96
|
-
useLegacyEnums?: boolean;
|
|
97
102
|
/**
|
|
98
103
|
* Write the files to disk (true or false)
|
|
99
104
|
* @default true
|