···103103 OpenApiResponseObject,
104104 OpenApiSchemaObject,
105105} from './openApi/types';
106106-export type { Hooks } from './parser/hooks';
106106+export type { GetNameContext, Hooks } from './parser/hooks';
107107export type { SchemaWithType } from './plugins/shared/types/schema';
108108export { definePluginConfig, mappers } from './plugins/shared/utils/config';
109109export type { PluginInstanceTypes } from './plugins/shared/utils/instance';
+16-10
packages/shared/src/parser/hooks.ts
···11import type { Node, Symbol, SymbolIn, SymbolMeta } from '@hey-api/codegen-core';
22+import type { MaybeFunc } from '@hey-api/types';
2334import type { SchemaProcessorContext } from '../ir/schema-processor';
45import type { IROperationObject, IRSchemaObject } from '../ir/types';
56import type { PluginInstance } from '../plugins/shared/utils/instance';
77+import type { NamingConfig } from '../utils/naming/types';
6879export type Hooks = {
810 /**
···217219 *
218220 * @returns The name to register the symbol with, or undefined to fallback to default behavior.
219221 */
220220- getName?: (ctx: {
221221- /** Arbitrary metadata about the symbol. */
222222- meta: SymbolMeta;
223223- /** The proposed name for the symbol. */
224224- name: string;
225225- /** The operation object associated with the symbol. */
226226- operation?: IROperationObject;
227227- /** The schema object associated with the symbol. */
228228- schema?: IRSchemaObject;
229229- }) => string | undefined;
222222+ getName?: (ctx: GetNameContext) => MaybeFunc<(ctx: GetNameContext) => string | undefined>;
230223 };
231224};
225225+226226+export interface GetNameContext {
227227+ /** Arbitrary metadata about the symbol. */
228228+ meta: SymbolMeta;
229229+ /** The proposed name for the symbol. */
230230+ name: string;
231231+ /** The naming configuration for the symbol. */
232232+ naming?: NamingConfig;
233233+ /** The operation object associated with the symbol. */
234234+ operation?: IROperationObject;
235235+ /** The schema object associated with the symbol. */
236236+ schema?: IRSchemaObject;
237237+}
+37-9
packages/shared/src/plugins/symbol.ts
···11-import type { SymbolIn, SymbolMeta } from '@hey-api/codegen-core';
11+import type { SymbolIn } from '@hey-api/codegen-core';
2233-import type { IROperationObject, IRSchemaObject } from '../ir/types';
33+import type { GetNameContext } from '../parser/hooks';
44+import { applyNaming } from '../utils/naming/naming';
45import type { PluginInstance } from './shared/utils/instance';
5667/**
78 * Helper function to build the input for symbol registration, applying naming hooks if provided.
89 */
99-export function buildSymbolIn(ctx: {
1010- meta: SymbolMeta;
1111- name: string;
1212- operation?: IROperationObject;
1010+export function buildSymbolIn({
1111+ plugin,
1212+ ...ctx
1313+}: GetNameContext & {
1314 plugin: {
1415 config: Pick<PluginInstance['config'], '~hooks'>;
1616+ context: {
1717+ config: {
1818+ parser: Pick<PluginInstance['context']['config']['parser'], 'hooks'>;
1919+ };
2020+ };
1521 };
1616- schema?: IRSchemaObject;
1722}): SymbolIn {
1818- const getName = ctx.plugin.config['~hooks']?.symbols?.getName ?? (() => {});
2323+ const hooks = [
2424+ plugin.config['~hooks']?.symbols?.getName,
2525+ plugin.context.config.parser.hooks.symbols?.getName,
2626+ ];
2727+ for (const hook of hooks) {
2828+ if (!hook) continue;
2929+3030+ const result = hook(ctx);
3131+ if (typeof result === 'function') {
3232+ const name = result(ctx);
3333+ if (name) {
3434+ return {
3535+ meta: ctx.meta,
3636+ name,
3737+ };
3838+ }
3939+ } else if (typeof result === 'string') {
4040+ return {
4141+ meta: ctx.meta,
4242+ name: ctx.naming ? applyNaming(result, ctx.naming) : result,
4343+ };
4444+ }
4545+ }
4646+1947 return {
2048 meta: ctx.meta,
2121- name: getName(ctx) ?? ctx.name,
4949+ name: ctx.naming ? applyNaming(ctx.name, ctx.naming) : ctx.name,
2250 };
2351}