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 #3558 from hey-api/fix/get-name-hook

refactor: improve getName hook

authored by

Lubos and committed by
GitHub
44ce085f dcee6fb8

+101 -55
+3 -2
packages/openapi-python/src/plugins/pydantic/shared/export.ts
··· 1 1 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'; 3 3 4 4 import { $ } from '../../../py-dsl'; 5 5 import type { PydanticPlugin } from '../types'; ··· 30 30 tool: 'pydantic', 31 31 ...meta, 32 32 }, 33 - name: applyNaming(name, naming), 33 + name, 34 + naming, 34 35 plugin, 35 36 schema, 36 37 }),
+4 -3
packages/openapi-ts/src/plugins/@hey-api/typescript/shared/clientOptions.ts
··· 1 1 import type { IR } from '@hey-api/shared'; 2 - import { applyNaming, buildSymbolIn, parseUrl } from '@hey-api/shared'; 2 + import { buildSymbolIn, parseUrl } from '@hey-api/shared'; 3 3 4 4 import { getTypedConfig } from '../../../../config/utils'; 5 5 import { ··· 53 53 role: 'options', 54 54 tool: 'typescript', 55 55 }, 56 - name: applyNaming('ClientOptions', { 56 + name: 'ClientOptions', 57 + naming: { 57 58 case: plugin.config.case, 58 - }), 59 + }, 59 60 plugin, 60 61 }), 61 62 );
+9 -5
packages/openapi-ts/src/plugins/@hey-api/typescript/shared/export.ts
··· 1 1 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'; 3 3 import { pathToJsonPointer } from '@hey-api/shared'; 4 4 5 5 import { createSchemaComment } from '../../../../plugins/shared/utils/schema'; ··· 106 106 resourceId, 107 107 tool: 'typescript', 108 108 }, 109 - name: applyNaming(name, plugin.config.definitions), 109 + name, 110 + naming: plugin.config.definitions, 110 111 plugin, 111 112 schema, 112 113 }), ··· 139 140 resourceId, 140 141 tool: 'typescript', 141 142 }, 142 - name: applyNaming(name, plugin.config.definitions), 143 + name, 144 + naming: plugin.config.definitions, 143 145 plugin, 144 146 schema, 145 147 }), ··· 167 169 resourceId, 168 170 tool: 'typescript', 169 171 }, 170 - name: applyNaming(name, plugin.config.definitions), 172 + name, 173 + naming: plugin.config.definitions, 171 174 plugin, 172 175 schema, 173 176 }), ··· 228 231 tags, 229 232 tool: 'typescript', 230 233 }, 231 - name: applyNaming(name, naming), 234 + name, 235 + naming, 232 236 plugin, 233 237 schema, 234 238 }),
+13 -13
packages/openapi-ts/src/plugins/@hey-api/typescript/shared/operation.ts
··· 1 1 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'; 8 3 9 4 import { $ } from '../../../../ts-dsl'; 10 5 import type { HeyApiTypeScriptPlugin } from '../types'; ··· 131 126 tags, 132 127 tool: 'typescript', 133 128 }, 134 - name: applyNaming(operation.id, plugin.config.requests), 129 + name: operation.id, 130 + naming: plugin.config.requests, 135 131 operation, 136 132 plugin, 137 133 }), ··· 168 164 tags, 169 165 tool: 'typescript', 170 166 }, 171 - name: applyNaming(operation.id, plugin.config.errors), 167 + name: operation.id, 168 + naming: plugin.config.errors, 172 169 operation, 173 170 plugin, 174 171 }), ··· 191 188 tags, 192 189 tool: 'typescript', 193 190 }, 194 - name: applyNaming(operation.id, { 191 + name: operation.id, 192 + naming: { 195 193 case: plugin.config.errors.case, 196 194 name: plugin.config.errors.error, 197 - }), 195 + }, 198 196 operation, 199 197 plugin, 200 198 }), ··· 231 229 tags, 232 230 tool: 'typescript', 233 231 }, 234 - name: applyNaming(operation.id, plugin.config.responses), 232 + name: operation.id, 233 + naming: plugin.config.responses, 235 234 operation, 236 235 plugin, 237 236 }), ··· 254 253 tags, 255 254 tool: 'typescript', 256 255 }, 257 - name: applyNaming(operation.id, { 256 + name: operation.id, 257 + naming: { 258 258 case: plugin.config.responses.case, 259 259 name: plugin.config.responses.response, 260 - }), 260 + }, 261 261 operation, 262 262 plugin, 263 263 }),
+6 -4
packages/openapi-ts/src/plugins/@hey-api/typescript/shared/webhook.ts
··· 1 1 import type { Symbol } from '@hey-api/codegen-core'; 2 2 import type { IR } from '@hey-api/shared'; 3 - import { applyNaming, buildSymbolIn } from '@hey-api/shared'; 3 + import { buildSymbolIn } from '@hey-api/shared'; 4 4 5 5 import { createSchemaComment } from '../../../../plugins/shared/utils/schema'; 6 6 import { $ } from '../../../../ts-dsl'; ··· 33 33 tags, 34 34 tool: 'typescript', 35 35 }, 36 - name: applyNaming(operation.id, { 36 + name: operation.id, 37 + naming: { 37 38 case: plugin.config.webhooks.case, 38 39 name: plugin.config.webhooks.payload, 39 - }), 40 + }, 40 41 operation, 41 42 plugin, 42 43 }), ··· 84 85 tags, 85 86 tool: 'typescript', 86 87 }, 87 - name: applyNaming(operation.id, plugin.config.webhooks), 88 + name: operation.id, 89 + naming: plugin.config.webhooks, 88 90 operation, 89 91 plugin, 90 92 }),
+4 -3
packages/openapi-ts/src/plugins/@hey-api/typescript/v1/plugin.ts
··· 1 1 import type { Symbol } from '@hey-api/codegen-core'; 2 2 import type { IR } from '@hey-api/shared'; 3 - import { applyNaming, buildSymbolIn, pathToJsonPointer } from '@hey-api/shared'; 3 + import { buildSymbolIn, pathToJsonPointer } from '@hey-api/shared'; 4 4 5 5 import { $ } from '../../../../ts-dsl'; 6 6 import { createClientOptions } from '../shared/clientOptions'; ··· 105 105 tool: 'typescript', 106 106 variant: 'container', 107 107 }, 108 - name: applyNaming('Webhooks', { 108 + name: 'Webhooks', 109 + naming: { 109 110 case: plugin.config.case, 110 - }), 111 + }, 111 112 plugin, 112 113 }), 113 114 );
+3 -2
packages/openapi-ts/src/plugins/valibot/shared/export.ts
··· 1 - import { applyNaming, buildSymbolIn, pathToName } from '@hey-api/shared'; 1 + import { buildSymbolIn, pathToName } from '@hey-api/shared'; 2 2 3 3 import { createSchemaComment } from '../../../plugins/shared/utils/schema'; 4 4 import { $ } from '../../../ts-dsl'; ··· 31 31 tool: 'valibot', 32 32 ...meta, 33 33 }, 34 - name: applyNaming(name, naming), 34 + name, 35 + naming, 35 36 plugin, 36 37 schema, 37 38 }),
+5 -3
packages/openapi-ts/src/plugins/zod/shared/export.ts
··· 1 - import { applyNaming, buildSymbolIn, pathToName } from '@hey-api/shared'; 1 + import { buildSymbolIn, pathToName } from '@hey-api/shared'; 2 2 3 3 import { createSchemaComment } from '../../../plugins/shared/utils/schema'; 4 4 import { $ } from '../../../ts-dsl'; ··· 32 32 tool: 'zod', 33 33 ...meta, 34 34 }, 35 - name: applyNaming(name, naming), 35 + name, 36 + naming, 36 37 plugin, 37 38 schema, 38 39 }), ··· 49 50 variant: 'infer', 50 51 ...meta, 51 52 }, 52 - name: applyNaming(name, naming.types.infer), 53 + name, 54 + naming: naming.types.infer, 53 55 plugin, 54 56 schema, 55 57 }),
+1 -1
packages/shared/src/index.ts
··· 103 103 OpenApiResponseObject, 104 104 OpenApiSchemaObject, 105 105 } from './openApi/types'; 106 - export type { Hooks } from './parser/hooks'; 106 + export type { GetNameContext, Hooks } from './parser/hooks'; 107 107 export type { SchemaWithType } from './plugins/shared/types/schema'; 108 108 export { definePluginConfig, mappers } from './plugins/shared/utils/config'; 109 109 export type { PluginInstanceTypes } from './plugins/shared/utils/instance';
+16 -10
packages/shared/src/parser/hooks.ts
··· 1 1 import type { Node, Symbol, SymbolIn, SymbolMeta } from '@hey-api/codegen-core'; 2 + import type { MaybeFunc } from '@hey-api/types'; 2 3 3 4 import type { SchemaProcessorContext } from '../ir/schema-processor'; 4 5 import type { IROperationObject, IRSchemaObject } from '../ir/types'; 5 6 import type { PluginInstance } from '../plugins/shared/utils/instance'; 7 + import type { NamingConfig } from '../utils/naming/types'; 6 8 7 9 export type Hooks = { 8 10 /** ··· 217 219 * 218 220 * @returns The name to register the symbol with, or undefined to fallback to default behavior. 219 221 */ 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>; 230 223 }; 231 224 }; 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 + }
+37 -9
packages/shared/src/plugins/symbol.ts
··· 1 - import type { SymbolIn, SymbolMeta } from '@hey-api/codegen-core'; 1 + import type { SymbolIn } from '@hey-api/codegen-core'; 2 2 3 - import type { IROperationObject, IRSchemaObject } from '../ir/types'; 3 + import type { GetNameContext } from '../parser/hooks'; 4 + import { applyNaming } from '../utils/naming/naming'; 4 5 import type { PluginInstance } from './shared/utils/instance'; 5 6 6 7 /** 7 8 * Helper function to build the input for symbol registration, applying naming hooks if provided. 8 9 */ 9 - export function buildSymbolIn(ctx: { 10 - meta: SymbolMeta; 11 - name: string; 12 - operation?: IROperationObject; 10 + export function buildSymbolIn({ 11 + plugin, 12 + ...ctx 13 + }: GetNameContext & { 13 14 plugin: { 14 15 config: Pick<PluginInstance['config'], '~hooks'>; 16 + context: { 17 + config: { 18 + parser: Pick<PluginInstance['context']['config']['parser'], 'hooks'>; 19 + }; 20 + }; 15 21 }; 16 - schema?: IRSchemaObject; 17 22 }): SymbolIn { 18 - const getName = ctx.plugin.config['~hooks']?.symbols?.getName ?? (() => {}); 23 + const hooks = [ 24 + plugin.config['~hooks']?.symbols?.getName, 25 + plugin.context.config.parser.hooks.symbols?.getName, 26 + ]; 27 + for (const hook of hooks) { 28 + if (!hook) continue; 29 + 30 + const result = hook(ctx); 31 + if (typeof result === 'function') { 32 + const name = result(ctx); 33 + if (name) { 34 + return { 35 + meta: ctx.meta, 36 + name, 37 + }; 38 + } 39 + } else if (typeof result === 'string') { 40 + return { 41 + meta: ctx.meta, 42 + name: ctx.naming ? applyNaming(result, ctx.naming) : result, 43 + }; 44 + } 45 + } 46 + 19 47 return { 20 48 meta: ctx.meta, 21 - name: getName(ctx) ?? ctx.name, 49 + name: ctx.naming ? applyNaming(ctx.name, ctx.naming) : ctx.name, 22 50 }; 23 51 }