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