@orpc/openapi 2.0.0-beta.49 → 2.0.0-beta.50
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapters/aws-lambda/index.d.ts +3 -15
- package/dist/adapters/aws-lambda/index.js +1 -1
- package/dist/adapters/aws-lambda/openapi-handler.d.ts +8 -9
- package/dist/adapters/aws-lambda/openapi-handler.js +19 -17
- package/dist/adapters/fastify/index.d.ts +2 -1
- package/dist/adapters/fastify/index.js +1 -1
- package/dist/adapters/fastify/openapi-handler.d.ts +8 -9
- package/dist/adapters/fastify/openapi-handler.js +16 -14
- package/dist/adapters/fetch/index.d.ts +3 -2
- package/dist/adapters/fetch/index.js +2 -2
- package/dist/adapters/fetch/openapi-handler.d.ts +8 -9
- package/dist/adapters/fetch/openapi-handler.js +16 -14
- package/dist/adapters/fetch/openapi-link.d.ts +9 -10
- package/dist/adapters/fetch/openapi-link.js +21 -19
- package/dist/adapters/node/index.d.ts +2 -1
- package/dist/adapters/node/index.js +1 -1
- package/dist/adapters/node/openapi-handler.d.ts +8 -9
- package/dist/adapters/node/openapi-handler.js +16 -14
- package/dist/adapters/standard/index.d.ts +4 -3
- package/dist/adapters/standard/index.js +3 -3
- package/dist/adapters/standard/openapi-handler-codec.d.ts +56 -57
- package/dist/adapters/standard/openapi-handler-codec.js +143 -205
- package/dist/adapters/standard/openapi-link-codec.d.ts +55 -59
- package/dist/adapters/standard/openapi-link-codec.js +262 -377
- package/dist/adapters/standard/openapi-matcher.d.ts +26 -26
- package/dist/adapters/standard/openapi-matcher.js +78 -108
- package/dist/adapters/standard/utils.js +28 -44
- package/dist/bracket-notation.d.ts +22 -22
- package/dist/bracket-notation.js +123 -173
- package/dist/client-factory.d.ts +12 -12
- package/dist/client-factory.js +13 -11
- package/dist/constants.d.ts +1 -4
- package/dist/constants.js +5 -4
- package/dist/extensions/route.d.ts +36 -36
- package/dist/extensions/route.js +7 -6
- package/dist/helpers/form-data.d.ts +1 -1
- package/dist/helpers/form-data.js +74 -86
- package/dist/helpers/index.d.ts +2 -1
- package/dist/helpers/index.js +1 -1
- package/dist/index.d.ts +15 -13
- package/dist/index.js +11 -11
- package/dist/meta.d.ts +298 -298
- package/dist/meta.js +54 -72
- package/dist/openapi-generator-components.d.ts +2 -31
- package/dist/openapi-generator-components.js +200 -248
- package/dist/openapi-generator-operation.d.ts +12 -33
- package/dist/openapi-generator-operation.js +369 -437
- package/dist/openapi-generator.d.ts +75 -80
- package/dist/openapi-generator.js +114 -142
- package/dist/openapi-json-serializer.d.ts +65 -65
- package/dist/openapi-json-serializer.js +218 -257
- package/dist/openapi-serializer.d.ts +31 -32
- package/dist/openapi-serializer.js +68 -92
- package/dist/plugins/index.d.ts +2 -1
- package/dist/plugins/index.js +1 -1
- package/dist/plugins/openapi-reference.d.ts +99 -102
- package/dist/plugins/openapi-reference.js +109 -132
- package/dist/router-utils.d.ts +7 -10
- package/dist/router-utils.js +30 -34
- package/dist/types.d.ts +8 -12
- package/dist/utils.d.ts +5 -10
- package/dist/utils.js +39 -42
- package/package.json +12 -12
- package/dist/adapters/standard/utils.d.ts +0 -4
- package/dist/types.js +0 -1
|
@@ -1,80 +1,74 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import {
|
|
7
|
-
import
|
|
8
|
-
export { OpenAPIGeneratorError } from './openapi-generator-operation.ts';
|
|
9
|
-
export type { OpenAPIErrorBodyDefinition } from './openapi-generator-operation.ts';
|
|
1
|
+
import { OpenAPIErrorBodyDefinition, OpenAPIGeneratorError } from "./openapi-generator-operation.js";
|
|
2
|
+
import { OpenAPIJsonSerializer } from "./openapi-json-serializer.js";
|
|
3
|
+
import { OpenAPIDocument, OpenAPIV3_2, OpenAPIVersion } from "./types.js";
|
|
4
|
+
import { Public, Value } from "@orpc/shared";
|
|
5
|
+
import { AnyProcedureContract, RouterContract } from "@orpc/contract";
|
|
6
|
+
import { JsonSchema, JsonSchemaConverter } from "@orpc/json-schema";
|
|
7
|
+
import { AnyProcedure, AnyRouter } from "@orpc/server";
|
|
10
8
|
export interface OpenAPIGeneratorOptions {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
9
|
+
converters?: JsonSchemaConverter[] | undefined;
|
|
10
|
+
/**
|
|
11
|
+
* The serializer used to serialize the generated OpenAPI documentation
|
|
12
|
+
*/
|
|
13
|
+
serializer?: Public<OpenAPIJsonSerializer> | undefined;
|
|
16
14
|
}
|
|
17
15
|
export interface OpenAPIGeneratorGenerateOptions<TVersion extends OpenAPIVersion> {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
*
|
|
75
|
-
* @default COMMON_ERROR_STATUS_MAP
|
|
76
|
-
*/
|
|
77
|
-
errorStatusMap?: Record<string, number> | undefined;
|
|
16
|
+
/**
|
|
17
|
+
* The OpenAPI version of the generated document.
|
|
18
|
+
* The document is generated as OpenAPI 3.2 and downgraded when an older version is requested.
|
|
19
|
+
* The minor version selects the conversion, the document carries the exact value, such as `3.1.0`.
|
|
20
|
+
*
|
|
21
|
+
* @default '3.2.0'
|
|
22
|
+
*/
|
|
23
|
+
version?: TVersion | undefined;
|
|
24
|
+
/**
|
|
25
|
+
* OpenAPI 3.2 document fields to start from, such as `info`, `servers`, or `components`.
|
|
26
|
+
* They are downgraded with the rest of the document when an older `version` is requested.
|
|
27
|
+
* The `openapi` field is derived from `version`.
|
|
28
|
+
*/
|
|
29
|
+
base?: Partial<Omit<OpenAPIV3_2.OpenAPIObject, "openapi">> | undefined;
|
|
30
|
+
/**
|
|
31
|
+
* Root-level `$defs` are always moved into `components.schemas`.
|
|
32
|
+
* Use this to customize the component name of a hoisted def.
|
|
33
|
+
*
|
|
34
|
+
* @remarks
|
|
35
|
+
* - Characters OpenAPI does not allow in component keys (anything but `a-z`, `A-Z`, `0-9`, `.`, `_`, `-`)
|
|
36
|
+
* are replaced by `_` in the returned name, e.g. `User Profile` becomes `User_Profile`.
|
|
37
|
+
* - The returned name is a preference, conflicting names are still postfixed (`Planet`, `PlanetInput`, `Planet2`, ...).
|
|
38
|
+
*
|
|
39
|
+
* @default defName => defName
|
|
40
|
+
*/
|
|
41
|
+
componentName?: ((defName: string, defSchema: JsonSchema) => string) | undefined;
|
|
42
|
+
/**
|
|
43
|
+
* Filter procedures. Return `false` to exclude a procedure from the OpenAPI specification.
|
|
44
|
+
*
|
|
45
|
+
* @default true
|
|
46
|
+
*/
|
|
47
|
+
filter?: Value<boolean, [contract: AnyProcedureContract | AnyProcedure, path: string[]]> | undefined;
|
|
48
|
+
/**
|
|
49
|
+
* Define a custom JSON schema for the error response body when using
|
|
50
|
+
* type-safe errors. Helps align ORPC error formatting with existing API
|
|
51
|
+
* response standards or conventions.
|
|
52
|
+
*
|
|
53
|
+
* @remarks
|
|
54
|
+
* - Return `defaultSchema()` to keep the default schema, or `null | undefined` to document an empty body, matching an `errorResponseBodyEncoder` that returns `undefined`.
|
|
55
|
+
* - The schema is an OpenAPI 3.2 Schema Object, it is downgraded with the rest of the document when an older `version` is requested.
|
|
56
|
+
*/
|
|
57
|
+
errorResponseBodySchema?: Value<JsonSchema | null | undefined, [definedErrors: OpenAPIErrorBodyDefinition[], status: number, defaultSchema: () => JsonSchema]> | undefined;
|
|
58
|
+
/**
|
|
59
|
+
* Media type of error responses in the generated document, e.g. `application/problem+json`.
|
|
60
|
+
* Should match the handler's `errorResponseContentType`.
|
|
61
|
+
*
|
|
62
|
+
* @default 'application/json'
|
|
63
|
+
*/
|
|
64
|
+
errorResponseContentType?: string | undefined;
|
|
65
|
+
/**
|
|
66
|
+
* Mapping ORPCError Code -> HTTP Status Code
|
|
67
|
+
* Codes missing from the map fall back to `DEFAULT_ERROR_STATUS` (400).
|
|
68
|
+
*
|
|
69
|
+
* @default COMMON_ERROR_STATUS_MAP
|
|
70
|
+
*/
|
|
71
|
+
errorStatusMap?: Record<string, number> | undefined;
|
|
78
72
|
}
|
|
79
73
|
/**
|
|
80
74
|
* Generates an OpenAPI document from a contract or router.
|
|
@@ -83,10 +77,11 @@ export interface OpenAPIGeneratorGenerateOptions<TVersion extends OpenAPIVersion
|
|
|
83
77
|
* @see {@link https://orpc.dev/docs/openapi/specification#openapi-generator | OpenAPI Specification - OpenAPI Generator}
|
|
84
78
|
*/
|
|
85
79
|
export declare class OpenAPIGenerator {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
80
|
+
private readonly serializer;
|
|
81
|
+
private readonly converter;
|
|
82
|
+
constructor(options?: OpenAPIGeneratorOptions);
|
|
83
|
+
generate<TVersion extends OpenAPIVersion = "3.2.0">(router: RouterContract | AnyRouter, options?: OpenAPIGeneratorGenerateOptions<TVersion>): Promise<OpenAPIDocument<TVersion>>;
|
|
84
|
+
private convertSchema;
|
|
85
|
+
private convertSchemas;
|
|
92
86
|
}
|
|
87
|
+
export { type OpenAPIErrorBodyDefinition, OpenAPIGeneratorError };
|
|
@@ -1,147 +1,119 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
|
|
1
|
+
import "./constants.js";
|
|
2
|
+
import { getOpenAPIMeta } from "./meta.js";
|
|
3
|
+
import { OpenAPIComponentRegistry } from "./openapi-generator-components.js";
|
|
4
|
+
import { OpenAPIGeneratorError, buildErrorResponse, buildRequest, buildSuccessResponse, toOpenAPIPath } from "./openapi-generator-operation.js";
|
|
5
|
+
import { OpenAPIJsonSerializer } from "./openapi-json-serializer.js";
|
|
6
|
+
import { getDynamicPathParams } from "./utils.js";
|
|
7
|
+
import { clone, mergeHttpPath, pathToHttpPath, toArray, value } from "@orpc/shared";
|
|
8
|
+
import { downgradeSpecV31ToV30, downgradeSpecV32ToV31 } from "@openapi-spec/downgrader";
|
|
9
|
+
import { COMMON_ERROR_STATUS_MAP } from "@orpc/client";
|
|
10
|
+
import { DelegatingJsonSchemaConverter, StandardJsonSchemaConverter, combineJsonSchemasWithComposition, hoistRecursiveRefToDef } from "@orpc/json-schema";
|
|
11
|
+
import { walkProcedureContractsAsync } from "@orpc/server";
|
|
12
|
+
// src/openapi-generator.ts
|
|
13
13
|
/**
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
});
|
|
109
|
-
if (errors.length) {
|
|
110
|
-
throw new OpenAPIGeneratorError(`[OpenAPIGenerator] Failed to generate the OpenAPI document (${errors.length} error${errors.length === 1 ? '' : 's'}):\n\n${errors.join('\n\n')}`);
|
|
111
|
-
}
|
|
112
|
-
// serialize first so the downgraders work on JSON values, e.g. `Infinity` as `null`
|
|
113
|
-
const serialized = this.serializer.serializeJson(doc).json;
|
|
114
|
-
return toVersionedOpenAPIDocument(serialized, version);
|
|
115
|
-
}
|
|
116
|
-
convertSchema(schema, direction) {
|
|
117
|
-
const [jsonSchema, optional] = this.converter.convert(schema, direction);
|
|
118
|
-
// composing or hoisting would retarget root-relative refs (`#`, `#/properties/...`),
|
|
119
|
-
// so move a schema that points into itself under a def first
|
|
120
|
-
return [hoistRecursiveRefToDef(strip$schemaField(jsonSchema)), optional];
|
|
121
|
-
}
|
|
122
|
-
convertSchemas(schemas, direction) {
|
|
123
|
-
if (!schemas || schemas.length <= 1) {
|
|
124
|
-
return this.convertSchema(schemas?.[0], direction);
|
|
125
|
-
}
|
|
126
|
-
const results = schemas.map((s) => this.convertSchema(s, direction));
|
|
127
|
-
return [
|
|
128
|
-
combineJsonSchemasWithComposition('allOf', results.map(([jsonSchema]) => jsonSchema)),
|
|
129
|
-
results.every(([, optional]) => optional),
|
|
130
|
-
];
|
|
131
|
-
}
|
|
132
|
-
}
|
|
14
|
+
* Generates an OpenAPI document from a contract or router.
|
|
15
|
+
* Relies on JSON schema converters to translate input, output, and error schemas into JSON Schemas.
|
|
16
|
+
*
|
|
17
|
+
* @see {@link https://orpc.dev/docs/openapi/specification#openapi-generator | OpenAPI Specification - OpenAPI Generator}
|
|
18
|
+
*/
|
|
19
|
+
var OpenAPIGenerator = class {
|
|
20
|
+
serializer;
|
|
21
|
+
converter;
|
|
22
|
+
constructor(options = {}) {
|
|
23
|
+
this.serializer = options.serializer ?? new OpenAPIJsonSerializer();
|
|
24
|
+
this.converter = new DelegatingJsonSchemaConverter([...toArray(options.converters), new StandardJsonSchemaConverter()]);
|
|
25
|
+
}
|
|
26
|
+
async generate(router, options = {}) {
|
|
27
|
+
const version = options.version ?? "3.2.0";
|
|
28
|
+
const doc = {
|
|
29
|
+
...clone(options.base),
|
|
30
|
+
openapi: "3.2.0",
|
|
31
|
+
info: options.base?.info ?? {
|
|
32
|
+
title: "API Reference",
|
|
33
|
+
version: "0.0.0"
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
const ctx = {
|
|
37
|
+
registry: new OpenAPIComponentRegistry(doc, options.componentName),
|
|
38
|
+
convertSchemas: (schemas, direction) => this.convertSchemas(schemas, direction),
|
|
39
|
+
errorStatusMap: options.errorStatusMap ?? COMMON_ERROR_STATUS_MAP,
|
|
40
|
+
errorResponseBodySchema: options.errorResponseBodySchema,
|
|
41
|
+
errorResponseContentType: options.errorResponseContentType ?? "application/json"
|
|
42
|
+
};
|
|
43
|
+
const errors = [];
|
|
44
|
+
const routeOwners = /* @__PURE__ */ new Map();
|
|
45
|
+
const operationIdOwners = /* @__PURE__ */ new Map();
|
|
46
|
+
await walkProcedureContractsAsync(router, (contract, path) => {
|
|
47
|
+
if (value(options.filter, contract, path) === false) return;
|
|
48
|
+
const procedurePath = path.length ? path.join(".") : "(root)";
|
|
49
|
+
try {
|
|
50
|
+
const def = contract["~orpc"];
|
|
51
|
+
const meta = getOpenAPIMeta(contract);
|
|
52
|
+
const method = (meta?.method ?? "POST").toLowerCase();
|
|
53
|
+
if (method === "query" && !version.startsWith("3.2.")) throw new OpenAPIGeneratorError(`QUERY operations require OpenAPI 3.2, but version "${version}" was requested.`);
|
|
54
|
+
const postPath = meta?.path ?? pathToHttpPath(path);
|
|
55
|
+
const httpPath = meta?.prefix ? mergeHttpPath(meta.prefix, postPath) : postPath;
|
|
56
|
+
const dynamicPathParams = getDynamicPathParams(httpPath);
|
|
57
|
+
const openApiPath = toOpenAPIPath(httpPath, dynamicPathParams);
|
|
58
|
+
let operation;
|
|
59
|
+
if (meta?.spec !== void 0 && typeof meta.spec !== "function") operation = meta.spec;
|
|
60
|
+
else {
|
|
61
|
+
operation = {
|
|
62
|
+
operationId: meta?.operationId ?? path.join("."),
|
|
63
|
+
summary: meta?.summary,
|
|
64
|
+
description: meta?.description,
|
|
65
|
+
deprecated: meta?.deprecated,
|
|
66
|
+
tags: meta?.tags?.map((tag) => tag)
|
|
67
|
+
};
|
|
68
|
+
buildRequest(ctx, operation, def, meta, dynamicPathParams);
|
|
69
|
+
buildSuccessResponse(ctx, operation, def, meta);
|
|
70
|
+
buildErrorResponse(ctx, operation, def);
|
|
71
|
+
}
|
|
72
|
+
if (typeof meta?.spec === "function") operation = meta.spec(operation);
|
|
73
|
+
const routeKey = `${method} ${toOpenAPIPath(httpPath, dynamicPathParams?.map((p) => ({
|
|
74
|
+
...p,
|
|
75
|
+
parameterName: ""
|
|
76
|
+
})))}`;
|
|
77
|
+
const routeOwner = routeOwners.get(routeKey);
|
|
78
|
+
if (routeOwner !== void 0) throw new OpenAPIGeneratorError(`${method.toUpperCase()} ${openApiPath} is already documented by the procedure at ${routeOwner}.\n OpenAPI allows one operation per method and path (path param names are ignored).\n Fix: change the method or path of one of them.`);
|
|
79
|
+
const { operationId } = operation;
|
|
80
|
+
if (operationId !== void 0) {
|
|
81
|
+
const operationIdOwner = operationIdOwners.get(operationId);
|
|
82
|
+
if (operationIdOwner !== void 0) throw new OpenAPIGeneratorError(`operationId "${operationId}" is already used by the procedure at ${operationIdOwner}.\n OpenAPI requires operationIds to be unique. Fix: give one of them a different operationId.`);
|
|
83
|
+
operationIdOwners.set(operationId, procedurePath);
|
|
84
|
+
}
|
|
85
|
+
routeOwners.set(routeKey, procedurePath);
|
|
86
|
+
doc.paths ??= {};
|
|
87
|
+
doc.paths[openApiPath] ??= {};
|
|
88
|
+
doc.paths[openApiPath][method] = operation;
|
|
89
|
+
} catch (e) {
|
|
90
|
+
if (!(e instanceof OpenAPIGeneratorError)) throw e;
|
|
91
|
+
errors.push(`Procedure at ${procedurePath}: ${e.message}`);
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
if (errors.length) throw new OpenAPIGeneratorError(`[OpenAPIGenerator] Failed to generate the OpenAPI document (${errors.length} error${errors.length === 1 ? "" : "s"}):\n\n${errors.join("\n\n")}`);
|
|
95
|
+
const serialized = this.serializer.serializeJson(doc).json;
|
|
96
|
+
return toVersionedOpenAPIDocument(serialized, version);
|
|
97
|
+
}
|
|
98
|
+
convertSchema(schema, direction) {
|
|
99
|
+
const [jsonSchema, optional] = this.converter.convert(schema, direction);
|
|
100
|
+
return [hoistRecursiveRefToDef(strip$schemaField(jsonSchema)), optional];
|
|
101
|
+
}
|
|
102
|
+
convertSchemas(schemas, direction) {
|
|
103
|
+
if (!schemas || schemas.length <= 1) return this.convertSchema(schemas?.[0], direction);
|
|
104
|
+
const results = schemas.map((s) => this.convertSchema(s, direction));
|
|
105
|
+
return [combineJsonSchemasWithComposition("allOf", results.map(([jsonSchema]) => jsonSchema)), results.every(([, optional]) => optional)];
|
|
106
|
+
}
|
|
107
|
+
};
|
|
133
108
|
function toVersionedOpenAPIDocument(doc, version) {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
: downgradeSpecV31ToV30(downgradeSpecV32ToV31(doc));
|
|
139
|
-
return { ...downgraded, openapi: version };
|
|
109
|
+
return {
|
|
110
|
+
...version.startsWith("3.2.") ? doc : version.startsWith("3.1.") ? downgradeSpecV32ToV31(doc) : downgradeSpecV31ToV30(downgradeSpecV32ToV31(doc)),
|
|
111
|
+
openapi: version
|
|
112
|
+
};
|
|
140
113
|
}
|
|
141
114
|
function strip$schemaField(schema) {
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
const { $schema: _schema, ...rest } = schema;
|
|
146
|
-
return rest;
|
|
115
|
+
if (typeof schema !== "object") return schema;
|
|
116
|
+
const { $schema: _schema, ...rest } = schema;
|
|
117
|
+
return rest;
|
|
147
118
|
}
|
|
119
|
+
export { OpenAPIGenerator, OpenAPIGeneratorError };
|
|
@@ -1,63 +1,63 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { Segment } from "@orpc/shared";
|
|
2
2
|
export type OpenAPIJsonSerialization = {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
json: unknown;
|
|
4
|
+
maps?: undefined;
|
|
5
|
+
blobs?: undefined;
|
|
6
6
|
} | {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
json: unknown;
|
|
8
|
+
maps: Segment[][];
|
|
9
|
+
blobs: Blob[];
|
|
10
10
|
};
|
|
11
11
|
export interface OpenAPIJsonSerializerHandler {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
12
|
+
condition(value: unknown): boolean;
|
|
13
|
+
serialize(value: any): unknown;
|
|
14
|
+
/**
|
|
15
|
+
* If false, the result of this serializer will not be further processed by other serializers,
|
|
16
|
+
* even if it matches their conditions and treat it as final serialized value.
|
|
17
|
+
* This can be useful for serializers that return primitive values, which should not be further processed.
|
|
18
|
+
* to improve performance and avoid potential issues with other serializers.
|
|
19
|
+
*
|
|
20
|
+
* @default false
|
|
21
|
+
*/
|
|
22
|
+
isTerminal?: boolean | undefined;
|
|
23
23
|
}
|
|
24
24
|
export interface OpenAPIJsonSerializerOptions {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
25
|
+
/**
|
|
26
|
+
* Extend or override the built-in type handlers used during serialization.
|
|
27
|
+
*
|
|
28
|
+
* Each key is a unique type identifier (e.g. `"date"`, `"bigint"`) and maps to a handler
|
|
29
|
+
* that defines how to detect and serialize values of that type.
|
|
30
|
+
*
|
|
31
|
+
* **Extending:** Add new keys to support custom types:
|
|
32
|
+
* ```ts
|
|
33
|
+
* handlers: {
|
|
34
|
+
* buffer: {
|
|
35
|
+
* condition: (v) => v instanceof Buffer,
|
|
36
|
+
* serialize: (v: Buffer) => v.toString('base64'),
|
|
37
|
+
* isTerminal: true,
|
|
38
|
+
* }
|
|
39
|
+
* }
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* **Overriding:** Use an existing key to replace a built-in handler:
|
|
43
|
+
* ```ts
|
|
44
|
+
* handlers: {
|
|
45
|
+
* date: {
|
|
46
|
+
* condition: (v) => v instanceof Date,
|
|
47
|
+
* serialize: (v: Date) => v.getTime(),
|
|
48
|
+
* isTerminal: true,
|
|
49
|
+
* }
|
|
50
|
+
* }
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* **Disabling:** Set a key to `undefined` to remove a built-in handler:
|
|
54
|
+
* ```ts
|
|
55
|
+
* handlers: { url: undefined }
|
|
56
|
+
* ```
|
|
57
|
+
*
|
|
58
|
+
* Built-in type keys: `undefined`, `bigint`, `date`, `nan`, `infinity`, `url`, `set`, `map`.
|
|
59
|
+
*/
|
|
60
|
+
handlers?: Record<string, undefined | OpenAPIJsonSerializerHandler> | undefined;
|
|
61
61
|
}
|
|
62
62
|
/**
|
|
63
63
|
* The JSON layer behind `OpenAPISerializer`: one-way serialization of types like `Date`, `BigInt`,
|
|
@@ -66,14 +66,14 @@ export interface OpenAPIJsonSerializerOptions {
|
|
|
66
66
|
* @see {@link https://orpc.dev/docs/openapi/serializer#openapi-json-serializer | OpenAPI Serializer - OpenAPI JSON Serializer}
|
|
67
67
|
*/
|
|
68
68
|
export declare class OpenAPIJsonSerializer {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
}
|
|
69
|
+
private readonly inlineBuiltInHandlers;
|
|
70
|
+
private readonly handlerEntries;
|
|
71
|
+
constructor(options?: OpenAPIJsonSerializerOptions);
|
|
72
|
+
serializeJson(data: unknown): OpenAPIJsonSerialization;
|
|
73
|
+
/**
|
|
74
|
+
* `segments` is a shared mutable stack (push/pop while walking),
|
|
75
|
+
* so it must be copied before being stored in `maps`.
|
|
76
|
+
*/
|
|
77
|
+
private serializeValue;
|
|
78
|
+
deserializeJson(serialized: OpenAPIJsonSerialization): unknown;
|
|
79
|
+
}
|