@orpc/openapi 0.0.0-next.e7ee5a9 → 0.0.0-next.e8524dc
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 -20
- package/dist/adapters/aws-lambda/index.d.mts +17 -0
- package/dist/adapters/aws-lambda/index.d.ts +17 -0
- package/dist/adapters/aws-lambda/index.mjs +18 -0
- package/dist/adapters/fetch/index.d.mts +12 -9
- package/dist/adapters/fetch/index.d.ts +12 -9
- package/dist/adapters/fetch/index.mjs +14 -6
- package/dist/adapters/node/index.d.mts +12 -9
- package/dist/adapters/node/index.d.ts +12 -9
- package/dist/adapters/node/index.mjs +9 -20
- package/dist/adapters/standard/index.d.mts +22 -11
- package/dist/adapters/standard/index.d.ts +22 -11
- package/dist/adapters/standard/index.mjs +5 -3
- package/dist/index.d.mts +96 -155
- package/dist/index.d.ts +96 -155
- package/dist/index.mjs +34 -654
- 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.B3hexduL.d.mts +101 -0
- package/dist/shared/openapi.B3hexduL.d.ts +101 -0
- package/dist/shared/{openapi.CDsfPHgw.mjs → openapi.C_UtQ8Us.mjs} +61 -30
- package/dist/shared/openapi.D3j94c9n.d.mts +12 -0
- package/dist/shared/openapi.D3j94c9n.d.ts +12 -0
- package/dist/shared/openapi.DrrBsJ0w.mjs +738 -0
- package/package.json +19 -22
- package/dist/adapters/hono/index.d.mts +0 -7
- package/dist/adapters/hono/index.d.ts +0 -7
- package/dist/adapters/hono/index.mjs +0 -10
- package/dist/adapters/next/index.d.mts +0 -7
- package/dist/adapters/next/index.d.ts +0 -7
- package/dist/adapters/next/index.mjs +0 -10
- package/dist/shared/openapi.BHG_gu5Z.mjs +0 -8
- package/dist/shared/openapi.D0VMNR6V.mjs +0 -25
- package/dist/shared/openapi.Dz_6xooR.d.mts +0 -7
- package/dist/shared/openapi.Dz_6xooR.d.ts +0 -7
package/dist/index.mjs
CHANGED
|
@@ -1,661 +1,41 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
import
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
1
|
+
import { c as customOpenAPIOperation } from './shared/openapi.DrrBsJ0w.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.DrrBsJ0w.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';
|
|
10
|
+
|
|
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()
|
|
20
|
+
)
|
|
21
|
+
);
|
|
22
|
+
} catch (e) {
|
|
23
|
+
if (e instanceof ORPCError) {
|
|
24
|
+
throw createORPCErrorFromJson(serializer.deserialize(
|
|
25
|
+
serializer.serialize(
|
|
26
|
+
e.toJSON(),
|
|
27
|
+
{ outputFormat: "plain" }
|
|
28
|
+
)
|
|
29
|
+
));
|
|
30
|
+
}
|
|
31
|
+
throw e;
|
|
22
32
|
}
|
|
23
33
|
});
|
|
24
|
-
|
|
25
|
-
function getOperationExtender(o) {
|
|
26
|
-
return o[OPERATION_EXTENDER_SYMBOL];
|
|
27
|
-
}
|
|
28
|
-
function extendOperation(operation, procedure) {
|
|
29
|
-
const operationExtenders = [];
|
|
30
|
-
for (const errorItem of Object.values(procedure["~orpc"].errorMap)) {
|
|
31
|
-
const maybeExtender = getOperationExtender(errorItem);
|
|
32
|
-
if (maybeExtender) {
|
|
33
|
-
operationExtenders.push(maybeExtender);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
if (isProcedure(procedure)) {
|
|
37
|
-
for (const middleware of procedure["~orpc"].middlewares) {
|
|
38
|
-
const maybeExtender = getOperationExtender(middleware);
|
|
39
|
-
if (maybeExtender) {
|
|
40
|
-
operationExtenders.push(maybeExtender);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
let currentOperation = operation;
|
|
45
|
-
for (const extender of operationExtenders) {
|
|
46
|
-
if (typeof extender === "function") {
|
|
47
|
-
currentOperation = extender(currentOperation, procedure);
|
|
48
|
-
} else {
|
|
49
|
-
currentOperation = {
|
|
50
|
-
...currentOperation,
|
|
51
|
-
...extender
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
return currentOperation;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
class OpenAPIContentBuilder {
|
|
59
|
-
constructor(schemaUtils) {
|
|
60
|
-
this.schemaUtils = schemaUtils;
|
|
61
|
-
}
|
|
62
|
-
build(jsonSchema, options) {
|
|
63
|
-
const isFileSchema = this.schemaUtils.isFileSchema.bind(this.schemaUtils);
|
|
64
|
-
const [matches, schema] = this.schemaUtils.filterSchemaBranches(jsonSchema, isFileSchema);
|
|
65
|
-
const files = matches;
|
|
66
|
-
const content = {};
|
|
67
|
-
for (const file of files) {
|
|
68
|
-
content[file.contentMediaType] = {
|
|
69
|
-
schema: file
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
const isStillHasFileSchema = findDeepMatches(isFileSchema, schema).values.length > 0;
|
|
73
|
-
if (schema !== void 0) {
|
|
74
|
-
content[isStillHasFileSchema ? "multipart/form-data" : "application/json"] = {
|
|
75
|
-
schema,
|
|
76
|
-
...options
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
return content;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
class OpenAPIError extends Error {
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
class OpenAPIInputStructureParser {
|
|
87
|
-
constructor(schemaConverter, schemaUtils, pathParser) {
|
|
88
|
-
this.schemaConverter = schemaConverter;
|
|
89
|
-
this.schemaUtils = schemaUtils;
|
|
90
|
-
this.pathParser = pathParser;
|
|
91
|
-
}
|
|
92
|
-
parse(contract, structure) {
|
|
93
|
-
const inputSchema = this.schemaConverter.convert(contract["~orpc"].inputSchema, { strategy: "input" });
|
|
94
|
-
const method = fallbackContractConfig("defaultMethod", contract["~orpc"].route?.method);
|
|
95
|
-
const httpPath = contract["~orpc"].route?.path;
|
|
96
|
-
if (this.schemaUtils.isAnySchema(inputSchema)) {
|
|
97
|
-
return {
|
|
98
|
-
paramsSchema: void 0,
|
|
99
|
-
querySchema: void 0,
|
|
100
|
-
headersSchema: void 0,
|
|
101
|
-
bodySchema: void 0
|
|
102
|
-
};
|
|
103
|
-
}
|
|
104
|
-
if (structure === "detailed") {
|
|
105
|
-
return this.parseDetailedSchema(inputSchema);
|
|
106
|
-
} else {
|
|
107
|
-
return this.parseCompactSchema(inputSchema, method, httpPath);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
parseDetailedSchema(inputSchema) {
|
|
111
|
-
if (!this.schemaUtils.isObjectSchema(inputSchema)) {
|
|
112
|
-
throw new OpenAPIError(`When input structure is 'detailed', input schema must be an object.`);
|
|
113
|
-
}
|
|
114
|
-
if (inputSchema.properties && Object.keys(inputSchema.properties).some((key) => !["params", "query", "headers", "body"].includes(key))) {
|
|
115
|
-
throw new OpenAPIError(`When input structure is 'detailed', input schema must be only can contain 'params', 'query', 'headers' and 'body' properties.`);
|
|
116
|
-
}
|
|
117
|
-
let paramsSchema = inputSchema.properties?.params;
|
|
118
|
-
let querySchema = inputSchema.properties?.query;
|
|
119
|
-
let headersSchema = inputSchema.properties?.headers;
|
|
120
|
-
const bodySchema = inputSchema.properties?.body;
|
|
121
|
-
if (paramsSchema !== void 0 && this.schemaUtils.isAnySchema(paramsSchema)) {
|
|
122
|
-
paramsSchema = void 0;
|
|
123
|
-
}
|
|
124
|
-
if (paramsSchema !== void 0 && !this.schemaUtils.isObjectSchema(paramsSchema)) {
|
|
125
|
-
throw new OpenAPIError(`When input structure is 'detailed', params schema in input schema must be an object.`);
|
|
126
|
-
}
|
|
127
|
-
if (querySchema !== void 0 && this.schemaUtils.isAnySchema(querySchema)) {
|
|
128
|
-
querySchema = void 0;
|
|
129
|
-
}
|
|
130
|
-
if (querySchema !== void 0 && !this.schemaUtils.isObjectSchema(querySchema)) {
|
|
131
|
-
throw new OpenAPIError(`When input structure is 'detailed', query schema in input schema must be an object.`);
|
|
132
|
-
}
|
|
133
|
-
if (headersSchema !== void 0 && this.schemaUtils.isAnySchema(headersSchema)) {
|
|
134
|
-
headersSchema = void 0;
|
|
135
|
-
}
|
|
136
|
-
if (headersSchema !== void 0 && !this.schemaUtils.isObjectSchema(headersSchema)) {
|
|
137
|
-
throw new OpenAPIError(`When input structure is 'detailed', headers schema in input schema must be an object.`);
|
|
138
|
-
}
|
|
139
|
-
return { paramsSchema, querySchema, headersSchema, bodySchema };
|
|
140
|
-
}
|
|
141
|
-
parseCompactSchema(inputSchema, method, httpPath) {
|
|
142
|
-
const dynamic = httpPath ? this.pathParser.parseDynamicParams(httpPath) : [];
|
|
143
|
-
if (dynamic.length === 0) {
|
|
144
|
-
if (method === "GET") {
|
|
145
|
-
let querySchema = inputSchema;
|
|
146
|
-
if (querySchema !== void 0 && this.schemaUtils.isAnySchema(querySchema)) {
|
|
147
|
-
querySchema = void 0;
|
|
148
|
-
}
|
|
149
|
-
if (querySchema !== void 0 && !this.schemaUtils.isObjectSchema(querySchema)) {
|
|
150
|
-
throw new OpenAPIError(`When input structure is 'compact' and method is 'GET', input schema must be an object.`);
|
|
151
|
-
}
|
|
152
|
-
return {
|
|
153
|
-
paramsSchema: void 0,
|
|
154
|
-
querySchema,
|
|
155
|
-
headersSchema: void 0,
|
|
156
|
-
bodySchema: void 0
|
|
157
|
-
};
|
|
158
|
-
}
|
|
159
|
-
return {
|
|
160
|
-
paramsSchema: void 0,
|
|
161
|
-
querySchema: void 0,
|
|
162
|
-
headersSchema: void 0,
|
|
163
|
-
bodySchema: inputSchema
|
|
164
|
-
};
|
|
165
|
-
}
|
|
166
|
-
if (!this.schemaUtils.isObjectSchema(inputSchema)) {
|
|
167
|
-
throw new OpenAPIError(`When input structure is 'compact' and path has dynamic parameters, input schema must be an object.`);
|
|
168
|
-
}
|
|
169
|
-
const [params, rest] = this.schemaUtils.separateObjectSchema(inputSchema, dynamic.map((v) => v.name));
|
|
170
|
-
return {
|
|
171
|
-
paramsSchema: params,
|
|
172
|
-
querySchema: method === "GET" ? rest : void 0,
|
|
173
|
-
headersSchema: void 0,
|
|
174
|
-
bodySchema: method !== "GET" ? rest : void 0
|
|
175
|
-
};
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
class OpenAPIOutputStructureParser {
|
|
180
|
-
constructor(schemaConverter, schemaUtils) {
|
|
181
|
-
this.schemaConverter = schemaConverter;
|
|
182
|
-
this.schemaUtils = schemaUtils;
|
|
183
|
-
}
|
|
184
|
-
parse(contract, structure) {
|
|
185
|
-
const outputSchema = this.schemaConverter.convert(contract["~orpc"].outputSchema, { strategy: "output" });
|
|
186
|
-
if (this.schemaUtils.isAnySchema(outputSchema)) {
|
|
187
|
-
return {
|
|
188
|
-
headersSchema: void 0,
|
|
189
|
-
bodySchema: void 0
|
|
190
|
-
};
|
|
191
|
-
}
|
|
192
|
-
if (structure === "detailed") {
|
|
193
|
-
return this.parseDetailedSchema(outputSchema);
|
|
194
|
-
} else {
|
|
195
|
-
return this.parseCompactSchema(outputSchema);
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
parseDetailedSchema(outputSchema) {
|
|
199
|
-
if (!this.schemaUtils.isObjectSchema(outputSchema)) {
|
|
200
|
-
throw new OpenAPIError(`When output structure is 'detailed', output schema must be an object.`);
|
|
201
|
-
}
|
|
202
|
-
if (outputSchema.properties && Object.keys(outputSchema.properties).some((key) => !["headers", "body"].includes(key))) {
|
|
203
|
-
throw new OpenAPIError(`When output structure is 'detailed', output schema must be only can contain 'headers' and 'body' properties.`);
|
|
204
|
-
}
|
|
205
|
-
let headersSchema = outputSchema.properties?.headers;
|
|
206
|
-
const bodySchema = outputSchema.properties?.body;
|
|
207
|
-
if (headersSchema !== void 0 && this.schemaUtils.isAnySchema(headersSchema)) {
|
|
208
|
-
headersSchema = void 0;
|
|
209
|
-
}
|
|
210
|
-
if (headersSchema !== void 0 && !this.schemaUtils.isObjectSchema(headersSchema)) {
|
|
211
|
-
throw new OpenAPIError(`When output structure is 'detailed', headers schema in output schema must be an object.`);
|
|
212
|
-
}
|
|
213
|
-
return { headersSchema, bodySchema };
|
|
214
|
-
}
|
|
215
|
-
parseCompactSchema(outputSchema) {
|
|
216
|
-
return {
|
|
217
|
-
headersSchema: void 0,
|
|
218
|
-
bodySchema: outputSchema
|
|
219
|
-
};
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
class OpenAPIParametersBuilder {
|
|
224
|
-
build(paramIn, jsonSchema, options) {
|
|
225
|
-
const parameters = [];
|
|
226
|
-
for (const name in jsonSchema.properties) {
|
|
227
|
-
const schema = jsonSchema.properties[name];
|
|
228
|
-
const paramExamples = jsonSchema.examples?.filter((example) => {
|
|
229
|
-
return isObject(example) && name in example;
|
|
230
|
-
}).map((example) => {
|
|
231
|
-
return example[name];
|
|
232
|
-
});
|
|
233
|
-
const paramSchema = {
|
|
234
|
-
examples: paramExamples?.length ? paramExamples : void 0,
|
|
235
|
-
...schema === true ? {} : schema === false ? { not: {} } : schema
|
|
236
|
-
};
|
|
237
|
-
const paramExample = get(options?.example, [name]);
|
|
238
|
-
parameters.push({
|
|
239
|
-
name,
|
|
240
|
-
in: paramIn,
|
|
241
|
-
required: typeof options?.required === "boolean" ? options.required : jsonSchema.required?.includes(name) ?? false,
|
|
242
|
-
schema: paramSchema,
|
|
243
|
-
example: paramExample,
|
|
244
|
-
style: options?.style
|
|
245
|
-
});
|
|
246
|
-
}
|
|
247
|
-
return parameters;
|
|
248
|
-
}
|
|
249
|
-
buildHeadersObject(jsonSchema, options) {
|
|
250
|
-
const parameters = this.build("header", jsonSchema, options);
|
|
251
|
-
const headersObject = {};
|
|
252
|
-
for (const param of parameters) {
|
|
253
|
-
headersObject[param.name] = omit(param, ["name", "in"]);
|
|
254
|
-
}
|
|
255
|
-
return headersObject;
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
class OpenAPIPathParser {
|
|
260
|
-
parseDynamicParams(path) {
|
|
261
|
-
const raws = path.match(/\{([^}]+)\}/g) ?? [];
|
|
262
|
-
return raws.map((raw) => {
|
|
263
|
-
const name = raw.slice(1, -1).split(":")[0];
|
|
264
|
-
return { name, raw };
|
|
265
|
-
});
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
class CompositeSchemaConverter {
|
|
270
|
-
converters;
|
|
271
|
-
constructor(converters) {
|
|
272
|
-
this.converters = converters;
|
|
273
|
-
}
|
|
274
|
-
condition() {
|
|
275
|
-
return true;
|
|
276
|
-
}
|
|
277
|
-
convert(schema, options) {
|
|
278
|
-
for (const converter of this.converters) {
|
|
279
|
-
if (converter.condition(schema, options)) {
|
|
280
|
-
return converter.convert(schema, options);
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
return {};
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
const NON_LOGIC_KEYWORDS = [
|
|
288
|
-
// Core Documentation Keywords
|
|
289
|
-
"$anchor",
|
|
290
|
-
"$comment",
|
|
291
|
-
"$defs",
|
|
292
|
-
"$id",
|
|
293
|
-
"title",
|
|
294
|
-
"description",
|
|
295
|
-
// Value Keywords
|
|
296
|
-
"default",
|
|
297
|
-
"deprecated",
|
|
298
|
-
"examples",
|
|
299
|
-
// Metadata Keywords
|
|
300
|
-
"$schema",
|
|
301
|
-
"definitions",
|
|
302
|
-
// Legacy, but still used
|
|
303
|
-
"readOnly",
|
|
304
|
-
"writeOnly",
|
|
305
|
-
// Display and UI Hints
|
|
306
|
-
"contentMediaType",
|
|
307
|
-
"contentEncoding",
|
|
308
|
-
"format",
|
|
309
|
-
// Custom Extensions
|
|
310
|
-
"$vocabulary",
|
|
311
|
-
"$dynamicAnchor",
|
|
312
|
-
"$dynamicRef"
|
|
313
|
-
];
|
|
314
|
-
|
|
315
|
-
class SchemaUtils {
|
|
316
|
-
isFileSchema(schema) {
|
|
317
|
-
return isObject(schema) && schema.type === "string" && typeof schema.contentMediaType === "string";
|
|
318
|
-
}
|
|
319
|
-
isObjectSchema(schema) {
|
|
320
|
-
return isObject(schema) && schema.type === "object";
|
|
321
|
-
}
|
|
322
|
-
isAnySchema(schema) {
|
|
323
|
-
return schema === true || Object.keys(schema).filter((key) => !NON_LOGIC_KEYWORDS.includes(key)).length === 0;
|
|
324
|
-
}
|
|
325
|
-
isUndefinableSchema(schema) {
|
|
326
|
-
const [matches] = this.filterSchemaBranches(schema, (schema2) => {
|
|
327
|
-
if (typeof schema2 === "boolean") {
|
|
328
|
-
return schema2;
|
|
329
|
-
}
|
|
330
|
-
return Object.keys(schema2).filter((key) => !NON_LOGIC_KEYWORDS.includes(key)).length === 0;
|
|
331
|
-
});
|
|
332
|
-
return matches.length > 0;
|
|
333
|
-
}
|
|
334
|
-
separateObjectSchema(schema, separatedProperties) {
|
|
335
|
-
const matched = { ...schema };
|
|
336
|
-
const rest = { ...schema };
|
|
337
|
-
matched.properties = Object.entries(schema.properties ?? {}).filter(([key]) => separatedProperties.includes(key)).reduce((acc, [key, value]) => {
|
|
338
|
-
acc[key] = value;
|
|
339
|
-
return acc;
|
|
340
|
-
}, {});
|
|
341
|
-
matched.required = schema.required?.filter((key) => separatedProperties.includes(key));
|
|
342
|
-
matched.examples = schema.examples?.map((example) => {
|
|
343
|
-
if (!isObject(example)) {
|
|
344
|
-
return example;
|
|
345
|
-
}
|
|
346
|
-
return Object.entries(example).reduce((acc, [key, value]) => {
|
|
347
|
-
if (separatedProperties.includes(key)) {
|
|
348
|
-
acc[key] = value;
|
|
349
|
-
}
|
|
350
|
-
return acc;
|
|
351
|
-
}, {});
|
|
352
|
-
});
|
|
353
|
-
rest.properties = Object.entries(schema.properties ?? {}).filter(([key]) => !separatedProperties.includes(key)).reduce((acc, [key, value]) => {
|
|
354
|
-
acc[key] = value;
|
|
355
|
-
return acc;
|
|
356
|
-
}, {});
|
|
357
|
-
rest.required = schema.required?.filter((key) => !separatedProperties.includes(key));
|
|
358
|
-
rest.examples = schema.examples?.map((example) => {
|
|
359
|
-
if (!isObject(example)) {
|
|
360
|
-
return example;
|
|
361
|
-
}
|
|
362
|
-
return Object.entries(example).reduce((acc, [key, value]) => {
|
|
363
|
-
if (!separatedProperties.includes(key)) {
|
|
364
|
-
acc[key] = value;
|
|
365
|
-
}
|
|
366
|
-
return acc;
|
|
367
|
-
}, {});
|
|
368
|
-
});
|
|
369
|
-
return [matched, rest];
|
|
370
|
-
}
|
|
371
|
-
filterSchemaBranches(schema, check, matches = []) {
|
|
372
|
-
if (check(schema)) {
|
|
373
|
-
matches.push(schema);
|
|
374
|
-
return [matches, void 0];
|
|
375
|
-
}
|
|
376
|
-
if (typeof schema === "boolean") {
|
|
377
|
-
return [matches, schema];
|
|
378
|
-
}
|
|
379
|
-
if (schema.anyOf && Object.keys(schema).every(
|
|
380
|
-
(k) => k === "anyOf" || NON_LOGIC_KEYWORDS.includes(k)
|
|
381
|
-
)) {
|
|
382
|
-
const anyOf = schema.anyOf.map((s) => this.filterSchemaBranches(s, check, matches)[1]).filter((v) => !!v);
|
|
383
|
-
if (anyOf.length === 1 && typeof anyOf[0] === "object") {
|
|
384
|
-
return [matches, { ...schema, anyOf: void 0, ...anyOf[0] }];
|
|
385
|
-
}
|
|
386
|
-
return [matches, { ...schema, anyOf }];
|
|
387
|
-
}
|
|
388
|
-
if (schema.oneOf && Object.keys(schema).every(
|
|
389
|
-
(k) => k === "oneOf" || NON_LOGIC_KEYWORDS.includes(k)
|
|
390
|
-
)) {
|
|
391
|
-
const oneOf = schema.oneOf.map((s) => this.filterSchemaBranches(s, check, matches)[1]).filter((v) => !!v);
|
|
392
|
-
if (oneOf.length === 1 && typeof oneOf[0] === "object") {
|
|
393
|
-
return [matches, { ...schema, oneOf: void 0, ...oneOf[0] }];
|
|
394
|
-
}
|
|
395
|
-
return [matches, { ...schema, oneOf }];
|
|
396
|
-
}
|
|
397
|
-
return [matches, schema];
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
class OpenAPIGenerator {
|
|
402
|
-
contentBuilder;
|
|
403
|
-
parametersBuilder;
|
|
404
|
-
schemaConverter;
|
|
405
|
-
schemaUtils;
|
|
406
|
-
jsonSerializer;
|
|
407
|
-
pathParser;
|
|
408
|
-
inputStructureParser;
|
|
409
|
-
outputStructureParser;
|
|
410
|
-
errorHandlerStrategy;
|
|
411
|
-
ignoreUndefinedPathProcedures;
|
|
412
|
-
considerMissingTagDefinitionAsError;
|
|
413
|
-
strictErrorResponses;
|
|
414
|
-
constructor(options) {
|
|
415
|
-
this.parametersBuilder = options?.parametersBuilder ?? new OpenAPIParametersBuilder();
|
|
416
|
-
this.schemaConverter = new CompositeSchemaConverter(options?.schemaConverters ?? []);
|
|
417
|
-
this.schemaUtils = options?.schemaUtils ?? new SchemaUtils();
|
|
418
|
-
this.jsonSerializer = options?.jsonSerializer ?? new OpenAPIJsonSerializer();
|
|
419
|
-
this.contentBuilder = options?.contentBuilder ?? new OpenAPIContentBuilder(this.schemaUtils);
|
|
420
|
-
this.pathParser = new OpenAPIPathParser();
|
|
421
|
-
this.inputStructureParser = options?.inputStructureParser ?? new OpenAPIInputStructureParser(this.schemaConverter, this.schemaUtils, this.pathParser);
|
|
422
|
-
this.outputStructureParser = options?.outputStructureParser ?? new OpenAPIOutputStructureParser(this.schemaConverter, this.schemaUtils);
|
|
423
|
-
this.errorHandlerStrategy = options?.errorHandlerStrategy ?? "throw";
|
|
424
|
-
this.ignoreUndefinedPathProcedures = options?.ignoreUndefinedPathProcedures ?? false;
|
|
425
|
-
this.considerMissingTagDefinitionAsError = options?.considerMissingTagDefinitionAsError ?? false;
|
|
426
|
-
this.strictErrorResponses = options?.strictErrorResponses ?? true;
|
|
427
|
-
}
|
|
428
|
-
async generate(router, doc) {
|
|
429
|
-
const builder = new OpenApiBuilder({
|
|
430
|
-
...doc,
|
|
431
|
-
openapi: "3.1.1"
|
|
432
|
-
});
|
|
433
|
-
const rootTags = doc.tags?.map((tag) => tag.name) ?? [];
|
|
434
|
-
await eachAllContractProcedure({
|
|
435
|
-
path: [],
|
|
436
|
-
router
|
|
437
|
-
}, ({ contract, path }) => {
|
|
438
|
-
try {
|
|
439
|
-
const def = contract["~orpc"];
|
|
440
|
-
if (this.ignoreUndefinedPathProcedures && def.route?.path === void 0) {
|
|
441
|
-
return;
|
|
442
|
-
}
|
|
443
|
-
const method = fallbackContractConfig("defaultMethod", def.route?.method);
|
|
444
|
-
const httpPath = def.route?.path ? toOpenAPI31RoutePattern(def.route?.path) : `/${path.map(encodeURIComponent).join("/")}`;
|
|
445
|
-
const { parameters, requestBody } = (() => {
|
|
446
|
-
const eventIteratorSchemaDetails = getEventIteratorSchemaDetails(def.inputSchema);
|
|
447
|
-
if (eventIteratorSchemaDetails) {
|
|
448
|
-
const requestBody3 = {
|
|
449
|
-
required: true,
|
|
450
|
-
content: {
|
|
451
|
-
"text/event-stream": {
|
|
452
|
-
schema: {
|
|
453
|
-
oneOf: [
|
|
454
|
-
{
|
|
455
|
-
type: "object",
|
|
456
|
-
properties: {
|
|
457
|
-
event: { type: "string", const: "message" },
|
|
458
|
-
data: this.schemaConverter.convert(eventIteratorSchemaDetails.yields, { strategy: "input" }),
|
|
459
|
-
id: { type: "string" },
|
|
460
|
-
retry: { type: "number" }
|
|
461
|
-
},
|
|
462
|
-
required: ["event", "data"]
|
|
463
|
-
},
|
|
464
|
-
{
|
|
465
|
-
type: "object",
|
|
466
|
-
properties: {
|
|
467
|
-
event: { type: "string", const: "done" },
|
|
468
|
-
data: this.schemaConverter.convert(eventIteratorSchemaDetails.returns, { strategy: "input" }),
|
|
469
|
-
id: { type: "string" },
|
|
470
|
-
retry: { type: "number" }
|
|
471
|
-
},
|
|
472
|
-
required: ["event", "data"]
|
|
473
|
-
},
|
|
474
|
-
{
|
|
475
|
-
type: "object",
|
|
476
|
-
properties: {
|
|
477
|
-
event: { type: "string", const: "error" },
|
|
478
|
-
data: {},
|
|
479
|
-
id: { type: "string" },
|
|
480
|
-
retry: { type: "number" }
|
|
481
|
-
},
|
|
482
|
-
required: ["event", "data"]
|
|
483
|
-
}
|
|
484
|
-
]
|
|
485
|
-
}
|
|
486
|
-
}
|
|
487
|
-
}
|
|
488
|
-
};
|
|
489
|
-
return { requestBody: requestBody3, parameters: [] };
|
|
490
|
-
}
|
|
491
|
-
const inputStructure = fallbackContractConfig("defaultInputStructure", def.route?.inputStructure);
|
|
492
|
-
const { paramsSchema, querySchema, headersSchema, bodySchema } = this.inputStructureParser.parse(contract, inputStructure);
|
|
493
|
-
const params = paramsSchema ? this.parametersBuilder.build("path", paramsSchema, {
|
|
494
|
-
required: true
|
|
495
|
-
}) : [];
|
|
496
|
-
const query = querySchema ? this.parametersBuilder.build("query", querySchema) : [];
|
|
497
|
-
const headers = headersSchema ? this.parametersBuilder.build("header", headersSchema) : [];
|
|
498
|
-
const parameters2 = [...params, ...query, ...headers];
|
|
499
|
-
const requestBody2 = bodySchema !== void 0 ? {
|
|
500
|
-
required: this.schemaUtils.isUndefinableSchema(bodySchema),
|
|
501
|
-
content: this.contentBuilder.build(bodySchema)
|
|
502
|
-
} : void 0;
|
|
503
|
-
return { parameters: parameters2, requestBody: requestBody2 };
|
|
504
|
-
})();
|
|
505
|
-
const { responses } = (() => {
|
|
506
|
-
const eventIteratorSchemaDetails = getEventIteratorSchemaDetails(def.outputSchema);
|
|
507
|
-
if (eventIteratorSchemaDetails) {
|
|
508
|
-
const responses3 = {};
|
|
509
|
-
responses3[fallbackContractConfig("defaultSuccessStatus", def.route?.successStatus)] = {
|
|
510
|
-
description: fallbackContractConfig("defaultSuccessDescription", def.route?.successDescription),
|
|
511
|
-
content: {
|
|
512
|
-
"text/event-stream": {
|
|
513
|
-
schema: {
|
|
514
|
-
oneOf: [
|
|
515
|
-
{
|
|
516
|
-
type: "object",
|
|
517
|
-
properties: {
|
|
518
|
-
event: { type: "string", const: "message" },
|
|
519
|
-
data: this.schemaConverter.convert(eventIteratorSchemaDetails.yields, { strategy: "input" }),
|
|
520
|
-
id: { type: "string" },
|
|
521
|
-
retry: { type: "number" }
|
|
522
|
-
},
|
|
523
|
-
required: ["event", "data"]
|
|
524
|
-
},
|
|
525
|
-
{
|
|
526
|
-
type: "object",
|
|
527
|
-
properties: {
|
|
528
|
-
event: { type: "string", const: "done" },
|
|
529
|
-
data: this.schemaConverter.convert(eventIteratorSchemaDetails.returns, { strategy: "input" }),
|
|
530
|
-
id: { type: "string" },
|
|
531
|
-
retry: { type: "number" }
|
|
532
|
-
},
|
|
533
|
-
required: ["event", "data"]
|
|
534
|
-
},
|
|
535
|
-
{
|
|
536
|
-
type: "object",
|
|
537
|
-
properties: {
|
|
538
|
-
event: { type: "string", const: "error" },
|
|
539
|
-
data: {},
|
|
540
|
-
id: { type: "string" },
|
|
541
|
-
retry: { type: "number" }
|
|
542
|
-
},
|
|
543
|
-
required: ["event", "data"]
|
|
544
|
-
}
|
|
545
|
-
]
|
|
546
|
-
}
|
|
547
|
-
}
|
|
548
|
-
}
|
|
549
|
-
};
|
|
550
|
-
return { responses: responses3 };
|
|
551
|
-
}
|
|
552
|
-
const outputStructure = fallbackContractConfig("defaultOutputStructure", def.route?.outputStructure);
|
|
553
|
-
const { headersSchema: resHeadersSchema, bodySchema: resBodySchema } = this.outputStructureParser.parse(contract, outputStructure);
|
|
554
|
-
const responses2 = {};
|
|
555
|
-
responses2[fallbackContractConfig("defaultSuccessStatus", def.route?.successStatus)] = {
|
|
556
|
-
description: fallbackContractConfig("defaultSuccessDescription", def.route?.successDescription),
|
|
557
|
-
content: resBodySchema !== void 0 ? this.contentBuilder.build(resBodySchema) : void 0,
|
|
558
|
-
headers: resHeadersSchema !== void 0 ? this.parametersBuilder.buildHeadersObject(resHeadersSchema) : void 0
|
|
559
|
-
};
|
|
560
|
-
return { responses: responses2 };
|
|
561
|
-
})();
|
|
562
|
-
const errors = group(Object.entries(def.errorMap ?? {}).filter(([_, config]) => config).map(([code, config]) => ({
|
|
563
|
-
...config,
|
|
564
|
-
code,
|
|
565
|
-
status: fallbackORPCErrorStatus(code, config?.status)
|
|
566
|
-
})), (error) => error.status);
|
|
567
|
-
for (const status in errors) {
|
|
568
|
-
const configs = errors[status];
|
|
569
|
-
if (!configs || configs.length === 0) {
|
|
570
|
-
continue;
|
|
571
|
-
}
|
|
572
|
-
const schemas = configs.map(({ data, code, message }) => {
|
|
573
|
-
const json = {
|
|
574
|
-
type: "object",
|
|
575
|
-
properties: {
|
|
576
|
-
defined: { const: true },
|
|
577
|
-
code: { const: code },
|
|
578
|
-
status: { const: Number(status) },
|
|
579
|
-
message: { type: "string", default: message },
|
|
580
|
-
data: {}
|
|
581
|
-
},
|
|
582
|
-
required: ["defined", "code", "status", "message"]
|
|
583
|
-
};
|
|
584
|
-
if (data) {
|
|
585
|
-
const dataJson = this.schemaConverter.convert(data, { strategy: "output" });
|
|
586
|
-
json.properties.data = dataJson;
|
|
587
|
-
if (!this.schemaUtils.isUndefinableSchema(dataJson)) {
|
|
588
|
-
json.required.push("data");
|
|
589
|
-
}
|
|
590
|
-
}
|
|
591
|
-
return json;
|
|
592
|
-
});
|
|
593
|
-
if (this.strictErrorResponses) {
|
|
594
|
-
schemas.push({
|
|
595
|
-
type: "object",
|
|
596
|
-
properties: {
|
|
597
|
-
defined: { const: false },
|
|
598
|
-
code: { type: "string" },
|
|
599
|
-
status: { type: "number" },
|
|
600
|
-
message: { type: "string" },
|
|
601
|
-
data: {}
|
|
602
|
-
},
|
|
603
|
-
required: ["defined", "code", "status", "message"]
|
|
604
|
-
});
|
|
605
|
-
}
|
|
606
|
-
const contentSchema = schemas.length === 1 ? schemas[0] : {
|
|
607
|
-
oneOf: schemas
|
|
608
|
-
};
|
|
609
|
-
responses[status] = {
|
|
610
|
-
description: status,
|
|
611
|
-
content: this.contentBuilder.build(contentSchema)
|
|
612
|
-
};
|
|
613
|
-
}
|
|
614
|
-
if (this.considerMissingTagDefinitionAsError && def.route?.tags) {
|
|
615
|
-
const missingTag = def.route?.tags.find((tag) => !rootTags.includes(tag));
|
|
616
|
-
if (missingTag !== void 0) {
|
|
617
|
-
throw new OpenAPIError(
|
|
618
|
-
`Tag "${missingTag}" is missing definition. Please define it in OpenAPI root tags object`
|
|
619
|
-
);
|
|
620
|
-
}
|
|
621
|
-
}
|
|
622
|
-
const operation = {
|
|
623
|
-
summary: def.route?.summary,
|
|
624
|
-
description: def.route?.description,
|
|
625
|
-
deprecated: def.route?.deprecated,
|
|
626
|
-
tags: def.route?.tags ? [...def.route.tags] : void 0,
|
|
627
|
-
operationId: path.join("."),
|
|
628
|
-
parameters: parameters.length ? parameters : void 0,
|
|
629
|
-
requestBody,
|
|
630
|
-
responses
|
|
631
|
-
};
|
|
632
|
-
const extendedOperation = extendOperation(operation, contract);
|
|
633
|
-
builder.addPath(httpPath, {
|
|
634
|
-
[method.toLocaleLowerCase()]: extendedOperation
|
|
635
|
-
});
|
|
636
|
-
} catch (e) {
|
|
637
|
-
if (e instanceof OpenAPIError) {
|
|
638
|
-
const error = new OpenAPIError(`
|
|
639
|
-
Generate OpenAPI Error: ${e.message}
|
|
640
|
-
Happened at path: ${path.join(".")}
|
|
641
|
-
`, { cause: e });
|
|
642
|
-
if (this.errorHandlerStrategy === "throw") {
|
|
643
|
-
throw error;
|
|
644
|
-
}
|
|
645
|
-
if (this.errorHandlerStrategy === "log") {
|
|
646
|
-
console.error(error);
|
|
647
|
-
}
|
|
648
|
-
} else {
|
|
649
|
-
throw e;
|
|
650
|
-
}
|
|
651
|
-
}
|
|
652
|
-
});
|
|
653
|
-
return this.jsonSerializer.serialize(builder.getSpec())[0];
|
|
654
|
-
}
|
|
34
|
+
return createRouterClient(router, options);
|
|
655
35
|
}
|
|
656
36
|
|
|
657
37
|
const oo = {
|
|
658
|
-
spec:
|
|
38
|
+
spec: customOpenAPIOperation
|
|
659
39
|
};
|
|
660
40
|
|
|
661
|
-
export {
|
|
41
|
+
export { createJsonifiedRouterClient, customOpenAPIOperation, oo };
|