diff --git a/.changeset/wicked-apples-teach.md b/.changeset/wicked-apples-teach.md new file mode 100644 index 000000000..48dc03d16 --- /dev/null +++ b/.changeset/wicked-apples-teach.md @@ -0,0 +1,5 @@ +--- +'@hey-api/openapi-ts': patch +--- + +fix: add ability to skip generating index file with output.indexFile diff --git a/packages/openapi-ts/src/generate/output.ts b/packages/openapi-ts/src/generate/output.ts index 4c66c6a7a..58f9f97ce 100644 --- a/packages/openapi-ts/src/generate/output.ts +++ b/packages/openapi-ts/src/generate/output.ts @@ -143,7 +143,11 @@ export const generateOutput = async ({ context }: { context: IR.Context }) => { continue; } - if (!file.isEmpty() && file.exportFromIndex) { + if ( + !file.isEmpty() && + file.exportFromIndex && + context.config.output.indexFile + ) { // TODO: parser - add export method for more granular control over // what's exported so we can support named exports indexFile.add( @@ -156,6 +160,8 @@ export const generateOutput = async ({ context }: { context: IR.Context }) => { file.write('\n\n'); } - indexFile.write(); + if (context.config.output.indexFile) { + indexFile.write(); + } } }; diff --git a/packages/openapi-ts/src/index.ts b/packages/openapi-ts/src/index.ts index 730229715..ea5f2f859 100644 --- a/packages/openapi-ts/src/index.ts +++ b/packages/openapi-ts/src/index.ts @@ -159,6 +159,7 @@ const getOutput = (userConfig: ClientConfig): Config['output'] => { let output: Config['output'] = { clean: true, format: false, + indexFile: true, lint: false, path: '', }; diff --git a/packages/openapi-ts/src/types/config.ts b/packages/openapi-ts/src/types/config.ts index 9a421a659..dc5d5225a 100644 --- a/packages/openapi-ts/src/types/config.ts +++ b/packages/openapi-ts/src/types/config.ts @@ -170,6 +170,14 @@ export interface ClientConfig { * @default false */ format?: Formatters | false; + /** + * Should the exports from plugin files be re-exported in the index + * barrel file? By default, this is enabled and only default plugins + * are re-exported. + * + * @default true + */ + indexFile?: boolean; /** * Process output folder with linter? * diff --git a/packages/openapi-ts/test/index.test.ts b/packages/openapi-ts/test/index.test.ts index f2ac8e145..2722910f2 100644 --- a/packages/openapi-ts/test/index.test.ts +++ b/packages/openapi-ts/test/index.test.ts @@ -59,7 +59,9 @@ describe('OpenAPI v3', () => { client: 'legacy/fetch', exportCore: true, input: '', - output: '', + output: { + path: '', + }, plugins: [ '@hey-api/sdk', { @@ -287,6 +289,18 @@ describe('OpenAPI v3', () => { description: 'generate legacy positional arguments', name: 'v3_legacy_positional_args', }, + { + config: createConfig({ + client: '@hey-api/client-fetch', + output: { + indexFile: false, + path: '', + }, + plugins: ['@hey-api/typescript'], + }), + description: 'generate output without index file', + name: 'v3_no_index', + }, { config: createConfig({ plugins: [ @@ -473,7 +487,10 @@ describe('OpenAPI v3', () => { await createClient({ ...config, input: V3_SPEC_PATH, - output, + output: { + ...(typeof config.output === 'object' ? config.output : {}), + path: output, + }, }); sync(`${output}**/*.ts`).forEach((file) => { const content = fs.readFileSync(file, 'utf8').toString();