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

refactor: clean up valibot resolver interface

Lubos edf99ed8 497f219e

Changed files
+97 -82
dev
packages
openapi-ts
src
plugins
+15 -13
dev/openapi-ts.config.ts
··· 431 431 }, 432 432 }, 433 433 '~resolvers': { 434 - number(ctx) { 435 - const { $, plugin, symbols } = ctx; 436 - const { v } = symbols; 437 - // ctx.nodes.base = () => { 438 - // // implement custom base number resolver 439 - // } 440 - const big = plugin.symbolOnce('Big', { 441 - external: 'big.js', 442 - importKind: 'default', 443 - }); 444 - return $(v).attr('instance').call(big); 445 - }, 434 + // number(ctx) { 435 + // const { $, plugin, symbols } = ctx; 436 + // const { v } = symbols; 437 + // // ctx.nodes.base = () => { 438 + // // // implement custom base number resolver 439 + // // } 440 + // const big = plugin.symbolOnce('Big', { 441 + // external: 'big.js', 442 + // importKind: 'default', 443 + // }); 444 + // return $(v).attr('instance').call(big); 445 + // }, 446 446 // object(ctx) { 447 447 // const { $ } = ctx; 448 448 // const additional = ctx.nodes.additionalProperties(ctx); ··· 458 458 // ctx.nodes.format = () => $('v').attr('isoDateTime').call(); 459 459 // } 460 460 // }, 461 - // validator({ $, plugin, schema, v }) { 461 + // validator(ctx) { 462 + // const { $, plugin, symbols } = ctx; 463 + // const { schema, v } = symbols; 462 464 // const vShadow = plugin.symbol('v'); 463 465 // const test = plugin.symbol('test'); 464 466 // const e = plugin.symbol('err');
+6 -6
packages/openapi-ts/src/plugins/valibot/shared/pipes.ts
··· 33 33 }; 34 34 35 35 export interface PipesUtils { 36 + /** 37 + * Push pipes into target array. 38 + */ 36 39 push: PushPipes; 40 + /** 41 + * Convert pipes to a single node. 42 + */ 37 43 toNode: PipesToNode; 38 44 } 39 45 ··· 41 47 * Functions for working with pipes. 42 48 */ 43 49 export const pipes: PipesUtils = { 44 - /** 45 - * Push pipes into target array. 46 - */ 47 50 push: pushPipes, 48 - /** 49 - * Convert pipes to a single node. 50 - */ 51 51 toNode: pipesToNode, 52 52 };
+34 -30
packages/openapi-ts/src/plugins/valibot/types.d.ts
··· 1 1 import type { Refs, Symbol } from '@hey-api/codegen-core'; 2 - import type ts from 'typescript'; 3 2 4 3 import type { IR } from '~/ir/types'; 5 4 import type { DefinePlugin, Plugin, SchemaWithType } from '~/plugins'; ··· 8 7 ShouldCoerceToBigInt, 9 8 } from '~/plugins/shared/utils/coerce'; 10 9 import type { GetIntegerLimit } from '~/plugins/shared/utils/formats'; 11 - import type { $, DollarTsDsl, TsDsl } from '~/ts-dsl'; 10 + import type { $, DollarTsDsl } from '~/ts-dsl'; 12 11 import type { StringCase, StringName } from '~/types/case'; 13 - import type { MaybeArray } from '~/types/utils'; 14 12 15 13 import type { IApi } from './api'; 16 14 import type { Pipe, PipeResult, PipesUtils } from './shared/pipes'; ··· 328 326 }; 329 327 }; 330 328 331 - type SharedResolverArgs = DollarTsDsl & { 329 + interface BaseResolverContext extends DollarTsDsl { 332 330 /** 333 331 * Functions for working with pipes. 334 332 */ 335 - pipes: PipesUtils; 333 + pipes: PipesUtils & { 334 + /** 335 + * The current builder state being processed by this resolver. 336 + * 337 + * In Valibot, this represents the current list of call expressions ("pipes") 338 + * being assembled to form a schema definition. 339 + * 340 + * Each pipe can be extended, modified, or replaced to customize how the 341 + * resulting schema is constructed. 342 + */ 343 + current: Pipes; 344 + }; 336 345 plugin: ValibotPlugin['Instance']; 337 346 /** 338 - * The current builder state being processed by this resolver. 339 - * 340 - * In Valibot, this represents the current list of call expressions ("pipes") 341 - * being assembled to form a schema definition. 342 - * 343 - * Each pipe can be extended, modified, or replaced to customize how the 344 - * resulting schema is constructed. 345 - */ 346 - result: Pipes; 347 - /** 348 347 * Provides access to commonly used symbols within the Valibot plugin. 349 348 */ 350 349 symbols: { 351 350 v: Symbol; 352 351 }; 353 - }; 352 + } 354 353 355 - export type NumberResolverContext = SharedResolverArgs & { 354 + export interface NumberResolverContext extends BaseResolverContext { 356 355 /** 357 356 * Nodes used to build different parts of the number schema. 358 357 */ ··· 371 370 maybeBigInt: MaybeBigInt; 372 371 shouldCoerceToBigInt: ShouldCoerceToBigInt; 373 372 }; 374 - }; 373 + } 375 374 376 - export type ObjectResolverContext = SharedResolverArgs & { 375 + export interface ObjectResolverContext extends BaseResolverContext { 377 376 /** 378 377 * Nodes used to build different parts of the object schema. 379 378 */ ··· 396 395 ast: Partial<Omit<Ast, 'typeName'>>; 397 396 state: Refs<PluginState>; 398 397 }; 399 - }; 398 + } 400 399 401 - export type StringResolverContext = SharedResolverArgs & { 400 + export interface StringResolverContext extends BaseResolverContext { 402 401 /** 403 402 * Nodes used to build different parts of the string schema. 404 403 */ ··· 412 411 pattern: (ctx: StringResolverContext) => PipeResult | undefined; 413 412 }; 414 413 schema: SchemaWithType<'string'>; 415 - }; 414 + } 416 415 417 - export type ValidatorResolverArgs = SharedResolverArgs & { 416 + export interface ValidatorResolverContext extends BaseResolverContext { 418 417 operation: IR.Operation; 419 - schema: Symbol; 420 - }; 418 + /** 419 + * Provides access to commonly used symbols within the Valibot plugin. 420 + */ 421 + symbols: BaseResolverContext['symbols'] & { 422 + schema: Symbol; 423 + }; 424 + } 421 425 422 426 type ValidatorResolver = ( 423 - args: ValidatorResolverArgs, 424 - ) => MaybeArray<TsDsl<ts.Statement>> | null | undefined; 427 + args: ValidatorResolverContext, 428 + ) => PipeResult | null | undefined; 425 429 426 430 type Resolvers = Plugin.Resolvers<{ 427 431 /** ··· 455 459 * 456 460 * Example path: `~resolvers.validator.request` or `~resolvers.validator.response` 457 461 * 458 - * Returning `undefined` from a resolver will apply the default generation logic. 462 + * Returning `undefined` will execute the default resolver logic. 459 463 */ 460 464 validator?: 461 465 | ValidatorResolver ··· 463 467 /** 464 468 * Controls how the request validator function body is generated. 465 469 * 466 - * Returning `undefined` will fall back to the default `.await().return()` logic. 470 + * Returning `undefined` will execute the default resolver logic. 467 471 */ 468 472 request?: ValidatorResolver; 469 473 /** 470 474 * Controls how the response validator function body is generated. 471 475 * 472 - * Returning `undefined` will fall back to the default `.await().return()` logic. 476 + * Returning `undefined` will execute the default resolver logic. 473 477 */ 474 478 response?: ValidatorResolver; 475 479 };
+17 -14
packages/openapi-ts/src/plugins/valibot/v1/api.ts
··· 2 2 3 3 import { pipes } from '../shared/pipes'; 4 4 import type { ValidatorArgs } from '../shared/types'; 5 - import type { ValidatorResolverArgs } from '../types'; 5 + import type { ValidatorResolverContext } from '../types'; 6 6 import { identifiers } from './constants'; 7 7 8 8 const validatorResolver = ( 9 - ctx: ValidatorResolverArgs, 9 + ctx: ValidatorResolverContext, 10 10 ): ReturnType<typeof $.return> => { 11 - const { schema, symbols } = ctx; 12 - const { v } = symbols; 11 + const { schema, v } = ctx.symbols; 13 12 return $(v) 14 13 .attr(identifiers.async.parseAsync) 15 14 .call(schema, 'data') ··· 30 29 }); 31 30 if (!symbol) return; 32 31 33 - const args: ValidatorResolverArgs = { 32 + const ctx: ValidatorResolverContext = { 34 33 $, 35 34 operation, 36 - pipes, 35 + pipes: { 36 + ...pipes, 37 + current: [], 38 + }, 37 39 plugin, 38 - result: [], 39 - schema: symbol, 40 40 symbols: { 41 + schema: symbol, 41 42 v: plugin.external('valibot.v'), 42 43 }, 43 44 }; ··· 46 47 typeof validator === 'function' ? validator : validator?.request; 47 48 const candidates = [resolver, validatorResolver]; 48 49 for (const candidate of candidates) { 49 - const statements = candidate?.(args); 50 + const statements = candidate?.(ctx); 50 51 if (statements === null) return; 51 52 if (statements !== undefined) { 52 53 return $.func() ··· 71 72 }); 72 73 if (!symbol) return; 73 74 74 - const args: ValidatorResolverArgs = { 75 + const ctx: ValidatorResolverContext = { 75 76 $, 76 77 operation, 77 - pipes, 78 + pipes: { 79 + ...pipes, 80 + current: [], 81 + }, 78 82 plugin, 79 - result: [], 80 - schema: symbol, 81 83 symbols: { 84 + schema: symbol, 82 85 v: plugin.external('valibot.v'), 83 86 }, 84 87 }; ··· 87 90 typeof validator === 'function' ? validator : validator?.response; 88 91 const candidates = [resolver, validatorResolver]; 89 92 for (const candidate of candidates) { 90 - const statements = candidate?.(args); 93 + const statements = candidate?.(ctx); 91 94 if (statements === null) return; 92 95 if (statements !== undefined) { 93 96 return $.func()
+9 -7
packages/openapi-ts/src/plugins/valibot/v1/toAst/number.ts
··· 100 100 101 101 function numberResolver(ctx: NumberResolverContext): Pipes { 102 102 const constNode = ctx.nodes.const(ctx); 103 - if (constNode) return ctx.pipes.push(ctx.result, constNode); 103 + if (constNode) return ctx.pipes.push(ctx.pipes.current, constNode); 104 104 105 105 const baseNode = ctx.nodes.base(ctx); 106 - if (baseNode) ctx.pipes.push(ctx.result, baseNode); 106 + if (baseNode) ctx.pipes.push(ctx.pipes.current, baseNode); 107 107 108 108 const minNode = ctx.nodes.min(ctx); 109 - if (minNode) ctx.pipes.push(ctx.result, minNode); 109 + if (minNode) ctx.pipes.push(ctx.pipes.current, minNode); 110 110 111 111 const maxNode = ctx.nodes.max(ctx); 112 - if (maxNode) ctx.pipes.push(ctx.result, maxNode); 112 + if (maxNode) ctx.pipes.push(ctx.pipes.current, maxNode); 113 113 114 - return ctx.result; 114 + return ctx.pipes.current; 115 115 } 116 116 117 117 export const numberToNode = ({ ··· 128 128 max: maxNode, 129 129 min: minNode, 130 130 }, 131 - pipes, 131 + pipes: { 132 + ...pipes, 133 + current: [], 134 + }, 132 135 plugin, 133 - result: [], 134 136 schema, 135 137 symbols: { 136 138 v: plugin.external('valibot.v'),
+4 -2
packages/openapi-ts/src/plugins/valibot/v1/toAst/object.ts
··· 98 98 base: baseNode, 99 99 shape: shapeNode, 100 100 }, 101 - pipes, 101 + pipes: { 102 + ...pipes, 103 + current: [], 104 + }, 102 105 plugin, 103 - result: [], 104 106 schema, 105 107 symbols: { 106 108 v: plugin.external('valibot.v'),
+12 -10
packages/openapi-ts/src/plugins/valibot/v1/toAst/string.ts
··· 80 80 81 81 function stringResolver(ctx: StringResolverContext): Pipes { 82 82 const constNode = ctx.nodes.const(ctx); 83 - if (constNode) return ctx.pipes.push(ctx.result, constNode); 83 + if (constNode) return ctx.pipes.push(ctx.pipes.current, constNode); 84 84 85 85 const baseNode = ctx.nodes.base(ctx); 86 - if (baseNode) ctx.pipes.push(ctx.result, baseNode); 86 + if (baseNode) ctx.pipes.push(ctx.pipes.current, baseNode); 87 87 88 88 const formatNode = ctx.nodes.format(ctx); 89 - if (formatNode) ctx.pipes.push(ctx.result, formatNode); 89 + if (formatNode) ctx.pipes.push(ctx.pipes.current, formatNode); 90 90 91 91 const lengthNode = ctx.nodes.length(ctx); 92 92 if (lengthNode) { 93 - ctx.pipes.push(ctx.result, lengthNode); 93 + ctx.pipes.push(ctx.pipes.current, lengthNode); 94 94 } else { 95 95 const minLengthNode = ctx.nodes.minLength(ctx); 96 - if (minLengthNode) ctx.pipes.push(ctx.result, minLengthNode); 96 + if (minLengthNode) ctx.pipes.push(ctx.pipes.current, minLengthNode); 97 97 98 98 const maxLengthNode = ctx.nodes.maxLength(ctx); 99 - if (maxLengthNode) ctx.pipes.push(ctx.result, maxLengthNode); 99 + if (maxLengthNode) ctx.pipes.push(ctx.pipes.current, maxLengthNode); 100 100 } 101 101 102 102 const patternNode = ctx.nodes.pattern(ctx); 103 - if (patternNode) ctx.pipes.push(ctx.result, patternNode); 103 + if (patternNode) ctx.pipes.push(ctx.pipes.current, patternNode); 104 104 105 - return ctx.result; 105 + return ctx.pipes.current; 106 106 } 107 107 108 108 export const stringToNode = ({ ··· 122 122 minLength: minLengthNode, 123 123 pattern: patternNode, 124 124 }, 125 - pipes, 125 + pipes: { 126 + ...pipes, 127 + current: [], 128 + }, 126 129 plugin, 127 - result: [], 128 130 schema, 129 131 symbols: { 130 132 v: plugin.external('valibot.v'),