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 #3556 from hey-api/feat/parser-hook-symbol-name

authored by

Lubos and committed by
GitHub
dcee6fb8 ebc84f56

+296 -161
+5
.changeset/flat-crews-lie.md
··· 1 + --- 2 + "@hey-api/openapi-ts": patch 3 + --- 4 + 5 + **plugin(@hey-api/typescript)**: implement `getName()` symbol hook
+5
.changeset/poor-grapes-knock.md
··· 1 + --- 2 + "@hey-api/openapi-ts": patch 3 + --- 4 + 5 + **plugin(zod)**: implement `getName()` symbol hook
+5
.changeset/stupid-crabs-joke.md
··· 1 + --- 2 + "@hey-api/openapi-ts": patch 3 + --- 4 + 5 + **plugin(valibot)**: implement `getName()` symbol hook
+5
.changeset/swift-llamas-notice.md
··· 1 + --- 2 + "@hey-api/shared": patch 3 + --- 4 + 5 + **parser**: add `getName()` symbol hook
+16 -10
packages/openapi-python/src/plugins/pydantic/shared/export.ts
··· 1 1 import type { Symbol } from '@hey-api/codegen-core'; 2 - import { applyNaming, pathToName } from '@hey-api/shared'; 2 + import { applyNaming, buildSymbolIn, pathToName } from '@hey-api/shared'; 3 3 4 4 import { $ } from '../../../py-dsl'; 5 5 import type { PydanticPlugin } from '../types'; ··· 15 15 namingAnchor, 16 16 path, 17 17 plugin, 18 + schema, 18 19 tags, 19 20 }: ProcessorContext & { 20 21 final: PydanticFinal; 21 22 }): void { 22 23 const name = pathToName(path, { anchor: namingAnchor }); 23 - const symbol = plugin.symbol(applyNaming(name, naming), { 24 - meta: { 25 - category: 'schema', 26 - path, 27 - tags, 28 - tool: 'pydantic', 29 - ...meta, 30 - }, 31 - }); 24 + const symbol = plugin.registerSymbol( 25 + buildSymbolIn({ 26 + meta: { 27 + category: 'schema', 28 + path, 29 + tags, 30 + tool: 'pydantic', 31 + ...meta, 32 + }, 33 + name: applyNaming(name, naming), 34 + plugin, 35 + schema, 36 + }), 37 + ); 32 38 33 39 if (final.enumMembers) { 34 40 exportEnumClass({ final, plugin, symbol });
+8 -7
packages/openapi-ts/src/plugins/@hey-api/typescript/shared/clientOptions.ts
··· 1 1 import type { IR } from '@hey-api/shared'; 2 - import { applyNaming, parseUrl } from '@hey-api/shared'; 2 + import { applyNaming, buildSymbolIn, parseUrl } from '@hey-api/shared'; 3 3 4 4 import { getTypedConfig } from '../../../../config/utils'; 5 5 import { ··· 45 45 types.push($.type.and($.type('string'), $.type.object())); 46 46 } 47 47 48 - const symbol = plugin.symbol( 49 - applyNaming('ClientOptions', { 50 - case: plugin.config.case, 51 - }), 52 - { 48 + const symbol = plugin.registerSymbol( 49 + buildSymbolIn({ 53 50 meta: { 54 51 category: 'type', 55 52 resource: 'client', 56 53 role: 'options', 57 54 tool: 'typescript', 58 55 }, 59 - }, 56 + name: applyNaming('ClientOptions', { 57 + case: plugin.config.case, 58 + }), 59 + plugin, 60 + }), 60 61 ); 61 62 62 63 const node = $.type
+55 -35
packages/openapi-ts/src/plugins/@hey-api/typescript/shared/export.ts
··· 1 1 import type { IR } from '@hey-api/shared'; 2 - import { applyNaming, pathToName, toCase } from '@hey-api/shared'; 2 + import { applyNaming, 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'; ··· 98 98 ? items.filter((item) => item.schema.const !== null) 99 99 : items; 100 100 101 - const symbolObject = plugin.symbol(applyNaming(name, plugin.config.definitions), { 102 - meta: { 103 - category: 'utility', 104 - resource: 'definition', 105 - resourceId, 106 - tool: 'typescript', 107 - }, 108 - }); 101 + const symbolObject = plugin.registerSymbol( 102 + buildSymbolIn({ 103 + meta: { 104 + category: 'utility', 105 + resource: 'definition', 106 + resourceId, 107 + tool: 'typescript', 108 + }, 109 + name: applyNaming(name, plugin.config.definitions), 110 + plugin, 111 + schema, 112 + }), 113 + ); 109 114 110 115 const objectNode = $.const(symbolObject) 111 116 .export() ··· 126 131 ); 127 132 plugin.node(objectNode); 128 133 129 - const symbol = plugin.symbol(applyNaming(name, plugin.config.definitions), { 130 - meta: { 131 - category: 'type', 132 - resource: 'definition', 133 - resourceId, 134 - tool: 'typescript', 135 - }, 136 - }); 134 + const symbol = plugin.registerSymbol( 135 + buildSymbolIn({ 136 + meta: { 137 + category: 'type', 138 + resource: 'definition', 139 + resourceId, 140 + tool: 'typescript', 141 + }, 142 + name: applyNaming(name, plugin.config.definitions), 143 + plugin, 144 + schema, 145 + }), 146 + ); 137 147 const node = $.type 138 148 .alias(symbol) 139 149 .export() ··· 149 159 ); 150 160 if (hasInvalidTypes) return false; 151 161 152 - const symbol = plugin.symbol(applyNaming(name, plugin.config.definitions), { 153 - meta: { 154 - category: 'type', 155 - resource: 'definition', 156 - resourceId, 157 - tool: 'typescript', 158 - }, 159 - }); 162 + const symbol = plugin.registerSymbol( 163 + buildSymbolIn({ 164 + meta: { 165 + category: 'type', 166 + resource: 'definition', 167 + resourceId, 168 + tool: 'typescript', 169 + }, 170 + name: applyNaming(name, plugin.config.definitions), 171 + plugin, 172 + schema, 173 + }), 174 + ); 160 175 const enumNode = $.enum(symbol) 161 176 .export() 162 177 .$if(plugin.config.comments && createSchemaComment(schema), (e, v) => e.doc(v)) ··· 203 218 return; 204 219 } 205 220 206 - const symbol = plugin.symbol(applyNaming(name, naming), { 207 - meta: { 208 - category: 'type', 209 - path, 210 - resource: 'definition', 211 - resourceId: $ref, 212 - tags, 213 - tool: 'typescript', 214 - }, 215 - }); 221 + const symbol = plugin.registerSymbol( 222 + buildSymbolIn({ 223 + meta: { 224 + category: 'type', 225 + path, 226 + resource: 'definition', 227 + resourceId: $ref, 228 + tags, 229 + tool: 'typescript', 230 + }, 231 + name: applyNaming(name, naming), 232 + plugin, 233 + schema, 234 + }), 235 + ); 216 236 217 237 const node = $.type 218 238 .alias(symbol)
+72 -50
packages/openapi-ts/src/plugins/@hey-api/typescript/shared/operation.ts
··· 1 1 import type { IR } from '@hey-api/shared'; 2 - import { applyNaming } from '@hey-api/shared'; 3 - import { operationResponsesMap } from '@hey-api/shared'; 4 - import { deduplicateSchema } from '@hey-api/shared'; 2 + import { 3 + applyNaming, 4 + buildSymbolIn, 5 + deduplicateSchema, 6 + operationResponsesMap, 7 + } from '@hey-api/shared'; 5 8 6 9 import { $ } from '../../../../ts-dsl'; 7 10 import type { HeyApiTypeScriptPlugin } from '../types'; ··· 117 120 schema: data, 118 121 }); 119 122 120 - const dataSymbol = plugin.symbol(applyNaming(operation.id, plugin.config.requests), { 121 - meta: { 122 - category: 'type', 123 - path, 124 - resource: 'operation', 125 - resourceId: operation.id, 126 - role: 'data', 127 - tags, 128 - tool: 'typescript', 129 - }, 130 - }); 123 + const dataSymbol = plugin.registerSymbol( 124 + buildSymbolIn({ 125 + meta: { 126 + category: 'type', 127 + path, 128 + resource: 'operation', 129 + resourceId: operation.id, 130 + role: 'data', 131 + tags, 132 + tool: 'typescript', 133 + }, 134 + name: applyNaming(operation.id, plugin.config.requests), 135 + operation, 136 + plugin, 137 + }), 138 + ); 131 139 const dataNode = $.type 132 140 .alias(dataSymbol) 133 141 .export() ··· 149 157 schema: errors, 150 158 }); 151 159 152 - const errorsSymbol = plugin.symbol(applyNaming(operation.id, plugin.config.errors), { 153 - meta: { 154 - category: 'type', 155 - path, 156 - resource: 'operation', 157 - resourceId: operation.id, 158 - role: 'errors', 159 - tags, 160 - tool: 'typescript', 161 - }, 162 - }); 160 + const errorsSymbol = plugin.registerSymbol( 161 + buildSymbolIn({ 162 + meta: { 163 + category: 'type', 164 + path, 165 + resource: 'operation', 166 + resourceId: operation.id, 167 + role: 'errors', 168 + tags, 169 + tool: 'typescript', 170 + }, 171 + name: applyNaming(operation.id, plugin.config.errors), 172 + operation, 173 + plugin, 174 + }), 175 + ); 163 176 const errorsNode = $.type 164 177 .alias(errorsSymbol) 165 178 .export() ··· 167 180 plugin.node(errorsNode); 168 181 169 182 if (error) { 170 - const errorSymbol = plugin.symbol( 171 - applyNaming(operation.id, { 172 - case: plugin.config.errors.case, 173 - name: plugin.config.errors.error, 174 - }), 175 - { 183 + const errorSymbol = plugin.registerSymbol( 184 + buildSymbolIn({ 176 185 meta: { 177 186 category: 'type', 178 187 path, ··· 182 191 tags, 183 192 tool: 'typescript', 184 193 }, 185 - }, 194 + name: applyNaming(operation.id, { 195 + case: plugin.config.errors.case, 196 + name: plugin.config.errors.error, 197 + }), 198 + operation, 199 + plugin, 200 + }), 186 201 ); 187 202 const errorNode = $.type 188 203 .alias(errorSymbol) ··· 205 220 schema: responses, 206 221 }); 207 222 208 - const responsesSymbol = plugin.symbol(applyNaming(operation.id, plugin.config.responses), { 209 - meta: { 210 - category: 'type', 211 - path, 212 - resource: 'operation', 213 - resourceId: operation.id, 214 - role: 'responses', 215 - tags, 216 - tool: 'typescript', 217 - }, 218 - }); 223 + const responsesSymbol = plugin.registerSymbol( 224 + buildSymbolIn({ 225 + meta: { 226 + category: 'type', 227 + path, 228 + resource: 'operation', 229 + resourceId: operation.id, 230 + role: 'responses', 231 + tags, 232 + tool: 'typescript', 233 + }, 234 + name: applyNaming(operation.id, plugin.config.responses), 235 + operation, 236 + plugin, 237 + }), 238 + ); 219 239 const responsesNode = $.type 220 240 .alias(responsesSymbol) 221 241 .export() ··· 223 243 plugin.node(responsesNode); 224 244 225 245 if (response) { 226 - const responseSymbol = plugin.symbol( 227 - applyNaming(operation.id, { 228 - case: plugin.config.responses.case, 229 - name: plugin.config.responses.response, 230 - }), 231 - { 246 + const responseSymbol = plugin.registerSymbol( 247 + buildSymbolIn({ 232 248 meta: { 233 249 category: 'type', 234 250 path, ··· 238 254 tags, 239 255 tool: 'typescript', 240 256 }, 241 - }, 257 + name: applyNaming(operation.id, { 258 + case: plugin.config.responses.case, 259 + name: plugin.config.responses.response, 260 + }), 261 + operation, 262 + plugin, 263 + }), 242 264 ); 243 265 const responseNode = $.type 244 266 .alias(responseSymbol)
+26 -19
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 } from '@hey-api/shared'; 3 + import { applyNaming, buildSymbolIn } from '@hey-api/shared'; 4 4 5 5 import { createSchemaComment } from '../../../../plugins/shared/utils/schema'; 6 6 import { $ } from '../../../../ts-dsl'; ··· 22 22 let symbolWebhookPayload: Symbol | undefined; 23 23 24 24 if (operation.body) { 25 - symbolWebhookPayload = plugin.symbol( 26 - applyNaming(operation.id, { 27 - case: plugin.config.webhooks.case, 28 - name: plugin.config.webhooks.payload, 29 - }), 30 - { 25 + symbolWebhookPayload = plugin.registerSymbol( 26 + buildSymbolIn({ 31 27 meta: { 32 28 category: 'type', 33 29 path, ··· 37 33 tags, 38 34 tool: 'typescript', 39 35 }, 40 - }, 36 + name: applyNaming(operation.id, { 37 + case: plugin.config.webhooks.case, 38 + name: plugin.config.webhooks.payload, 39 + }), 40 + operation, 41 + plugin, 42 + }), 41 43 ); 42 44 43 45 const payloadResult = processor.process({ ··· 71 73 .prop('path', (p) => p.required(false).type($.type('never'))) 72 74 .prop('query', (p) => p.required(false).type($.type('never'))); 73 75 74 - const symbol = plugin.symbol(applyNaming(operation.id, plugin.config.webhooks), { 75 - meta: { 76 - category: 'type', 77 - path, 78 - resource: 'webhook', 79 - resourceId: operation.id, 80 - role: 'data', 81 - tags, 82 - tool: 'typescript', 83 - }, 84 - }); 76 + const symbol = plugin.registerSymbol( 77 + buildSymbolIn({ 78 + meta: { 79 + category: 'type', 80 + path, 81 + resource: 'webhook', 82 + resourceId: operation.id, 83 + role: 'data', 84 + tags, 85 + tool: 'typescript', 86 + }, 87 + name: applyNaming(operation.id, plugin.config.webhooks), 88 + operation, 89 + plugin, 90 + }), 91 + ); 85 92 86 93 const node = $.type.alias(symbol).export().type(requestType); 87 94 plugin.node(node);
+8 -7
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, pathToJsonPointer } from '@hey-api/shared'; 3 + import { applyNaming, buildSymbolIn, pathToJsonPointer } from '@hey-api/shared'; 4 4 5 5 import { $ } from '../../../../ts-dsl'; 6 6 import { createClientOptions } from '../shared/clientOptions'; ··· 97 97 createClientOptions({ nodeIndex: nodeClientIndex, plugin, servers }); 98 98 99 99 if (webhooks.length > 0) { 100 - const symbol = plugin.symbol( 101 - applyNaming('Webhooks', { 102 - case: plugin.config.case, 103 - }), 104 - { 100 + const symbol = plugin.registerSymbol( 101 + buildSymbolIn({ 105 102 meta: { 106 103 category: 'type', 107 104 resource: 'webhook', 108 105 tool: 'typescript', 109 106 variant: 'container', 110 107 }, 111 - }, 108 + name: applyNaming('Webhooks', { 109 + case: plugin.config.case, 110 + }), 111 + plugin, 112 + }), 112 113 ); 113 114 const node = $.type 114 115 .alias(symbol)
+16 -10
packages/openapi-ts/src/plugins/valibot/shared/export.ts
··· 1 - import { applyNaming, pathToName } from '@hey-api/shared'; 1 + import { applyNaming, buildSymbolIn, pathToName } from '@hey-api/shared'; 2 2 3 3 import { createSchemaComment } from '../../../plugins/shared/utils/schema'; 4 4 import { $ } from '../../../ts-dsl'; ··· 21 21 const v = plugin.external('valibot.v'); 22 22 23 23 const name = pathToName(path, { anchor: namingAnchor }); 24 - const symbol = plugin.symbol(applyNaming(name, naming), { 25 - meta: { 26 - category: 'schema', 27 - path, 28 - tags, 29 - tool: 'valibot', 30 - ...meta, 31 - }, 32 - }); 24 + 25 + const symbol = plugin.registerSymbol( 26 + buildSymbolIn({ 27 + meta: { 28 + category: 'schema', 29 + path, 30 + tags, 31 + tool: 'valibot', 32 + ...meta, 33 + }, 34 + name: applyNaming(name, naming), 35 + plugin, 36 + schema, 37 + }), 38 + ); 33 39 34 40 const statement = $.const(symbol) 35 41 .export()
+31 -20
packages/openapi-ts/src/plugins/zod/shared/export.ts
··· 1 - import { applyNaming, pathToName } from '@hey-api/shared'; 1 + import { applyNaming, buildSymbolIn, pathToName } from '@hey-api/shared'; 2 2 3 3 import { createSchemaComment } from '../../../plugins/shared/utils/schema'; 4 4 import { $ } from '../../../ts-dsl'; ··· 22 22 const z = plugin.external('zod.z'); 23 23 24 24 const name = pathToName(path, { anchor: namingAnchor }); 25 - const symbol = plugin.symbol(applyNaming(name, naming), { 26 - meta: { 27 - category: 'schema', 28 - path, 29 - tags, 30 - tool: 'zod', 31 - ...meta, 32 - }, 33 - }); 25 + 26 + const symbol = plugin.registerSymbol( 27 + buildSymbolIn({ 28 + meta: { 29 + category: 'schema', 30 + path, 31 + tags, 32 + tool: 'zod', 33 + ...meta, 34 + }, 35 + name: applyNaming(name, naming), 36 + plugin, 37 + schema, 38 + }), 39 + ); 34 40 35 41 const typeInferSymbol = naming.types.infer.enabled 36 - ? plugin.symbol(applyNaming(name, naming.types.infer), { 37 - meta: { 38 - category: 'type', 39 - path, 40 - tags, 41 - tool: 'zod', 42 - variant: 'infer', 43 - ...meta, 44 - }, 45 - }) 42 + ? plugin.registerSymbol( 43 + buildSymbolIn({ 44 + meta: { 45 + category: 'type', 46 + path, 47 + tags, 48 + tool: 'zod', 49 + variant: 'infer', 50 + ...meta, 51 + }, 52 + name: applyNaming(name, naming.types.infer), 53 + plugin, 54 + schema, 55 + }), 56 + ) 46 57 : undefined; 47 58 48 59 const statement = $.const(symbol)
+1
packages/shared/src/index.ts
··· 108 108 export { definePluginConfig, mappers } from './plugins/shared/utils/config'; 109 109 export type { PluginInstanceTypes } from './plugins/shared/utils/instance'; 110 110 export { PluginInstance } from './plugins/shared/utils/instance'; 111 + export { buildSymbolIn } from './plugins/symbol'; 111 112 export type { 112 113 AnyPluginName, 113 114 DefinePlugin,
+19 -2
packages/shared/src/parser/hooks.ts
··· 1 - import type { Node, Symbol, SymbolIn } from '@hey-api/codegen-core'; 1 + import type { Node, Symbol, SymbolIn, SymbolMeta } from '@hey-api/codegen-core'; 2 2 3 3 import type { SchemaProcessorContext } from '../ir/schema-processor'; 4 - import type { IROperationObject } from '../ir/types'; 4 + import type { IROperationObject, IRSchemaObject } from '../ir/types'; 5 5 import type { PluginInstance } from '../plugins/shared/utils/instance'; 6 6 7 7 export type Hooks = { ··· 210 210 * @returns The file path to output the symbol to, or undefined to fallback to default behavior. 211 211 */ 212 212 getFilePath?: (symbol: Symbol) => string | undefined; 213 + /** 214 + * Optional output strategy to override default plugin behavior. 215 + * 216 + * Use this to customize symbol names. 217 + * 218 + * @returns The name to register the symbol with, or undefined to fallback to default behavior. 219 + */ 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; 213 230 }; 214 231 };
+1 -1
packages/shared/src/plugins/shared/utils/instance.ts
··· 353 353 } 354 354 355 355 /** 356 - * @deprecated use `plugin.symbol()` instead 356 + * Alias for `symbol()` method with single argument. 357 357 */ 358 358 registerSymbol(symbol: SymbolIn): Symbol<ResolvedNode> { 359 359 return this.symbol(symbol.name, symbol) as Symbol<ResolvedNode>;
+23
packages/shared/src/plugins/symbol.ts
··· 1 + import type { SymbolIn, SymbolMeta } from '@hey-api/codegen-core'; 2 + 3 + import type { IROperationObject, IRSchemaObject } from '../ir/types'; 4 + import type { PluginInstance } from './shared/utils/instance'; 5 + 6 + /** 7 + * Helper function to build the input for symbol registration, applying naming hooks if provided. 8 + */ 9 + export function buildSymbolIn(ctx: { 10 + meta: SymbolMeta; 11 + name: string; 12 + operation?: IROperationObject; 13 + plugin: { 14 + config: Pick<PluginInstance['config'], '~hooks'>; 15 + }; 16 + schema?: IRSchemaObject; 17 + }): SymbolIn { 18 + const getName = ctx.plugin.config['~hooks']?.symbols?.getName ?? (() => {}); 19 + return { 20 + meta: ctx.meta, 21 + name: getName(ctx) ?? ctx.name, 22 + }; 23 + }