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 #3509 from hey-api/refactor/valibot-union-intersection

refactor: extract valibot union and intersection handler

authored by

Lubos and committed by
GitHub
02e5991d e96c2e2f

+107 -29
+38
packages/openapi-ts/src/plugins/valibot/v1/toAst/intersection.ts
··· 1 + import type { IR } from '@hey-api/shared'; 2 + 3 + import { $ } from '../../../../ts-dsl'; 4 + import type { Pipes } from '../../shared/pipes'; 5 + import { pipesToNode } from '../../shared/pipes'; 6 + import type { CompositeHandlerResult, ValibotFinal, ValibotResult } from '../../shared/types'; 7 + import type { ValibotPlugin } from '../../types'; 8 + import { identifiers } from '../constants'; 9 + 10 + interface IntersectionToPipesContext { 11 + applyModifiers: (result: ValibotResult, options?: { optional?: boolean }) => ValibotFinal; 12 + childResults: Array<ValibotResult>; 13 + parentSchema: IR.SchemaObject; 14 + plugin: ValibotPlugin['Instance']; 15 + } 16 + 17 + export function intersectionToPipes(ctx: IntersectionToPipesContext): CompositeHandlerResult { 18 + const { applyModifiers, childResults, plugin } = ctx; 19 + 20 + const v = plugin.external('valibot.v'); 21 + let pipes: Pipes; 22 + 23 + if (childResults.length === 0) { 24 + pipes = [$(v).attr(identifiers.schemas.any).call()]; 25 + } else if (childResults.length === 1) { 26 + const finalResult = applyModifiers(childResults[0]!); 27 + pipes = finalResult.pipes; 28 + } else { 29 + const itemNodes = childResults.map((item) => pipesToNode(item.pipes, plugin)); 30 + pipes = [ 31 + $(v) 32 + .attr(identifiers.schemas.intersect) 33 + .call($.array(...itemNodes)), 34 + ]; 35 + } 36 + 37 + return { childResults, pipes }; 38 + }
+49
packages/openapi-ts/src/plugins/valibot/v1/toAst/union.ts
··· 1 + import type { IR } from '@hey-api/shared'; 2 + 3 + import { $ } from '../../../../ts-dsl'; 4 + import type { Pipes } from '../../shared/pipes'; 5 + import { pipesToNode } from '../../shared/pipes'; 6 + import type { CompositeHandlerResult, ValibotFinal, ValibotResult } from '../../shared/types'; 7 + import type { ValibotPlugin } from '../../types'; 8 + import { identifiers } from '../constants'; 9 + 10 + interface UnionToPipesContext { 11 + applyModifiers: (result: ValibotResult, options?: { optional?: boolean }) => ValibotFinal; 12 + childResults: Array<ValibotResult>; 13 + parentSchema: IR.SchemaObject; 14 + plugin: ValibotPlugin['Instance']; 15 + schemas: ReadonlyArray<IR.SchemaObject>; 16 + } 17 + 18 + export function unionToPipes(ctx: UnionToPipesContext): CompositeHandlerResult { 19 + const { childResults, plugin, schemas } = ctx; 20 + 21 + const nonNullItems: Array<ValibotResult> = []; 22 + childResults.forEach((item, index) => { 23 + const schema = schemas[index]!; 24 + if (schema.type !== 'null') { 25 + nonNullItems.push(item); 26 + } 27 + }); 28 + 29 + const v = plugin.external('valibot.v'); 30 + let pipes: Pipes; 31 + 32 + if (nonNullItems.length === 0) { 33 + pipes = [$(v).attr(identifiers.schemas.null).call()]; 34 + } else if (nonNullItems.length === 1) { 35 + pipes = nonNullItems[0]!.pipes; 36 + } else { 37 + const itemNodes = nonNullItems.map((i) => pipesToNode(i.pipes, plugin)); 38 + pipes = [ 39 + $(v) 40 + .attr(identifiers.schemas.union) 41 + .call($.array(...itemNodes)), 42 + ]; 43 + } 44 + 45 + return { 46 + childResults, 47 + pipes, 48 + }; 49 + }
+20 -29
packages/openapi-ts/src/plugins/valibot/v1/walker.ts
··· 15 15 import { arrayToPipes } from './toAst/array'; 16 16 import { booleanToPipes } from './toAst/boolean'; 17 17 import { enumToPipes } from './toAst/enum'; 18 + import { intersectionToPipes } from './toAst/intersection'; 18 19 import { neverToPipes } from './toAst/never'; 19 20 import { nullToPipes } from './toAst/null'; 20 21 import { numberToPipes } from './toAst/number'; ··· 22 23 import { stringToPipes } from './toAst/string'; 23 24 import { tupleToPipes } from './toAst/tuple'; 24 25 import { undefinedToPipes } from './toAst/undefined'; 26 + import { unionToPipes } from './toAst/union'; 25 27 import { unknownToPipes } from './toAst/unknown'; 26 28 import { voidToPipes } from './toAst/void'; 27 29 ··· 143 145 } 144 146 }, 145 147 intersection(items, schemas, parentSchema, ctx) { 146 - const v = ctx.plugin.external('valibot.v'); 147 - const itemNodes = items.map((item) => pipesToNode(item.pipes, ctx.plugin)); 148 + const applyModifiers = (result: ValibotResult, opts?: { optional?: boolean }) => 149 + this.applyModifiers(result, ctx, opts) as ValibotFinal; 150 + 151 + const { pipes } = intersectionToPipes({ 152 + applyModifiers, 153 + childResults: items, 154 + parentSchema, 155 + plugin: ctx.plugin, 156 + }); 148 157 149 158 return { 150 159 meta: composeMeta(items, { default: parentSchema.default }), 151 - pipes: [ 152 - $(v) 153 - .attr(identifiers.schemas.intersect) 154 - .call($.array(...itemNodes)), 155 - ], 160 + pipes, 156 161 }; 157 162 }, 158 163 never(schema, ctx) { ··· 290 295 }; 291 296 }, 292 297 union(items, schemas, parentSchema, ctx) { 293 - const v = ctx.plugin.external('valibot.v'); 298 + const applyModifiers = (result: ValibotResult, opts?: { optional?: boolean }) => 299 + this.applyModifiers(result, ctx, opts) as ValibotFinal; 294 300 295 301 const hasNull = schemas.some((s) => s.type === 'null') || items.some((i) => i.meta.nullable); 296 302 297 - const nonNullItems: Array<ValibotResult> = []; 298 - items.forEach((item, index) => { 299 - const schema = schemas[index]!; 300 - if (schema.type !== 'null') { 301 - nonNullItems.push(item); 302 - } 303 + const { pipes } = unionToPipes({ 304 + applyModifiers, 305 + childResults: items, 306 + parentSchema, 307 + plugin: ctx.plugin, 308 + schemas, 303 309 }); 304 - 305 - let pipes: Pipes; 306 - 307 - if (nonNullItems.length === 0) { 308 - pipes = [$(v).attr(identifiers.schemas.null).call()]; 309 - } else if (nonNullItems.length === 1) { 310 - pipes = nonNullItems[0]!.pipes; 311 - } else { 312 - const itemNodes = nonNullItems.map((i) => pipesToNode(i.pipes, ctx.plugin)); 313 - pipes = [ 314 - $(v) 315 - .attr(identifiers.schemas.union) 316 - .call($.array(...itemNodes)), 317 - ]; 318 - } 319 310 320 311 return { 321 312 meta: composeMeta(items, {