fork of hey-api/openapi-ts because I need some additional things
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #3486 from hey-api/fix/output-header-func

fix: support output header function signature in client and core files

authored by

Lubos and committed by
GitHub
6e7765f6 f255c260

+37 -37
+5
.changeset/bitter-lines-hammer.md
··· 1 + --- 2 + "@hey-api/shared": patch 3 + --- 4 + 5 + **output**: context file is optional
+5
.changeset/poor-beds-arrive.md
··· 1 + --- 2 + "@hey-api/openapi-ts": patch 3 + --- 4 + 5 + **output(header)**: fix: support function signature in client and core files
+3 -18
packages/openapi-python/src/generate/client.ts
··· 4 4 5 5 import type { IProject, ProjectRenderMeta } from '@hey-api/codegen-core'; 6 6 import type { DefinePlugin, OutputHeader } from '@hey-api/shared'; 7 - import { ensureDirSync } from '@hey-api/shared'; 7 + import { ensureDirSync, outputHeaderToPrefix } from '@hey-api/shared'; 8 8 9 9 import type { Config } from '../config/types'; 10 10 import type { Client } from '../plugins/@hey-api/client-core/types'; ··· 51 51 clientPath: path.resolve(__dirname, 'clients', clientName), 52 52 corePath: path.resolve(__dirname, 'clients', 'core'), 53 53 }; 54 - } 55 - 56 - /** 57 - * Converts an {@link OutputHeader} value to a string prefix for file content. 58 - * Returns an empty string when the header is null, undefined, or a function 59 - * (functions require a render context which is not available for bundled files). 60 - */ 61 - function outputHeaderToPrefix(header: OutputHeader): string { 62 - if (header == null || typeof header === 'function') return ''; 63 - const lines = 64 - typeof header === 'string' 65 - ? header.split(/\r?\n/) 66 - : header.flatMap((line) => line.split(/\r?\n/)); 67 - const content = lines.join('\n'); 68 - return content ? `${content}\n\n` : ''; 69 54 } 70 55 71 56 /** ··· 186 171 meta: ProjectRenderMeta; 187 172 outputPath: string; 188 173 plugin: DefinePlugin<Client.Config & { name: string }>['Config']; 189 - project?: IProject; 174 + project: IProject; 190 175 }): Map<string, string> | undefined { 191 176 const renamed = new Map<string, string>(); 192 177 const devMode = isDevMode(); 193 - const headerPrefix = outputHeaderToPrefix(header); 178 + const headerPrefix = outputHeaderToPrefix(header, project); 194 179 195 180 // copy Hey API clients to output 196 181 const isHeyApiClientPlugin = plugin.name.startsWith('@hey-api/client-');
+3 -18
packages/openapi-ts/src/generate/client.ts
··· 4 4 5 5 import type { IProject, ProjectRenderMeta } from '@hey-api/codegen-core'; 6 6 import type { DefinePlugin, OutputHeader } from '@hey-api/shared'; 7 - import { ensureDirSync } from '@hey-api/shared'; 7 + import { ensureDirSync, outputHeaderToPrefix } from '@hey-api/shared'; 8 8 9 9 import type { Config } from '../config/types'; 10 10 import type { Client } from '../plugins/@hey-api/client-core/types'; ··· 51 51 clientPath: path.resolve(__dirname, 'clients', clientName), 52 52 corePath: path.resolve(__dirname, 'clients', 'core'), 53 53 }; 54 - } 55 - 56 - /** 57 - * Converts an {@link OutputHeader} value to a string prefix for file content. 58 - * Returns an empty string when the header is null, undefined, or a function 59 - * (functions require a render context which is not available for bundled files). 60 - */ 61 - function outputHeaderToPrefix(header: OutputHeader): string { 62 - if (header == null || typeof header === 'function') return ''; 63 - const lines = 64 - typeof header === 'string' 65 - ? header.split(/\r?\n/) 66 - : header.flatMap((line) => line.split(/\r?\n/)); 67 - const content = lines.join('\n'); 68 - return content ? `${content}\n\n` : ''; 69 54 } 70 55 71 56 /** ··· 186 171 meta: ProjectRenderMeta; 187 172 outputPath: string; 188 173 plugin: DefinePlugin<Client.Config & { name: string }>['Config']; 189 - project?: IProject; 174 + project: IProject; 190 175 }): Map<string, string> | undefined { 191 176 const renamed = new Map<string, string>(); 192 177 const devMode = isDevMode(); 193 - const headerPrefix = outputHeaderToPrefix(header); 178 + const headerPrefix = outputHeaderToPrefix(header, project); 194 179 195 180 // copy Hey API clients to output 196 181 const isHeyApiClientPlugin = plugin.name.startsWith('@hey-api/client-');
+5 -1
packages/shared/src/config/output/types.ts
··· 1 1 import type { RenderContext } from '@hey-api/codegen-core'; 2 2 import type { MaybeArray, MaybeFunc } from '@hey-api/types'; 3 3 4 - export type OutputHeader = MaybeFunc<(ctx: RenderContext) => MaybeArray<string> | null | undefined>; 4 + export type OutputHeader = MaybeFunc< 5 + ( 6 + ctx: Omit<RenderContext, 'file'> & Pick<Partial<RenderContext>, 'file'>, 7 + ) => MaybeArray<string> | null | undefined 8 + >;
+1
packages/shared/src/index.ts
··· 113 113 export type { WatchValues } from './types/watch'; 114 114 export { escapeComment } from './utils/escape'; 115 115 export { utils } from './utils/exports'; 116 + export { outputHeaderToPrefix } from './utils/header'; 116 117 export { inputToApiRegistry } from './utils/input'; 117 118 export { heyApiRegistryBaseUrl } from './utils/input/heyApi'; 118 119 export { MinHeap } from './utils/minHeap';
+15
packages/shared/src/utils/header.ts
··· 1 + import type { IProject } from '@hey-api/codegen-core'; 2 + 3 + import type { OutputHeader } from '../config/output/types'; 4 + 5 + /** 6 + * Converts an {@link OutputHeader} value to a string prefix for file content. 7 + */ 8 + export function outputHeaderToPrefix(header: OutputHeader, project: IProject): string { 9 + let lines = typeof header === 'function' ? header({ project }) : header; 10 + if (lines === null || lines === undefined) return ''; 11 + lines = 12 + typeof lines === 'string' ? lines.split(/\r?\n/) : lines.flatMap((line) => line.split(/\r?\n/)); 13 + const content = lines.join('\n'); 14 + return content ? `${content}\n\n` : ''; 15 + }