fork of hey-api/openapi-ts because I need some additional things
1import type { SchemaWithType } from '@hey-api/shared';
2
3import { $ } from '../../../../py-dsl';
4import type { BooleanResolverContext } from '../../resolvers';
5import type { PydanticType } from '../../shared/types';
6import type { PydanticPlugin } from '../../types';
7
8function constNode(ctx: BooleanResolverContext): PydanticType | undefined {
9 const { plugin, schema } = ctx;
10
11 if (typeof schema.const === 'boolean') {
12 const literal = plugin.external('typing.Literal');
13 return {
14 type: $(literal).slice($.literal(schema.const)),
15 };
16 }
17}
18
19// eslint-disable-next-line @typescript-eslint/no-unused-vars
20function baseNode(_ctx: BooleanResolverContext): PydanticType {
21 return {
22 type: 'bool',
23 };
24}
25
26function booleanResolver(ctx: BooleanResolverContext): PydanticType {
27 const constResult = ctx.nodes.const(ctx);
28 if (constResult) return constResult;
29
30 return ctx.nodes.base(ctx);
31}
32
33export function booleanToType({
34 plugin,
35 schema,
36}: {
37 plugin: PydanticPlugin['Instance'];
38 schema: SchemaWithType<'boolean'>;
39}): PydanticType {
40 const ctx: BooleanResolverContext = {
41 $,
42 nodes: {
43 base: baseNode,
44 const: constNode,
45 },
46 plugin,
47 schema,
48 };
49
50 const resolver = plugin.config['~resolvers']?.boolean;
51 return resolver?.(ctx) ?? booleanResolver(ctx);
52}