@orpc/openapi 0.46.0 → 1.0.0-beta.1
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 +3 -1
- package/dist/adapters/fetch/index.d.mts +3 -2
- package/dist/adapters/fetch/index.d.ts +3 -2
- package/dist/adapters/fetch/index.mjs +5 -4
- package/dist/adapters/hono/index.d.mts +2 -1
- package/dist/adapters/hono/index.d.ts +2 -1
- package/dist/adapters/hono/index.mjs +5 -4
- package/dist/adapters/next/index.d.mts +2 -1
- package/dist/adapters/next/index.d.ts +2 -1
- package/dist/adapters/next/index.mjs +5 -4
- package/dist/adapters/node/index.d.mts +3 -2
- package/dist/adapters/node/index.d.ts +3 -2
- package/dist/adapters/node/index.mjs +12 -8
- package/dist/adapters/standard/index.d.mts +18 -7
- package/dist/adapters/standard/index.d.ts +18 -7
- package/dist/adapters/standard/index.mjs +3 -3
- package/dist/index.d.mts +91 -140
- package/dist/index.d.ts +91 -140
- package/dist/index.mjs +225 -583
- package/dist/shared/{openapi.CDsfPHgw.mjs → openapi.BNHmrMe2.mjs} +21 -24
- package/dist/shared/openapi.DZzpQAb-.mjs +231 -0
- package/dist/shared/openapi.Dv-KT_Bx.mjs +33 -0
- package/dist/shared/openapi.IfmmOyba.d.mts +8 -0
- package/dist/shared/openapi.IfmmOyba.d.ts +8 -0
- package/package.json +11 -11
- package/dist/shared/openapi.BHG_gu5Z.mjs +0 -8
- package/dist/shared/openapi.BcJH4F9P.mjs +0 -27
- 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,18 +1,14 @@
|
|
1
|
-
import { isProcedure,
|
2
|
-
import {
|
3
|
-
export { OpenApiBuilder } from 'openapi3-ts/oas31';
|
4
|
-
import { findDeepMatches, isObject, get, omit, group } from '@orpc/shared';
|
5
|
-
import { fallbackORPCErrorStatus } from '@orpc/client';
|
1
|
+
import { isProcedure, resolveContractProcedures, toHttpPath } from '@orpc/server';
|
2
|
+
import { fallbackORPCErrorStatus, fallbackORPCErrorMessage } from '@orpc/client';
|
6
3
|
import { fallbackContractConfig, getEventIteratorSchemaDetails } from '@orpc/contract';
|
7
|
-
import {
|
8
|
-
import
|
9
|
-
|
4
|
+
import { StandardOpenAPIJsonSerializer } from '@orpc/openapi-client/standard';
|
5
|
+
import { clone } from '@orpc/shared';
|
6
|
+
import { t as toOpenAPIMethod, a as toOpenAPIPath, b as toOpenAPIEventIteratorContent, g as getDynamicParams, i as isAnySchema, c as isObjectSchema, d as separateObjectSchema, e as checkParamsSchema, f as toOpenAPIParameters, h as toOpenAPIContent, j as toOpenAPISchema } from './shared/openapi.DZzpQAb-.mjs';
|
7
|
+
export { L as LOGIC_KEYWORDS, l as filterSchemaBranches, k as isFileSchema, s as standardizeHTTPPath } from './shared/openapi.DZzpQAb-.mjs';
|
10
8
|
export { Format as JSONSchemaFormat } from 'json-schema-typed/draft-2020-12';
|
11
|
-
import { t as toOpenAPI31RoutePattern } from './shared/openapi.BHG_gu5Z.mjs';
|
12
|
-
export { s as standardizeHTTPPath } from './shared/openapi.BHG_gu5Z.mjs';
|
13
9
|
|
14
10
|
const OPERATION_EXTENDER_SYMBOL = Symbol("ORPC_OPERATION_EXTENDER");
|
15
|
-
function
|
11
|
+
function customOpenAPIOperation(o, extend) {
|
16
12
|
return new Proxy(o, {
|
17
13
|
get(target, prop, receiver) {
|
18
14
|
if (prop === OPERATION_EXTENDER_SYMBOL) {
|
@@ -22,576 +18,267 @@ function setOperationExtender(o, extend) {
|
|
22
18
|
}
|
23
19
|
});
|
24
20
|
}
|
25
|
-
function
|
21
|
+
function getCustomOpenAPIOperation(o) {
|
26
22
|
return o[OPERATION_EXTENDER_SYMBOL];
|
27
23
|
}
|
28
|
-
function
|
29
|
-
const
|
30
|
-
for (const errorItem of Object.values(
|
31
|
-
const maybeExtender =
|
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;
|
32
28
|
if (maybeExtender) {
|
33
|
-
|
29
|
+
operationCustoms.push(maybeExtender);
|
34
30
|
}
|
35
31
|
}
|
36
|
-
if (isProcedure(
|
37
|
-
for (const middleware of
|
38
|
-
const maybeExtender =
|
32
|
+
if (isProcedure(contract)) {
|
33
|
+
for (const middleware of contract["~orpc"].middlewares) {
|
34
|
+
const maybeExtender = getCustomOpenAPIOperation(middleware);
|
39
35
|
if (maybeExtender) {
|
40
|
-
|
36
|
+
operationCustoms.push(maybeExtender);
|
41
37
|
}
|
42
38
|
}
|
43
39
|
}
|
44
40
|
let currentOperation = operation;
|
45
|
-
for (const
|
46
|
-
if (typeof
|
47
|
-
currentOperation =
|
41
|
+
for (const custom of operationCustoms) {
|
42
|
+
if (typeof custom === "function") {
|
43
|
+
currentOperation = custom(currentOperation, contract);
|
48
44
|
} else {
|
49
45
|
currentOperation = {
|
50
46
|
...currentOperation,
|
51
|
-
...
|
47
|
+
...custom
|
52
48
|
};
|
53
49
|
}
|
54
50
|
}
|
55
51
|
return currentOperation;
|
56
52
|
}
|
57
53
|
|
58
|
-
class
|
59
|
-
|
60
|
-
|
54
|
+
class CompositeSchemaConverter {
|
55
|
+
converters;
|
56
|
+
constructor(converters) {
|
57
|
+
this.converters = converters;
|
61
58
|
}
|
62
|
-
|
63
|
-
const
|
64
|
-
|
65
|
-
|
66
|
-
|
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
|
-
};
|
59
|
+
convert(schema, options) {
|
60
|
+
for (const converter of this.converters) {
|
61
|
+
if (converter.condition(schema, options)) {
|
62
|
+
return converter.convert(schema, options);
|
63
|
+
}
|
78
64
|
}
|
79
|
-
return
|
65
|
+
return [false, {}];
|
80
66
|
}
|
81
67
|
}
|
82
68
|
|
83
|
-
class
|
69
|
+
class OpenAPIGeneratorError extends Error {
|
84
70
|
}
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
this.
|
90
|
-
this.
|
71
|
+
class OpenAPIGenerator {
|
72
|
+
serializer;
|
73
|
+
converter;
|
74
|
+
constructor(options = {}) {
|
75
|
+
this.serializer = new StandardOpenAPIJsonSerializer(options);
|
76
|
+
this.converter = new CompositeSchemaConverter(options.schemaConverters ?? []);
|
91
77
|
}
|
92
|
-
|
93
|
-
const
|
94
|
-
|
95
|
-
const
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
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
|
+
);
|
108
117
|
}
|
118
|
+
return this.serializer.serialize(doc)[0];
|
109
119
|
}
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
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;
|
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" })
|
129
|
+
)
|
130
|
+
};
|
131
|
+
return;
|
135
132
|
}
|
136
|
-
|
137
|
-
|
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
138
|
}
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
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;
|
148
152
|
}
|
149
|
-
|
150
|
-
|
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
|
+
);
|
151
161
|
}
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
162
|
+
ref.parameters ??= [];
|
163
|
+
ref.parameters.push(...toOpenAPIParameters(schema, "query"));
|
164
|
+
} else {
|
165
|
+
ref.requestBody = {
|
166
|
+
required,
|
167
|
+
content: toOpenAPIContent(schema)
|
157
168
|
};
|
158
169
|
}
|
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.`);
|
170
|
+
return;
|
168
171
|
}
|
169
|
-
const
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
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
|
-
};
|
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;
|
191
177
|
}
|
192
|
-
if (
|
193
|
-
|
194
|
-
|
195
|
-
|
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
|
+
);
|
196
182
|
}
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
if (headersSchema !== void 0 && this.schemaUtils.isAnySchema(headersSchema)) {
|
208
|
-
headersSchema = void 0;
|
183
|
+
for (const from of ["params", "query", "headers"]) {
|
184
|
+
const fromSchema = schema.properties?.[from];
|
185
|
+
if (fromSchema !== void 0) {
|
186
|
+
if (!isObjectSchema(fromSchema)) {
|
187
|
+
throw error;
|
188
|
+
}
|
189
|
+
const parameterIn = from === "params" ? "path" : from === "headers" ? "header" : "query";
|
190
|
+
ref.parameters ??= [];
|
191
|
+
ref.parameters.push(...toOpenAPIParameters(fromSchema, parameterIn));
|
192
|
+
}
|
209
193
|
}
|
210
|
-
if (
|
211
|
-
|
194
|
+
if (schema.properties?.body !== void 0) {
|
195
|
+
ref.requestBody = {
|
196
|
+
required: schema.required?.includes("body"),
|
197
|
+
content: toOpenAPIContent(schema.properties.body)
|
198
|
+
};
|
212
199
|
}
|
213
|
-
return { headersSchema, bodySchema };
|
214
|
-
}
|
215
|
-
parseCompactSchema(outputSchema) {
|
216
|
-
return {
|
217
|
-
headersSchema: void 0,
|
218
|
-
bodySchema: outputSchema
|
219
|
-
};
|
220
200
|
}
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
const
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
...schema === true ? {} : schema === false ? { not: {} } : schema
|
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
|
+
)
|
236
215
|
};
|
237
|
-
|
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
|
-
});
|
216
|
+
return;
|
246
217
|
}
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
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;
|
254
226
|
}
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
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
|
-
}
|
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;
|
282
232
|
}
|
283
|
-
|
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;
|
233
|
+
if (json.properties?.headers !== void 0) {
|
234
|
+
if (!isObjectSchema(json.properties.headers)) {
|
235
|
+
throw error;
|
329
236
|
}
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
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;
|
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
|
+
};
|
361
243
|
}
|
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
244
|
}
|
379
|
-
if (
|
380
|
-
|
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 }];
|
245
|
+
if (json.properties?.body !== void 0) {
|
246
|
+
ref.responses[status].content = toOpenAPIContent(json.properties.body);
|
387
247
|
}
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
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;
|
394
256
|
}
|
395
|
-
|
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
|
+
});
|
396
272
|
}
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
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({
|
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
|
+
{
|
595
282
|
type: "object",
|
596
283
|
properties: {
|
597
284
|
defined: { const: false },
|
@@ -601,61 +288,16 @@ class OpenAPIGenerator {
|
|
601
288
|
data: {}
|
602
289
|
},
|
603
290
|
required: ["defined", "code", "status", "message"]
|
604
|
-
}
|
605
|
-
|
606
|
-
|
607
|
-
|
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];
|
291
|
+
}
|
292
|
+
]
|
293
|
+
})
|
294
|
+
};
|
295
|
+
}
|
654
296
|
}
|
655
297
|
}
|
656
298
|
|
657
299
|
const oo = {
|
658
|
-
spec:
|
300
|
+
spec: customOpenAPIOperation
|
659
301
|
};
|
660
302
|
|
661
|
-
export { CompositeSchemaConverter,
|
303
|
+
export { CompositeSchemaConverter, OpenAPIGenerator, applyCustomOpenAPIOperation, checkParamsSchema, customOpenAPIOperation, getCustomOpenAPIOperation, getDynamicParams, isAnySchema, isObjectSchema, oo, separateObjectSchema, toOpenAPIContent, toOpenAPIEventIteratorContent, toOpenAPIMethod, toOpenAPIParameters, toOpenAPIPath, toOpenAPISchema };
|