Skip to content

Commit 44ce085

Browse files
authored
Merge pull request #3558 from hey-api/fix/get-name-hook
refactor: improve getName hook
2 parents dcee6fb + 3c61b3b commit 44ce085

11 files changed

Lines changed: 101 additions & 55 deletions

File tree

packages/openapi-python/src/plugins/pydantic/shared/export.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Symbol } from '@hey-api/codegen-core';
2-
import { applyNaming, buildSymbolIn, pathToName } from '@hey-api/shared';
2+
import { buildSymbolIn, pathToName } from '@hey-api/shared';
33

44
import { $ } from '../../../py-dsl';
55
import type { PydanticPlugin } from '../types';
@@ -30,7 +30,8 @@ export function exportAst({
3030
tool: 'pydantic',
3131
...meta,
3232
},
33-
name: applyNaming(name, naming),
33+
name,
34+
naming,
3435
plugin,
3536
schema,
3637
}),

packages/openapi-ts/src/plugins/@hey-api/typescript/shared/clientOptions.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { IR } from '@hey-api/shared';
2-
import { applyNaming, buildSymbolIn, parseUrl } from '@hey-api/shared';
2+
import { buildSymbolIn, parseUrl } from '@hey-api/shared';
33

44
import { getTypedConfig } from '../../../../config/utils';
55
import {
@@ -53,9 +53,10 @@ export const createClientOptions = ({
5353
role: 'options',
5454
tool: 'typescript',
5555
},
56-
name: applyNaming('ClientOptions', {
56+
name: 'ClientOptions',
57+
naming: {
5758
case: plugin.config.case,
58-
}),
59+
},
5960
plugin,
6061
}),
6162
);

packages/openapi-ts/src/plugins/@hey-api/typescript/shared/export.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { IR } from '@hey-api/shared';
2-
import { applyNaming, buildSymbolIn, pathToName, toCase } from '@hey-api/shared';
2+
import { buildSymbolIn, pathToName, toCase } from '@hey-api/shared';
33
import { pathToJsonPointer } from '@hey-api/shared';
44

55
import { createSchemaComment } from '../../../../plugins/shared/utils/schema';
@@ -106,7 +106,8 @@ function buildEnumExport({
106106
resourceId,
107107
tool: 'typescript',
108108
},
109-
name: applyNaming(name, plugin.config.definitions),
109+
name,
110+
naming: plugin.config.definitions,
110111
plugin,
111112
schema,
112113
}),
@@ -139,7 +140,8 @@ function buildEnumExport({
139140
resourceId,
140141
tool: 'typescript',
141142
},
142-
name: applyNaming(name, plugin.config.definitions),
143+
name,
144+
naming: plugin.config.definitions,
143145
plugin,
144146
schema,
145147
}),
@@ -167,7 +169,8 @@ function buildEnumExport({
167169
resourceId,
168170
tool: 'typescript',
169171
},
170-
name: applyNaming(name, plugin.config.definitions),
172+
name,
173+
naming: plugin.config.definitions,
171174
plugin,
172175
schema,
173176
}),
@@ -228,7 +231,8 @@ export function exportAst({
228231
tags,
229232
tool: 'typescript',
230233
},
231-
name: applyNaming(name, naming),
234+
name,
235+
naming,
232236
plugin,
233237
schema,
234238
}),

packages/openapi-ts/src/plugins/@hey-api/typescript/shared/operation.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import type { IR } from '@hey-api/shared';
2-
import {
3-
applyNaming,
4-
buildSymbolIn,
5-
deduplicateSchema,
6-
operationResponsesMap,
7-
} from '@hey-api/shared';
2+
import { buildSymbolIn, deduplicateSchema, operationResponsesMap } from '@hey-api/shared';
83

94
import { $ } from '../../../../ts-dsl';
105
import type { HeyApiTypeScriptPlugin } from '../types';
@@ -131,7 +126,8 @@ export const operationToType = ({
131126
tags,
132127
tool: 'typescript',
133128
},
134-
name: applyNaming(operation.id, plugin.config.requests),
129+
name: operation.id,
130+
naming: plugin.config.requests,
135131
operation,
136132
plugin,
137133
}),
@@ -168,7 +164,8 @@ export const operationToType = ({
168164
tags,
169165
tool: 'typescript',
170166
},
171-
name: applyNaming(operation.id, plugin.config.errors),
167+
name: operation.id,
168+
naming: plugin.config.errors,
172169
operation,
173170
plugin,
174171
}),
@@ -191,10 +188,11 @@ export const operationToType = ({
191188
tags,
192189
tool: 'typescript',
193190
},
194-
name: applyNaming(operation.id, {
191+
name: operation.id,
192+
naming: {
195193
case: plugin.config.errors.case,
196194
name: plugin.config.errors.error,
197-
}),
195+
},
198196
operation,
199197
plugin,
200198
}),
@@ -231,7 +229,8 @@ export const operationToType = ({
231229
tags,
232230
tool: 'typescript',
233231
},
234-
name: applyNaming(operation.id, plugin.config.responses),
232+
name: operation.id,
233+
naming: plugin.config.responses,
235234
operation,
236235
plugin,
237236
}),
@@ -254,10 +253,11 @@ export const operationToType = ({
254253
tags,
255254
tool: 'typescript',
256255
},
257-
name: applyNaming(operation.id, {
256+
name: operation.id,
257+
naming: {
258258
case: plugin.config.responses.case,
259259
name: plugin.config.responses.response,
260-
}),
260+
},
261261
operation,
262262
plugin,
263263
}),

packages/openapi-ts/src/plugins/@hey-api/typescript/shared/webhook.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Symbol } from '@hey-api/codegen-core';
22
import type { IR } from '@hey-api/shared';
3-
import { applyNaming, buildSymbolIn } from '@hey-api/shared';
3+
import { buildSymbolIn } from '@hey-api/shared';
44

55
import { createSchemaComment } from '../../../../plugins/shared/utils/schema';
66
import { $ } from '../../../../ts-dsl';
@@ -33,10 +33,11 @@ export function webhookToType({
3333
tags,
3434
tool: 'typescript',
3535
},
36-
name: applyNaming(operation.id, {
36+
name: operation.id,
37+
naming: {
3738
case: plugin.config.webhooks.case,
3839
name: plugin.config.webhooks.payload,
39-
}),
40+
},
4041
operation,
4142
plugin,
4243
}),
@@ -84,7 +85,8 @@ export function webhookToType({
8485
tags,
8586
tool: 'typescript',
8687
},
87-
name: applyNaming(operation.id, plugin.config.webhooks),
88+
name: operation.id,
89+
naming: plugin.config.webhooks,
8890
operation,
8991
plugin,
9092
}),

packages/openapi-ts/src/plugins/@hey-api/typescript/v1/plugin.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Symbol } from '@hey-api/codegen-core';
22
import type { IR } from '@hey-api/shared';
3-
import { applyNaming, buildSymbolIn, pathToJsonPointer } from '@hey-api/shared';
3+
import { buildSymbolIn, pathToJsonPointer } from '@hey-api/shared';
44

55
import { $ } from '../../../../ts-dsl';
66
import { createClientOptions } from '../shared/clientOptions';
@@ -105,9 +105,10 @@ export const handlerV1: HeyApiTypeScriptPlugin['Handler'] = ({ plugin }) => {
105105
tool: 'typescript',
106106
variant: 'container',
107107
},
108-
name: applyNaming('Webhooks', {
108+
name: 'Webhooks',
109+
naming: {
109110
case: plugin.config.case,
110-
}),
111+
},
111112
plugin,
112113
}),
113114
);

packages/openapi-ts/src/plugins/valibot/shared/export.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { applyNaming, buildSymbolIn, pathToName } from '@hey-api/shared';
1+
import { buildSymbolIn, pathToName } from '@hey-api/shared';
22

33
import { createSchemaComment } from '../../../plugins/shared/utils/schema';
44
import { $ } from '../../../ts-dsl';
@@ -31,7 +31,8 @@ export function exportAst({
3131
tool: 'valibot',
3232
...meta,
3333
},
34-
name: applyNaming(name, naming),
34+
name,
35+
naming,
3536
plugin,
3637
schema,
3738
}),

packages/openapi-ts/src/plugins/zod/shared/export.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { applyNaming, buildSymbolIn, pathToName } from '@hey-api/shared';
1+
import { buildSymbolIn, pathToName } from '@hey-api/shared';
22

33
import { createSchemaComment } from '../../../plugins/shared/utils/schema';
44
import { $ } from '../../../ts-dsl';
@@ -32,7 +32,8 @@ export function exportAst({
3232
tool: 'zod',
3333
...meta,
3434
},
35-
name: applyNaming(name, naming),
35+
name,
36+
naming,
3637
plugin,
3738
schema,
3839
}),
@@ -49,7 +50,8 @@ export function exportAst({
4950
variant: 'infer',
5051
...meta,
5152
},
52-
name: applyNaming(name, naming.types.infer),
53+
name,
54+
naming: naming.types.infer,
5355
plugin,
5456
schema,
5557
}),

packages/shared/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export type {
103103
OpenApiResponseObject,
104104
OpenApiSchemaObject,
105105
} from './openApi/types';
106-
export type { Hooks } from './parser/hooks';
106+
export type { GetNameContext, Hooks } from './parser/hooks';
107107
export type { SchemaWithType } from './plugins/shared/types/schema';
108108
export { definePluginConfig, mappers } from './plugins/shared/utils/config';
109109
export type { PluginInstanceTypes } from './plugins/shared/utils/instance';

packages/shared/src/parser/hooks.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import type { Node, Symbol, SymbolIn, SymbolMeta } from '@hey-api/codegen-core';
2+
import type { MaybeFunc } from '@hey-api/types';
23

34
import type { SchemaProcessorContext } from '../ir/schema-processor';
45
import type { IROperationObject, IRSchemaObject } from '../ir/types';
56
import type { PluginInstance } from '../plugins/shared/utils/instance';
7+
import type { NamingConfig } from '../utils/naming/types';
68

79
export type Hooks = {
810
/**
@@ -217,15 +219,19 @@ export type Hooks = {
217219
*
218220
* @returns The name to register the symbol with, or undefined to fallback to default behavior.
219221
*/
220-
getName?: (ctx: {
221-
/** Arbitrary metadata about the symbol. */
222-
meta: SymbolMeta;
223-
/** The proposed name for the symbol. */
224-
name: string;
225-
/** The operation object associated with the symbol. */
226-
operation?: IROperationObject;
227-
/** The schema object associated with the symbol. */
228-
schema?: IRSchemaObject;
229-
}) => string | undefined;
222+
getName?: (ctx: GetNameContext) => MaybeFunc<(ctx: GetNameContext) => string | undefined>;
230223
};
231224
};
225+
226+
export interface GetNameContext {
227+
/** Arbitrary metadata about the symbol. */
228+
meta: SymbolMeta;
229+
/** The proposed name for the symbol. */
230+
name: string;
231+
/** The naming configuration for the symbol. */
232+
naming?: NamingConfig;
233+
/** The operation object associated with the symbol. */
234+
operation?: IROperationObject;
235+
/** The schema object associated with the symbol. */
236+
schema?: IRSchemaObject;
237+
}

0 commit comments

Comments
 (0)