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 #3548 from hey-api/feat/typescript-resolvers

feat: add more resolvers to typescript

authored by

Lubos and committed by
GitHub
c34225fa 7b3771fd

+600 -50
+245 -1
packages/openapi-ts/src/plugins/@hey-api/typescript/resolvers.ts
··· 1 - import type { Plugin, SchemaVisitorContext, SchemaWithType, Walker } from '@hey-api/shared'; 1 + import type { IR, Plugin, SchemaVisitorContext, SchemaWithType, Walker } from '@hey-api/shared'; 2 2 3 3 import type { $, DollarTsDsl } from '../../../ts-dsl'; 4 4 import type { Type, TypeScriptResult } from './shared/types'; ··· 6 6 7 7 export type Resolvers = Plugin.Resolvers<{ 8 8 /** 9 + * Resolver for array schemas. 10 + * 11 + * Allows customization of how array types are rendered. 12 + * 13 + * Returning `undefined` will execute the default resolver logic. 14 + */ 15 + array?: (ctx: ArrayResolverContext) => Type | undefined; 16 + /** 17 + * Resolver for boolean schemas. 18 + * 19 + * Allows customization of how boolean types are rendered. 20 + * 21 + * Returning `undefined` will execute the default resolver logic. 22 + */ 23 + boolean?: (ctx: BooleanResolverContext) => Type | undefined; 24 + /** 9 25 * Resolver for enum schemas. 10 26 * 11 27 * Allows customization of how enum types are rendered. ··· 14 30 */ 15 31 enum?: (ctx: EnumResolverContext) => Type | undefined; 16 32 /** 33 + * Resolver for intersection schemas. 34 + * 35 + * Allows customization of how intersection types are rendered. 36 + * 37 + * Returning `undefined` will execute the default resolver logic. 38 + */ 39 + intersection?: (ctx: IntersectionResolverContext) => Type | undefined; 40 + /** 41 + * Resolver for never schemas. 42 + * 43 + * Allows customization of how never types are rendered. 44 + * 45 + * Returning `undefined` will execute the default resolver logic. 46 + */ 47 + never?: (ctx: NeverResolverContext) => Type | undefined; 48 + /** 49 + * Resolver for null schemas. 50 + * 51 + * Allows customization of how null types are rendered. 52 + * 53 + * Returning `undefined` will execute the default resolver logic. 54 + */ 55 + null?: (ctx: NullResolverContext) => Type | undefined; 56 + /** 17 57 * Resolver for number schemas. 18 58 * 19 59 * Allows customization of how number types are rendered. ··· 37 77 * Returning `undefined` will execute the default resolver logic. 38 78 */ 39 79 string?: (ctx: StringResolverContext) => Type | undefined; 80 + /** 81 + * Resolver for tuple schemas. 82 + * 83 + * Allows customization of how tuple types are rendered. 84 + * 85 + * Returning `undefined` will execute the default resolver logic. 86 + */ 87 + tuple?: (ctx: TupleResolverContext) => Type | undefined; 88 + /** 89 + * Resolver for undefined schemas. 90 + * 91 + * Allows customization of how undefined types are rendered. 92 + * 93 + * Returning `undefined` will execute the default resolver logic. 94 + */ 95 + undefined?: (ctx: UndefinedResolverContext) => Type | undefined; 96 + /** 97 + * Resolver for union schemas. 98 + * 99 + * Allows customization of how union types are rendered. 100 + * 101 + * Returning `undefined` will execute the default resolver logic. 102 + */ 103 + union?: (ctx: UnionResolverContext) => Type | undefined; 104 + /** 105 + * Resolver for unknown schemas. 106 + * 107 + * Allows customization of how unknown types are rendered. 108 + * 109 + * Returning `undefined` will execute the default resolver logic. 110 + */ 111 + unknown?: (ctx: UnknownResolverContext) => Type | undefined; 112 + /** 113 + * Resolver for void schemas. 114 + * 115 + * Allows customization of how void types are rendered. 116 + * 117 + * Returning `undefined` will execute the default resolver logic. 118 + */ 119 + void?: (ctx: VoidResolverContext) => Type | undefined; 40 120 }>; 41 121 42 122 interface BaseContext extends DollarTsDsl { ··· 44 124 plugin: HeyApiTypeScriptPlugin['Instance']; 45 125 } 46 126 127 + export interface ArrayResolverContext extends BaseContext { 128 + /** 129 + * Nodes used to build different parts of the result. 130 + */ 131 + nodes: { 132 + /** 133 + * Returns the base array type expression. 134 + */ 135 + base: (ctx: ArrayResolverContext) => Type; 136 + }; 137 + schema: SchemaWithType<'array'>; 138 + walk: Walker<TypeScriptResult, HeyApiTypeScriptPlugin['Instance']>; 139 + walkerCtx: SchemaVisitorContext<HeyApiTypeScriptPlugin['Instance']>; 140 + } 141 + 142 + export interface BooleanResolverContext extends BaseContext { 143 + /** 144 + * Nodes used to build different parts of the result. 145 + */ 146 + nodes: { 147 + /** 148 + * Returns the base boolean type expression. 149 + */ 150 + base: (ctx: BooleanResolverContext) => Type; 151 + /** 152 + * Returns the literal type for const values. 153 + */ 154 + const: (ctx: BooleanResolverContext) => Type | undefined; 155 + }; 156 + schema: SchemaWithType<'boolean'>; 157 + } 158 + 47 159 export interface EnumResolverContext extends BaseContext { 48 160 /** 49 161 * Nodes used to build different parts of the result. ··· 70 182 schema: SchemaWithType<'enum'>; 71 183 } 72 184 185 + export interface IntersectionResolverContext extends BaseContext { 186 + /** 187 + * The child results from walking intersection members. 188 + */ 189 + childResults: ReadonlyArray<TypeScriptResult>; 190 + /** 191 + * Nodes used to build different parts of the result. 192 + */ 193 + nodes: { 194 + /** 195 + * Returns the base intersection type expression. 196 + */ 197 + base: (ctx: IntersectionResolverContext) => Type; 198 + }; 199 + /** 200 + * The parent schema containing the intersection. 201 + */ 202 + parentSchema: IR.SchemaObject; 203 + /** 204 + * The individual schemas being intersected. 205 + */ 206 + schemas: ReadonlyArray<IR.SchemaObject>; 207 + } 208 + 209 + export interface NeverResolverContext extends BaseContext { 210 + /** 211 + * Nodes used to build different parts of the result. 212 + */ 213 + nodes: { 214 + /** 215 + * Returns the base never type expression. 216 + */ 217 + base: (ctx: NeverResolverContext) => Type; 218 + }; 219 + schema: SchemaWithType<'never'>; 220 + } 221 + 222 + export interface NullResolverContext extends BaseContext { 223 + /** 224 + * Nodes used to build different parts of the result. 225 + */ 226 + nodes: { 227 + /** 228 + * Returns the base null type expression. 229 + */ 230 + base: (ctx: NullResolverContext) => Type; 231 + }; 232 + schema: SchemaWithType<'null'>; 233 + } 234 + 73 235 export interface NumberResolverContext extends BaseContext { 74 236 /** 75 237 * Nodes used to build different parts of the result. ··· 126 288 }; 127 289 schema: SchemaWithType<'string'>; 128 290 } 291 + 292 + export interface TupleResolverContext extends BaseContext { 293 + /** 294 + * Nodes used to build different parts of the result. 295 + */ 296 + nodes: { 297 + /** 298 + * Returns the base tuple type expression. 299 + */ 300 + base: (ctx: TupleResolverContext) => Type; 301 + /** 302 + * Returns the literal type for const tuple values. 303 + */ 304 + const: (ctx: TupleResolverContext) => Type | undefined; 305 + }; 306 + schema: SchemaWithType<'tuple'>; 307 + walk: Walker<TypeScriptResult, HeyApiTypeScriptPlugin['Instance']>; 308 + walkerCtx: SchemaVisitorContext<HeyApiTypeScriptPlugin['Instance']>; 309 + } 310 + 311 + export interface UnionResolverContext extends BaseContext { 312 + /** 313 + * The child results from walking union members. 314 + */ 315 + childResults: ReadonlyArray<TypeScriptResult>; 316 + /** 317 + * Nodes used to build different parts of the result. 318 + */ 319 + nodes: { 320 + /** 321 + * Returns the base union type expression. 322 + */ 323 + base: (ctx: UnionResolverContext) => Type; 324 + }; 325 + /** 326 + * The parent schema containing the union. 327 + */ 328 + parentSchema: IR.SchemaObject; 329 + /** 330 + * The individual schemas being unioned. 331 + */ 332 + schemas: ReadonlyArray<IR.SchemaObject>; 333 + } 334 + 335 + export interface UndefinedResolverContext extends BaseContext { 336 + /** 337 + * Nodes used to build different parts of the result. 338 + */ 339 + nodes: { 340 + /** 341 + * Returns the base undefined type expression. 342 + */ 343 + base: (ctx: UndefinedResolverContext) => Type; 344 + }; 345 + schema: SchemaWithType<'undefined'>; 346 + } 347 + 348 + export interface UnknownResolverContext extends BaseContext { 349 + /** 350 + * Nodes used to build different parts of the result. 351 + */ 352 + nodes: { 353 + /** 354 + * Returns the base unknown type expression. 355 + */ 356 + base: (ctx: UnknownResolverContext) => Type; 357 + }; 358 + schema: SchemaWithType<'unknown'>; 359 + } 360 + 361 + export interface VoidResolverContext extends BaseContext { 362 + /** 363 + * Nodes used to build different parts of the result. 364 + */ 365 + nodes: { 366 + /** 367 + * Returns the base void type expression. 368 + */ 369 + base: (ctx: VoidResolverContext) => Type; 370 + }; 371 + schema: SchemaWithType<'void'>; 372 + }
+37 -13
packages/openapi-ts/src/plugins/@hey-api/typescript/v1/toAst/array.ts
··· 1 1 import { ref } from '@hey-api/codegen-core'; 2 - import type { SchemaWithType } from '@hey-api/shared'; 3 - import type { Walker } from '@hey-api/shared'; 2 + import type { SchemaVisitorContext, SchemaWithType, Walker } from '@hey-api/shared'; 4 3 import { deduplicateSchema } from '@hey-api/shared'; 5 4 6 5 import { $ } from '../../../../../ts-dsl'; 7 - import type { Type } from '../../shared/types'; 8 - import type { TypeScriptResult } from '../../shared/types'; 6 + import type { ArrayResolverContext } from '../../resolvers'; 7 + import type { Type, TypeScriptResult } from '../../shared/types'; 9 8 import type { HeyApiTypeScriptPlugin } from '../../types'; 10 9 11 - export function arrayToAst({ 12 - plugin, 13 - schema, 14 - walk, 15 - }: { 16 - plugin: HeyApiTypeScriptPlugin['Instance']; 17 - schema: SchemaWithType<'array'>; 18 - walk: Walker<TypeScriptResult, HeyApiTypeScriptPlugin['Instance']>; 19 - }): Type { 10 + function baseNode(ctx: ArrayResolverContext): Type { 11 + const { plugin, schema, walk } = ctx; 12 + 20 13 if (!schema.items) { 21 14 return $.type('Array').generic($.type(plugin.config.topType)); 22 15 } ··· 40 33 ? $.type('Array').generic($.type.and(...itemResults.map((r) => r.type))) 41 34 : $.type('Array').generic($.type.or(...itemResults.map((r) => r.type))); 42 35 } 36 + 37 + function arrayResolver(ctx: ArrayResolverContext): Type { 38 + return ctx.nodes.base(ctx); 39 + } 40 + 41 + export function arrayToAst({ 42 + plugin, 43 + schema, 44 + walk, 45 + walkerCtx, 46 + }: { 47 + plugin: HeyApiTypeScriptPlugin['Instance']; 48 + schema: SchemaWithType<'array'>; 49 + walk: Walker<TypeScriptResult, HeyApiTypeScriptPlugin['Instance']>; 50 + walkerCtx: SchemaVisitorContext<HeyApiTypeScriptPlugin['Instance']>; 51 + }): Type { 52 + const ctx: ArrayResolverContext = { 53 + $, 54 + nodes: { 55 + base: baseNode, 56 + }, 57 + plugin, 58 + schema, 59 + walk, 60 + walkerCtx, 61 + }; 62 + 63 + const resolver = plugin.config['~resolvers']?.array; 64 + const result = resolver?.(ctx); 65 + return result ?? arrayResolver(ctx); 66 + }
+33 -4
packages/openapi-ts/src/plugins/@hey-api/typescript/v1/toAst/boolean.ts
··· 1 1 import type { SchemaWithType } from '@hey-api/shared'; 2 2 3 3 import { $ } from '../../../../../ts-dsl'; 4 + import type { BooleanResolverContext } from '../../resolvers'; 4 5 import type { Type } from '../../shared/types'; 5 6 import type { HeyApiTypeScriptPlugin } from '../../types'; 6 7 8 + function baseNode(): Type { 9 + return $.type('boolean'); 10 + } 11 + 12 + function constNode(ctx: BooleanResolverContext): Type | undefined { 13 + const { schema } = ctx; 14 + if (schema.const !== undefined) { 15 + return $.type.fromValue(schema.const); 16 + } 17 + return undefined; 18 + } 19 + 20 + function booleanResolver(ctx: BooleanResolverContext): Type { 21 + const constResult = ctx.nodes.const(ctx); 22 + if (constResult) return constResult; 23 + 24 + return ctx.nodes.base(ctx); 25 + } 26 + 7 27 export function booleanToAst({ 28 + plugin, 8 29 schema, 9 30 }: { 10 31 plugin: HeyApiTypeScriptPlugin['Instance']; 11 32 schema: SchemaWithType<'boolean'>; 12 33 }): Type { 13 - if (schema.const !== undefined) { 14 - return $.type.fromValue(schema.const); 15 - } 34 + const ctx: BooleanResolverContext = { 35 + $, 36 + nodes: { 37 + base: baseNode, 38 + const: constNode, 39 + }, 40 + plugin, 41 + schema, 42 + }; 16 43 17 - return $.type('boolean'); 44 + const resolver = plugin.config['~resolvers']?.boolean; 45 + const result = resolver?.(ctx); 46 + return result ?? booleanResolver(ctx); 18 47 }
+47
packages/openapi-ts/src/plugins/@hey-api/typescript/v1/toAst/intersection.ts
··· 1 + import type { IR } from '@hey-api/shared'; 2 + 3 + import { $ } from '../../../../../ts-dsl'; 4 + import type { IntersectionResolverContext } from '../../resolvers'; 5 + import type { Type, TypeScriptResult } from '../../shared/types'; 6 + import type { HeyApiTypeScriptPlugin } from '../../types'; 7 + 8 + function baseNode(ctx: IntersectionResolverContext): Type { 9 + const { childResults } = ctx; 10 + 11 + if (childResults.length === 1) { 12 + return childResults[0]!.type; 13 + } 14 + 15 + return $.type.and(...childResults.map((r) => r.type)); 16 + } 17 + 18 + function intersectionResolver(ctx: IntersectionResolverContext): Type { 19 + return ctx.nodes.base(ctx); 20 + } 21 + 22 + export function intersectionToAst({ 23 + childResults, 24 + parentSchema, 25 + plugin, 26 + schemas, 27 + }: { 28 + childResults: ReadonlyArray<TypeScriptResult>; 29 + parentSchema: IR.SchemaObject; 30 + plugin: HeyApiTypeScriptPlugin['Instance']; 31 + schemas: ReadonlyArray<IR.SchemaObject>; 32 + }): Type { 33 + const ctx: IntersectionResolverContext = { 34 + $, 35 + childResults, 36 + nodes: { 37 + base: baseNode, 38 + }, 39 + parentSchema, 40 + plugin, 41 + schemas, 42 + }; 43 + 44 + const resolver = plugin.config['~resolvers']?.intersection; 45 + const result = resolver?.(ctx); 46 + return result ?? intersectionResolver(ctx); 47 + }
+25 -3
packages/openapi-ts/src/plugins/@hey-api/typescript/v1/toAst/never.ts
··· 1 1 import type { SchemaWithType } from '@hey-api/shared'; 2 2 3 3 import { $ } from '../../../../../ts-dsl'; 4 + import type { NeverResolverContext } from '../../resolvers'; 4 5 import type { Type } from '../../shared/types'; 5 6 import type { HeyApiTypeScriptPlugin } from '../../types'; 6 7 7 - // eslint-disable-next-line @typescript-eslint/no-unused-vars 8 - export function neverToAst(args: { 8 + function baseNode(): Type { 9 + return $.type('never'); 10 + } 11 + 12 + function neverResolver(ctx: NeverResolverContext): Type { 13 + return ctx.nodes.base(ctx); 14 + } 15 + 16 + export function neverToAst({ 17 + plugin, 18 + schema, 19 + }: { 9 20 plugin: HeyApiTypeScriptPlugin['Instance']; 10 21 schema: SchemaWithType<'never'>; 11 22 }): Type { 12 - return $.type('never'); 23 + const ctx: NeverResolverContext = { 24 + $, 25 + nodes: { 26 + base: baseNode, 27 + }, 28 + plugin, 29 + schema, 30 + }; 31 + 32 + const resolver = plugin.config['~resolvers']?.never; 33 + const result = resolver?.(ctx); 34 + return result ?? neverResolver(ctx); 13 35 }
+25 -3
packages/openapi-ts/src/plugins/@hey-api/typescript/v1/toAst/null.ts
··· 1 1 import type { SchemaWithType } from '@hey-api/shared'; 2 2 3 3 import { $ } from '../../../../../ts-dsl'; 4 + import type { NullResolverContext } from '../../resolvers'; 4 5 import type { Type } from '../../shared/types'; 5 6 import type { HeyApiTypeScriptPlugin } from '../../types'; 6 7 7 - // eslint-disable-next-line @typescript-eslint/no-unused-vars 8 - export function nullToAst(args: { 8 + function baseNode(): Type { 9 + return $.type.literal(null); 10 + } 11 + 12 + function nullResolver(ctx: NullResolverContext): Type { 13 + return ctx.nodes.base(ctx); 14 + } 15 + 16 + export function nullToAst({ 17 + plugin, 18 + schema, 19 + }: { 9 20 plugin: HeyApiTypeScriptPlugin['Instance']; 10 21 schema: SchemaWithType<'null'>; 11 22 }): Type { 12 - return $.type.literal(null); 23 + const ctx: NullResolverContext = { 24 + $, 25 + nodes: { 26 + base: baseNode, 27 + }, 28 + plugin, 29 + schema, 30 + }; 31 + 32 + const resolver = plugin.config['~resolvers']?.null; 33 + const result = resolver?.(ctx); 34 + return result ?? nullResolver(ctx); 13 35 }
+51 -15
packages/openapi-ts/src/plugins/@hey-api/typescript/v1/toAst/tuple.ts
··· 1 1 import { ref } from '@hey-api/codegen-core'; 2 - import type { SchemaWithType } from '@hey-api/shared'; 3 - import type { Walker } from '@hey-api/shared'; 2 + import type { SchemaVisitorContext, SchemaWithType, Walker } from '@hey-api/shared'; 4 3 5 4 import { $ } from '../../../../../ts-dsl'; 6 - import type { Type } from '../../shared/types'; 7 - import type { TypeScriptResult } from '../../shared/types'; 5 + import type { TupleResolverContext } from '../../resolvers'; 6 + import type { Type, TypeScriptResult } from '../../shared/types'; 8 7 import type { HeyApiTypeScriptPlugin } from '../../types'; 9 8 9 + function baseNode(ctx: TupleResolverContext): Type { 10 + const { plugin, schema, walk } = ctx; 11 + const itemTypes: Array<Type> = []; 12 + 13 + if (schema.items) { 14 + schema.items.forEach((item) => { 15 + const result = walk(item, { path: ref([]), plugin }); 16 + itemTypes.push(result.type); 17 + }); 18 + } 19 + 20 + return $.type.tuple(...itemTypes); 21 + } 22 + 23 + function constNode(ctx: TupleResolverContext): Type | undefined { 24 + const { schema } = ctx; 25 + 26 + if (!schema.const || !Array.isArray(schema.const)) { 27 + return undefined; 28 + } 29 + 30 + const itemTypes = schema.const.map((value) => $.type.fromValue(value)); 31 + return $.type.tuple(...itemTypes); 32 + } 33 + 34 + function tupleResolver(ctx: TupleResolverContext): Type { 35 + const constResult = ctx.nodes.const(ctx); 36 + if (constResult) return constResult; 37 + 38 + return ctx.nodes.base(ctx); 39 + } 40 + 10 41 export function tupleToAst({ 11 42 plugin, 12 43 schema, 13 44 walk, 45 + walkerCtx, 14 46 }: { 15 47 plugin: HeyApiTypeScriptPlugin['Instance']; 16 48 schema: SchemaWithType<'tuple'>; 17 49 walk: Walker<TypeScriptResult, HeyApiTypeScriptPlugin['Instance']>; 50 + walkerCtx: SchemaVisitorContext<HeyApiTypeScriptPlugin['Instance']>; 18 51 }): Type { 19 - let itemTypes: Array<Type> = []; 20 - 21 - if (schema.const && Array.isArray(schema.const)) { 22 - itemTypes = schema.const.map((value) => $.type.fromValue(value)); 23 - } else if (schema.items) { 24 - schema.items.forEach((item) => { 25 - const result = walk(item, { path: ref([]), plugin }); 26 - itemTypes.push(result.type); 27 - }); 28 - } 52 + const ctx: TupleResolverContext = { 53 + $, 54 + nodes: { 55 + base: baseNode, 56 + const: constNode, 57 + }, 58 + plugin, 59 + schema, 60 + walk, 61 + walkerCtx, 62 + }; 29 63 30 - return $.type.tuple(...itemTypes); 64 + const resolver = plugin.config['~resolvers']?.tuple; 65 + const result = resolver?.(ctx); 66 + return result ?? tupleResolver(ctx); 31 67 }
+25 -3
packages/openapi-ts/src/plugins/@hey-api/typescript/v1/toAst/undefined.ts
··· 1 1 import type { SchemaWithType } from '@hey-api/shared'; 2 2 3 3 import { $ } from '../../../../../ts-dsl'; 4 + import type { UndefinedResolverContext } from '../../resolvers'; 4 5 import type { Type } from '../../shared/types'; 5 6 import type { HeyApiTypeScriptPlugin } from '../../types'; 6 7 7 - // eslint-disable-next-line @typescript-eslint/no-unused-vars 8 - export function undefinedToAst(args: { 8 + function baseNode(): Type { 9 + return $.type('undefined'); 10 + } 11 + 12 + function undefinedResolver(ctx: UndefinedResolverContext): Type { 13 + return ctx.nodes.base(ctx); 14 + } 15 + 16 + export function undefinedToAst({ 17 + plugin, 18 + schema, 19 + }: { 9 20 plugin: HeyApiTypeScriptPlugin['Instance']; 10 21 schema: SchemaWithType<'undefined'>; 11 22 }): Type { 12 - return $.type('undefined'); 23 + const ctx: UndefinedResolverContext = { 24 + $, 25 + nodes: { 26 + base: baseNode, 27 + }, 28 + plugin, 29 + schema, 30 + }; 31 + 32 + const resolver = plugin.config['~resolvers']?.undefined; 33 + const result = resolver?.(ctx); 34 + return result ?? undefinedResolver(ctx); 13 35 }
+47
packages/openapi-ts/src/plugins/@hey-api/typescript/v1/toAst/union.ts
··· 1 + import type { IR } from '@hey-api/shared'; 2 + 3 + import { $ } from '../../../../../ts-dsl'; 4 + import type { UnionResolverContext } from '../../resolvers'; 5 + import type { Type, TypeScriptResult } from '../../shared/types'; 6 + import type { HeyApiTypeScriptPlugin } from '../../types'; 7 + 8 + function baseNode(ctx: UnionResolverContext): Type { 9 + const { childResults } = ctx; 10 + 11 + if (childResults.length === 1) { 12 + return childResults[0]!.type; 13 + } 14 + 15 + return $.type.or(...childResults.map((r) => r.type)); 16 + } 17 + 18 + function unionResolver(ctx: UnionResolverContext): Type { 19 + return ctx.nodes.base(ctx); 20 + } 21 + 22 + export function unionToAst({ 23 + childResults, 24 + parentSchema, 25 + plugin, 26 + schemas, 27 + }: { 28 + childResults: ReadonlyArray<TypeScriptResult>; 29 + parentSchema: IR.SchemaObject; 30 + plugin: HeyApiTypeScriptPlugin['Instance']; 31 + schemas: ReadonlyArray<IR.SchemaObject>; 32 + }): Type { 33 + const ctx: UnionResolverContext = { 34 + $, 35 + childResults, 36 + nodes: { 37 + base: baseNode, 38 + }, 39 + parentSchema, 40 + plugin, 41 + schemas, 42 + }; 43 + 44 + const resolver = plugin.config['~resolvers']?.union; 45 + const result = resolver?.(ctx); 46 + return result ?? unionResolver(ctx); 47 + }
+22 -1
packages/openapi-ts/src/plugins/@hey-api/typescript/v1/toAst/unknown.ts
··· 1 1 import type { SchemaWithType } from '@hey-api/shared'; 2 2 3 3 import { $ } from '../../../../../ts-dsl'; 4 + import type { UnknownResolverContext } from '../../resolvers'; 4 5 import type { Type } from '../../shared/types'; 5 6 import type { HeyApiTypeScriptPlugin } from '../../types'; 6 7 8 + function baseNode(ctx: UnknownResolverContext): Type { 9 + return $.type(ctx.plugin.config.topType); 10 + } 11 + 12 + function unknownResolver(ctx: UnknownResolverContext): Type { 13 + return ctx.nodes.base(ctx); 14 + } 15 + 7 16 export function unknownToAst({ 8 17 plugin, 18 + schema, 9 19 }: { 10 20 plugin: HeyApiTypeScriptPlugin['Instance']; 11 21 schema: SchemaWithType<'unknown'>; 12 22 }): Type { 13 - return $.type(plugin.config.topType); 23 + const ctx: UnknownResolverContext = { 24 + $, 25 + nodes: { 26 + base: baseNode, 27 + }, 28 + plugin, 29 + schema, 30 + }; 31 + 32 + const resolver = plugin.config['~resolvers']?.unknown; 33 + const result = resolver?.(ctx); 34 + return result ?? unknownResolver(ctx); 14 35 }
+25 -3
packages/openapi-ts/src/plugins/@hey-api/typescript/v1/toAst/void.ts
··· 1 1 import type { SchemaWithType } from '@hey-api/shared'; 2 2 3 3 import { $ } from '../../../../../ts-dsl'; 4 + import type { VoidResolverContext } from '../../resolvers'; 4 5 import type { Type } from '../../shared/types'; 5 6 import type { HeyApiTypeScriptPlugin } from '../../types'; 6 7 7 - // eslint-disable-next-line @typescript-eslint/no-unused-vars 8 - export function voidToAst(args: { 8 + function baseNode(): Type { 9 + return $.type('void'); 10 + } 11 + 12 + function voidResolver(ctx: VoidResolverContext): Type { 13 + return ctx.nodes.base(ctx); 14 + } 15 + 16 + export function voidToAst({ 17 + plugin, 18 + schema, 19 + }: { 9 20 plugin: HeyApiTypeScriptPlugin['Instance']; 10 21 schema: SchemaWithType<'void'>; 11 22 }): Type { 12 - return $.type('void'); 23 + const ctx: VoidResolverContext = { 24 + $, 25 + nodes: { 26 + base: baseNode, 27 + }, 28 + plugin, 29 + schema, 30 + }; 31 + 32 + const resolver = plugin.config['~resolvers']?.void; 33 + const result = resolver?.(ctx); 34 + return result ?? voidResolver(ctx); 13 35 }
+18 -4
packages/openapi-ts/src/plugins/@hey-api/typescript/v1/walker.ts
··· 10 10 import { arrayToAst } from './toAst/array'; 11 11 import { booleanToAst } from './toAst/boolean'; 12 12 import { enumToAst } from './toAst/enum'; 13 + import { intersectionToAst } from './toAst/intersection'; 13 14 import { neverToAst } from './toAst/never'; 14 15 import { nullToAst } from './toAst/null'; 15 16 import { numberToAst } from './toAst/number'; ··· 17 18 import { stringToAst } from './toAst/string'; 18 19 import { tupleToAst } from './toAst/tuple'; 19 20 import { undefinedToAst } from './toAst/undefined'; 21 + import { unionToAst } from './toAst/union'; 20 22 import { unknownToAst } from './toAst/unknown'; 21 23 import { voidToAst } from './toAst/void'; 22 24 ··· 42 44 plugin: ctx.plugin, 43 45 schema, 44 46 walk, 47 + walkerCtx: ctx, 45 48 }); 46 49 return { 47 50 meta: defaultMeta(schema), ··· 98 101 } 99 102 } 100 103 }, 101 - intersection(items, schemas, parentSchema) { 102 - const type = items.length === 1 ? items[0]!.type : $.type.and(...items.map((r) => r.type)); 104 + intersection(items, schemas, parentSchema, ctx) { 105 + const type = intersectionToAst({ 106 + childResults: items, 107 + parentSchema, 108 + plugin: ctx.plugin, 109 + schemas, 110 + }); 103 111 104 112 return { 105 113 meta: inheritMeta(parentSchema, items), ··· 177 185 plugin: ctx.plugin, 178 186 schema, 179 187 walk, 188 + walkerCtx: ctx, 180 189 }); 181 190 return { 182 191 meta: defaultMeta(schema), ··· 190 199 type, 191 200 }; 192 201 }, 193 - union(items, schemas, parentSchema) { 194 - const type = items.length === 1 ? items[0]!.type : $.type.or(...items.map((r) => r.type)); 202 + union(items, schemas, parentSchema, ctx) { 203 + const type = unionToAst({ 204 + childResults: items, 205 + parentSchema, 206 + plugin: ctx.plugin, 207 + schemas, 208 + }); 195 209 196 210 return { 197 211 meta: inheritMeta(parentSchema, items),