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 #2877 from hey-api/refactor/selector-valibot

refactor: remove selectors from valibot plugin

authored by

Lubos and committed by
GitHub
830a26a3 9cc82cd2

+124 -98
+1 -1
dev/openapi-ts.config.ts
··· 346 346 // definitions: 'z{{name}}Definition', 347 347 exportFromIndex: true, 348 348 // metadata: true, 349 - // name: 'valibot', 349 + name: 'valibot', 350 350 // requests: { 351 351 // case: 'PascalCase', 352 352 // name: '{{name}}Data',
+8 -3
packages/openapi-ts/src/index.ts
··· 15 15 } 16 16 17 17 interface SymbolMeta { 18 - category?: 'sdk' | 'type' | (string & {}); 18 + category?: 'external' | 'schema' | 'sdk' | 'type' | (string & {}); 19 19 /** 20 20 * Path to the resource this symbol represents. 21 21 */ ··· 24 24 * Name of the plugin that registered this symbol. 25 25 */ 26 26 pluginName?: string; 27 - resource?: 'client' | 'definition' | 'operation' | (string & {}); 27 + resource?: 28 + | 'client' 29 + | 'definition' 30 + | 'operation' 31 + | 'webhook' 32 + | (string & {}); 28 33 resourceId?: string; 29 34 role?: 30 35 | 'data' ··· 38 43 * Tags associated with this symbol. 39 44 */ 40 45 tags?: Set<string>; 41 - tool?: string; 46 + tool?: 'valibot' | (string & {}); 42 47 variant?: 'container' | (string & {}); 43 48 } 44 49 }
-28
packages/openapi-ts/src/plugins/valibot/api.ts
··· 1 - import type { Selector } from '@hey-api/codegen-core'; 2 1 import type ts from 'typescript'; 3 2 4 - import type { Plugin } from '~/plugins'; 5 - 6 3 import type { ValidatorArgs } from './shared/types'; 7 4 import { createRequestValidatorV1, createResponseValidatorV1 } from './v1/api'; 8 5 9 - type SelectorType = 10 - | 'data' 11 - | 'external' 12 - | 'ref' 13 - | 'responses' 14 - | 'webhook-request'; 15 - 16 6 export type IApi = { 17 7 createRequestValidator: (args: ValidatorArgs) => ts.ArrowFunction | undefined; 18 8 createResponseValidator: ( 19 9 args: ValidatorArgs, 20 10 ) => ts.ArrowFunction | undefined; 21 - /** 22 - * @param type Selector type. 23 - * @param value Depends on `type`: 24 - * - `data`: `operation.id` string 25 - * - `external`: external modules 26 - * - `ref`: `$ref` JSON pointer 27 - * - `responses`: `operation.id` string 28 - * - `webhook-request`: `operation.id` string 29 - * @returns Selector array 30 - * @deprecated 31 - */ 32 - selector: (type: SelectorType, value?: string) => Selector; 33 11 }; 34 12 35 13 export class Api implements IApi { 36 - constructor(public meta: Plugin.Name<'valibot'>) {} 37 - 38 14 createRequestValidator(args: ValidatorArgs): ts.ArrowFunction | undefined { 39 15 return createRequestValidatorV1(args); 40 16 } 41 17 42 18 createResponseValidator(args: ValidatorArgs): ts.ArrowFunction | undefined { 43 19 return createResponseValidatorV1(args); 44 - } 45 - 46 - selector(...args: ReadonlyArray<string | undefined>): Selector { 47 - return [this.meta.name, ...(args as Selector)]; 48 20 } 49 21 }
+1 -3
packages/openapi-ts/src/plugins/valibot/config.ts
··· 5 5 import type { ValibotPlugin } from './types'; 6 6 7 7 export const defaultConfig: ValibotPlugin['Config'] = { 8 - api: new Api({ 9 - name: 'valibot', 10 - }), 8 + api: new Api(), 11 9 config: { 12 10 case: 'camelCase', 13 11 comments: true,
+4 -3
packages/openapi-ts/src/plugins/valibot/shared/export.ts
··· 20 20 schema: IR.SchemaObject; 21 21 symbol: Symbol; 22 22 }): void => { 23 - const v = plugin.referenceSymbol( 24 - plugin.api.selector('external', 'valibot.v'), 25 - ); 23 + const v = plugin.referenceSymbol({ 24 + category: 'external', 25 + resource: 'valibot.v', 26 + }); 26 27 27 28 const statement = tsc.constVariable({ 28 29 comment: plugin.config.comments
+10 -2
packages/openapi-ts/src/plugins/valibot/shared/operation.ts
··· 119 119 const symbol = plugin.registerSymbol({ 120 120 exported: true, 121 121 meta: { 122 + category: 'schema', 122 123 path: state.path.value, 124 + resource: 'operation', 125 + resourceId: operation.id, 126 + role: 'data', 123 127 tags: state.tags?.value, 128 + tool: 'valibot', 124 129 }, 125 130 name: buildName({ 126 131 config: plugin.config.requests, 127 132 name: operation.id, 128 133 }), 129 - selector: plugin.api.selector('data', operation.id), 130 134 }); 131 135 exportAst({ 132 136 ast, ··· 147 151 const symbol = plugin.registerSymbol({ 148 152 exported: true, 149 153 meta: { 154 + category: 'schema', 150 155 path, 156 + resource: 'operation', 157 + resourceId: operation.id, 158 + role: 'responses', 151 159 tags: state.tags?.value, 160 + tool: 'valibot', 152 161 }, 153 162 name: buildName({ 154 163 config: plugin.config.responses, 155 164 name: operation.id, 156 165 }), 157 - selector: plugin.api.selector('responses', operation.id), 158 166 }); 159 167 exportAst({ 160 168 ast,
+4 -3
packages/openapi-ts/src/plugins/valibot/shared/pipesToAst.ts
··· 16 16 return pipes[0]!; 17 17 } 18 18 19 - const v = plugin.referenceSymbol( 20 - plugin.api.selector('external', 'valibot.v'), 21 - ); 19 + const v = plugin.referenceSymbol({ 20 + category: 'external', 21 + resource: 'valibot.v', 22 + }); 22 23 const expression = tsc.callExpression({ 23 24 functionName: tsc.propertyAccessExpression({ 24 25 expression: v.placeholder,
+5 -1
packages/openapi-ts/src/plugins/valibot/shared/webhook.ts
··· 118 118 const symbol = plugin.registerSymbol({ 119 119 exported: true, 120 120 meta: { 121 + category: 'schema', 121 122 path: state.path.value, 123 + resource: 'webhook', 124 + resourceId: operation.id, 125 + role: 'data', 122 126 tags: state.tags?.value, 127 + tool: 'valibot', 123 128 }, 124 129 name: buildName({ 125 130 config: plugin.config.webhooks, 126 131 name: operation.id, 127 132 }), 128 - selector: plugin.api.selector('webhook-request', operation.id), 129 133 }); 130 134 exportAst({ 131 135 ast,
+22 -10
packages/openapi-ts/src/plugins/valibot/v1/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: 'valibot', 18 + }); 13 19 if (!symbol) return; 14 20 15 - const v = plugin.referenceSymbol( 16 - plugin.api.selector('external', 'valibot.v'), 17 - ); 21 + const v = plugin.referenceSymbol({ 22 + category: 'external', 23 + resource: 'valibot.v', 24 + }); 18 25 19 26 const dataParameterName = 'data'; 20 27 ··· 48 55 operation, 49 56 plugin, 50 57 }: ValidatorArgs): ts.ArrowFunction | undefined => { 51 - const symbol = plugin.getSymbol( 52 - plugin.api.selector('responses', operation.id), 53 - ); 58 + const symbol = plugin.getSymbol({ 59 + category: 'schema', 60 + resource: 'operation', 61 + resourceId: operation.id, 62 + role: 'responses', 63 + tool: 'valibot', 64 + }); 54 65 if (!symbol) return; 55 66 56 - const v = plugin.referenceSymbol( 57 - plugin.api.selector('external', 'valibot.v'), 58 - ); 67 + const v = plugin.referenceSymbol({ 68 + category: 'external', 69 + resource: 'valibot.v', 70 + }); 59 71 60 72 const dataParameterName = 'data'; 61 73
+21 -8
packages/openapi-ts/src/plugins/valibot/v1/plugin.ts
··· 1 + import type { SymbolMeta } from '@hey-api/codegen-core'; 1 2 import type ts from 'typescript'; 2 3 3 4 import { deduplicateSchema } from '~/ir/schema'; ··· 36 37 pipes: [], 37 38 }; 38 39 39 - const v = plugin.referenceSymbol( 40 - plugin.api.selector('external', 'valibot.v'), 41 - ); 40 + const v = plugin.referenceSymbol({ 41 + category: 'external', 42 + resource: 'valibot.v', 43 + }); 42 44 43 45 if (schema.$ref) { 44 - const selector = plugin.api.selector('ref', schema.$ref); 45 - const refSymbol = plugin.referenceSymbol(selector); 46 - if (plugin.isSymbolRegistered(selector)) { 46 + const query: SymbolMeta = { 47 + category: 'schema', 48 + resource: 'definition', 49 + resourceId: schema.$ref, 50 + tool: 'valibot', 51 + }; 52 + const refSymbol = plugin.referenceSymbol(query); 53 + if (plugin.isSymbolRegistered(query)) { 47 54 const ref = tsc.identifier({ text: refSymbol.placeholder }); 48 55 ast.pipes.push(ref); 49 56 } else { ··· 214 221 const symbol = plugin.registerSymbol({ 215 222 exported: true, 216 223 meta: { 224 + category: 'schema', 217 225 path: state.path.value, 226 + resource: 'definition', 227 + resourceId: $ref, 218 228 tags: state.tags?.value, 229 + tool: 'valibot', 219 230 }, 220 231 name: buildName({ 221 232 config: plugin.config.definitions, 222 233 name: baseName, 223 234 }), 224 - selector: plugin.api.selector('ref', $ref), 225 235 }); 226 236 exportAst({ 227 237 ast, ··· 236 246 plugin.registerSymbol({ 237 247 external: 'valibot', 238 248 importKind: 'namespace', 249 + meta: { 250 + category: 'external', 251 + resource: 'valibot.v', 252 + }, 239 253 name: 'v', 240 - selector: plugin.api.selector('external', 'valibot.v'), 241 254 }); 242 255 243 256 plugin.forEach(
+4 -3
packages/openapi-ts/src/plugins/valibot/v1/toAst/array.ts
··· 20 20 pipes: [], 21 21 }; 22 22 23 - const v = plugin.referenceSymbol( 24 - plugin.api.selector('external', 'valibot.v'), 25 - ); 23 + const v = plugin.referenceSymbol({ 24 + category: 'external', 25 + resource: 'valibot.v', 26 + }); 26 27 const functionName = tsc.propertyAccessExpression({ 27 28 expression: v.placeholder, 28 29 name: identifiers.schemas.array,
+4 -3
packages/openapi-ts/src/plugins/valibot/v1/toAst/boolean.ts
··· 10 10 }: IrSchemaToAstOptions & { 11 11 schema: SchemaWithType<'boolean'>; 12 12 }) => { 13 - const v = plugin.referenceSymbol( 14 - plugin.api.selector('external', 'valibot.v'), 15 - ); 13 + const v = plugin.referenceSymbol({ 14 + category: 'external', 15 + resource: 'valibot.v', 16 + }); 16 17 17 18 if (typeof schema.const === 'boolean') { 18 19 const expression = tsc.callExpression({
+4 -3
packages/openapi-ts/src/plugins/valibot/v1/toAst/enum.ts
··· 41 41 }); 42 42 } 43 43 44 - const v = plugin.referenceSymbol( 45 - plugin.api.selector('external', 'valibot.v'), 46 - ); 44 + const v = plugin.referenceSymbol({ 45 + category: 'external', 46 + resource: 'valibot.v', 47 + }); 47 48 48 49 let resultExpression = tsc.callExpression({ 49 50 functionName: tsc.propertyAccessExpression({
+4 -3
packages/openapi-ts/src/plugins/valibot/v1/toAst/never.ts
··· 9 9 }: IrSchemaToAstOptions & { 10 10 schema: SchemaWithType<'never'>; 11 11 }) => { 12 - const v = plugin.referenceSymbol( 13 - plugin.api.selector('external', 'valibot.v'), 14 - ); 12 + const v = plugin.referenceSymbol({ 13 + category: 'external', 14 + resource: 'valibot.v', 15 + }); 15 16 const expression = tsc.callExpression({ 16 17 functionName: tsc.propertyAccessExpression({ 17 18 expression: v.placeholder,
+4 -3
packages/openapi-ts/src/plugins/valibot/v1/toAst/null.ts
··· 9 9 }: IrSchemaToAstOptions & { 10 10 schema: SchemaWithType<'null'>; 11 11 }) => { 12 - const v = plugin.referenceSymbol( 13 - plugin.api.selector('external', 'valibot.v'), 14 - ); 12 + const v = plugin.referenceSymbol({ 13 + category: 'external', 14 + resource: 'valibot.v', 15 + }); 15 16 const expression = tsc.callExpression({ 16 17 functionName: tsc.propertyAccessExpression({ 17 18 expression: v.placeholder,
+4 -3
packages/openapi-ts/src/plugins/valibot/v1/toAst/number.ts
··· 24 24 const isBigInt = needsBigIntForFormat(format); 25 25 const formatInfo = isIntegerFormat(format) ? INTEGER_FORMATS[format] : null; 26 26 27 - const v = plugin.referenceSymbol( 28 - plugin.api.selector('external', 'valibot.v'), 29 - ); 27 + const v = plugin.referenceSymbol({ 28 + category: 'external', 29 + resource: 'valibot.v', 30 + }); 30 31 31 32 // Return early if const is defined since we can create a literal type directly without additional validation 32 33 if (schema.const !== undefined && schema.const !== null) {
+4 -3
packages/openapi-ts/src/plugins/valibot/v1/toAst/object.ts
··· 68 68 ); 69 69 } 70 70 71 - const v = plugin.referenceSymbol( 72 - plugin.api.selector('external', 'valibot.v'), 73 - ); 71 + const v = plugin.referenceSymbol({ 72 + category: 'external', 73 + resource: 'valibot.v', 74 + }); 74 75 75 76 // Handle additionalProperties with a schema (not just true/false) 76 77 // This supports objects with dynamic keys (e.g., Record<string, T>)
+4 -3
packages/openapi-ts/src/plugins/valibot/v1/toAst/string.ts
··· 13 13 }: IrSchemaToAstOptions & { 14 14 schema: SchemaWithType<'string'>; 15 15 }) => { 16 - const v = plugin.referenceSymbol( 17 - plugin.api.selector('external', 'valibot.v'), 18 - ); 16 + const v = plugin.referenceSymbol({ 17 + category: 'external', 18 + resource: 'valibot.v', 19 + }); 19 20 20 21 if (typeof schema.const === 'string') { 21 22 const expression = tsc.callExpression({
+4 -3
packages/openapi-ts/src/plugins/valibot/v1/toAst/tuple.ts
··· 17 17 }): Omit<Ast, 'typeName'> => { 18 18 const result: Partial<Omit<Ast, 'typeName'>> = {}; 19 19 20 - const v = plugin.referenceSymbol( 21 - plugin.api.selector('external', 'valibot.v'), 22 - ); 20 + const v = plugin.referenceSymbol({ 21 + category: 'external', 22 + resource: 'valibot.v', 23 + }); 23 24 24 25 if (schema.const && Array.isArray(schema.const)) { 25 26 const tupleElements = schema.const.map((value) =>
+4 -3
packages/openapi-ts/src/plugins/valibot/v1/toAst/undefined.ts
··· 9 9 }: IrSchemaToAstOptions & { 10 10 schema: SchemaWithType<'undefined'>; 11 11 }) => { 12 - const v = plugin.referenceSymbol( 13 - plugin.api.selector('external', 'valibot.v'), 14 - ); 12 + const v = plugin.referenceSymbol({ 13 + category: 'external', 14 + resource: 'valibot.v', 15 + }); 15 16 16 17 const expression = tsc.callExpression({ 17 18 functionName: tsc.propertyAccessExpression({
+4 -3
packages/openapi-ts/src/plugins/valibot/v1/toAst/unknown.ts
··· 9 9 }: IrSchemaToAstOptions & { 10 10 schema: SchemaWithType<'unknown'>; 11 11 }) => { 12 - const v = plugin.referenceSymbol( 13 - plugin.api.selector('external', 'valibot.v'), 14 - ); 12 + const v = plugin.referenceSymbol({ 13 + category: 'external', 14 + resource: 'valibot.v', 15 + }); 15 16 16 17 const expression = tsc.callExpression({ 17 18 functionName: tsc.propertyAccessExpression({
+4 -3
packages/openapi-ts/src/plugins/valibot/v1/toAst/void.ts
··· 9 9 }: IrSchemaToAstOptions & { 10 10 schema: SchemaWithType<'void'>; 11 11 }) => { 12 - const v = plugin.referenceSymbol( 13 - plugin.api.selector('external', 'valibot.v'), 14 - ); 12 + const v = plugin.referenceSymbol({ 13 + category: 'external', 14 + resource: 'valibot.v', 15 + }); 15 16 16 17 const expression = tsc.callExpression({ 17 18 functionName: tsc.propertyAccessExpression({