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