@hey-api/openapi-ts 0.31.0 → 0.31.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.
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
- We provide a variety of possible clients to use when generating your `openapi-ts` client. If one is not specified by the user, we will try to infer the client to use. If the inferred client is not correct, you can set it with the client config parameter. The following are available:
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
- - `angular`: An [Angular](https://angular.io/) client using [RxJS](https://rxjs.dev/).
106
- - `axios`: An [Axios](https://axios-http.com/docs/intro) client.
107
- - `fetch`: A [Fetch API](https://developer.mozilla.org/docs/Web/API/Fetch_API) client.
108
- > NOTE: The Fetch API is experimental in Node.js 18+ and was stabilized in [Node.js v21](https://nodejs.org/docs/latest-v21.x/api/globals.html#fetch)
109
- - `node`: A [Node.js](https://nodejs.org/) client using [node-fetch](https://www.npmjs.com/package/node-fetch).
110
- - `xhr`: A [XMLHttpRequest](https://developer.mozilla.org/docs/Web/API/XMLHttpRequest) client.
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
- We do not generate 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 want to iterate through possible field values without manually typing arrays, you can export enums by running
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: true,
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 your enums as plain JavaScript objects. For example, `Foo` will generate the following
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
- ### Config API
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
- ```sh
167
- $ openapi-ts --help
168
-
169
- Usage: openapi-ts [options]
170
-
171
- Options:
172
- -V, --version output the version number
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
@@ -18,7 +18,7 @@ const params = program
18
18
  .option('--name <value>', 'Custom client class name')
19
19
  .option('--useOptions [value]', 'Use options instead of arguments')
20
20
  .option('--base [value]', 'Manually set base in OpenAPI config instead of inferring from server value')
21
- .option('--enums', 'Generate JavaScript objects from enum definitions')
21
+ .option('--enums <value>', 'Export enum definitions (javascript, typescript)')
22
22
  .option('--exportCore <value>', 'Write core files to disk')
23
23
  .option('--exportServices <value>', 'Write services to disk')
24
24
  .option('--exportModels <value>', 'Write models to disk')
@@ -36,7 +36,6 @@ const params = program
36
36
  .option('--request <value>', 'Path to custom request file')
37
37
  .option('--write', 'Write files to disk? (used for testing)')
38
38
  .option('--no-write', 'Skip writing files to disk (used for testing)')
39
- .option('--useLegacyEnums', 'Generate Typescript enum definitions')
40
39
  .parse(process.argv)
41
40
  .opts();
42
41
 
@@ -9,10 +9,10 @@ interface UserConfig {
9
9
  */
10
10
  client?: 'angular' | 'axios' | 'fetch' | 'node' | 'xhr';
11
11
  /**
12
- * Generate JavaScript objects from enum definitions?
12
+ * Export enum definitions?
13
13
  * @default false
14
14
  */
15
- enums?: boolean;
15
+ enums?: 'javascript' | 'typescript' | false;
16
16
  /**
17
17
  * Generate core client classes?
18
18
  * @default true
@@ -89,11 +89,6 @@ interface UserConfig {
89
89
  * @default true
90
90
  */
91
91
  useOptions?: boolean;
92
- /**
93
- * Generate Typescript enum definitions
94
- * @default false
95
- */
96
- useLegacyEnums?: boolean;
97
92
  /**
98
93
  * Write the files to disk (true or false)
99
94
  * @default true