UNPKG

typed-openapi

Version:
243 lines (237 loc) 8.86 kB
import * as openapi3_ts_oas31 from 'openapi3-ts/oas31'; import { OpenAPIObject, ReferenceObject, OperationObject, SchemaObject } from 'openapi3-ts/oas31'; import { SchemaObject as SchemaObject$1 } from 'openapi3-ts/oas30'; 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, nameTransform?: NameTransformOptions) => { get: <T = LibSchemaObject>(ref: string) => NonNullable<T>; unwrap: <T extends ReferenceObject | {}>(component: T) => Exclude<T, 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> { } declare const mapOpenApiEndpoints: (doc: OpenAPIObject, options?: { nameTransform?: NameTransformOptions; }) => { doc: OpenAPIObject; refs: { get: <T = LibSchemaObject>(ref: string) => NonNullable<T>; unwrap: <T extends openapi3_ts_oas31.ReferenceObject | {}>(component: T) => Exclude<T, 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; responses?: Record<string, AnyBox>; responseHeaders?: Record<string, Box<BoxObject>>; }; type Endpoint<TConfig extends DefaultEndpoint = DefaultEndpoint> = { operation: OperationObject; method: Method; path: string; parameters?: TConfig["parameters"]; requestFormat: RequestFormat; meta: { alias: string; hasParameters: boolean; areParametersRequired: boolean; }; responses?: TConfig["responses"]; responseHeaders?: TConfig["responseHeaders"]; }; type LibSchemaObject = SchemaObject & SchemaObject$1; type BoxDefinition = { type: string; params: unknown; value: string; }; type BoxParams = string | BoxDefinition; type WithSchema = { schema: LibSchemaObject | 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 NameTransformOptions = { transformSchemaName?: (name: string) => string; transformEndpointName?: (endpoint: { alias: string; operation: OperationObject; method: Method; path: string; }) => string; }; type OpenapiSchemaConvertContext = { factory: FactoryCreator | GenericFactory; refs: RefResolver; onBox?: (box: Box<AnyBoxDef>) => Box<AnyBoxDef>; nameTransform?: NameTransformOptions; }; 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; }; export { type AnyBoxDef as A, type BoxFactory as B, type EndpointParameters as E, type FactoryCreator as F, type GenericFactory as G, type LibSchemaObject as L, type Method as M, type NameTransformOptions as N, type OpenapiSchemaConvertContext as O, type RefInfo as R, type StringOrBox as S, type WithSchema as W, type OpenapiSchemaConvertArgs as a, Box as b, type Endpoint as c, createRefResolver as d, type RefResolver as e, type BoxDefinition as f, type BoxParams as g, type BoxUnion as h, type BoxIntersection as i, type BoxArray as j, type BoxOptional as k, type BoxRef as l, mapOpenApiEndpoints as m, type BoxLiteral as n, type BoxKeyword as o, type BoxObject as p, type AnyBox as q };