typed-openapi
Version:
280 lines (269 loc) • 10.6 kB
TypeScript
import * as openapi3_ts_oas31 from 'openapi3-ts/oas31';
import { OpenAPIObject, ReferenceObject, SchemaObject, OperationObject } from 'openapi3-ts/oas31';
import { SchemaObject as SchemaObject$1 } from 'openapi3-ts/oas30';
import * as arktype_internal_methods_string_ts from 'arktype/internal/methods/string.ts';
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 = 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> {
}
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 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 OpenapiSchemaConvertContext["factory"]>(f: T) => T;
/**
* Create a box-factory using your schema provider and automatically add the input schema to each box.
*/
declare const createBoxFactory: (schema: LibSchemaObject | ReferenceObject, ctx: OpenapiSchemaConvertContext) => BoxFactory;
declare const mapOpenApiEndpoints: (doc: OpenAPIObject) => {
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;
response: AnyBox;
responseHeaders?: Record<string, 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"];
responseHeaders?: TConfig["responseHeaders"];
};
type GeneratorOptions$1 = ReturnType<typeof mapOpenApiEndpoints> & {
runtime?: "none" | keyof typeof runtimeValidationGenerator;
schemasOnly?: boolean;
};
declare const allowedRuntimes: arktype_internal_methods_string_ts.StringType<"none" | "arktype" | "io-ts" | "typebox" | "valibot" | "yup" | "zod", {}>;
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$1) => string;
type GeneratorOptions = ReturnType<typeof mapOpenApiEndpoints>;
type GeneratorContext = Required<GeneratorOptions>;
declare const generateTanstackQueryFile: (ctx: GeneratorContext & {
relativeApiClientPath: string;
}) => Promise<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 { type AnyBox, type AnyBoxDef, type BoxArray, type BoxDefinition, type BoxFactory, type BoxIntersection, type BoxKeyword, type BoxLiteral, type BoxObject, type BoxOptional, type BoxParams, type BoxRef, type BoxUnion, type Endpoint, type EndpointParameters, type FactoryCreator, type GenericFactory, type LibSchemaObject, type OpenapiSchemaConvertArgs, type OpenapiSchemaConvertContext, type OutputRuntime, type RefInfo, type RefResolver, type StringOrBox, type WithSchema, createBoxFactory, createFactory, createRefResolver, generateFile, generateTanstackQueryFile, mapOpenApiEndpoints, openApiSchemaToTs, tsFactory, unwrap };