diff --git a/packages/openapi-python/src/plugins/pydantic/shared/export.ts b/packages/openapi-python/src/plugins/pydantic/shared/export.ts index cbf09a8d2..e7f75a330 100644 --- a/packages/openapi-python/src/plugins/pydantic/shared/export.ts +++ b/packages/openapi-python/src/plugins/pydantic/shared/export.ts @@ -1,5 +1,5 @@ import type { Symbol } from '@hey-api/codegen-core'; -import { applyNaming, buildSymbolIn, pathToName } from '@hey-api/shared'; +import { buildSymbolIn, pathToName } from '@hey-api/shared'; import { $ } from '../../../py-dsl'; import type { PydanticPlugin } from '../types'; @@ -30,7 +30,8 @@ export function exportAst({ tool: 'pydantic', ...meta, }, - name: applyNaming(name, naming), + name, + naming, plugin, schema, }), diff --git a/packages/openapi-ts/src/plugins/@hey-api/typescript/shared/clientOptions.ts b/packages/openapi-ts/src/plugins/@hey-api/typescript/shared/clientOptions.ts index 838fa20a8..496514836 100644 --- a/packages/openapi-ts/src/plugins/@hey-api/typescript/shared/clientOptions.ts +++ b/packages/openapi-ts/src/plugins/@hey-api/typescript/shared/clientOptions.ts @@ -1,5 +1,5 @@ import type { IR } from '@hey-api/shared'; -import { applyNaming, buildSymbolIn, parseUrl } from '@hey-api/shared'; +import { buildSymbolIn, parseUrl } from '@hey-api/shared'; import { getTypedConfig } from '../../../../config/utils'; import { @@ -53,9 +53,10 @@ export const createClientOptions = ({ role: 'options', tool: 'typescript', }, - name: applyNaming('ClientOptions', { + name: 'ClientOptions', + naming: { case: plugin.config.case, - }), + }, plugin, }), ); diff --git a/packages/openapi-ts/src/plugins/@hey-api/typescript/shared/export.ts b/packages/openapi-ts/src/plugins/@hey-api/typescript/shared/export.ts index 258aa692e..b42166552 100644 --- a/packages/openapi-ts/src/plugins/@hey-api/typescript/shared/export.ts +++ b/packages/openapi-ts/src/plugins/@hey-api/typescript/shared/export.ts @@ -1,5 +1,5 @@ import type { IR } from '@hey-api/shared'; -import { applyNaming, buildSymbolIn, pathToName, toCase } from '@hey-api/shared'; +import { buildSymbolIn, pathToName, toCase } from '@hey-api/shared'; import { pathToJsonPointer } from '@hey-api/shared'; import { createSchemaComment } from '../../../../plugins/shared/utils/schema'; @@ -106,7 +106,8 @@ function buildEnumExport({ resourceId, tool: 'typescript', }, - name: applyNaming(name, plugin.config.definitions), + name, + naming: plugin.config.definitions, plugin, schema, }), @@ -139,7 +140,8 @@ function buildEnumExport({ resourceId, tool: 'typescript', }, - name: applyNaming(name, plugin.config.definitions), + name, + naming: plugin.config.definitions, plugin, schema, }), @@ -167,7 +169,8 @@ function buildEnumExport({ resourceId, tool: 'typescript', }, - name: applyNaming(name, plugin.config.definitions), + name, + naming: plugin.config.definitions, plugin, schema, }), @@ -228,7 +231,8 @@ export function exportAst({ tags, tool: 'typescript', }, - name: applyNaming(name, naming), + name, + naming, plugin, schema, }), diff --git a/packages/openapi-ts/src/plugins/@hey-api/typescript/shared/operation.ts b/packages/openapi-ts/src/plugins/@hey-api/typescript/shared/operation.ts index 93a5e4d60..f48c57c98 100644 --- a/packages/openapi-ts/src/plugins/@hey-api/typescript/shared/operation.ts +++ b/packages/openapi-ts/src/plugins/@hey-api/typescript/shared/operation.ts @@ -1,10 +1,5 @@ import type { IR } from '@hey-api/shared'; -import { - applyNaming, - buildSymbolIn, - deduplicateSchema, - operationResponsesMap, -} from '@hey-api/shared'; +import { buildSymbolIn, deduplicateSchema, operationResponsesMap } from '@hey-api/shared'; import { $ } from '../../../../ts-dsl'; import type { HeyApiTypeScriptPlugin } from '../types'; @@ -131,7 +126,8 @@ export const operationToType = ({ tags, tool: 'typescript', }, - name: applyNaming(operation.id, plugin.config.requests), + name: operation.id, + naming: plugin.config.requests, operation, plugin, }), @@ -168,7 +164,8 @@ export const operationToType = ({ tags, tool: 'typescript', }, - name: applyNaming(operation.id, plugin.config.errors), + name: operation.id, + naming: plugin.config.errors, operation, plugin, }), @@ -191,10 +188,11 @@ export const operationToType = ({ tags, tool: 'typescript', }, - name: applyNaming(operation.id, { + name: operation.id, + naming: { case: plugin.config.errors.case, name: plugin.config.errors.error, - }), + }, operation, plugin, }), @@ -231,7 +229,8 @@ export const operationToType = ({ tags, tool: 'typescript', }, - name: applyNaming(operation.id, plugin.config.responses), + name: operation.id, + naming: plugin.config.responses, operation, plugin, }), @@ -254,10 +253,11 @@ export const operationToType = ({ tags, tool: 'typescript', }, - name: applyNaming(operation.id, { + name: operation.id, + naming: { case: plugin.config.responses.case, name: plugin.config.responses.response, - }), + }, operation, plugin, }), diff --git a/packages/openapi-ts/src/plugins/@hey-api/typescript/shared/webhook.ts b/packages/openapi-ts/src/plugins/@hey-api/typescript/shared/webhook.ts index 3bf859bd3..1bd5ed5f1 100644 --- a/packages/openapi-ts/src/plugins/@hey-api/typescript/shared/webhook.ts +++ b/packages/openapi-ts/src/plugins/@hey-api/typescript/shared/webhook.ts @@ -1,6 +1,6 @@ import type { Symbol } from '@hey-api/codegen-core'; import type { IR } from '@hey-api/shared'; -import { applyNaming, buildSymbolIn } from '@hey-api/shared'; +import { buildSymbolIn } from '@hey-api/shared'; import { createSchemaComment } from '../../../../plugins/shared/utils/schema'; import { $ } from '../../../../ts-dsl'; @@ -33,10 +33,11 @@ export function webhookToType({ tags, tool: 'typescript', }, - name: applyNaming(operation.id, { + name: operation.id, + naming: { case: plugin.config.webhooks.case, name: plugin.config.webhooks.payload, - }), + }, operation, plugin, }), @@ -84,7 +85,8 @@ export function webhookToType({ tags, tool: 'typescript', }, - name: applyNaming(operation.id, plugin.config.webhooks), + name: operation.id, + naming: plugin.config.webhooks, operation, plugin, }), diff --git a/packages/openapi-ts/src/plugins/@hey-api/typescript/v1/plugin.ts b/packages/openapi-ts/src/plugins/@hey-api/typescript/v1/plugin.ts index 2fad04255..b277d0d0f 100644 --- a/packages/openapi-ts/src/plugins/@hey-api/typescript/v1/plugin.ts +++ b/packages/openapi-ts/src/plugins/@hey-api/typescript/v1/plugin.ts @@ -1,6 +1,6 @@ import type { Symbol } from '@hey-api/codegen-core'; import type { IR } from '@hey-api/shared'; -import { applyNaming, buildSymbolIn, pathToJsonPointer } from '@hey-api/shared'; +import { buildSymbolIn, pathToJsonPointer } from '@hey-api/shared'; import { $ } from '../../../../ts-dsl'; import { createClientOptions } from '../shared/clientOptions'; @@ -105,9 +105,10 @@ export const handlerV1: HeyApiTypeScriptPlugin['Handler'] = ({ plugin }) => { tool: 'typescript', variant: 'container', }, - name: applyNaming('Webhooks', { + name: 'Webhooks', + naming: { case: plugin.config.case, - }), + }, plugin, }), ); diff --git a/packages/openapi-ts/src/plugins/valibot/shared/export.ts b/packages/openapi-ts/src/plugins/valibot/shared/export.ts index 7edad3da4..e2971af91 100644 --- a/packages/openapi-ts/src/plugins/valibot/shared/export.ts +++ b/packages/openapi-ts/src/plugins/valibot/shared/export.ts @@ -1,4 +1,4 @@ -import { applyNaming, buildSymbolIn, pathToName } from '@hey-api/shared'; +import { buildSymbolIn, pathToName } from '@hey-api/shared'; import { createSchemaComment } from '../../../plugins/shared/utils/schema'; import { $ } from '../../../ts-dsl'; @@ -31,7 +31,8 @@ export function exportAst({ tool: 'valibot', ...meta, }, - name: applyNaming(name, naming), + name, + naming, plugin, schema, }), diff --git a/packages/openapi-ts/src/plugins/zod/shared/export.ts b/packages/openapi-ts/src/plugins/zod/shared/export.ts index f6db83ac4..07b811cd6 100644 --- a/packages/openapi-ts/src/plugins/zod/shared/export.ts +++ b/packages/openapi-ts/src/plugins/zod/shared/export.ts @@ -1,4 +1,4 @@ -import { applyNaming, buildSymbolIn, pathToName } from '@hey-api/shared'; +import { buildSymbolIn, pathToName } from '@hey-api/shared'; import { createSchemaComment } from '../../../plugins/shared/utils/schema'; import { $ } from '../../../ts-dsl'; @@ -32,7 +32,8 @@ export function exportAst({ tool: 'zod', ...meta, }, - name: applyNaming(name, naming), + name, + naming, plugin, schema, }), @@ -49,7 +50,8 @@ export function exportAst({ variant: 'infer', ...meta, }, - name: applyNaming(name, naming.types.infer), + name, + naming: naming.types.infer, plugin, schema, }), diff --git a/packages/shared/src/index.ts b/packages/shared/src/index.ts index efb28190a..b5cfbdeb4 100644 --- a/packages/shared/src/index.ts +++ b/packages/shared/src/index.ts @@ -103,7 +103,7 @@ export type { OpenApiResponseObject, OpenApiSchemaObject, } from './openApi/types'; -export type { Hooks } from './parser/hooks'; +export type { GetNameContext, Hooks } from './parser/hooks'; export type { SchemaWithType } from './plugins/shared/types/schema'; export { definePluginConfig, mappers } from './plugins/shared/utils/config'; export type { PluginInstanceTypes } from './plugins/shared/utils/instance'; diff --git a/packages/shared/src/parser/hooks.ts b/packages/shared/src/parser/hooks.ts index 7d3f92e93..e0e55b06e 100644 --- a/packages/shared/src/parser/hooks.ts +++ b/packages/shared/src/parser/hooks.ts @@ -1,8 +1,10 @@ import type { Node, Symbol, SymbolIn, SymbolMeta } from '@hey-api/codegen-core'; +import type { MaybeFunc } from '@hey-api/types'; import type { SchemaProcessorContext } from '../ir/schema-processor'; import type { IROperationObject, IRSchemaObject } from '../ir/types'; import type { PluginInstance } from '../plugins/shared/utils/instance'; +import type { NamingConfig } from '../utils/naming/types'; export type Hooks = { /** @@ -217,15 +219,19 @@ export type Hooks = { * * @returns The name to register the symbol with, or undefined to fallback to default behavior. */ - getName?: (ctx: { - /** Arbitrary metadata about the symbol. */ - meta: SymbolMeta; - /** The proposed name for the symbol. */ - name: string; - /** The operation object associated with the symbol. */ - operation?: IROperationObject; - /** The schema object associated with the symbol. */ - schema?: IRSchemaObject; - }) => string | undefined; + getName?: (ctx: GetNameContext) => MaybeFunc<(ctx: GetNameContext) => string | undefined>; }; }; + +export interface GetNameContext { + /** Arbitrary metadata about the symbol. */ + meta: SymbolMeta; + /** The proposed name for the symbol. */ + name: string; + /** The naming configuration for the symbol. */ + naming?: NamingConfig; + /** The operation object associated with the symbol. */ + operation?: IROperationObject; + /** The schema object associated with the symbol. */ + schema?: IRSchemaObject; +} diff --git a/packages/shared/src/plugins/symbol.ts b/packages/shared/src/plugins/symbol.ts index 2981f2b9d..9ae64e647 100644 --- a/packages/shared/src/plugins/symbol.ts +++ b/packages/shared/src/plugins/symbol.ts @@ -1,23 +1,51 @@ -import type { SymbolIn, SymbolMeta } from '@hey-api/codegen-core'; +import type { SymbolIn } from '@hey-api/codegen-core'; -import type { IROperationObject, IRSchemaObject } from '../ir/types'; +import type { GetNameContext } from '../parser/hooks'; +import { applyNaming } from '../utils/naming/naming'; import type { PluginInstance } from './shared/utils/instance'; /** * Helper function to build the input for symbol registration, applying naming hooks if provided. */ -export function buildSymbolIn(ctx: { - meta: SymbolMeta; - name: string; - operation?: IROperationObject; +export function buildSymbolIn({ + plugin, + ...ctx +}: GetNameContext & { plugin: { config: Pick; + context: { + config: { + parser: Pick; + }; + }; }; - schema?: IRSchemaObject; }): SymbolIn { - const getName = ctx.plugin.config['~hooks']?.symbols?.getName ?? (() => {}); + const hooks = [ + plugin.config['~hooks']?.symbols?.getName, + plugin.context.config.parser.hooks.symbols?.getName, + ]; + for (const hook of hooks) { + if (!hook) continue; + + const result = hook(ctx); + if (typeof result === 'function') { + const name = result(ctx); + if (name) { + return { + meta: ctx.meta, + name, + }; + } + } else if (typeof result === 'string') { + return { + meta: ctx.meta, + name: ctx.naming ? applyNaming(result, ctx.naming) : result, + }; + } + } + return { meta: ctx.meta, - name: getName(ctx) ?? ctx.name, + name: ctx.naming ? applyNaming(ctx.name, ctx.naming) : ctx.name, }; }