@orpc/openapi 0.0.0-next.f4d410a → 0.0.0-next.f50512c
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/README.md +28 -22
- package/dist/adapters/aws-lambda/index.d.mts +19 -0
- package/dist/adapters/aws-lambda/index.d.ts +19 -0
- package/dist/adapters/aws-lambda/index.mjs +18 -0
- package/dist/adapters/fetch/index.d.mts +13 -9
- package/dist/adapters/fetch/index.d.ts +13 -9
- package/dist/adapters/fetch/index.mjs +14 -7
- package/dist/adapters/node/index.d.mts +13 -9
- package/dist/adapters/node/index.d.ts +13 -9
- package/dist/adapters/node/index.mjs +9 -26
- package/dist/adapters/standard/index.d.mts +7 -13
- package/dist/adapters/standard/index.d.ts +7 -13
- package/dist/adapters/standard/index.mjs +5 -3
- package/dist/index.d.mts +37 -47
- package/dist/index.d.ts +37 -47
- package/dist/index.mjs +30 -292
- package/dist/plugins/index.d.mts +69 -0
- package/dist/plugins/index.d.ts +69 -0
- package/dist/plugins/index.mjs +108 -0
- package/dist/shared/{openapi.BNHmrMe2.mjs → openapi.BkBlt1Qf.mjs} +53 -11
- package/dist/shared/openapi.BtoY8ZFF.mjs +742 -0
- package/dist/shared/openapi.CQmjvnb0.d.mts +31 -0
- package/dist/shared/openapi.CQmjvnb0.d.ts +31 -0
- package/dist/shared/openapi.DPAN3GVs.d.mts +108 -0
- package/dist/shared/openapi.DPAN3GVs.d.ts +108 -0
- package/package.json +19 -22
- package/dist/adapters/hono/index.d.mts +0 -8
- package/dist/adapters/hono/index.d.ts +0 -8
- package/dist/adapters/hono/index.mjs +0 -11
- package/dist/adapters/next/index.d.mts +0 -8
- package/dist/adapters/next/index.d.ts +0 -8
- package/dist/adapters/next/index.mjs +0 -11
- package/dist/shared/openapi.DZzpQAb-.mjs +0 -231
- package/dist/shared/openapi.Dv-KT_Bx.mjs +0 -33
- package/dist/shared/openapi.IfmmOyba.d.mts +0 -8
- package/dist/shared/openapi.IfmmOyba.d.ts +0 -8
package/dist/index.d.ts
CHANGED
|
@@ -1,15 +1,23 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
export {
|
|
4
|
-
import {
|
|
5
|
-
import { AnyRouter } from '@orpc/server';
|
|
1
|
+
import { OpenAPI, AnyContractProcedure } from '@orpc/contract';
|
|
2
|
+
export { OpenAPI } from '@orpc/contract';
|
|
3
|
+
export { e as CompositeSchemaConverter, C as ConditionalSchemaConverter, b as OpenAPIGenerator, a as OpenAPIGeneratorGenerateOptions, O as OpenAPIGeneratorOptions, c as SchemaConvertOptions, d as SchemaConverter, S as SchemaConverterComponent } from './shared/openapi.DPAN3GVs.js';
|
|
4
|
+
import { HTTPPath, HTTPMethod } from '@orpc/client';
|
|
6
5
|
import { JSONSchema } from 'json-schema-typed/draft-2020-12';
|
|
7
|
-
export { JSONSchema, Format as JSONSchemaFormat } from 'json-schema-typed/draft-2020-12';
|
|
6
|
+
export { JSONSchema, ContentEncoding as JSONSchemaContentEncoding, Format as JSONSchemaFormat, TypeName as JSONSchemaTypeName } from 'json-schema-typed/draft-2020-12';
|
|
7
|
+
import { JsonifiedClient } from '@orpc/openapi-client';
|
|
8
|
+
import { AnyRouter, ClientContext, Lazyable, CreateProcedureClientOptions, InferRouterInitialContext, Schema, ErrorMap, Meta, RouterClient } from '@orpc/server';
|
|
9
|
+
import { MaybeOptionalOptions } from '@orpc/shared';
|
|
10
|
+
import '@orpc/openapi-client/standard';
|
|
8
11
|
|
|
9
|
-
type OverrideOperationValue = Partial<
|
|
12
|
+
type OverrideOperationValue = Partial<OpenAPI.OperationObject> | ((current: OpenAPI.OperationObject, procedure: AnyContractProcedure) => OpenAPI.OperationObject);
|
|
13
|
+
/**
|
|
14
|
+
* Customize The Operation Object by proxy an error map item or a middleware.
|
|
15
|
+
*
|
|
16
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#customizing-operation-objects Customizing Operation Objects Docs}
|
|
17
|
+
*/
|
|
10
18
|
declare function customOpenAPIOperation<T extends object>(o: T, extend: OverrideOperationValue): T;
|
|
11
19
|
declare function getCustomOpenAPIOperation(o: object): OverrideOperationValue | undefined;
|
|
12
|
-
declare function applyCustomOpenAPIOperation(operation:
|
|
20
|
+
declare function applyCustomOpenAPIOperation(operation: OpenAPI.OperationObject, contract: AnyContractProcedure): OpenAPI.OperationObject;
|
|
13
21
|
|
|
14
22
|
/**
|
|
15
23
|
* @internal
|
|
@@ -29,36 +37,6 @@ type FileSchema = JSONSchema & {
|
|
|
29
37
|
*/
|
|
30
38
|
declare const LOGIC_KEYWORDS: string[];
|
|
31
39
|
|
|
32
|
-
interface SchemaConvertOptions {
|
|
33
|
-
strategy: 'input' | 'output';
|
|
34
|
-
}
|
|
35
|
-
interface SchemaConverter {
|
|
36
|
-
convert(schema: AnySchema | undefined, options: SchemaConvertOptions): [required: boolean, jsonSchema: JSONSchema];
|
|
37
|
-
}
|
|
38
|
-
interface ConditionalSchemaConverter extends SchemaConverter {
|
|
39
|
-
condition(schema: AnySchema | undefined, options: SchemaConvertOptions): boolean;
|
|
40
|
-
}
|
|
41
|
-
declare class CompositeSchemaConverter implements SchemaConverter {
|
|
42
|
-
private readonly converters;
|
|
43
|
-
constructor(converters: ConditionalSchemaConverter[]);
|
|
44
|
-
convert(schema: AnySchema | undefined, options: SchemaConvertOptions): [required: boolean, jsonSchema: JSONSchema];
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
interface OpenAPIGeneratorOptions extends StandardOpenAPIJsonSerializerOptions {
|
|
48
|
-
schemaConverters?: ConditionalSchemaConverter[];
|
|
49
|
-
}
|
|
50
|
-
declare class OpenAPIGenerator {
|
|
51
|
-
#private;
|
|
52
|
-
private readonly serializer;
|
|
53
|
-
private readonly converter;
|
|
54
|
-
constructor(options?: OpenAPIGeneratorOptions);
|
|
55
|
-
generate(router: AnyContractRouter | AnyRouter, base: Omit<OpenAPIV3_1.Document, 'openapi'>): Promise<OpenAPIV3_1.Document>;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* @internal
|
|
60
|
-
*/
|
|
61
|
-
declare function standardizeHTTPPath(path: HTTPPath): HTTPPath;
|
|
62
40
|
/**
|
|
63
41
|
* @internal
|
|
64
42
|
*/
|
|
@@ -70,19 +48,15 @@ declare function toOpenAPIMethod(method: HTTPMethod): Lowercase<HTTPMethod>;
|
|
|
70
48
|
/**
|
|
71
49
|
* @internal
|
|
72
50
|
*/
|
|
73
|
-
declare function
|
|
51
|
+
declare function toOpenAPIContent(schema: JSONSchema): Record<string, OpenAPI.MediaTypeObject>;
|
|
74
52
|
/**
|
|
75
53
|
* @internal
|
|
76
54
|
*/
|
|
77
|
-
declare function
|
|
55
|
+
declare function toOpenAPIEventIteratorContent([yieldsRequired, yieldsSchema]: [boolean, JSONSchema], [returnsRequired, returnsSchema]: [boolean, JSONSchema]): Record<string, OpenAPI.MediaTypeObject>;
|
|
78
56
|
/**
|
|
79
57
|
* @internal
|
|
80
58
|
*/
|
|
81
|
-
declare function
|
|
82
|
-
/**
|
|
83
|
-
* @internal
|
|
84
|
-
*/
|
|
85
|
-
declare function toOpenAPIParameters(schema: ObjectSchema, parameterIn: 'path' | 'query' | 'header' | 'cookie'): OpenAPIV3_1.ParameterObject[];
|
|
59
|
+
declare function toOpenAPIParameters(schema: ObjectSchema, parameterIn: 'path' | 'query' | 'header' | 'cookie'): OpenAPI.ParameterObject[];
|
|
86
60
|
/**
|
|
87
61
|
* @internal
|
|
88
62
|
*/
|
|
@@ -90,7 +64,10 @@ declare function checkParamsSchema(schema: ObjectSchema, params: string[]): bool
|
|
|
90
64
|
/**
|
|
91
65
|
* @internal
|
|
92
66
|
*/
|
|
93
|
-
declare function toOpenAPISchema(schema: JSONSchema):
|
|
67
|
+
declare function toOpenAPISchema(schema: JSONSchema): OpenAPI.SchemaObject & object;
|
|
68
|
+
declare function resolveOpenAPIJsonSchemaRef(doc: OpenAPI.Document, schema: JSONSchema): JSONSchema;
|
|
69
|
+
|
|
70
|
+
declare function createJsonifiedRouterClient<T extends AnyRouter, TClientContext extends ClientContext>(router: Lazyable<T | undefined>, ...rest: MaybeOptionalOptions<CreateProcedureClientOptions<InferRouterInitialContext<T>, Schema<unknown, unknown>, ErrorMap, Meta, TClientContext>>): JsonifiedClient<RouterClient<T, TClientContext>>;
|
|
94
71
|
|
|
95
72
|
/**
|
|
96
73
|
*@internal
|
|
@@ -112,9 +89,22 @@ declare function separateObjectSchema(schema: ObjectSchema, separatedProperties:
|
|
|
112
89
|
* @internal
|
|
113
90
|
*/
|
|
114
91
|
declare function filterSchemaBranches(schema: JSONSchema, check: (schema: JSONSchema) => boolean, matches?: JSONSchema[]): [matches: JSONSchema[], rest: JSONSchema | undefined];
|
|
92
|
+
declare function applySchemaOptionality(required: boolean, schema: JSONSchema): JSONSchema;
|
|
93
|
+
/**
|
|
94
|
+
* Takes a JSON schema and, if it's primarily a union type (anyOf, oneOf),
|
|
95
|
+
* recursively expands it into an array of its constituent, non-union base schemas.
|
|
96
|
+
* If the schema is not a simple union or is a base type, it's returned as a single-element array.
|
|
97
|
+
*/
|
|
98
|
+
declare function expandUnionSchema(schema: JSONSchema): JSONSchema[];
|
|
99
|
+
declare function expandArrayableSchema(schema: JSONSchema): undefined | [items: JSONSchema, array: JSONSchema & {
|
|
100
|
+
type: 'array';
|
|
101
|
+
items?: JSONSchema;
|
|
102
|
+
}];
|
|
103
|
+
declare function isPrimitiveSchema(schema: JSONSchema): boolean;
|
|
115
104
|
|
|
116
105
|
declare const oo: {
|
|
117
106
|
spec: typeof customOpenAPIOperation;
|
|
118
107
|
};
|
|
119
108
|
|
|
120
|
-
export {
|
|
109
|
+
export { LOGIC_KEYWORDS, applyCustomOpenAPIOperation, applySchemaOptionality, checkParamsSchema, createJsonifiedRouterClient, customOpenAPIOperation, expandArrayableSchema, expandUnionSchema, filterSchemaBranches, getCustomOpenAPIOperation, isAnySchema, isFileSchema, isObjectSchema, isPrimitiveSchema, oo, resolveOpenAPIJsonSchemaRef, separateObjectSchema, toOpenAPIContent, toOpenAPIEventIteratorContent, toOpenAPIMethod, toOpenAPIParameters, toOpenAPIPath, toOpenAPISchema };
|
|
110
|
+
export type { FileSchema, ObjectSchema, OverrideOperationValue };
|
package/dist/index.mjs
CHANGED
|
@@ -1,303 +1,41 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
import { StandardOpenAPIJsonSerializer } from '@orpc/openapi-client/standard';
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
export {
|
|
8
|
-
|
|
1
|
+
import { c as customOpenAPIOperation } from './shared/openapi.BtoY8ZFF.mjs';
|
|
2
|
+
export { C as CompositeSchemaConverter, L as LOGIC_KEYWORDS, O as OpenAPIGenerator, a as applyCustomOpenAPIOperation, n as applySchemaOptionality, h as checkParamsSchema, p as expandArrayableSchema, o as expandUnionSchema, m as filterSchemaBranches, g as getCustomOpenAPIOperation, l as isAnySchema, j as isFileSchema, k as isObjectSchema, q as isPrimitiveSchema, r as resolveOpenAPIJsonSchemaRef, s as separateObjectSchema, d as toOpenAPIContent, e as toOpenAPIEventIteratorContent, b as toOpenAPIMethod, f as toOpenAPIParameters, t as toOpenAPIPath, i as toOpenAPISchema } from './shared/openapi.BtoY8ZFF.mjs';
|
|
3
|
+
import { createORPCErrorFromJson } from '@orpc/client';
|
|
4
|
+
import { StandardOpenAPISerializer, StandardOpenAPIJsonSerializer, StandardBracketNotationSerializer } from '@orpc/openapi-client/standard';
|
|
5
|
+
import { ORPCError, createRouterClient } from '@orpc/server';
|
|
6
|
+
import { resolveMaybeOptionalOptions } from '@orpc/shared';
|
|
7
|
+
export { ContentEncoding as JSONSchemaContentEncoding, Format as JSONSchemaFormat, TypeName as JSONSchemaTypeName } from 'json-schema-typed/draft-2020-12';
|
|
8
|
+
import '@orpc/client/standard';
|
|
9
|
+
import '@orpc/contract';
|
|
9
10
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
function getCustomOpenAPIOperation(o) {
|
|
22
|
-
return o[OPERATION_EXTENDER_SYMBOL];
|
|
23
|
-
}
|
|
24
|
-
function applyCustomOpenAPIOperation(operation, contract) {
|
|
25
|
-
const operationCustoms = [];
|
|
26
|
-
for (const errorItem of Object.values(contract["~orpc"].errorMap)) {
|
|
27
|
-
const maybeExtender = errorItem ? getCustomOpenAPIOperation(errorItem) : void 0;
|
|
28
|
-
if (maybeExtender) {
|
|
29
|
-
operationCustoms.push(maybeExtender);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
if (isProcedure(contract)) {
|
|
33
|
-
for (const middleware of contract["~orpc"].middlewares) {
|
|
34
|
-
const maybeExtender = getCustomOpenAPIOperation(middleware);
|
|
35
|
-
if (maybeExtender) {
|
|
36
|
-
operationCustoms.push(maybeExtender);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
let currentOperation = operation;
|
|
41
|
-
for (const custom of operationCustoms) {
|
|
42
|
-
if (typeof custom === "function") {
|
|
43
|
-
currentOperation = custom(currentOperation, contract);
|
|
44
|
-
} else {
|
|
45
|
-
currentOperation = {
|
|
46
|
-
...currentOperation,
|
|
47
|
-
...custom
|
|
48
|
-
};
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
return currentOperation;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
class CompositeSchemaConverter {
|
|
55
|
-
converters;
|
|
56
|
-
constructor(converters) {
|
|
57
|
-
this.converters = converters;
|
|
58
|
-
}
|
|
59
|
-
convert(schema, options) {
|
|
60
|
-
for (const converter of this.converters) {
|
|
61
|
-
if (converter.condition(schema, options)) {
|
|
62
|
-
return converter.convert(schema, options);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
return [false, {}];
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
class OpenAPIGeneratorError extends Error {
|
|
70
|
-
}
|
|
71
|
-
class OpenAPIGenerator {
|
|
72
|
-
serializer;
|
|
73
|
-
converter;
|
|
74
|
-
constructor(options = {}) {
|
|
75
|
-
this.serializer = new StandardOpenAPIJsonSerializer(options);
|
|
76
|
-
this.converter = new CompositeSchemaConverter(options.schemaConverters ?? []);
|
|
77
|
-
}
|
|
78
|
-
async generate(router, base) {
|
|
79
|
-
const doc = clone(base);
|
|
80
|
-
doc.openapi = "3.1.1";
|
|
81
|
-
const errors = [];
|
|
82
|
-
await resolveContractProcedures({ path: [], router }, ({ contract, path }) => {
|
|
83
|
-
const operationId = path.join(".");
|
|
84
|
-
try {
|
|
85
|
-
const def = contract["~orpc"];
|
|
86
|
-
const method = toOpenAPIMethod(fallbackContractConfig("defaultMethod", def.route.method));
|
|
87
|
-
const httpPath = toOpenAPIPath(def.route.path ?? toHttpPath(path));
|
|
88
|
-
const operationObjectRef = {
|
|
89
|
-
operationId,
|
|
90
|
-
summary: def.route.summary,
|
|
91
|
-
description: def.route.description,
|
|
92
|
-
deprecated: def.route.deprecated,
|
|
93
|
-
tags: def.route.tags?.map((tag) => tag)
|
|
94
|
-
};
|
|
95
|
-
this.#request(operationObjectRef, def);
|
|
96
|
-
this.#successResponse(operationObjectRef, def);
|
|
97
|
-
this.#errorResponse(operationObjectRef, def);
|
|
98
|
-
doc.paths ??= {};
|
|
99
|
-
doc.paths[httpPath] ??= {};
|
|
100
|
-
doc.paths[httpPath][method] = applyCustomOpenAPIOperation(operationObjectRef, contract);
|
|
101
|
-
} catch (e) {
|
|
102
|
-
if (!(e instanceof OpenAPIGeneratorError)) {
|
|
103
|
-
throw e;
|
|
104
|
-
}
|
|
105
|
-
errors.push(
|
|
106
|
-
`[OpenAPIGenerator] Error occurred while generating OpenAPI for procedure at path: ${operationId}
|
|
107
|
-
${e.message}`
|
|
108
|
-
);
|
|
109
|
-
}
|
|
110
|
-
});
|
|
111
|
-
if (errors.length) {
|
|
112
|
-
throw new OpenAPIGeneratorError(
|
|
113
|
-
`Some error occurred during OpenAPI generation:
|
|
114
|
-
|
|
115
|
-
${errors.join("\n\n")}`
|
|
116
|
-
);
|
|
117
|
-
}
|
|
118
|
-
return this.serializer.serialize(doc)[0];
|
|
119
|
-
}
|
|
120
|
-
#request(ref, def) {
|
|
121
|
-
const method = fallbackContractConfig("defaultMethod", def.route.method);
|
|
122
|
-
const details = getEventIteratorSchemaDetails(def.inputSchema);
|
|
123
|
-
if (details) {
|
|
124
|
-
ref.requestBody = {
|
|
125
|
-
required: true,
|
|
126
|
-
content: toOpenAPIEventIteratorContent(
|
|
127
|
-
this.converter.convert(details.yields, { strategy: "input" }),
|
|
128
|
-
this.converter.convert(details.returns, { strategy: "input" })
|
|
11
|
+
function createJsonifiedRouterClient(router, ...rest) {
|
|
12
|
+
const options = resolveMaybeOptionalOptions(rest);
|
|
13
|
+
const serializer = new StandardOpenAPISerializer(new StandardOpenAPIJsonSerializer(), new StandardBracketNotationSerializer());
|
|
14
|
+
options.interceptors ??= [];
|
|
15
|
+
options.interceptors.unshift(async (options2) => {
|
|
16
|
+
try {
|
|
17
|
+
return serializer.deserialize(
|
|
18
|
+
serializer.serialize(
|
|
19
|
+
await options2.next()
|
|
129
20
|
)
|
|
130
|
-
};
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
|
-
const dynamicParams = getDynamicParams(def.route.path);
|
|
134
|
-
const inputStructure = fallbackContractConfig("defaultInputStructure", def.route.inputStructure);
|
|
135
|
-
let [required, schema] = this.converter.convert(def.inputSchema, { strategy: "input" });
|
|
136
|
-
if (isAnySchema(schema) && !dynamicParams?.length) {
|
|
137
|
-
return;
|
|
138
|
-
}
|
|
139
|
-
if (inputStructure === "compact") {
|
|
140
|
-
if (dynamicParams?.length) {
|
|
141
|
-
const error2 = new OpenAPIGeneratorError(
|
|
142
|
-
'When input structure is "compact", and path has dynamic params, input schema must be an object with all dynamic params as required.'
|
|
143
|
-
);
|
|
144
|
-
if (!isObjectSchema(schema)) {
|
|
145
|
-
throw error2;
|
|
146
|
-
}
|
|
147
|
-
const [paramsSchema, rest] = separateObjectSchema(schema, dynamicParams);
|
|
148
|
-
schema = rest;
|
|
149
|
-
required = rest.required ? rest.required.length !== 0 : false;
|
|
150
|
-
if (!checkParamsSchema(paramsSchema, dynamicParams)) {
|
|
151
|
-
throw error2;
|
|
152
|
-
}
|
|
153
|
-
ref.parameters ??= [];
|
|
154
|
-
ref.parameters.push(...toOpenAPIParameters(paramsSchema, "path"));
|
|
155
|
-
}
|
|
156
|
-
if (method === "GET") {
|
|
157
|
-
if (!isObjectSchema(schema)) {
|
|
158
|
-
throw new OpenAPIGeneratorError(
|
|
159
|
-
'When method is "GET", input schema must satisfy: object | any | unknown'
|
|
160
|
-
);
|
|
161
|
-
}
|
|
162
|
-
ref.parameters ??= [];
|
|
163
|
-
ref.parameters.push(...toOpenAPIParameters(schema, "query"));
|
|
164
|
-
} else {
|
|
165
|
-
ref.requestBody = {
|
|
166
|
-
required,
|
|
167
|
-
content: toOpenAPIContent(schema)
|
|
168
|
-
};
|
|
169
|
-
}
|
|
170
|
-
return;
|
|
171
|
-
}
|
|
172
|
-
const error = new OpenAPIGeneratorError(
|
|
173
|
-
'When input structure is "detailed", input schema must satisfy: { params?: Record<string, unknown>, query?: Record<string, unknown>, headers?: Record<string, unknown>, body?: unknown }'
|
|
174
|
-
);
|
|
175
|
-
if (!isObjectSchema(schema)) {
|
|
176
|
-
throw error;
|
|
177
|
-
}
|
|
178
|
-
if (dynamicParams?.length && (schema.properties?.params === void 0 || !isObjectSchema(schema.properties.params) || !checkParamsSchema(schema.properties.params, dynamicParams))) {
|
|
179
|
-
throw new OpenAPIGeneratorError(
|
|
180
|
-
'When input structure is "detailed" and path has dynamic params, the "params" schema must be an object with all dynamic params as required.'
|
|
181
21
|
);
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
ref.parameters ??= [];
|
|
191
|
-
ref.parameters.push(...toOpenAPIParameters(fromSchema, parameterIn));
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
if (schema.properties?.body !== void 0) {
|
|
195
|
-
ref.requestBody = {
|
|
196
|
-
required: schema.required?.includes("body"),
|
|
197
|
-
content: toOpenAPIContent(schema.properties.body)
|
|
198
|
-
};
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
#successResponse(ref, def) {
|
|
202
|
-
const outputSchema = def.outputSchema;
|
|
203
|
-
const status = fallbackContractConfig("defaultSuccessStatus", def.route.successStatus);
|
|
204
|
-
const description = fallbackContractConfig("defaultSuccessDescription", def.route?.successDescription);
|
|
205
|
-
const eventIteratorSchemaDetails = getEventIteratorSchemaDetails(outputSchema);
|
|
206
|
-
const outputStructure = fallbackContractConfig("defaultOutputStructure", def.route.outputStructure);
|
|
207
|
-
if (eventIteratorSchemaDetails) {
|
|
208
|
-
ref.responses ??= {};
|
|
209
|
-
ref.responses[status] = {
|
|
210
|
-
description,
|
|
211
|
-
content: toOpenAPIEventIteratorContent(
|
|
212
|
-
this.converter.convert(eventIteratorSchemaDetails.yields, { strategy: "output" }),
|
|
213
|
-
this.converter.convert(eventIteratorSchemaDetails.returns, { strategy: "output" })
|
|
214
|
-
)
|
|
215
|
-
};
|
|
216
|
-
return;
|
|
217
|
-
}
|
|
218
|
-
const [_, json] = this.converter.convert(outputSchema, { strategy: "output" });
|
|
219
|
-
ref.responses ??= {};
|
|
220
|
-
ref.responses[status] = {
|
|
221
|
-
description
|
|
222
|
-
};
|
|
223
|
-
if (outputStructure === "compact") {
|
|
224
|
-
ref.responses[status].content = toOpenAPIContent(json);
|
|
225
|
-
return;
|
|
226
|
-
}
|
|
227
|
-
const error = new OpenAPIGeneratorError(
|
|
228
|
-
'When output structure is "detailed", output schema must satisfy: { headers?: Record<string, unknown>, body?: unknown }'
|
|
229
|
-
);
|
|
230
|
-
if (!isObjectSchema(json)) {
|
|
231
|
-
throw error;
|
|
232
|
-
}
|
|
233
|
-
if (json.properties?.headers !== void 0) {
|
|
234
|
-
if (!isObjectSchema(json.properties.headers)) {
|
|
235
|
-
throw error;
|
|
236
|
-
}
|
|
237
|
-
for (const key in json.properties.headers.properties) {
|
|
238
|
-
ref.responses[status].headers ??= {};
|
|
239
|
-
ref.responses[status].headers[key] = {
|
|
240
|
-
schema: toOpenAPISchema(json.properties.headers.properties[key]),
|
|
241
|
-
required: json.properties.headers.required?.includes(key)
|
|
242
|
-
};
|
|
22
|
+
} catch (e) {
|
|
23
|
+
if (e instanceof ORPCError) {
|
|
24
|
+
throw createORPCErrorFromJson(serializer.deserialize(
|
|
25
|
+
serializer.serialize(
|
|
26
|
+
e.toJSON(),
|
|
27
|
+
{ outputFormat: "plain" }
|
|
28
|
+
)
|
|
29
|
+
));
|
|
243
30
|
}
|
|
31
|
+
throw e;
|
|
244
32
|
}
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
#errorResponse(ref, def) {
|
|
250
|
-
const errorMap = def.errorMap;
|
|
251
|
-
const errors = {};
|
|
252
|
-
for (const code in errorMap) {
|
|
253
|
-
const config = errorMap[code];
|
|
254
|
-
if (!config) {
|
|
255
|
-
continue;
|
|
256
|
-
}
|
|
257
|
-
const status = fallbackORPCErrorStatus(code, config.status);
|
|
258
|
-
const message = fallbackORPCErrorMessage(code, config.message);
|
|
259
|
-
const [dataRequired, dataSchema] = this.converter.convert(config.data, { strategy: "output" });
|
|
260
|
-
errors[status] ??= [];
|
|
261
|
-
errors[status].push({
|
|
262
|
-
type: "object",
|
|
263
|
-
properties: {
|
|
264
|
-
defined: { const: true },
|
|
265
|
-
code: { const: code },
|
|
266
|
-
status: { const: status },
|
|
267
|
-
message: { type: "string", default: message },
|
|
268
|
-
data: dataSchema
|
|
269
|
-
},
|
|
270
|
-
required: dataRequired ? ["defined", "code", "status", "message", "data"] : ["defined", "code", "status", "message"]
|
|
271
|
-
});
|
|
272
|
-
}
|
|
273
|
-
ref.responses ??= {};
|
|
274
|
-
for (const status in errors) {
|
|
275
|
-
const schemas = errors[status];
|
|
276
|
-
ref.responses[status] = {
|
|
277
|
-
description: status,
|
|
278
|
-
content: toOpenAPIContent({
|
|
279
|
-
oneOf: [
|
|
280
|
-
...schemas,
|
|
281
|
-
{
|
|
282
|
-
type: "object",
|
|
283
|
-
properties: {
|
|
284
|
-
defined: { const: false },
|
|
285
|
-
code: { type: "string" },
|
|
286
|
-
status: { type: "number" },
|
|
287
|
-
message: { type: "string" },
|
|
288
|
-
data: {}
|
|
289
|
-
},
|
|
290
|
-
required: ["defined", "code", "status", "message"]
|
|
291
|
-
}
|
|
292
|
-
]
|
|
293
|
-
})
|
|
294
|
-
};
|
|
295
|
-
}
|
|
296
|
-
}
|
|
33
|
+
});
|
|
34
|
+
return createRouterClient(router, options);
|
|
297
35
|
}
|
|
298
36
|
|
|
299
37
|
const oo = {
|
|
300
38
|
spec: customOpenAPIOperation
|
|
301
39
|
};
|
|
302
40
|
|
|
303
|
-
export {
|
|
41
|
+
export { createJsonifiedRouterClient, customOpenAPIOperation, oo };
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { OpenAPI } from '@orpc/contract';
|
|
2
|
+
import { Context, HTTPPath, Router } from '@orpc/server';
|
|
3
|
+
import { StandardHandlerInterceptorOptions, StandardHandlerPlugin, StandardHandlerOptions } from '@orpc/server/standard';
|
|
4
|
+
import { Value, Promisable } from '@orpc/shared';
|
|
5
|
+
import { O as OpenAPIGeneratorOptions, a as OpenAPIGeneratorGenerateOptions } from '../shared/openapi.DPAN3GVs.mjs';
|
|
6
|
+
import '@orpc/openapi-client/standard';
|
|
7
|
+
import 'json-schema-typed/draft-2020-12';
|
|
8
|
+
|
|
9
|
+
interface OpenAPIReferencePluginOptions<T extends Context> extends OpenAPIGeneratorOptions {
|
|
10
|
+
/**
|
|
11
|
+
* Options to pass to the OpenAPI generate.
|
|
12
|
+
*
|
|
13
|
+
*/
|
|
14
|
+
specGenerateOptions?: Value<Promisable<OpenAPIGeneratorGenerateOptions>, [StandardHandlerInterceptorOptions<T>]>;
|
|
15
|
+
/**
|
|
16
|
+
* The URL path at which to serve the OpenAPI JSON.
|
|
17
|
+
*
|
|
18
|
+
* @default '/spec.json'
|
|
19
|
+
*/
|
|
20
|
+
specPath?: HTTPPath;
|
|
21
|
+
/**
|
|
22
|
+
* The URL path at which to serve the API reference UI.
|
|
23
|
+
*
|
|
24
|
+
* @default '/'
|
|
25
|
+
*/
|
|
26
|
+
docsPath?: HTTPPath;
|
|
27
|
+
/**
|
|
28
|
+
* The document title for the API reference UI.
|
|
29
|
+
*
|
|
30
|
+
* @default 'API Reference'
|
|
31
|
+
*/
|
|
32
|
+
docsTitle?: Value<Promisable<string>, [StandardHandlerInterceptorOptions<T>]>;
|
|
33
|
+
/**
|
|
34
|
+
* Arbitrary configuration object for the UI.
|
|
35
|
+
*/
|
|
36
|
+
docsConfig?: Value<Promisable<Record<string, unknown>>, [StandardHandlerInterceptorOptions<T>]>;
|
|
37
|
+
/**
|
|
38
|
+
* HTML to inject into the <head> of the docs page.
|
|
39
|
+
*
|
|
40
|
+
* @default ''
|
|
41
|
+
*/
|
|
42
|
+
docsHead?: Value<Promisable<string>, [StandardHandlerInterceptorOptions<T>]>;
|
|
43
|
+
/**
|
|
44
|
+
* URL of the external script bundle for the reference UI.
|
|
45
|
+
*
|
|
46
|
+
* @default 'https://cdn.jsdelivr.net/npm/@scalar/api-reference'
|
|
47
|
+
*/
|
|
48
|
+
docsScriptUrl?: Value<Promisable<string>, [StandardHandlerInterceptorOptions<T>]>;
|
|
49
|
+
/**
|
|
50
|
+
* Override function to generate the full HTML for the docs page.
|
|
51
|
+
*/
|
|
52
|
+
renderDocsHtml?: (specUrl: string, title: string, head: string, scriptUrl: string, config: Record<string, unknown> | undefined, spec: OpenAPI.Document) => string;
|
|
53
|
+
}
|
|
54
|
+
declare class OpenAPIReferencePlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
|
55
|
+
private readonly generator;
|
|
56
|
+
private readonly specGenerateOptions;
|
|
57
|
+
private readonly specPath;
|
|
58
|
+
private readonly docsPath;
|
|
59
|
+
private readonly docsTitle;
|
|
60
|
+
private readonly docsHead;
|
|
61
|
+
private readonly docsScriptUrl;
|
|
62
|
+
private readonly docsConfig;
|
|
63
|
+
private readonly renderDocsHtml;
|
|
64
|
+
constructor(options?: OpenAPIReferencePluginOptions<T>);
|
|
65
|
+
init(options: StandardHandlerOptions<T>, router: Router<any, T>): void;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export { OpenAPIReferencePlugin };
|
|
69
|
+
export type { OpenAPIReferencePluginOptions };
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { OpenAPI } from '@orpc/contract';
|
|
2
|
+
import { Context, HTTPPath, Router } from '@orpc/server';
|
|
3
|
+
import { StandardHandlerInterceptorOptions, StandardHandlerPlugin, StandardHandlerOptions } from '@orpc/server/standard';
|
|
4
|
+
import { Value, Promisable } from '@orpc/shared';
|
|
5
|
+
import { O as OpenAPIGeneratorOptions, a as OpenAPIGeneratorGenerateOptions } from '../shared/openapi.DPAN3GVs.js';
|
|
6
|
+
import '@orpc/openapi-client/standard';
|
|
7
|
+
import 'json-schema-typed/draft-2020-12';
|
|
8
|
+
|
|
9
|
+
interface OpenAPIReferencePluginOptions<T extends Context> extends OpenAPIGeneratorOptions {
|
|
10
|
+
/**
|
|
11
|
+
* Options to pass to the OpenAPI generate.
|
|
12
|
+
*
|
|
13
|
+
*/
|
|
14
|
+
specGenerateOptions?: Value<Promisable<OpenAPIGeneratorGenerateOptions>, [StandardHandlerInterceptorOptions<T>]>;
|
|
15
|
+
/**
|
|
16
|
+
* The URL path at which to serve the OpenAPI JSON.
|
|
17
|
+
*
|
|
18
|
+
* @default '/spec.json'
|
|
19
|
+
*/
|
|
20
|
+
specPath?: HTTPPath;
|
|
21
|
+
/**
|
|
22
|
+
* The URL path at which to serve the API reference UI.
|
|
23
|
+
*
|
|
24
|
+
* @default '/'
|
|
25
|
+
*/
|
|
26
|
+
docsPath?: HTTPPath;
|
|
27
|
+
/**
|
|
28
|
+
* The document title for the API reference UI.
|
|
29
|
+
*
|
|
30
|
+
* @default 'API Reference'
|
|
31
|
+
*/
|
|
32
|
+
docsTitle?: Value<Promisable<string>, [StandardHandlerInterceptorOptions<T>]>;
|
|
33
|
+
/**
|
|
34
|
+
* Arbitrary configuration object for the UI.
|
|
35
|
+
*/
|
|
36
|
+
docsConfig?: Value<Promisable<Record<string, unknown>>, [StandardHandlerInterceptorOptions<T>]>;
|
|
37
|
+
/**
|
|
38
|
+
* HTML to inject into the <head> of the docs page.
|
|
39
|
+
*
|
|
40
|
+
* @default ''
|
|
41
|
+
*/
|
|
42
|
+
docsHead?: Value<Promisable<string>, [StandardHandlerInterceptorOptions<T>]>;
|
|
43
|
+
/**
|
|
44
|
+
* URL of the external script bundle for the reference UI.
|
|
45
|
+
*
|
|
46
|
+
* @default 'https://cdn.jsdelivr.net/npm/@scalar/api-reference'
|
|
47
|
+
*/
|
|
48
|
+
docsScriptUrl?: Value<Promisable<string>, [StandardHandlerInterceptorOptions<T>]>;
|
|
49
|
+
/**
|
|
50
|
+
* Override function to generate the full HTML for the docs page.
|
|
51
|
+
*/
|
|
52
|
+
renderDocsHtml?: (specUrl: string, title: string, head: string, scriptUrl: string, config: Record<string, unknown> | undefined, spec: OpenAPI.Document) => string;
|
|
53
|
+
}
|
|
54
|
+
declare class OpenAPIReferencePlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
|
55
|
+
private readonly generator;
|
|
56
|
+
private readonly specGenerateOptions;
|
|
57
|
+
private readonly specPath;
|
|
58
|
+
private readonly docsPath;
|
|
59
|
+
private readonly docsTitle;
|
|
60
|
+
private readonly docsHead;
|
|
61
|
+
private readonly docsScriptUrl;
|
|
62
|
+
private readonly docsConfig;
|
|
63
|
+
private readonly renderDocsHtml;
|
|
64
|
+
constructor(options?: OpenAPIReferencePluginOptions<T>);
|
|
65
|
+
init(options: StandardHandlerOptions<T>, router: Router<any, T>): void;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export { OpenAPIReferencePlugin };
|
|
69
|
+
export type { OpenAPIReferencePluginOptions };
|