Skip to content

Commit 0a6fca5

Browse files
authored
Merge pull request #2284 from hey-api/fix/typescript-name
fix(typescript): add name option
2 parents 8273845 + 236550f commit 0a6fca5

File tree

161 files changed

+3702
-5567
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

161 files changed

+3702
-5567
lines changed

.changeset/silent-donkeys-itch.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@hey-api/openapi-ts': minor
3+
---
4+
5+
### Removed `typescript+namespace` enums mode
6+
7+
Due to a simpler TypeScript plugin implementation, the `typescript+namespace` enums mode is no longer necessary. This mode was used in the past to group inline enums under the same namespace. With the latest changes, this behavior is no longer supported. You can either choose to ignore inline enums (default), or use the `enums` transform (added in v0.78.0) to convert them into reusable components which will get exported as usual.

docs/openapi-ts/clients/axios.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,5 +222,9 @@ client.setConfig({
222222

223223
You can use any of the approaches mentioned in [Configuration](#configuration), depending on how granular you want your custom instance to be.
224224

225+
## Config API
226+
227+
You can view the complete list of options in the [UserConfig](https://github.com/hey-api/openapi-ts/blob/main/packages/openapi-ts/src/plugins/@hey-api/client-axios/types.d.ts) interface.
228+
225229
<!--@include: ../../examples.md-->
226230
<!--@include: ../../sponsors.md-->

docs/openapi-ts/clients/fetch.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,5 +299,9 @@ client.setConfig({
299299

300300
You can use any of the approaches mentioned in [Configuration](#configuration), depending on how granular you want your custom method to be.
301301

302+
## Config API
303+
304+
You can view the complete list of options in the [UserConfig](https://github.com/hey-api/openapi-ts/blob/main/packages/openapi-ts/src/plugins/@hey-api/client-fetch/types.d.ts) interface.
305+
302306
<!--@include: ../../examples.md-->
303307
<!--@include: ../../sponsors.md-->

docs/openapi-ts/clients/next-js.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,5 +286,9 @@ client.setConfig({
286286

287287
You can use any of the approaches mentioned in [Configuration](#configuration), depending on how granular you want your custom method to be.
288288

289+
## Config API
290+
291+
You can view the complete list of options in the [UserConfig](https://github.com/hey-api/openapi-ts/blob/main/packages/openapi-ts/src/plugins/@hey-api/client-next/types.d.ts) interface.
292+
289293
<!--@include: ../../examples.md-->
290294
<!--@include: ../../sponsors.md-->

docs/openapi-ts/clients/nuxt.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,5 +248,9 @@ client.setConfig({
248248

249249
You can use any of the approaches mentioned in [Configuration](#configuration), depending on how granular you want your custom method to be.
250250

251+
## Config API
252+
253+
You can view the complete list of options in the [UserConfig](https://github.com/hey-api/openapi-ts/blob/main/packages/openapi-ts/src/plugins/@hey-api/client-nuxt/types.d.ts) interface.
254+
251255
<!--@include: ../../examples.md-->
252256
<!--@include: ../../sponsors.md-->

docs/openapi-ts/migrating.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ description: Migrating to @hey-api/openapi-ts.
77

88
While we try to avoid breaking changes, sometimes it's unavoidable in order to offer you the latest features. This page lists changes that require updates to your code. If you run into a problem with migration, please [open an issue](https://github.com/hey-api/openapi-ts/issues).
99

10+
## v0.79.0
11+
12+
### Removed `typescript+namespace` enums mode
13+
14+
Due to a simpler TypeScript plugin implementation, the `typescript+namespace` enums mode is no longer necessary. This mode was used in the past to group inline enums under the same namespace. With the latest changes, this behavior is no longer supported. You can either choose to ignore inline enums (default), or use the `enums` transform (added in v0.78.0) to convert them into reusable components which will get exported as usual.
15+
1016
## v0.78.0
1117

1218
### Added `parser` options

docs/openapi-ts/output/json-schema.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,9 @@ if (userInput.length > maxInputLength) {
9696
}
9797
```
9898

99+
## Config API
100+
101+
You can view the complete list of options in the [UserConfig](https://github.com/hey-api/openapi-ts/blob/main/packages/openapi-ts/src/plugins/@hey-api/schemas/types.d.ts) interface.
102+
99103
<!--@include: ../../examples.md-->
100104
<!--@include: ../../sponsors.md-->

docs/openapi-ts/output/sdk.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,5 +224,9 @@ export default {
224224

225225
Learn more about available validators on the [Validators](/openapi-ts/validators) page.
226226

227+
## Config API
228+
229+
You can view the complete list of options in the [UserConfig](https://github.com/hey-api/openapi-ts/blob/main/packages/openapi-ts/src/plugins/@hey-api/sdk/types.d.ts) interface.
230+
227231
<!--@include: ../../examples.md-->
228232
<!--@include: ../../sponsors.md-->

docs/openapi-ts/output/typescript.md

Lines changed: 62 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,67 +7,89 @@ description: Learn about files generated with @hey-api/openapi-ts.
77

88
TypeScript interfaces are located in the `types.gen.ts` file. This is the only file that does not impact your bundle size and runtime performance. It will get discarded during build time, unless you configured to emit runtime [enums](#enums).
99

10-
This file contains three different categories of interfaces created from your input:
10+
## Installation
1111

12-
- reusable components
13-
- operation request, response, and error data
14-
- enums
12+
In your [configuration](/openapi-ts/get-started), add `@hey-api/typescript` to your plugins and you'll be ready to generate TypeScript artifacts. :tada:
1513

16-
Depending on your input and configuration, some of these categories might be missing or differ in your output (and that's okay!).
14+
```js
15+
export default {
16+
input: 'https://get.heyapi.dev/hey-api/backend',
17+
output: 'src/client',
18+
plugins: [
19+
// ...other plugins
20+
'@hey-api/typescript', // [!code ++]
21+
],
22+
};
23+
```
1724

18-
::: code-group
25+
:::tip
26+
The `@hey-api/typescript` plugin might be implicitly added to your `plugins` if another plugin depends on it.
27+
:::
1928

20-
```ts [types.gen.ts]
21-
export type Pet = {
22-
id?: number;
23-
name: string;
24-
};
29+
## Output
30+
31+
The TypeScript plugin will generate the following artifacts, depending on the input specification.
32+
33+
## Requests
2534

35+
A single request type is generated for each endpoint. It may contain a request body, parameters, and headers.
36+
37+
```ts
2638
export type AddPetData = {
27-
body: Pet;
39+
body: {
40+
id?: number;
41+
name: string;
42+
};
43+
path?: never;
44+
query?: never;
45+
url: '/pets';
2846
};
29-
30-
export type AddPetResponse = Pet;
3147
```
3248

33-
:::
49+
You can customize the naming and casing pattern for `requests` schemas using the `.name` and `.case` options.
3450

35-
Every generated interface inside `types.gen.ts` is exported. You can import individual exports in your application and use them as necessary.
51+
## Responses
3652

37-
## Configuration
53+
A single type is generated for all endpoint's responses.
3854

39-
You can modify the contents of `types.gen.ts` by configuring the `@hey-api/typescript` plugin. Note that you must specify the default plugins to preserve the default output.
55+
```ts
56+
export type AddPetResponses = {
57+
200: {
58+
id?: number;
59+
name: string;
60+
};
61+
};
4062

41-
```js
42-
import { defaultPlugins } from '@hey-api/openapi-ts';
63+
export type AddPetResponse = AddPetResponses[keyof AddPetResponses];
64+
```
4365

44-
export default {
45-
input: 'https://get.heyapi.dev/hey-api/backend',
46-
output: 'src/client',
47-
plugins: [
48-
...defaultPlugins,
49-
{
50-
name: '@hey-api/typescript',
51-
// ...custom options // [!code ++]
52-
},
53-
],
66+
You can customize the naming and casing pattern for `responses` schemas using the `.name` and `.case` options.
67+
68+
## Definitions
69+
70+
A type is generated for every reusable definition from your input.
71+
72+
```ts
73+
export type Pet = {
74+
id?: number;
75+
name: string;
5476
};
5577
```
5678

79+
You can customize the naming and casing pattern for `definitions` schemas using the `.name` and `.case` options.
80+
5781
## Enums
5882

59-
By default, `@hey-api/openapi-ts` will only emit enums as types. You may want to generate runtime artifacts. A good use case is iterating through possible field values without manually typing arrays. To emit runtime enums, set `enums` to a valid option.
83+
By default, `@hey-api/typescript` will emit enums only as types. You may want to generate runtime artifacts. A good use case is iterating through possible field values without manually typing arrays. To emit runtime enums, set `enums` to a valid option.
6084

6185
::: code-group
6286

6387
```js [disabled]
64-
import { defaultPlugins } from '@hey-api/openapi-ts';
65-
6688
export default {
6789
input: 'https://get.heyapi.dev/hey-api/backend',
6890
output: 'src/client',
6991
plugins: [
70-
...defaultPlugins,
92+
// ...other plugins
7193
{
7294
enums: false, // default // [!code ++]
7395
name: '@hey-api/typescript',
@@ -77,13 +99,11 @@ export default {
7799
```
78100

79101
```js [javascript]
80-
import { defaultPlugins } from '@hey-api/openapi-ts';
81-
82102
export default {
83103
input: 'https://get.heyapi.dev/hey-api/backend',
84104
output: 'src/client',
85105
plugins: [
86-
...defaultPlugins,
106+
// ...other plugins
87107
{
88108
enums: 'javascript', // [!code ++]
89109
name: '@hey-api/typescript',
@@ -93,13 +113,11 @@ export default {
93113
```
94114

95115
```js [typescript]
96-
import { defaultPlugins } from '@hey-api/openapi-ts';
97-
98116
export default {
99117
input: 'https://get.heyapi.dev/hey-api/backend',
100118
output: 'src/client',
101119
plugins: [
102-
...defaultPlugins,
120+
// ...other plugins
103121
{
104122
enums: 'typescript', // [!code ++]
105123
name: '@hey-api/typescript',
@@ -112,5 +130,9 @@ export default {
112130

113131
We recommend exporting enums as plain JavaScript objects. [TypeScript enums](https://www.typescriptlang.org/docs/handbook/enums.html) are not a type-level extension of JavaScript and pose [typing challenges](https://dev.to/ivanzm123/dont-use-enums-in-typescript-they-are-very-dangerous-57bh).
114132

133+
## Config API
134+
135+
You can view the complete list of options in the [UserConfig](https://github.com/hey-api/openapi-ts/blob/main/packages/openapi-ts/src/plugins/@hey-api/typescript/types.d.ts) interface.
136+
115137
<!--@include: ../../examples.md-->
116138
<!--@include: ../../sponsors.md-->

docs/openapi-ts/plugins/custom.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export type { MyPlugin } from './types';
3333
```ts [types.d.ts]
3434
import type { DefinePlugin } from '@hey-api/openapi-ts';
3535

36-
export type Config = {
36+
export type UserConfig = {
3737
/**
3838
* Plugin name. Must be unique.
3939
*/
@@ -52,7 +52,7 @@ export type Config = {
5252
myOption?: boolean;
5353
};
5454

55-
export type MyPlugin = DefinePlugin<Config>;
55+
export type MyPlugin = DefinePlugin<UserConfig>;
5656
```
5757

5858
:::

0 commit comments

Comments
 (0)