From 942913af15ae10c5fc3c5e456800829b7dd9c10a Mon Sep 17 00:00:00 2001 From: Lubos Date: Tue, 3 Mar 2026 23:39:50 +0100 Subject: [PATCH] fix: support output header function signature in client and core files --- .changeset/bitter-lines-hammer.md | 5 +++++ .changeset/poor-beds-arrive.md | 5 +++++ .../openapi-python/src/generate/client.ts | 21 +++---------------- packages/openapi-ts/src/generate/client.ts | 21 +++---------------- packages/shared/src/config/output/types.ts | 6 +++++- packages/shared/src/index.ts | 1 + packages/shared/src/utils/header.ts | 15 +++++++++++++ 7 files changed, 37 insertions(+), 37 deletions(-) create mode 100644 .changeset/bitter-lines-hammer.md create mode 100644 .changeset/poor-beds-arrive.md create mode 100644 packages/shared/src/utils/header.ts diff --git a/.changeset/bitter-lines-hammer.md b/.changeset/bitter-lines-hammer.md new file mode 100644 index 000000000..af32ddeea --- /dev/null +++ b/.changeset/bitter-lines-hammer.md @@ -0,0 +1,5 @@ +--- +"@hey-api/shared": patch +--- + +**output**: context file is optional diff --git a/.changeset/poor-beds-arrive.md b/.changeset/poor-beds-arrive.md new file mode 100644 index 000000000..862444385 --- /dev/null +++ b/.changeset/poor-beds-arrive.md @@ -0,0 +1,5 @@ +--- +"@hey-api/openapi-ts": patch +--- + +**output(header)**: fix: support function signature in client and core files diff --git a/packages/openapi-python/src/generate/client.ts b/packages/openapi-python/src/generate/client.ts index e7b24d5ca..a746bd055 100644 --- a/packages/openapi-python/src/generate/client.ts +++ b/packages/openapi-python/src/generate/client.ts @@ -4,7 +4,7 @@ import { fileURLToPath } from 'node:url'; import type { IProject, ProjectRenderMeta } from '@hey-api/codegen-core'; import type { DefinePlugin, OutputHeader } from '@hey-api/shared'; -import { ensureDirSync } from '@hey-api/shared'; +import { ensureDirSync, outputHeaderToPrefix } from '@hey-api/shared'; import type { Config } from '../config/types'; import type { Client } from '../plugins/@hey-api/client-core/types'; @@ -53,21 +53,6 @@ function getClientBundlePaths(pluginName: string): { }; } -/** - * Converts an {@link OutputHeader} value to a string prefix for file content. - * Returns an empty string when the header is null, undefined, or a function - * (functions require a render context which is not available for bundled files). - */ -function outputHeaderToPrefix(header: OutputHeader): string { - if (header == null || typeof header === 'function') return ''; - const lines = - typeof header === 'string' - ? header.split(/\r?\n/) - : header.flatMap((line) => line.split(/\r?\n/)); - const content = lines.join('\n'); - return content ? `${content}\n\n` : ''; -} - /** * Returns absolute path to the client folder. This is hard-coded for now. */ @@ -186,11 +171,11 @@ export function generateClientBundle({ meta: ProjectRenderMeta; outputPath: string; plugin: DefinePlugin['Config']; - project?: IProject; + project: IProject; }): Map | undefined { const renamed = new Map(); const devMode = isDevMode(); - const headerPrefix = outputHeaderToPrefix(header); + const headerPrefix = outputHeaderToPrefix(header, project); // copy Hey API clients to output const isHeyApiClientPlugin = plugin.name.startsWith('@hey-api/client-'); diff --git a/packages/openapi-ts/src/generate/client.ts b/packages/openapi-ts/src/generate/client.ts index b7057c47b..aa626e543 100644 --- a/packages/openapi-ts/src/generate/client.ts +++ b/packages/openapi-ts/src/generate/client.ts @@ -4,7 +4,7 @@ import { fileURLToPath } from 'node:url'; import type { IProject, ProjectRenderMeta } from '@hey-api/codegen-core'; import type { DefinePlugin, OutputHeader } from '@hey-api/shared'; -import { ensureDirSync } from '@hey-api/shared'; +import { ensureDirSync, outputHeaderToPrefix } from '@hey-api/shared'; import type { Config } from '../config/types'; import type { Client } from '../plugins/@hey-api/client-core/types'; @@ -53,21 +53,6 @@ function getClientBundlePaths(pluginName: string): { }; } -/** - * Converts an {@link OutputHeader} value to a string prefix for file content. - * Returns an empty string when the header is null, undefined, or a function - * (functions require a render context which is not available for bundled files). - */ -function outputHeaderToPrefix(header: OutputHeader): string { - if (header == null || typeof header === 'function') return ''; - const lines = - typeof header === 'string' - ? header.split(/\r?\n/) - : header.flatMap((line) => line.split(/\r?\n/)); - const content = lines.join('\n'); - return content ? `${content}\n\n` : ''; -} - /** * Returns absolute path to the client folder. This is hard-coded for now. */ @@ -186,11 +171,11 @@ export function generateClientBundle({ meta: ProjectRenderMeta; outputPath: string; plugin: DefinePlugin['Config']; - project?: IProject; + project: IProject; }): Map | undefined { const renamed = new Map(); const devMode = isDevMode(); - const headerPrefix = outputHeaderToPrefix(header); + const headerPrefix = outputHeaderToPrefix(header, project); // copy Hey API clients to output const isHeyApiClientPlugin = plugin.name.startsWith('@hey-api/client-'); diff --git a/packages/shared/src/config/output/types.ts b/packages/shared/src/config/output/types.ts index f43e3ab43..f6ddb82a2 100644 --- a/packages/shared/src/config/output/types.ts +++ b/packages/shared/src/config/output/types.ts @@ -1,4 +1,8 @@ import type { RenderContext } from '@hey-api/codegen-core'; import type { MaybeArray, MaybeFunc } from '@hey-api/types'; -export type OutputHeader = MaybeFunc<(ctx: RenderContext) => MaybeArray | null | undefined>; +export type OutputHeader = MaybeFunc< + ( + ctx: Omit & Pick, 'file'>, + ) => MaybeArray | null | undefined +>; diff --git a/packages/shared/src/index.ts b/packages/shared/src/index.ts index 6122573c9..6984a9367 100644 --- a/packages/shared/src/index.ts +++ b/packages/shared/src/index.ts @@ -113,6 +113,7 @@ export type { Logs } from './types/logs'; export type { WatchValues } from './types/watch'; export { escapeComment } from './utils/escape'; export { utils } from './utils/exports'; +export { outputHeaderToPrefix } from './utils/header'; export { inputToApiRegistry } from './utils/input'; export { heyApiRegistryBaseUrl } from './utils/input/heyApi'; export { MinHeap } from './utils/minHeap'; diff --git a/packages/shared/src/utils/header.ts b/packages/shared/src/utils/header.ts new file mode 100644 index 000000000..06ef1d84a --- /dev/null +++ b/packages/shared/src/utils/header.ts @@ -0,0 +1,15 @@ +import type { IProject } from '@hey-api/codegen-core'; + +import type { OutputHeader } from '../config/output/types'; + +/** + * Converts an {@link OutputHeader} value to a string prefix for file content. + */ +export function outputHeaderToPrefix(header: OutputHeader, project: IProject): string { + let lines = typeof header === 'function' ? header({ project }) : header; + if (lines === null || lines === undefined) return ''; + lines = + typeof lines === 'string' ? lines.split(/\r?\n/) : lines.flatMap((line) => line.split(/\r?\n/)); + const content = lines.join('\n'); + return content ? `${content}\n\n` : ''; +} -- knotmirror 0.0.1