fork of hey-api/openapi-ts because I need some additional things

Merge pull request #2881 from hey-api/refactor/selector-fastify

refactor: remove selectors from fastify plugin

authored by

Lubos and committed by
GitHub
56f3a250 c2d16f21

+53 -67
+8 -2
packages/openapi-ts/src/index.ts
··· 48 48 /** 49 49 * Tags associated with this symbol. 50 50 */ 51 - tags?: Set<string>; 52 - tool?: 'arktype' | 'typescript' | 'valibot' | 'zod' | (string & {}); 51 + tags?: ReadonlyArray<string>; 52 + tool?: 53 + | 'arktype' 54 + | 'fastify' 55 + | 'typescript' 56 + | 'valibot' 57 + | 'zod' 58 + | (string & {}); 53 59 variant?: 'container' | (string & {}); 54 60 } 55 61 }
-22
packages/openapi-ts/src/plugins/arktype/api.ts
··· 1 - import type { Selector } from '@hey-api/codegen-core'; 2 1 import type ts from 'typescript'; 3 - 4 - import type { Plugin } from '~/plugins'; 5 2 6 3 import type { ValidatorArgs } from './shared/types'; 7 4 import { createRequestValidatorV2, createResponseValidatorV2 } from './v2/api'; 8 5 9 - type SelectorType = 'data' | 'ref' | 'responses' | 'webhook-request'; 10 - 11 6 export type IApi = { 12 7 createRequestValidator: (args: ValidatorArgs) => ts.ArrowFunction | undefined; 13 8 createResponseValidator: ( 14 9 args: ValidatorArgs, 15 10 ) => ts.ArrowFunction | undefined; 16 - /** 17 - * @param type Selector type. 18 - * @param value Depends on `type`: 19 - * - `data`: `operation.id` string 20 - * - `ref`: `$ref` JSON pointer 21 - * - `responses`: `operation.id` string 22 - * - `webhook-request`: `operation.id` string 23 - * @returns Selector array 24 - * @deprecated 25 - */ 26 - selector: (type: SelectorType, value?: string) => Selector; 27 11 }; 28 12 29 13 export class Api implements IApi { 30 - constructor(public meta: Plugin.Name<'arktype'>) {} 31 - 32 14 createRequestValidator(args: ValidatorArgs): ts.ArrowFunction | undefined { 33 15 return createRequestValidatorV2(args); 34 16 } 35 17 36 18 createResponseValidator(args: ValidatorArgs): ts.ArrowFunction | undefined { 37 19 return createResponseValidatorV2(args); 38 - } 39 - 40 - selector(...args: ReadonlyArray<string | undefined>): Selector { 41 - return [this.meta.name, ...(args as Selector)]; 42 20 } 43 21 }
+1 -3
packages/openapi-ts/src/plugins/arktype/config.ts
··· 5 5 import type { ArktypePlugin } from './types'; 6 6 7 7 export const defaultConfig: ArktypePlugin['Config'] = { 8 - api: new Api({ 9 - name: 'arktype', 10 - }), 8 + api: new Api(), 11 9 config: { 12 10 case: 'PascalCase', 13 11 comments: true,
+14 -4
packages/openapi-ts/src/plugins/arktype/v2/api.ts
··· 9 9 operation, 10 10 plugin, 11 11 }: ValidatorArgs): ts.ArrowFunction | undefined => { 12 - const symbol = plugin.getSymbol(plugin.api.selector('data', operation.id)); 12 + const symbol = plugin.getSymbol({ 13 + category: 'schema', 14 + resource: 'operation', 15 + resourceId: operation.id, 16 + role: 'data', 17 + tool: 'arktype', 18 + }); 13 19 if (!symbol) return; 14 20 15 21 // const out = User({ ··· 56 62 operation, 57 63 plugin, 58 64 }: ValidatorArgs): ts.ArrowFunction | undefined => { 59 - const symbol = plugin.getSymbol( 60 - plugin.api.selector('responses', operation.id), 61 - ); 65 + const symbol = plugin.getSymbol({ 66 + category: 'schema', 67 + resource: 'operation', 68 + resourceId: operation.id, 69 + role: 'responses', 70 + tool: 'arktype', 71 + }); 62 72 if (!symbol) return; 63 73 64 74 const dataParameterName = 'data';
+15 -4
packages/openapi-ts/src/plugins/arktype/v2/plugin.ts
··· 1 + import type { SymbolMeta } from '@hey-api/codegen-core'; 2 + 1 3 import { deduplicateSchema } from '~/ir/schema'; 2 4 import type { IR } from '~/ir/types'; 3 5 import { buildName } from '~/openApi/shared/utils/name'; ··· 33 35 // }); 34 36 35 37 if (schema.$ref) { 36 - const selector = plugin.api.selector('ref', schema.$ref); 37 - const refSymbol = plugin.referenceSymbol(selector); 38 - if (plugin.isSymbolRegistered(selector)) { 38 + const query: SymbolMeta = { 39 + category: 'schema', 40 + resource: 'definition', 41 + resourceId: schema.$ref, 42 + tool: 'arktype', 43 + }; 44 + const refSymbol = plugin.referenceSymbol(query); 45 + if (plugin.isSymbolRegistered(query)) { 39 46 const ref = tsc.identifier({ text: refSymbol.placeholder }); 40 47 ast.expression = ref; 41 48 } else { ··· 251 258 const symbol = plugin.registerSymbol({ 252 259 exported: true, 253 260 meta: { 261 + category: 'schema', 254 262 path: state.path.value, 263 + resource: 'definition', 264 + resourceId: $ref, 265 + tags: state.tags?.value, 266 + tool: 'arktype', 255 267 }, 256 268 name: buildName({ 257 269 config: plugin.config.definitions, 258 270 name: baseName, 259 271 }), 260 - selector: plugin.api.selector('ref', $ref), 261 272 }); 262 273 const typeInferSymbol = plugin.config.definitions.types.infer.enabled 263 274 ? plugin.registerSymbol({
+2 -23
packages/openapi-ts/src/plugins/fastify/api.ts
··· 1 - import type { Selector } from '@hey-api/codegen-core'; 1 + export type IApi = any; 2 2 3 - import type { Plugin } from '~/plugins'; 4 - 5 - type SelectorType = 'RouteHandler'; 6 - 7 - export type IApi = { 8 - /** 9 - * @param type Selector type. 10 - * @param value Depends on `type`: 11 - * - `RouteHandler`: never 12 - * @returns Selector array 13 - * @deprecated 14 - */ 15 - selector: (type: SelectorType, value?: string) => Selector; 16 - }; 17 - 18 - export class Api implements IApi { 19 - constructor(public meta: Plugin.Name<'fastify'>) {} 20 - 21 - selector(...args: ReadonlyArray<string | undefined>): Selector { 22 - return [this.meta.name, ...(args as Selector)]; 23 - } 24 - } 3 + export class Api implements IApi {}
+1 -3
packages/openapi-ts/src/plugins/fastify/config.ts
··· 5 5 import type { FastifyPlugin } from './types'; 6 6 7 7 export const defaultConfig: FastifyPlugin['Config'] = { 8 - api: new Api({ 9 - name: 'fastify', 10 - }), 8 + api: new Api(), 11 9 config: { 12 10 exportFromIndex: false, 13 11 },
+10 -4
packages/openapi-ts/src/plugins/fastify/plugin.ts
··· 143 143 return; 144 144 } 145 145 146 - const symbolRouteHandler = plugin.referenceSymbol( 147 - plugin.api.selector('RouteHandler'), 148 - ); 146 + const symbolRouteHandler = plugin.referenceSymbol({ 147 + category: 'type', 148 + resource: 'route-handler', 149 + tool: 'fastify', 150 + }); 149 151 const routeHandler: Property = { 150 152 name: operation.id, 151 153 type: tsc.typeReferenceNode({ ··· 165 167 plugin.registerSymbol({ 166 168 external: 'fastify', 167 169 kind: 'type', 170 + meta: { 171 + category: 'type', 172 + resource: 'route-handler', 173 + tool: 'fastify', 174 + }, 168 175 name: 'RouteHandler', 169 - selector: plugin.api.selector('RouteHandler'), 170 176 }); 171 177 172 178 const symbolRouteHandlers = plugin.registerSymbol({
+1 -1
packages/openapi-ts/src/plugins/shared/types/instance.d.ts
··· 13 13 /** 14 14 * Tags associated with the node. 15 15 */ 16 - tags?: Set<string>; 16 + tags?: ReadonlyArray<string>; 17 17 }; 18 18 19 19 type WalkEvents = BaseEvent &
+1 -1
packages/openapi-ts/src/plugins/shared/utils/instance.ts
··· 186 186 const baseEvent: BaseEvent = { 187 187 _path: jsonPointerToPath(pointer), 188 188 pointer, 189 - tags: nodeInfo.tags, 189 + tags: nodeInfo.tags ? Array.from(nodeInfo.tags) : undefined, 190 190 }; 191 191 switch (result.kind) { 192 192 case 'operation':