|
| 1 | +import type { Plugin, SchemaVisitorContext, SchemaWithType, Walker } from '@hey-api/shared'; |
| 2 | + |
| 3 | +import type { $, DollarTsDsl } from '../../../ts-dsl'; |
| 4 | +import type { HeyApiTypeScriptPlugin, Type, TypeScriptResult } from './shared/types'; |
| 5 | + |
| 6 | +export type Resolvers = Plugin.Resolvers<{ |
| 7 | + /** |
| 8 | + * Resolver for enum schemas. |
| 9 | + * |
| 10 | + * Allows customization of how enum types are rendered. |
| 11 | + * |
| 12 | + * Returning `undefined` will execute the default resolver logic. |
| 13 | + */ |
| 14 | + enum?: (ctx: EnumResolverContext) => Type | undefined; |
| 15 | + /** |
| 16 | + * Resolver for number schemas. |
| 17 | + * |
| 18 | + * Allows customization of how number types are rendered. |
| 19 | + * |
| 20 | + * Returning `undefined` will execute the default resolver logic. |
| 21 | + */ |
| 22 | + number?: (ctx: NumberResolverContext) => Type | undefined; |
| 23 | + /** |
| 24 | + * Resolver for object schemas. |
| 25 | + * |
| 26 | + * Allows customization of how object types are rendered. |
| 27 | + * |
| 28 | + * Returning `undefined` will execute the default resolver logic. |
| 29 | + */ |
| 30 | + object?: (ctx: ObjectResolverContext) => Type | undefined; |
| 31 | + /** |
| 32 | + * Resolver for string schemas. |
| 33 | + * |
| 34 | + * Allows customization of how string types are rendered. |
| 35 | + * |
| 36 | + * Returning `undefined` will execute the default resolver logic. |
| 37 | + */ |
| 38 | + string?: (ctx: StringResolverContext) => Type | undefined; |
| 39 | +}>; |
| 40 | + |
| 41 | +interface BaseContext extends DollarTsDsl { |
| 42 | + /** The plugin instance. */ |
| 43 | + plugin: HeyApiTypeScriptPlugin['Instance']; |
| 44 | +} |
| 45 | + |
| 46 | +export interface EnumResolverContext extends BaseContext { |
| 47 | + /** |
| 48 | + * Nodes used to build different parts of the result. |
| 49 | + */ |
| 50 | + nodes: { |
| 51 | + /** |
| 52 | + * Returns the base enum type expression. |
| 53 | + */ |
| 54 | + base: (ctx: EnumResolverContext) => Type; |
| 55 | + /** |
| 56 | + * Returns parsed enum items with metadata about the enum members. |
| 57 | + */ |
| 58 | + items: (ctx: EnumResolverContext) => { |
| 59 | + /** |
| 60 | + * String literal values for use with union types. |
| 61 | + */ |
| 62 | + enumMembers: Array<ReturnType<typeof $.type.literal>>; |
| 63 | + /** |
| 64 | + * Whether the enum includes a null value. |
| 65 | + */ |
| 66 | + isNullable: boolean; |
| 67 | + }; |
| 68 | + }; |
| 69 | + schema: SchemaWithType<'enum'>; |
| 70 | +} |
| 71 | + |
| 72 | +export interface NumberResolverContext extends BaseContext { |
| 73 | + /** |
| 74 | + * Nodes used to build different parts of the result. |
| 75 | + */ |
| 76 | + nodes: { |
| 77 | + /** |
| 78 | + * Returns the base number type expression. |
| 79 | + */ |
| 80 | + base: (ctx: NumberResolverContext) => Type; |
| 81 | + /** |
| 82 | + * Returns the literal type for const values. |
| 83 | + */ |
| 84 | + const: (ctx: NumberResolverContext) => Type | undefined; |
| 85 | + }; |
| 86 | + schema: SchemaWithType<'integer' | 'number'>; |
| 87 | +} |
| 88 | + |
| 89 | +export interface ObjectResolverContext extends BaseContext { |
| 90 | + /** |
| 91 | + * Nodes used to build different parts of the result. |
| 92 | + */ |
| 93 | + nodes: { |
| 94 | + /** |
| 95 | + * Returns the base object type expression. |
| 96 | + */ |
| 97 | + base: (ctx: ObjectResolverContext) => Type; |
| 98 | + /** |
| 99 | + * Returns the shape (properties) of the object type. |
| 100 | + */ |
| 101 | + shape: (ctx: ObjectResolverContext) => ReturnType<typeof $.type.object>; |
| 102 | + }; |
| 103 | + schema: SchemaWithType<'object'>; |
| 104 | + walk: Walker<TypeScriptResult, HeyApiTypeScriptPlugin['Instance']>; |
| 105 | + walkerCtx: SchemaVisitorContext<HeyApiTypeScriptPlugin['Instance']>; |
| 106 | +} |
| 107 | + |
| 108 | +export interface StringResolverContext extends BaseContext { |
| 109 | + /** |
| 110 | + * Nodes used to build different parts of the result. |
| 111 | + */ |
| 112 | + nodes: { |
| 113 | + /** |
| 114 | + * Returns the base string type expression. |
| 115 | + */ |
| 116 | + base: (ctx: StringResolverContext) => Type; |
| 117 | + /** |
| 118 | + * Returns the literal type for const values. |
| 119 | + */ |
| 120 | + const: (ctx: StringResolverContext) => Type | undefined; |
| 121 | + /** |
| 122 | + * Returns the format-specific type expression. |
| 123 | + */ |
| 124 | + format: (ctx: StringResolverContext) => Type | undefined; |
| 125 | + }; |
| 126 | + schema: SchemaWithType<'string'>; |
| 127 | +} |
0 commit comments