Skip to content

Commit 933472a

Browse files
authored
Merge pull request #1697 from hey-api/fix/export-from-index
fix: add ability to export any plugin output from index.ts
2 parents de79917 + dec3fed commit 933472a

File tree

26 files changed

+155
-12
lines changed

26 files changed

+155
-12
lines changed

.changeset/clever-plums-return.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@hey-api/openapi-ts': patch
3+
---
4+
5+
fix: add exportFromIndex option to all plugins

docs/openapi-ts/custom-plugin.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ In the file above, we define a `my-plugin` plugin which will generate a `my-plug
9494
By default, your plugin output won't be re-exported from the `index.ts` file. To enable this feature, add `exportFromIndex: true` to your `config.ts` file.
9595

9696
::: warning
97-
Re-exporting your plugin from `index.ts` may result in broken output due to naming conflicts. Ensure your exported identifiers are unique.
97+
Re-exporting your plugin from index file may result in broken output due to naming conflicts. Ensure your exported identifiers are unique.
9898
:::
9999

100100
## Handler

docs/openapi-ts/output.md

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,56 @@ export * from './types.gen';
4545

4646
:::
4747

48-
We recommend importing artifacts from their files to avoid ambiguity, but we leave this choice up to you.
48+
### Disable index file
49+
50+
We recommend importing artifacts from their respective files to avoid ambiguity, but we leave this choice up to you.
4951

5052
```ts
51-
import type { Pet } from './client'; // 👎 // [!code --]
52-
import type { Pet } from './client/types.gen'; // 👍 // [!code ++]
53+
import type { Pet } from './client';
54+
// or
55+
import type { Pet } from './client/types.gen';
56+
```
57+
58+
If you're not importing artifacts from the index file, you can skip generating it altogether by setting the `output.indexFile` option to `false`.
59+
60+
```js
61+
import { defaultPlugins } from '@hey-api/openapi-ts';
62+
63+
export default {
64+
input: 'path/to/openapi.json',
65+
output: {
66+
indexFile: false, // [!code ++]
67+
path: 'src/client',
68+
},
69+
plugins: ['@hey-api/client-fetch'],
70+
};
5371
```
5472

73+
### Re-export more files
74+
75+
You can choose which files should be re-exported by setting the `exportFromIndex` option to `true` on any plugin. For example, here's how you would re-export [Zod](/openapi-ts/plugins/zod) plugin exports.
76+
77+
```js
78+
import { defaultPlugins } from '@hey-api/openapi-ts';
79+
80+
export default {
81+
input: 'path/to/openapi.json',
82+
output: 'src/client',
83+
plugins: [
84+
...defaultPlugins,
85+
'@hey-api/client-fetch',
86+
{
87+
exportFromIndex: true, // [!code ++]
88+
name: 'zod',
89+
},
90+
],
91+
};
92+
```
93+
94+
::: warning
95+
Re-exporting additional files from index file may result in broken output due to naming conflicts.
96+
:::
97+
5598
## Client
5699

57100
`client.gen.ts` is generated by [client plugins](/openapi-ts/clients). If you choose to generate SDKs (enabled by default), you must specify a client which will create this file.

packages/openapi-ts/src/plugins/@hey-api/client-core/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ export const clientDefaultConfig = {
33
_tags: ['client'],
44
baseUrl: true,
55
bundle: false,
6+
exportFromIndex: false,
67
output: 'client',
78
} as const;

packages/openapi-ts/src/plugins/@hey-api/client-core/types.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ export namespace Client {
4242
* @default false
4343
*/
4444
bundle?: boolean;
45+
/**
46+
* Should the exports from the generated files be re-exported in the index
47+
* barrel file?
48+
*
49+
* @default false
50+
*/
51+
exportFromIndex?: boolean;
4552
/**
4653
* Name of the generated file.
4754
*

packages/openapi-ts/src/plugins/@hey-api/schemas/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { Config } from './types';
66
export const defaultConfig: Plugin.Config<Config> = {
77
_handler: handler,
88
_handlerLegacy: handlerLegacy,
9+
exportFromIndex: false,
910
name: '@hey-api/schemas',
1011
nameBuilder: (name) => `${name}Schema`,
1112
output: 'schemas',

packages/openapi-ts/src/plugins/@hey-api/schemas/types.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ import type { SchemaObject as OpenApiV3_1_XSchemaObject } from '../../../openApi
88
import type { Plugin } from '../../types';
99

1010
export interface Config extends Plugin.Name<'@hey-api/schemas'> {
11+
/**
12+
* Should the exports from the generated files be re-exported in the index
13+
* barrel file?
14+
*
15+
* @default false
16+
*/
17+
exportFromIndex?: boolean;
1118
/**
1219
* Customise the schema name. By default, `{{name}}Schema` is used. `name` is a
1320
* valid JavaScript/TypeScript identifier, e.g. if your schema name is

packages/openapi-ts/src/plugins/@hey-api/sdk/types.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ export interface Config extends Plugin.Name<'@hey-api/sdk'> {
3939
* @default true
4040
*/
4141
client?: PluginClientNames | boolean;
42+
/**
43+
* Should the exports from the generated files be re-exported in the index
44+
* barrel file?
45+
*
46+
* @default true
47+
*/
48+
exportFromIndex?: boolean;
4249
/**
4350
* @deprecated
4451
*

packages/openapi-ts/src/plugins/@hey-api/transformers/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export const defaultConfig: Plugin.Config<Config> = {
1010
_tags: ['transformer'],
1111
bigInt: true,
1212
dates: true,
13+
exportFromIndex: false,
1314
name: '@hey-api/transformers',
1415
output: 'transformers',
1516
};

packages/openapi-ts/src/plugins/@hey-api/transformers/types.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ export interface Config extends Plugin.Name<'@hey-api/transformers'> {
1313
* @default true
1414
*/
1515
dates?: boolean;
16+
/**
17+
* Should the exports from the generated files be re-exported in the index
18+
* barrel file?
19+
*
20+
* @default false
21+
*/
22+
exportFromIndex?: boolean;
1623
/**
1724
* Name of the generated file.
1825
*

0 commit comments

Comments
 (0)