UNPKG

typed-openapi

Version:
269 lines (259 loc) 9.88 kB
import * as openapi3_ts_oas31 from 'openapi3-ts/oas31'; import { OpenAPIObject, SchemaObject, ReferenceObject, OperationObject } from 'openapi3-ts/oas31'; import * as arktype from 'arktype'; import * as Codegen from '@sinclair/typebox-codegen'; declare class Box<T extends AnyBoxDef = AnyBoxDef> { definition: T; type: T["type"]; value: T["value"]; params: T["params"]; schema: T["schema"]; ctx: T["ctx"]; constructor(definition: T); toJSON(): { type: T["type"]; value: T["value"]; }; toString(): string; recompute(callback: OpenapiSchemaConvertContext["onBox"]): Box<AnyBoxDef>; static fromJSON(json: string): Box<any>; static isBox(box: unknown): box is Box<AnyBoxDef>; static isUnion(box: Box<AnyBoxDef>): box is Box<BoxUnion>; static isIntersection(box: Box<AnyBoxDef>): box is Box<BoxIntersection>; static isArray(box: Box<AnyBoxDef>): box is Box<BoxArray>; static isOptional(box: Box<AnyBoxDef>): box is Box<BoxOptional>; static isReference(box: Box<AnyBoxDef>): box is Box<BoxRef>; static isKeyword(box: Box<AnyBoxDef>): box is Box<BoxKeyword>; static isObject(box: Box<AnyBoxDef>): box is Box<BoxObject>; static isLiteral(box: Box<AnyBoxDef>): box is Box<BoxLiteral>; } type RefInfo = { /** * The (potentially autocorrected) ref * @example "#/components/schemas/MySchema" */ ref: string; /** * The name of the ref * @example "MySchema" * */ name: string; normalized: string; kind: "schemas" | "responses" | "parameters" | "requestBodies" | "headers"; }; declare const createRefResolver: (doc: OpenAPIObject, factory: GenericFactory) => { get: <T = SchemaObject>(ref: string) => NonNullable<T>; unwrap: <T_1 extends {} | ReferenceObject>(component: T_1) => Exclude<T_1, ReferenceObject>; getInfosByRef: (ref: string) => RefInfo; infos: Map<string, RefInfo>; /** * Get the schemas in the order they should be generated, depending on their dependencies * so that a schema is generated before the ones that depend on it */ getOrderedSchemas: () => [schema: Box<AnyBoxDef>, infos: RefInfo][]; directDependencies: Map<string, Set<string>>; transitiveDependencies: Map<string, Set<string>>; }; interface RefResolver extends ReturnType<typeof createRefResolver> { } type BoxDefinition = { type: string; params: unknown; value: string; }; type BoxParams = string | BoxDefinition; type WithSchema = { schema: SchemaObject | ReferenceObject | undefined; ctx: OpenapiSchemaConvertContext; }; type BoxUnion = WithSchema & { type: "union"; params: { types: Array<BoxParams>; }; value: string; }; type BoxIntersection = WithSchema & { type: "intersection"; params: { types: Array<BoxParams>; }; value: string; }; type BoxArray = WithSchema & { type: "array"; params: { type: BoxParams; }; value: string; }; type BoxOptional = WithSchema & { type: "optional"; params: { type: BoxParams; }; value: string; }; type BoxRef = WithSchema & { type: "ref"; params: { name: string; generics?: BoxParams[] | undefined; }; value: string; }; type BoxLiteral = WithSchema & { type: "literal"; params: {}; value: string; }; type BoxKeyword = WithSchema & { type: "keyword"; params: { name: string; }; value: string; }; type BoxObject = WithSchema & { type: "object"; params: { props: Record<string, BoxParams>; }; value: string; }; type AnyBoxDef = BoxUnion | BoxIntersection | BoxArray | BoxOptional | BoxRef | BoxLiteral | BoxKeyword | BoxObject; type AnyBox = Box<AnyBoxDef>; type OpenapiSchemaConvertArgs = { schema: SchemaObject | ReferenceObject; ctx: OpenapiSchemaConvertContext; meta?: {} | undefined; }; type FactoryCreator = (schema: SchemaObject | ReferenceObject, ctx: OpenapiSchemaConvertContext) => GenericFactory; type OpenapiSchemaConvertContext = { factory: FactoryCreator | GenericFactory; refs: RefResolver; onBox?: (box: Box<AnyBoxDef>) => Box<AnyBoxDef>; }; type StringOrBox = string | Box<AnyBoxDef>; type BoxFactory = { union: (types: Array<StringOrBox>) => Box<BoxUnion>; intersection: (types: Array<StringOrBox>) => Box<BoxIntersection>; array: (type: StringOrBox) => Box<BoxArray>; object: (props: Record<string, StringOrBox>) => Box<BoxObject>; optional: (type: StringOrBox) => Box<BoxOptional>; reference: (name: string, generics?: Array<StringOrBox> | undefined) => Box<BoxRef>; literal: (value: StringOrBox) => Box<BoxLiteral>; string: () => Box<BoxKeyword>; number: () => Box<BoxKeyword>; boolean: () => Box<BoxKeyword>; unknown: () => Box<BoxKeyword>; any: () => Box<BoxKeyword>; never: () => Box<BoxKeyword>; }; type GenericFactory = { callback?: OpenapiSchemaConvertContext["onBox"]; union: (types: Array<StringOrBox>) => string; intersection: (types: Array<StringOrBox>) => string; array: (type: StringOrBox) => string; object: (props: Record<string, StringOrBox>) => string; optional: (type: StringOrBox) => string; reference: (name: string, generics?: Array<StringOrBox> | undefined) => string; literal: (value: StringOrBox) => string; string: () => string; number: () => string; boolean: () => string; unknown: () => string; any: () => string; never: () => string; }; declare const unwrap: (param: StringOrBox) => string; declare const createFactory: <T extends GenericFactory | FactoryCreator>(f: T) => T; /** * Create a box-factory using your schema provider and automatically add the input schema to each box. */ declare const createBoxFactory: (schema: SchemaObject | ReferenceObject, ctx: OpenapiSchemaConvertContext) => BoxFactory; declare const mapOpenApiEndpoints: (doc: OpenAPIObject) => { doc: OpenAPIObject; refs: { get: <T = openapi3_ts_oas31.SchemaObject>(ref: string) => NonNullable<T>; unwrap: <T_1 extends {} | openapi3_ts_oas31.ReferenceObject>(component: T_1) => Exclude<T_1, openapi3_ts_oas31.ReferenceObject>; getInfosByRef: (ref: string) => RefInfo; infos: Map<string, RefInfo>; getOrderedSchemas: () => [schema: Box<AnyBoxDef>, infos: RefInfo][]; directDependencies: Map<string, Set<string>>; transitiveDependencies: Map<string, Set<string>>; }; endpointList: Endpoint<DefaultEndpoint>[]; factory: { union: (types: StringOrBox[]) => string; intersection: (types: StringOrBox[]) => string; array: (type: StringOrBox) => string; optional: (type: StringOrBox) => string; reference: (name: string, typeArgs: StringOrBox[] | undefined) => string; literal: (value: StringOrBox) => string; string: () => "string"; number: () => "number"; boolean: () => "boolean"; unknown: () => "unknown"; any: () => "any"; never: () => "never"; object: (props: Record<string, StringOrBox>) => string; }; }; type MutationMethod = "post" | "put" | "patch" | "delete"; type Method = "get" | "head" | "options" | MutationMethod; type EndpointParameters = { body?: Box<BoxRef>; query?: Box<BoxRef> | Record<string, AnyBox>; header?: Box<BoxRef> | Record<string, AnyBox>; path?: Box<BoxRef> | Record<string, AnyBox>; }; type RequestFormat = "json" | "form-data" | "form-url" | "binary" | "text"; type DefaultEndpoint = { parameters?: EndpointParameters | undefined; response: AnyBox; }; type Endpoint<TConfig extends DefaultEndpoint = DefaultEndpoint> = { operation: OperationObject; method: Method; path: string; parameters?: TConfig["parameters"]; requestFormat: RequestFormat; meta: { alias: string; hasParameters: boolean; areParametersRequired: boolean; }; response: TConfig["response"]; }; type GeneratorOptions = ReturnType<typeof mapOpenApiEndpoints> & { runtime?: "none" | keyof typeof runtimeValidationGenerator; }; declare const allowedRuntimes: arktype.Type<"zod" | "arktype" | "typebox" | "valibot" | "yup" | "io-ts" | "none">; type OutputRuntime = typeof allowedRuntimes.infer; declare const runtimeValidationGenerator: { arktype: typeof Codegen.ModelToArkType.Generate; "io-ts": typeof Codegen.ModelToIoTs.Generate; typebox: typeof Codegen.ModelToTypeBox.Generate; valibot: typeof Codegen.ModelToValibot.Generate; yup: typeof Codegen.ModelToYup.Generate; zod: typeof Codegen.ModelToZod.Generate; }; declare const generateFile: (options: GeneratorOptions) => string; declare const openApiSchemaToTs: ({ schema, meta: _inheritedMeta, ctx }: OpenapiSchemaConvertArgs) => Box<AnyBoxDef>; declare const tsFactory: { union: (types: StringOrBox[]) => string; intersection: (types: StringOrBox[]) => string; array: (type: StringOrBox) => string; optional: (type: StringOrBox) => string; reference: (name: string, typeArgs: StringOrBox[] | undefined) => string; literal: (value: StringOrBox) => string; string: () => "string"; number: () => "number"; boolean: () => "boolean"; unknown: () => "unknown"; any: () => "any"; never: () => "never"; object: (props: Record<string, StringOrBox>) => string; }; export { AnyBox, AnyBoxDef, BoxArray, BoxDefinition, BoxFactory, BoxIntersection, BoxKeyword, BoxLiteral, BoxObject, BoxOptional, BoxParams, BoxRef, BoxUnion, Endpoint, EndpointParameters, FactoryCreator, GenericFactory, OpenapiSchemaConvertArgs, OpenapiSchemaConvertContext, OutputRuntime, RefInfo, RefResolver, StringOrBox, WithSchema, createBoxFactory, createFactory, createRefResolver, generateFile, mapOpenApiEndpoints, openApiSchemaToTs, tsFactory, unwrap };