@orpc/openapi 1.0.0-beta.7 → 1.1.0
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 +13 -17
- package/dist/adapters/fetch/index.d.mts +6 -0
- package/dist/adapters/fetch/index.d.ts +6 -0
- package/dist/adapters/node/index.d.mts +6 -0
- package/dist/adapters/node/index.d.ts +6 -0
- package/dist/index.d.mts +13 -32
- package/dist/index.d.ts +13 -32
- package/dist/index.mjs +9 -530
- package/dist/plugins/index.d.mts +70 -0
- package/dist/plugins/index.d.ts +70 -0
- package/dist/plugins/index.mjs +97 -0
- package/dist/shared/openapi.DP97kr00.d.mts +47 -0
- package/dist/shared/openapi.DP97kr00.d.ts +47 -0
- package/dist/shared/openapi.fMEQd3Yd.mjs +544 -0
- package/package.json +13 -8
package/dist/index.mjs
CHANGED
@@ -1,536 +1,15 @@
|
|
1
|
-
import {
|
2
|
-
|
3
|
-
import { toHttpPath } from '@orpc/client/standard';
|
4
|
-
import { fallbackContractConfig, getEventIteratorSchemaDetails } from '@orpc/contract';
|
5
|
-
import { standardizeHTTPPath, StandardOpenAPIJsonSerializer, getDynamicParams } from '@orpc/openapi-client/standard';
|
6
|
-
import { isObject, findDeepMatches, toArray, clone } from '@orpc/shared';
|
1
|
+
import { c as customOpenAPIOperation } from './shared/openapi.fMEQd3Yd.mjs';
|
2
|
+
export { C as CompositeSchemaConverter, L as LOGIC_KEYWORDS, O as OpenAPIGenerator, a as applyCustomOpenAPIOperation, n as applySchemaOptionality, h as checkParamsSchema, m as filterSchemaBranches, g as getCustomOpenAPIOperation, l as isAnySchema, j as isFileSchema, k as isObjectSchema, s as separateObjectSchema, d as toOpenAPIContent, e as toOpenAPIEventIteratorContent, b as toOpenAPIMethod, f as toOpenAPIParameters, t as toOpenAPIPath, i as toOpenAPISchema } from './shared/openapi.fMEQd3Yd.mjs';
|
7
3
|
export { Format as JSONSchemaFormat } from 'json-schema-typed/draft-2020-12';
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
return extend;
|
15
|
-
}
|
16
|
-
return Reflect.get(target, prop, receiver);
|
17
|
-
}
|
18
|
-
});
|
19
|
-
}
|
20
|
-
function getCustomOpenAPIOperation(o) {
|
21
|
-
return o[OPERATION_EXTENDER_SYMBOL];
|
22
|
-
}
|
23
|
-
function applyCustomOpenAPIOperation(operation, contract) {
|
24
|
-
const operationCustoms = [];
|
25
|
-
for (const errorItem of Object.values(contract["~orpc"].errorMap)) {
|
26
|
-
const maybeExtender = errorItem ? getCustomOpenAPIOperation(errorItem) : void 0;
|
27
|
-
if (maybeExtender) {
|
28
|
-
operationCustoms.push(maybeExtender);
|
29
|
-
}
|
30
|
-
}
|
31
|
-
if (isProcedure(contract)) {
|
32
|
-
for (const middleware of contract["~orpc"].middlewares) {
|
33
|
-
const maybeExtender = getCustomOpenAPIOperation(middleware);
|
34
|
-
if (maybeExtender) {
|
35
|
-
operationCustoms.push(maybeExtender);
|
36
|
-
}
|
37
|
-
}
|
38
|
-
}
|
39
|
-
let currentOperation = operation;
|
40
|
-
for (const custom of operationCustoms) {
|
41
|
-
if (typeof custom === "function") {
|
42
|
-
currentOperation = custom(currentOperation, contract);
|
43
|
-
} else {
|
44
|
-
currentOperation = {
|
45
|
-
...currentOperation,
|
46
|
-
...custom
|
47
|
-
};
|
48
|
-
}
|
49
|
-
}
|
50
|
-
return currentOperation;
|
51
|
-
}
|
52
|
-
|
53
|
-
const LOGIC_KEYWORDS = [
|
54
|
-
"$dynamicRef",
|
55
|
-
"$ref",
|
56
|
-
"additionalItems",
|
57
|
-
"additionalProperties",
|
58
|
-
"allOf",
|
59
|
-
"anyOf",
|
60
|
-
"const",
|
61
|
-
"contains",
|
62
|
-
"contentEncoding",
|
63
|
-
"contentMediaType",
|
64
|
-
"contentSchema",
|
65
|
-
"dependencies",
|
66
|
-
"dependentRequired",
|
67
|
-
"dependentSchemas",
|
68
|
-
"else",
|
69
|
-
"enum",
|
70
|
-
"exclusiveMaximum",
|
71
|
-
"exclusiveMinimum",
|
72
|
-
"format",
|
73
|
-
"if",
|
74
|
-
"items",
|
75
|
-
"maxContains",
|
76
|
-
"maximum",
|
77
|
-
"maxItems",
|
78
|
-
"maxLength",
|
79
|
-
"maxProperties",
|
80
|
-
"minContains",
|
81
|
-
"minimum",
|
82
|
-
"minItems",
|
83
|
-
"minLength",
|
84
|
-
"minProperties",
|
85
|
-
"multipleOf",
|
86
|
-
"not",
|
87
|
-
"oneOf",
|
88
|
-
"pattern",
|
89
|
-
"patternProperties",
|
90
|
-
"prefixItems",
|
91
|
-
"properties",
|
92
|
-
"propertyNames",
|
93
|
-
"required",
|
94
|
-
"then",
|
95
|
-
"type",
|
96
|
-
"unevaluatedItems",
|
97
|
-
"unevaluatedProperties",
|
98
|
-
"uniqueItems"
|
99
|
-
];
|
100
|
-
|
101
|
-
function isFileSchema(schema) {
|
102
|
-
return isObject(schema) && schema.type === "string" && typeof schema.contentMediaType === "string";
|
103
|
-
}
|
104
|
-
function isObjectSchema(schema) {
|
105
|
-
return isObject(schema) && schema.type === "object";
|
106
|
-
}
|
107
|
-
function isAnySchema(schema) {
|
108
|
-
if (schema === true) {
|
109
|
-
return true;
|
110
|
-
}
|
111
|
-
if (Object.keys(schema).every((k) => !LOGIC_KEYWORDS.includes(k))) {
|
112
|
-
return true;
|
113
|
-
}
|
114
|
-
return false;
|
115
|
-
}
|
116
|
-
function separateObjectSchema(schema, separatedProperties) {
|
117
|
-
if (Object.keys(schema).some((k) => k !== "type" && k !== "properties" && k !== "required" && LOGIC_KEYWORDS.includes(k))) {
|
118
|
-
return [{ type: "object" }, schema];
|
119
|
-
}
|
120
|
-
const matched = { ...schema };
|
121
|
-
const rest = { ...schema };
|
122
|
-
matched.properties = schema.properties && Object.entries(schema.properties).filter(([key]) => separatedProperties.includes(key)).reduce((acc, [key, value]) => {
|
123
|
-
acc[key] = value;
|
124
|
-
return acc;
|
125
|
-
}, {});
|
126
|
-
matched.required = schema.required?.filter((key) => separatedProperties.includes(key));
|
127
|
-
matched.examples = schema.examples?.map((example) => {
|
128
|
-
if (!isObject(example)) {
|
129
|
-
return example;
|
130
|
-
}
|
131
|
-
return Object.entries(example).reduce((acc, [key, value]) => {
|
132
|
-
if (separatedProperties.includes(key)) {
|
133
|
-
acc[key] = value;
|
134
|
-
}
|
135
|
-
return acc;
|
136
|
-
}, {});
|
137
|
-
});
|
138
|
-
rest.properties = schema.properties && Object.entries(schema.properties).filter(([key]) => !separatedProperties.includes(key)).reduce((acc, [key, value]) => {
|
139
|
-
acc[key] = value;
|
140
|
-
return acc;
|
141
|
-
}, {});
|
142
|
-
rest.required = schema.required?.filter((key) => !separatedProperties.includes(key));
|
143
|
-
rest.examples = schema.examples?.map((example) => {
|
144
|
-
if (!isObject(example)) {
|
145
|
-
return example;
|
146
|
-
}
|
147
|
-
return Object.entries(example).reduce((acc, [key, value]) => {
|
148
|
-
if (!separatedProperties.includes(key)) {
|
149
|
-
acc[key] = value;
|
150
|
-
}
|
151
|
-
return acc;
|
152
|
-
}, {});
|
153
|
-
});
|
154
|
-
return [matched, rest];
|
155
|
-
}
|
156
|
-
function filterSchemaBranches(schema, check, matches = []) {
|
157
|
-
if (check(schema)) {
|
158
|
-
matches.push(schema);
|
159
|
-
return [matches, void 0];
|
160
|
-
}
|
161
|
-
if (isObject(schema)) {
|
162
|
-
for (const keyword of ["anyOf", "oneOf"]) {
|
163
|
-
if (schema[keyword] && Object.keys(schema).every(
|
164
|
-
(k) => k === keyword || !LOGIC_KEYWORDS.includes(k)
|
165
|
-
)) {
|
166
|
-
const rest = schema[keyword].map((s) => filterSchemaBranches(s, check, matches)[1]).filter((v) => !!v);
|
167
|
-
if (rest.length === 1 && typeof rest[0] === "object") {
|
168
|
-
return [matches, { ...schema, [keyword]: void 0, ...rest[0] }];
|
169
|
-
}
|
170
|
-
return [matches, { ...schema, [keyword]: rest }];
|
171
|
-
}
|
172
|
-
}
|
173
|
-
}
|
174
|
-
return [matches, schema];
|
175
|
-
}
|
176
|
-
function applySchemaOptionality(required, schema) {
|
177
|
-
if (required) {
|
178
|
-
return schema;
|
179
|
-
}
|
180
|
-
return {
|
181
|
-
anyOf: [
|
182
|
-
schema,
|
183
|
-
{ not: {} }
|
184
|
-
]
|
185
|
-
};
|
186
|
-
}
|
187
|
-
|
188
|
-
function toOpenAPIPath(path) {
|
189
|
-
return standardizeHTTPPath(path).replace(/\/\{\+([^}]+)\}/g, "/{$1}");
|
190
|
-
}
|
191
|
-
function toOpenAPIMethod(method) {
|
192
|
-
return method.toLocaleLowerCase();
|
193
|
-
}
|
194
|
-
function toOpenAPIContent(schema) {
|
195
|
-
const content = {};
|
196
|
-
const [matches, restSchema] = filterSchemaBranches(schema, isFileSchema);
|
197
|
-
for (const file of matches) {
|
198
|
-
content[file.contentMediaType] = {
|
199
|
-
schema: toOpenAPISchema(file)
|
200
|
-
};
|
201
|
-
}
|
202
|
-
if (restSchema !== void 0) {
|
203
|
-
content["application/json"] = {
|
204
|
-
schema: toOpenAPISchema(restSchema)
|
205
|
-
};
|
206
|
-
const isStillHasFileSchema = findDeepMatches((v) => isObject(v) && isFileSchema(v), restSchema).values.length > 0;
|
207
|
-
if (isStillHasFileSchema) {
|
208
|
-
content["multipart/form-data"] = {
|
209
|
-
schema: toOpenAPISchema(restSchema)
|
210
|
-
};
|
211
|
-
}
|
212
|
-
}
|
213
|
-
return content;
|
214
|
-
}
|
215
|
-
function toOpenAPIEventIteratorContent([yieldsRequired, yieldsSchema], [returnsRequired, returnsSchema]) {
|
216
|
-
return {
|
217
|
-
"text/event-stream": {
|
218
|
-
schema: toOpenAPISchema({
|
219
|
-
oneOf: [
|
220
|
-
{
|
221
|
-
type: "object",
|
222
|
-
properties: {
|
223
|
-
event: { const: "message" },
|
224
|
-
data: yieldsSchema,
|
225
|
-
id: { type: "string" },
|
226
|
-
retry: { type: "number" }
|
227
|
-
},
|
228
|
-
required: yieldsRequired ? ["event", "data"] : ["event"]
|
229
|
-
},
|
230
|
-
{
|
231
|
-
type: "object",
|
232
|
-
properties: {
|
233
|
-
event: { const: "done" },
|
234
|
-
data: returnsSchema,
|
235
|
-
id: { type: "string" },
|
236
|
-
retry: { type: "number" }
|
237
|
-
},
|
238
|
-
required: returnsRequired ? ["event", "data"] : ["event"]
|
239
|
-
},
|
240
|
-
{
|
241
|
-
type: "object",
|
242
|
-
properties: {
|
243
|
-
event: { const: "error" },
|
244
|
-
data: {},
|
245
|
-
id: { type: "string" },
|
246
|
-
retry: { type: "number" }
|
247
|
-
},
|
248
|
-
required: ["event"]
|
249
|
-
}
|
250
|
-
]
|
251
|
-
})
|
252
|
-
}
|
253
|
-
};
|
254
|
-
}
|
255
|
-
function toOpenAPIParameters(schema, parameterIn) {
|
256
|
-
const parameters = [];
|
257
|
-
for (const key in schema.properties) {
|
258
|
-
const keySchema = schema.properties[key];
|
259
|
-
parameters.push({
|
260
|
-
name: key,
|
261
|
-
in: parameterIn,
|
262
|
-
required: schema.required?.includes(key),
|
263
|
-
style: parameterIn === "query" ? "deepObject" : void 0,
|
264
|
-
explode: parameterIn === "query" ? true : void 0,
|
265
|
-
schema: toOpenAPISchema(keySchema)
|
266
|
-
});
|
267
|
-
}
|
268
|
-
return parameters;
|
269
|
-
}
|
270
|
-
function checkParamsSchema(schema, params) {
|
271
|
-
const properties = Object.keys(schema.properties ?? {});
|
272
|
-
const required = schema.required ?? [];
|
273
|
-
if (properties.length !== params.length || properties.some((v) => !params.includes(v))) {
|
274
|
-
return false;
|
275
|
-
}
|
276
|
-
if (required.length !== params.length || required.some((v) => !params.includes(v))) {
|
277
|
-
return false;
|
278
|
-
}
|
279
|
-
return true;
|
280
|
-
}
|
281
|
-
function toOpenAPISchema(schema) {
|
282
|
-
return schema === true ? {} : schema === false ? { not: {} } : schema;
|
283
|
-
}
|
284
|
-
|
285
|
-
class CompositeSchemaConverter {
|
286
|
-
converters;
|
287
|
-
constructor(converters) {
|
288
|
-
this.converters = converters;
|
289
|
-
}
|
290
|
-
convert(schema, options) {
|
291
|
-
for (const converter of this.converters) {
|
292
|
-
if (converter.condition(schema, options)) {
|
293
|
-
return converter.convert(schema, options);
|
294
|
-
}
|
295
|
-
}
|
296
|
-
return [false, {}];
|
297
|
-
}
|
298
|
-
}
|
299
|
-
|
300
|
-
class OpenAPIGeneratorError extends Error {
|
301
|
-
}
|
302
|
-
class OpenAPIGenerator {
|
303
|
-
serializer;
|
304
|
-
converter;
|
305
|
-
constructor(options = {}) {
|
306
|
-
this.serializer = new StandardOpenAPIJsonSerializer(options);
|
307
|
-
this.converter = new CompositeSchemaConverter(toArray(options.schemaConverters));
|
308
|
-
}
|
309
|
-
async generate(router, base) {
|
310
|
-
const doc = clone(base);
|
311
|
-
doc.openapi = "3.1.1";
|
312
|
-
const errors = [];
|
313
|
-
await resolveContractProcedures({ path: [], router }, ({ contract, path }) => {
|
314
|
-
const operationId = path.join(".");
|
315
|
-
try {
|
316
|
-
const def = contract["~orpc"];
|
317
|
-
const method = toOpenAPIMethod(fallbackContractConfig("defaultMethod", def.route.method));
|
318
|
-
const httpPath = toOpenAPIPath(def.route.path ?? toHttpPath(path));
|
319
|
-
const operationObjectRef = {
|
320
|
-
operationId,
|
321
|
-
summary: def.route.summary,
|
322
|
-
description: def.route.description,
|
323
|
-
deprecated: def.route.deprecated,
|
324
|
-
tags: def.route.tags?.map((tag) => tag)
|
325
|
-
};
|
326
|
-
this.#request(operationObjectRef, def);
|
327
|
-
this.#successResponse(operationObjectRef, def);
|
328
|
-
this.#errorResponse(operationObjectRef, def);
|
329
|
-
doc.paths ??= {};
|
330
|
-
doc.paths[httpPath] ??= {};
|
331
|
-
doc.paths[httpPath][method] = applyCustomOpenAPIOperation(operationObjectRef, contract);
|
332
|
-
} catch (e) {
|
333
|
-
if (!(e instanceof OpenAPIGeneratorError)) {
|
334
|
-
throw e;
|
335
|
-
}
|
336
|
-
errors.push(
|
337
|
-
`[OpenAPIGenerator] Error occurred while generating OpenAPI for procedure at path: ${operationId}
|
338
|
-
${e.message}`
|
339
|
-
);
|
340
|
-
}
|
341
|
-
});
|
342
|
-
if (errors.length) {
|
343
|
-
throw new OpenAPIGeneratorError(
|
344
|
-
`Some error occurred during OpenAPI generation:
|
345
|
-
|
346
|
-
${errors.join("\n\n")}`
|
347
|
-
);
|
348
|
-
}
|
349
|
-
return this.serializer.serialize(doc)[0];
|
350
|
-
}
|
351
|
-
#request(ref, def) {
|
352
|
-
const method = fallbackContractConfig("defaultMethod", def.route.method);
|
353
|
-
const details = getEventIteratorSchemaDetails(def.inputSchema);
|
354
|
-
if (details) {
|
355
|
-
ref.requestBody = {
|
356
|
-
required: true,
|
357
|
-
content: toOpenAPIEventIteratorContent(
|
358
|
-
this.converter.convert(details.yields, { strategy: "input" }),
|
359
|
-
this.converter.convert(details.returns, { strategy: "input" })
|
360
|
-
)
|
361
|
-
};
|
362
|
-
return;
|
363
|
-
}
|
364
|
-
const dynamicParams = getDynamicParams(def.route.path)?.map((v) => v.name);
|
365
|
-
const inputStructure = fallbackContractConfig("defaultInputStructure", def.route.inputStructure);
|
366
|
-
let [required, schema] = this.converter.convert(def.inputSchema, { strategy: "input" });
|
367
|
-
if (isAnySchema(schema) && !dynamicParams?.length) {
|
368
|
-
return;
|
369
|
-
}
|
370
|
-
if (inputStructure === "compact") {
|
371
|
-
if (dynamicParams?.length) {
|
372
|
-
const error2 = new OpenAPIGeneratorError(
|
373
|
-
'When input structure is "compact", and path has dynamic params, input schema must be an object with all dynamic params as required.'
|
374
|
-
);
|
375
|
-
if (!isObjectSchema(schema)) {
|
376
|
-
throw error2;
|
377
|
-
}
|
378
|
-
const [paramsSchema, rest] = separateObjectSchema(schema, dynamicParams);
|
379
|
-
schema = rest;
|
380
|
-
required = rest.required ? rest.required.length !== 0 : false;
|
381
|
-
if (!checkParamsSchema(paramsSchema, dynamicParams)) {
|
382
|
-
throw error2;
|
383
|
-
}
|
384
|
-
ref.parameters ??= [];
|
385
|
-
ref.parameters.push(...toOpenAPIParameters(paramsSchema, "path"));
|
386
|
-
}
|
387
|
-
if (method === "GET") {
|
388
|
-
if (!isObjectSchema(schema)) {
|
389
|
-
throw new OpenAPIGeneratorError(
|
390
|
-
'When method is "GET", input schema must satisfy: object | any | unknown'
|
391
|
-
);
|
392
|
-
}
|
393
|
-
ref.parameters ??= [];
|
394
|
-
ref.parameters.push(...toOpenAPIParameters(schema, "query"));
|
395
|
-
} else {
|
396
|
-
ref.requestBody = {
|
397
|
-
required,
|
398
|
-
content: toOpenAPIContent(schema)
|
399
|
-
};
|
400
|
-
}
|
401
|
-
return;
|
402
|
-
}
|
403
|
-
const error = new OpenAPIGeneratorError(
|
404
|
-
'When input structure is "detailed", input schema must satisfy: { params?: Record<string, unknown>, query?: Record<string, unknown>, headers?: Record<string, unknown>, body?: unknown }'
|
405
|
-
);
|
406
|
-
if (!isObjectSchema(schema)) {
|
407
|
-
throw error;
|
408
|
-
}
|
409
|
-
if (dynamicParams?.length && (schema.properties?.params === void 0 || !isObjectSchema(schema.properties.params) || !checkParamsSchema(schema.properties.params, dynamicParams))) {
|
410
|
-
throw new OpenAPIGeneratorError(
|
411
|
-
'When input structure is "detailed" and path has dynamic params, the "params" schema must be an object with all dynamic params as required.'
|
412
|
-
);
|
413
|
-
}
|
414
|
-
for (const from of ["params", "query", "headers"]) {
|
415
|
-
const fromSchema = schema.properties?.[from];
|
416
|
-
if (fromSchema !== void 0) {
|
417
|
-
if (!isObjectSchema(fromSchema)) {
|
418
|
-
throw error;
|
419
|
-
}
|
420
|
-
const parameterIn = from === "params" ? "path" : from === "headers" ? "header" : "query";
|
421
|
-
ref.parameters ??= [];
|
422
|
-
ref.parameters.push(...toOpenAPIParameters(fromSchema, parameterIn));
|
423
|
-
}
|
424
|
-
}
|
425
|
-
if (schema.properties?.body !== void 0) {
|
426
|
-
ref.requestBody = {
|
427
|
-
required: schema.required?.includes("body"),
|
428
|
-
content: toOpenAPIContent(schema.properties.body)
|
429
|
-
};
|
430
|
-
}
|
431
|
-
}
|
432
|
-
#successResponse(ref, def) {
|
433
|
-
const outputSchema = def.outputSchema;
|
434
|
-
const status = fallbackContractConfig("defaultSuccessStatus", def.route.successStatus);
|
435
|
-
const description = fallbackContractConfig("defaultSuccessDescription", def.route?.successDescription);
|
436
|
-
const eventIteratorSchemaDetails = getEventIteratorSchemaDetails(outputSchema);
|
437
|
-
const outputStructure = fallbackContractConfig("defaultOutputStructure", def.route.outputStructure);
|
438
|
-
if (eventIteratorSchemaDetails) {
|
439
|
-
ref.responses ??= {};
|
440
|
-
ref.responses[status] = {
|
441
|
-
description,
|
442
|
-
content: toOpenAPIEventIteratorContent(
|
443
|
-
this.converter.convert(eventIteratorSchemaDetails.yields, { strategy: "output" }),
|
444
|
-
this.converter.convert(eventIteratorSchemaDetails.returns, { strategy: "output" })
|
445
|
-
)
|
446
|
-
};
|
447
|
-
return;
|
448
|
-
}
|
449
|
-
const [required, json] = this.converter.convert(outputSchema, { strategy: "output" });
|
450
|
-
ref.responses ??= {};
|
451
|
-
ref.responses[status] = {
|
452
|
-
description
|
453
|
-
};
|
454
|
-
if (outputStructure === "compact") {
|
455
|
-
ref.responses[status].content = toOpenAPIContent(applySchemaOptionality(required, json));
|
456
|
-
return;
|
457
|
-
}
|
458
|
-
const error = new OpenAPIGeneratorError(
|
459
|
-
'When output structure is "detailed", output schema must satisfy: { headers?: Record<string, unknown>, body?: unknown }'
|
460
|
-
);
|
461
|
-
if (!isObjectSchema(json)) {
|
462
|
-
throw error;
|
463
|
-
}
|
464
|
-
if (json.properties?.headers !== void 0) {
|
465
|
-
if (!isObjectSchema(json.properties.headers)) {
|
466
|
-
throw error;
|
467
|
-
}
|
468
|
-
for (const key in json.properties.headers.properties) {
|
469
|
-
ref.responses[status].headers ??= {};
|
470
|
-
ref.responses[status].headers[key] = {
|
471
|
-
schema: toOpenAPISchema(json.properties.headers.properties[key]),
|
472
|
-
required: json.properties.headers.required?.includes(key)
|
473
|
-
};
|
474
|
-
}
|
475
|
-
}
|
476
|
-
if (json.properties?.body !== void 0) {
|
477
|
-
ref.responses[status].content = toOpenAPIContent(
|
478
|
-
applySchemaOptionality(json.required?.includes("body") ?? false, json.properties.body)
|
479
|
-
);
|
480
|
-
}
|
481
|
-
}
|
482
|
-
#errorResponse(ref, def) {
|
483
|
-
const errorMap = def.errorMap;
|
484
|
-
const errors = {};
|
485
|
-
for (const code in errorMap) {
|
486
|
-
const config = errorMap[code];
|
487
|
-
if (!config) {
|
488
|
-
continue;
|
489
|
-
}
|
490
|
-
const status = fallbackORPCErrorStatus(code, config.status);
|
491
|
-
const message = fallbackORPCErrorMessage(code, config.message);
|
492
|
-
const [dataRequired, dataSchema] = this.converter.convert(config.data, { strategy: "output" });
|
493
|
-
errors[status] ??= [];
|
494
|
-
errors[status].push({
|
495
|
-
type: "object",
|
496
|
-
properties: {
|
497
|
-
defined: { const: true },
|
498
|
-
code: { const: code },
|
499
|
-
status: { const: status },
|
500
|
-
message: { type: "string", default: message },
|
501
|
-
data: dataSchema
|
502
|
-
},
|
503
|
-
required: dataRequired ? ["defined", "code", "status", "message", "data"] : ["defined", "code", "status", "message"]
|
504
|
-
});
|
505
|
-
}
|
506
|
-
ref.responses ??= {};
|
507
|
-
for (const status in errors) {
|
508
|
-
const schemas = errors[status];
|
509
|
-
ref.responses[status] = {
|
510
|
-
description: status,
|
511
|
-
content: toOpenAPIContent({
|
512
|
-
oneOf: [
|
513
|
-
...schemas,
|
514
|
-
{
|
515
|
-
type: "object",
|
516
|
-
properties: {
|
517
|
-
defined: { const: false },
|
518
|
-
code: { type: "string" },
|
519
|
-
status: { type: "number" },
|
520
|
-
message: { type: "string" },
|
521
|
-
data: {}
|
522
|
-
},
|
523
|
-
required: ["defined", "code", "status", "message"]
|
524
|
-
}
|
525
|
-
]
|
526
|
-
})
|
527
|
-
};
|
528
|
-
}
|
529
|
-
}
|
530
|
-
}
|
4
|
+
import '@orpc/client';
|
5
|
+
import '@orpc/client/standard';
|
6
|
+
import '@orpc/contract';
|
7
|
+
import '@orpc/openapi-client/standard';
|
8
|
+
import '@orpc/server';
|
9
|
+
import '@orpc/shared';
|
531
10
|
|
532
11
|
const oo = {
|
533
12
|
spec: customOpenAPIOperation
|
534
13
|
};
|
535
14
|
|
536
|
-
export {
|
15
|
+
export { customOpenAPIOperation, oo };
|
@@ -0,0 +1,70 @@
|
|
1
|
+
import { Context, HTTPPath, Router } from '@orpc/server';
|
2
|
+
import { StandardHandlerInterceptorOptions, StandardHandlerPlugin, StandardHandlerOptions } from '@orpc/server/standard';
|
3
|
+
import { Value } from '@orpc/shared';
|
4
|
+
import { O as OpenAPIGeneratorOptions, a as OpenAPIGeneratorGenerateOptions } from '../shared/openapi.DP97kr00.mjs';
|
5
|
+
import '@orpc/contract';
|
6
|
+
import '@orpc/openapi-client/standard';
|
7
|
+
import 'openapi-types';
|
8
|
+
import 'json-schema-typed/draft-2020-12';
|
9
|
+
|
10
|
+
interface OpenAPIReferencePluginOptions<T extends Context> extends OpenAPIGeneratorOptions {
|
11
|
+
/**
|
12
|
+
* Options to pass to the OpenAPI generate.
|
13
|
+
*
|
14
|
+
*/
|
15
|
+
specGenerateOptions?: Value<OpenAPIGeneratorGenerateOptions, [StandardHandlerInterceptorOptions<T>]>;
|
16
|
+
/**
|
17
|
+
* The URL path at which to serve the OpenAPI JSON.
|
18
|
+
*
|
19
|
+
* @default '/spec.json'
|
20
|
+
*/
|
21
|
+
specPath?: HTTPPath;
|
22
|
+
/**
|
23
|
+
* The URL path at which to serve the API reference UI.
|
24
|
+
*
|
25
|
+
* @default '/'
|
26
|
+
*/
|
27
|
+
docsPath?: HTTPPath;
|
28
|
+
/**
|
29
|
+
* The document title for the API reference UI.
|
30
|
+
*
|
31
|
+
* @default 'API Reference'
|
32
|
+
*/
|
33
|
+
docsTitle?: Value<string, [StandardHandlerInterceptorOptions<T>]>;
|
34
|
+
/**
|
35
|
+
* Arbitrary configuration object for the UI.
|
36
|
+
*/
|
37
|
+
docsConfig?: Value<object, [StandardHandlerInterceptorOptions<T>]>;
|
38
|
+
/**
|
39
|
+
* HTML to inject into the <head> of the docs page.
|
40
|
+
*
|
41
|
+
* @default ''
|
42
|
+
*/
|
43
|
+
docsHead?: Value<string, [StandardHandlerInterceptorOptions<T>]>;
|
44
|
+
/**
|
45
|
+
* URL of the external script bundle for the reference UI.
|
46
|
+
*
|
47
|
+
* @default 'https://cdn.jsdelivr.net/npm/@scalar/api-reference'
|
48
|
+
*/
|
49
|
+
docsScriptUrl?: Value<string, [StandardHandlerInterceptorOptions<T>]>;
|
50
|
+
/**
|
51
|
+
* Override function to generate the full HTML for the docs page.
|
52
|
+
*/
|
53
|
+
renderDocsHtml?: (specUrl: string, title: string, head: string, scriptUrl: string, config: object | undefined) => string;
|
54
|
+
}
|
55
|
+
declare class OpenAPIReferencePlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
56
|
+
private readonly generator;
|
57
|
+
private readonly specGenerateOptions;
|
58
|
+
private readonly specPath;
|
59
|
+
private readonly docsPath;
|
60
|
+
private readonly docsTitle;
|
61
|
+
private readonly docsHead;
|
62
|
+
private readonly docsScriptUrl;
|
63
|
+
private readonly docsConfig;
|
64
|
+
private readonly renderDocsHtml;
|
65
|
+
constructor(options?: OpenAPIReferencePluginOptions<T>);
|
66
|
+
init(options: StandardHandlerOptions<T>, router: Router<any, T>): void;
|
67
|
+
}
|
68
|
+
|
69
|
+
export { OpenAPIReferencePlugin };
|
70
|
+
export type { OpenAPIReferencePluginOptions };
|
@@ -0,0 +1,70 @@
|
|
1
|
+
import { Context, HTTPPath, Router } from '@orpc/server';
|
2
|
+
import { StandardHandlerInterceptorOptions, StandardHandlerPlugin, StandardHandlerOptions } from '@orpc/server/standard';
|
3
|
+
import { Value } from '@orpc/shared';
|
4
|
+
import { O as OpenAPIGeneratorOptions, a as OpenAPIGeneratorGenerateOptions } from '../shared/openapi.DP97kr00.js';
|
5
|
+
import '@orpc/contract';
|
6
|
+
import '@orpc/openapi-client/standard';
|
7
|
+
import 'openapi-types';
|
8
|
+
import 'json-schema-typed/draft-2020-12';
|
9
|
+
|
10
|
+
interface OpenAPIReferencePluginOptions<T extends Context> extends OpenAPIGeneratorOptions {
|
11
|
+
/**
|
12
|
+
* Options to pass to the OpenAPI generate.
|
13
|
+
*
|
14
|
+
*/
|
15
|
+
specGenerateOptions?: Value<OpenAPIGeneratorGenerateOptions, [StandardHandlerInterceptorOptions<T>]>;
|
16
|
+
/**
|
17
|
+
* The URL path at which to serve the OpenAPI JSON.
|
18
|
+
*
|
19
|
+
* @default '/spec.json'
|
20
|
+
*/
|
21
|
+
specPath?: HTTPPath;
|
22
|
+
/**
|
23
|
+
* The URL path at which to serve the API reference UI.
|
24
|
+
*
|
25
|
+
* @default '/'
|
26
|
+
*/
|
27
|
+
docsPath?: HTTPPath;
|
28
|
+
/**
|
29
|
+
* The document title for the API reference UI.
|
30
|
+
*
|
31
|
+
* @default 'API Reference'
|
32
|
+
*/
|
33
|
+
docsTitle?: Value<string, [StandardHandlerInterceptorOptions<T>]>;
|
34
|
+
/**
|
35
|
+
* Arbitrary configuration object for the UI.
|
36
|
+
*/
|
37
|
+
docsConfig?: Value<object, [StandardHandlerInterceptorOptions<T>]>;
|
38
|
+
/**
|
39
|
+
* HTML to inject into the <head> of the docs page.
|
40
|
+
*
|
41
|
+
* @default ''
|
42
|
+
*/
|
43
|
+
docsHead?: Value<string, [StandardHandlerInterceptorOptions<T>]>;
|
44
|
+
/**
|
45
|
+
* URL of the external script bundle for the reference UI.
|
46
|
+
*
|
47
|
+
* @default 'https://cdn.jsdelivr.net/npm/@scalar/api-reference'
|
48
|
+
*/
|
49
|
+
docsScriptUrl?: Value<string, [StandardHandlerInterceptorOptions<T>]>;
|
50
|
+
/**
|
51
|
+
* Override function to generate the full HTML for the docs page.
|
52
|
+
*/
|
53
|
+
renderDocsHtml?: (specUrl: string, title: string, head: string, scriptUrl: string, config: object | undefined) => string;
|
54
|
+
}
|
55
|
+
declare class OpenAPIReferencePlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
56
|
+
private readonly generator;
|
57
|
+
private readonly specGenerateOptions;
|
58
|
+
private readonly specPath;
|
59
|
+
private readonly docsPath;
|
60
|
+
private readonly docsTitle;
|
61
|
+
private readonly docsHead;
|
62
|
+
private readonly docsScriptUrl;
|
63
|
+
private readonly docsConfig;
|
64
|
+
private readonly renderDocsHtml;
|
65
|
+
constructor(options?: OpenAPIReferencePluginOptions<T>);
|
66
|
+
init(options: StandardHandlerOptions<T>, router: Router<any, T>): void;
|
67
|
+
}
|
68
|
+
|
69
|
+
export { OpenAPIReferencePlugin };
|
70
|
+
export type { OpenAPIReferencePluginOptions };
|