@orpc/openapi
Version:
<div align="center"> <image align="center" src="https://orpc.unnoq.com/logo.webp" width=280 alt="oRPC logo" /> </div>
109 lines (105 loc) • 4.5 kB
TypeScript
import { AnySchema, OpenAPI, AnyContractProcedure, AnyContractRouter } from '@orpc/contract';
import { StandardOpenAPIJsonSerializerOptions } from '@orpc/openapi-client/standard';
import { AnyProcedure, TraverseContractProcedureCallbackOptions, AnyRouter } from '@orpc/server';
import { Promisable, Value } from '@orpc/shared';
import { JSONSchema } from '@orpc/interop/json-schema-typed/draft-2020-12';
interface SchemaConverterComponent {
allowedStrategies: readonly SchemaConvertOptions['strategy'][];
schema: AnySchema;
required: boolean;
ref: string;
}
interface SchemaConvertOptions {
strategy: 'input' | 'output';
/**
* Common components should use `$ref` to represent themselves if matched.
*/
components?: readonly SchemaConverterComponent[];
/**
* Minimum schema structure depth required before using `$ref` for components.
*
* For example, if set to 2, `$ref` will only be used for schemas nested at depth 2 or greater.
*
* @default 0 - No depth limit;
*/
minStructureDepthForRef?: number;
}
interface SchemaConverter {
convert(schema: AnySchema | undefined, options: SchemaConvertOptions): Promisable<[required: boolean, jsonSchema: JSONSchema]>;
}
interface ConditionalSchemaConverter extends SchemaConverter {
condition(schema: AnySchema | undefined, options: SchemaConvertOptions): Promisable<boolean>;
}
declare class CompositeSchemaConverter implements SchemaConverter {
private readonly converters;
constructor(converters: readonly ConditionalSchemaConverter[]);
convert(schema: AnySchema | undefined, options: SchemaConvertOptions): Promise<[required: boolean, jsonSchema: JSONSchema]>;
}
interface OpenAPIGeneratorOptions extends StandardOpenAPIJsonSerializerOptions {
schemaConverters?: ConditionalSchemaConverter[];
}
interface OpenAPIGeneratorGenerateOptions extends Partial<Omit<OpenAPI.Document, 'openapi'>> {
/**
* Exclude procedures from the OpenAPI specification.
*
* @deprecated Use `filter` option instead.
* @default () => false
*/
exclude?: (procedure: AnyProcedure | AnyContractProcedure, path: readonly string[]) => boolean;
/**
* Filter procedures. Return `false` to exclude a procedure from the OpenAPI specification.
*
* @default true
*/
filter?: Value<boolean, [options: TraverseContractProcedureCallbackOptions]>;
/**
* Common schemas to be used for $ref resolution.
*/
commonSchemas?: Record<string, {
/**
* Determines which schema definition to use when input and output schemas differ.
* This is needed because some schemas transform data differently between input and output,
* making it impossible to use a single $ref for both cases.
*
* @example
* ```ts
* // This schema transforms a string input into a number output
* const Schema = z.string()
* .transform(v => Number(v))
* .pipe(z.number())
*
* // Input schema: { type: 'string' }
* // Output schema: { type: 'number' }
* ```
*
* When schemas differ between input and output, you must explicitly choose
* which version to use for the OpenAPI specification.
*
* @default 'input' - Uses the input schema definition by default
*/
strategy?: SchemaConvertOptions['strategy'];
schema: AnySchema;
} | {
error: 'UndefinedError';
schema?: never;
}>;
}
/**
* The generator that converts oRPC routers/contracts to OpenAPI specifications.
*
* @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification OpenAPI Specification Docs}
*/
declare class OpenAPIGenerator {
#private;
private readonly serializer;
private readonly converter;
constructor(options?: OpenAPIGeneratorOptions);
/**
* Generates OpenAPI specifications from oRPC routers/contracts.
*
* @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification OpenAPI Specification Docs}
*/
generate(router: AnyContractRouter | AnyRouter, options?: OpenAPIGeneratorGenerateOptions): Promise<OpenAPI.Document>;
}
export { OpenAPIGenerator as b, CompositeSchemaConverter as e };
export type { ConditionalSchemaConverter as C, OpenAPIGeneratorOptions as O, SchemaConverterComponent as S, OpenAPIGeneratorGenerateOptions as a, SchemaConvertOptions as c, SchemaConverter as d };