Skip to content

Commit

Permalink
Merge pull request #1697 from hey-api/fix/export-from-index
Browse files Browse the repository at this point in the history
fix: add ability to export any plugin output from index.ts
  • Loading branch information
mrlubos authored Feb 10, 2025
2 parents de79917 + dec3fed commit 933472a
Show file tree
Hide file tree
Showing 26 changed files with 155 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/clever-plums-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hey-api/openapi-ts': patch
---

fix: add exportFromIndex option to all plugins
2 changes: 1 addition & 1 deletion docs/openapi-ts/custom-plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ In the file above, we define a `my-plugin` plugin which will generate a `my-plug
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.

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

## Handler
Expand Down
49 changes: 46 additions & 3 deletions docs/openapi-ts/output.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,56 @@ export * from './types.gen';

:::

We recommend importing artifacts from their files to avoid ambiguity, but we leave this choice up to you.
### Disable index file

We recommend importing artifacts from their respective files to avoid ambiguity, but we leave this choice up to you.

```ts
import type { Pet } from './client'; // 👎 // [!code --]
import type { Pet } from './client/types.gen'; // 👍 // [!code ++]
import type { Pet } from './client';
// or
import type { Pet } from './client/types.gen';
```

If you're not importing artifacts from the index file, you can skip generating it altogether by setting the `output.indexFile` option to `false`.

```js
import { defaultPlugins } from '@hey-api/openapi-ts';

export default {
input: 'path/to/openapi.json',
output: {
indexFile: false, // [!code ++]
path: 'src/client',
},
plugins: ['@hey-api/client-fetch'],
};
```

### Re-export more files

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.

```js
import { defaultPlugins } from '@hey-api/openapi-ts';

export default {
input: 'path/to/openapi.json',
output: 'src/client',
plugins: [
...defaultPlugins,
'@hey-api/client-fetch',
{
exportFromIndex: true, // [!code ++]
name: 'zod',
},
],
};
```

::: warning
Re-exporting additional files from index file may result in broken output due to naming conflicts.
:::

## Client

`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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ export const clientDefaultConfig = {
_tags: ['client'],
baseUrl: true,
bundle: false,
exportFromIndex: false,
output: 'client',
} as const;
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ export namespace Client {
* @default false
*/
bundle?: boolean;
/**
* Should the exports from the generated files be re-exported in the index
* barrel file?
*
* @default false
*/
exportFromIndex?: boolean;
/**
* Name of the generated file.
*
Expand Down
1 change: 1 addition & 0 deletions packages/openapi-ts/src/plugins/@hey-api/schemas/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { Config } from './types';
export const defaultConfig: Plugin.Config<Config> = {
_handler: handler,
_handlerLegacy: handlerLegacy,
exportFromIndex: false,
name: '@hey-api/schemas',
nameBuilder: (name) => `${name}Schema`,
output: 'schemas',
Expand Down
7 changes: 7 additions & 0 deletions packages/openapi-ts/src/plugins/@hey-api/schemas/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ import type { SchemaObject as OpenApiV3_1_XSchemaObject } from '../../../openApi
import type { Plugin } from '../../types';

export interface Config extends Plugin.Name<'@hey-api/schemas'> {
/**
* Should the exports from the generated files be re-exported in the index
* barrel file?
*
* @default false
*/
exportFromIndex?: boolean;
/**
* Customise the schema name. By default, `{{name}}Schema` is used. `name` is a
* valid JavaScript/TypeScript identifier, e.g. if your schema name is
Expand Down
7 changes: 7 additions & 0 deletions packages/openapi-ts/src/plugins/@hey-api/sdk/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ export interface Config extends Plugin.Name<'@hey-api/sdk'> {
* @default true
*/
client?: PluginClientNames | boolean;
/**
* Should the exports from the generated files be re-exported in the index
* barrel file?
*
* @default true
*/
exportFromIndex?: boolean;
/**
* @deprecated
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const defaultConfig: Plugin.Config<Config> = {
_tags: ['transformer'],
bigInt: true,
dates: true,
exportFromIndex: false,
name: '@hey-api/transformers',
output: 'transformers',
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ export interface Config extends Plugin.Name<'@hey-api/transformers'> {
* @default true
*/
dates?: boolean;
/**
* Should the exports from the generated files be re-exported in the index
* barrel file?
*
* @default false
*/
exportFromIndex?: boolean;
/**
* Name of the generated file.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ export interface Config extends Plugin.Name<'@hey-api/typescript'> {
* @default 'SCREAMING_SNAKE_CASE'
*/
enumsCase?: StringCase;
/**
* Should the exports from the generated files be re-exported in the index
* barrel file?
*
* @default true
*/
exportFromIndex?: boolean;
/**
* By default, inline enums (enums not defined as reusable components in
* the input file) are generated as inlined union types. You can set
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const defaultConfig: Plugin.Config<Config> = {
_dependencies: ['@hey-api/sdk', '@hey-api/typescript'],
_handler: handler,
_handlerLegacy: handlerLegacy,
exportFromIndex: false,
infiniteQueryOptions: true,
mutationOptions: true,
name: '@tanstack/angular-query-experimental',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ import type { Plugin } from '../../types';

export interface Config
extends Plugin.Name<'@tanstack/angular-query-experimental'> {
/**
* Should the exports from the generated files be re-exported in the index
* barrel file?
*
* @default false
*/
exportFromIndex?: boolean;
/**
* Generate {@link https://tanstack.com/query/v5/docs/framework/angular/reference/infiniteQueryOptions `infiniteQueryOptions()`} helpers? These will be generated from GET and POST requests where a pagination parameter is detected.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const defaultConfig: Plugin.Config<Config> = {
_dependencies: ['@hey-api/sdk', '@hey-api/typescript'],
_handler: handler,
_handlerLegacy: handlerLegacy,
exportFromIndex: false,
infiniteQueryOptions: true,
mutationOptions: true,
name: '@tanstack/react-query',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import type { Plugin } from '../../types';

export interface Config extends Plugin.Name<'@tanstack/react-query'> {
/**
* Should the exports from the generated files be re-exported in the index
* barrel file?
*
* @default false
*/
exportFromIndex?: boolean;
/**
* Generate {@link https://tanstack.com/query/v5/docs/framework/react/reference/infiniteQueryOptions `infiniteQueryOptions()`} helpers? These will be generated from GET and POST requests where a pagination parameter is detected.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const defaultConfig: Plugin.Config<Config> = {
_dependencies: ['@hey-api/sdk', '@hey-api/typescript'],
_handler: handler,
_handlerLegacy: handlerLegacy,
exportFromIndex: false,
infiniteQueryOptions: true,
mutationOptions: true,
name: '@tanstack/solid-query',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import type { Plugin } from '../../types';

export interface Config extends Plugin.Name<'@tanstack/solid-query'> {
/**
* Should the exports from the generated files be re-exported in the index
* barrel file?
*
* @default false
*/
exportFromIndex?: boolean;
/**
* Generate `createInfiniteQuery()` helpers? These will be generated from GET and POST requests where a pagination parameter is detected.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const defaultConfig: Plugin.Config<Config> = {
_dependencies: ['@hey-api/sdk', '@hey-api/typescript'],
_handler: handler,
_handlerLegacy: handlerLegacy,
exportFromIndex: false,
infiniteQueryOptions: true,
mutationOptions: true,
name: '@tanstack/svelte-query',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import type { Plugin } from '../../types';

export interface Config extends Plugin.Name<'@tanstack/svelte-query'> {
/**
* Should the exports from the generated files be re-exported in the index
* barrel file?
*
* @default false
*/
exportFromIndex?: boolean;
/**
* Generate `createInfiniteQuery()` helpers? These will be generated from GET and POST requests where a pagination parameter is detected.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const defaultConfig: Plugin.Config<Config> = {
_dependencies: ['@hey-api/sdk', '@hey-api/typescript'],
_handler: handler,
_handlerLegacy: handlerLegacy,
exportFromIndex: false,
infiniteQueryOptions: true,
mutationOptions: true,
name: '@tanstack/vue-query',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import type { Plugin } from '../../types';

export interface Config extends Plugin.Name<'@tanstack/vue-query'> {
/**
* Should the exports from the generated files be re-exported in the index
* barrel file?
*
* @default false
*/
exportFromIndex?: boolean;
/**
* Generate {@link https://tanstack.com/query/v5/docs/framework/vue/reference/infiniteQueryOptions `infiniteQueryOptions()`} helpers? These will be generated from GET and POST requests where a pagination parameter is detected.
*
Expand Down
1 change: 1 addition & 0 deletions packages/openapi-ts/src/plugins/fastify/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const defaultConfig: Plugin.Config<Config> = {
_dependencies: ['@hey-api/typescript'],
_handler: handler,
_handlerLegacy: () => {},
exportFromIndex: false,
name: 'fastify',
output: 'fastify',
};
Expand Down
7 changes: 7 additions & 0 deletions packages/openapi-ts/src/plugins/fastify/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import type { Plugin } from '../types';

export interface Config extends Plugin.Name<'fastify'> {
/**
* Should the exports from the generated files be re-exported in the index
* barrel file?
*
* @default false
*/
exportFromIndex?: boolean;
/**
* Name of the generated file.
*
Expand Down
1 change: 1 addition & 0 deletions packages/openapi-ts/src/plugins/zod/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const defaultConfig: Plugin.Config<Config> = {
_handler: handler,
_handlerLegacy: () => {},
_tags: ['validator'],
exportFromIndex: false,
name: 'zod',
output: 'zod',
};
Expand Down
7 changes: 7 additions & 0 deletions packages/openapi-ts/src/plugins/zod/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
import type { Plugin } from '../types';

export interface Config extends Plugin.Name<'zod'> {
/**
* Should the exports from the generated files be re-exported in the index
* barrel file?
*
* @default false
*/
exportFromIndex?: boolean;
/**
* Customise the Zod schema name. By default, `z{{name}}` is used,
* where `name` is a definition name or an operation name.
Expand Down
17 changes: 9 additions & 8 deletions packages/openapi-ts/test/openapi-ts.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ export default defineConfig({
path: path.resolve(__dirname, 'generated', 'sample'),
},
plugins: [
'legacy/xhr',
// @ts-ignore
// {
// baseUrl: false,
// // bundle: true,
// name: '@hey-api/client-fetch',
// strictBaseUrl: true,
// },
{
baseUrl: false,
// bundle: true,
exportFromIndex: true,
name: '@hey-api/client-fetch',
strictBaseUrl: true,
},
// @ts-ignore
{
// name: '@hey-api/schemas',
Expand Down Expand Up @@ -81,7 +81,8 @@ export default defineConfig({
},
// @ts-ignore
{
// name: 'zod',
exportFromIndex: true,
name: 'zod',
},
],
// useOptions: false,
Expand Down

0 comments on commit 933472a

Please sign in to comment.