@orpc/openapi 0.0.0-next.e7d7758 → 0.0.0-next.e98b833
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 +92 -0
- package/dist/adapters/fetch/index.d.mts +13 -0
- package/dist/adapters/fetch/index.d.ts +13 -0
- package/dist/adapters/fetch/index.mjs +11 -0
- package/dist/adapters/hono/index.d.mts +6 -0
- package/dist/adapters/hono/index.d.ts +6 -0
- package/dist/adapters/hono/index.mjs +11 -0
- package/dist/adapters/next/index.d.mts +6 -0
- package/dist/adapters/next/index.d.ts +6 -0
- package/dist/adapters/next/index.mjs +11 -0
- package/dist/adapters/node/index.d.mts +13 -0
- package/dist/adapters/node/index.d.ts +13 -0
- package/dist/adapters/node/index.mjs +33 -0
- package/dist/adapters/standard/index.d.mts +34 -0
- package/dist/adapters/standard/index.d.ts +34 -0
- package/dist/adapters/standard/index.mjs +7 -0
- package/dist/index.d.mts +119 -0
- package/dist/index.d.ts +119 -0
- package/dist/index.mjs +303 -0
- package/dist/shared/openapi.CJTe38Ya.mjs +145 -0
- package/dist/shared/openapi.CbzTVvGL.mjs +31 -0
- package/dist/shared/openapi.DZzpQAb-.mjs +231 -0
- package/package.json +37 -30
- package/dist/chunk-KNYXLM77.js +0 -107
- package/dist/chunk-YXHH6XHB.js +0 -642
- package/dist/fetch.js +0 -46
- package/dist/index.js +0 -490
- package/dist/node.js +0 -46
- package/dist/src/adapters/fetch/bracket-notation.d.ts +0 -84
- package/dist/src/adapters/fetch/index.d.ts +0 -10
- package/dist/src/adapters/fetch/input-structure-compact.d.ts +0 -6
- package/dist/src/adapters/fetch/input-structure-detailed.d.ts +0 -11
- package/dist/src/adapters/fetch/openapi-handler-server.d.ts +0 -7
- package/dist/src/adapters/fetch/openapi-handler-serverless.d.ts +0 -7
- package/dist/src/adapters/fetch/openapi-handler.d.ts +0 -33
- package/dist/src/adapters/fetch/openapi-payload-codec.d.ts +0 -15
- package/dist/src/adapters/fetch/openapi-procedure-matcher.d.ts +0 -19
- package/dist/src/adapters/fetch/schema-coercer.d.ts +0 -10
- package/dist/src/adapters/node/index.d.ts +0 -5
- package/dist/src/adapters/node/openapi-handler-server.d.ts +0 -7
- package/dist/src/adapters/node/openapi-handler-serverless.d.ts +0 -7
- package/dist/src/adapters/node/openapi-handler.d.ts +0 -12
- package/dist/src/adapters/node/types.d.ts +0 -2
- package/dist/src/index.d.ts +0 -12
- package/dist/src/json-serializer.d.ts +0 -5
- 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 -60
- package/dist/src/openapi-input-structure-parser.d.ts +0 -22
- 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 -18
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
import { isProcedure, resolveContractProcedures, toHttpPath } from '@orpc/server';
|
|
2
|
+
import { fallbackORPCErrorStatus, fallbackORPCErrorMessage } from '@orpc/client';
|
|
3
|
+
import { fallbackContractConfig, getEventIteratorSchemaDetails } from '@orpc/contract';
|
|
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';
|
|
9
|
+
|
|
10
|
+
const OPERATION_EXTENDER_SYMBOL = Symbol("ORPC_OPERATION_EXTENDER");
|
|
11
|
+
function customOpenAPIOperation(o, extend) {
|
|
12
|
+
return new Proxy(o, {
|
|
13
|
+
get(target, prop, receiver) {
|
|
14
|
+
if (prop === OPERATION_EXTENDER_SYMBOL) {
|
|
15
|
+
return extend;
|
|
16
|
+
}
|
|
17
|
+
return Reflect.get(target, prop, receiver);
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
function getCustomOpenAPIOperation(o) {
|
|
22
|
+
return o[OPERATION_EXTENDER_SYMBOL];
|
|
23
|
+
}
|
|
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;
|
|
28
|
+
if (maybeExtender) {
|
|
29
|
+
operationCustoms.push(maybeExtender);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
if (isProcedure(contract)) {
|
|
33
|
+
for (const middleware of contract["~orpc"].middlewares) {
|
|
34
|
+
const maybeExtender = getCustomOpenAPIOperation(middleware);
|
|
35
|
+
if (maybeExtender) {
|
|
36
|
+
operationCustoms.push(maybeExtender);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
let currentOperation = operation;
|
|
41
|
+
for (const custom of operationCustoms) {
|
|
42
|
+
if (typeof custom === "function") {
|
|
43
|
+
currentOperation = custom(currentOperation, contract);
|
|
44
|
+
} else {
|
|
45
|
+
currentOperation = {
|
|
46
|
+
...currentOperation,
|
|
47
|
+
...custom
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return currentOperation;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
class CompositeSchemaConverter {
|
|
55
|
+
converters;
|
|
56
|
+
constructor(converters) {
|
|
57
|
+
this.converters = converters;
|
|
58
|
+
}
|
|
59
|
+
convert(schema, options) {
|
|
60
|
+
for (const converter of this.converters) {
|
|
61
|
+
if (converter.condition(schema, options)) {
|
|
62
|
+
return converter.convert(schema, options);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return [false, {}];
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
class OpenAPIGeneratorError extends Error {
|
|
70
|
+
}
|
|
71
|
+
class OpenAPIGenerator {
|
|
72
|
+
serializer;
|
|
73
|
+
converter;
|
|
74
|
+
constructor(options = {}) {
|
|
75
|
+
this.serializer = new OpenAPISerializer();
|
|
76
|
+
this.converter = new CompositeSchemaConverter(options.schemaConverters ?? []);
|
|
77
|
+
}
|
|
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
|
+
);
|
|
117
|
+
}
|
|
118
|
+
return this.serializer.serialize(doc);
|
|
119
|
+
}
|
|
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;
|
|
132
|
+
}
|
|
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
|
+
}
|
|
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;
|
|
152
|
+
}
|
|
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
|
+
);
|
|
161
|
+
}
|
|
162
|
+
ref.parameters ??= [];
|
|
163
|
+
ref.parameters.push(...toOpenAPIParameters(schema, "query"));
|
|
164
|
+
} else {
|
|
165
|
+
ref.requestBody = {
|
|
166
|
+
required,
|
|
167
|
+
content: toOpenAPIContent(schema)
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
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;
|
|
177
|
+
}
|
|
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
|
+
);
|
|
182
|
+
}
|
|
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
|
+
}
|
|
193
|
+
}
|
|
194
|
+
if (schema.properties?.body !== void 0) {
|
|
195
|
+
ref.requestBody = {
|
|
196
|
+
required: schema.required?.includes("body"),
|
|
197
|
+
content: toOpenAPIContent(schema.properties.body)
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
}
|
|
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
|
+
)
|
|
215
|
+
};
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
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;
|
|
226
|
+
}
|
|
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;
|
|
232
|
+
}
|
|
233
|
+
if (json.properties?.headers !== void 0) {
|
|
234
|
+
if (!isObjectSchema(json.properties.headers)) {
|
|
235
|
+
throw error;
|
|
236
|
+
}
|
|
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
|
+
};
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
if (json.properties?.body !== void 0) {
|
|
246
|
+
ref.responses[status].content = toOpenAPIContent(json.properties.body);
|
|
247
|
+
}
|
|
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;
|
|
256
|
+
}
|
|
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
|
+
});
|
|
272
|
+
}
|
|
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
|
+
{
|
|
282
|
+
type: "object",
|
|
283
|
+
properties: {
|
|
284
|
+
defined: { const: false },
|
|
285
|
+
code: { type: "string" },
|
|
286
|
+
status: { type: "number" },
|
|
287
|
+
message: { type: "string" },
|
|
288
|
+
data: {}
|
|
289
|
+
},
|
|
290
|
+
required: ["defined", "code", "status", "message"]
|
|
291
|
+
}
|
|
292
|
+
]
|
|
293
|
+
})
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
const oo = {
|
|
300
|
+
spec: customOpenAPIOperation
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
export { CompositeSchemaConverter, OpenAPIGenerator, applyCustomOpenAPIOperation, checkParamsSchema, customOpenAPIOperation, getCustomOpenAPIOperation, getDynamicParams, isAnySchema, isObjectSchema, oo, separateObjectSchema, toOpenAPIContent, toOpenAPIEventIteratorContent, toOpenAPIMethod, toOpenAPIParameters, toOpenAPIPath, toOpenAPISchema };
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { fallbackContractConfig } from '@orpc/contract';
|
|
2
|
+
import { isObject } from '@orpc/shared';
|
|
3
|
+
import { traverseContractProcedures, toHttpPath, isProcedure, getLazyMeta, unlazy, getRouter, createContractedProcedure } from '@orpc/server';
|
|
4
|
+
import { createRouter, addRoute, findRoute } from 'rou3';
|
|
5
|
+
import { s as standardizeHTTPPath } from './openapi.DZzpQAb-.mjs';
|
|
6
|
+
|
|
7
|
+
class OpenAPICodec {
|
|
8
|
+
constructor(serializer) {
|
|
9
|
+
this.serializer = serializer;
|
|
10
|
+
}
|
|
11
|
+
async decode(request, params, procedure) {
|
|
12
|
+
const inputStructure = fallbackContractConfig("defaultInputStructure", procedure["~orpc"].route.inputStructure);
|
|
13
|
+
if (inputStructure === "compact") {
|
|
14
|
+
const data = request.method === "GET" ? this.serializer.deserialize(request.url.searchParams) : this.serializer.deserialize(await request.body());
|
|
15
|
+
if (data === void 0) {
|
|
16
|
+
return params;
|
|
17
|
+
}
|
|
18
|
+
if (isObject(data)) {
|
|
19
|
+
return {
|
|
20
|
+
...params,
|
|
21
|
+
...data
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
return data;
|
|
25
|
+
}
|
|
26
|
+
const deserializeSearchParams = () => {
|
|
27
|
+
return this.serializer.deserialize(request.url.searchParams);
|
|
28
|
+
};
|
|
29
|
+
return {
|
|
30
|
+
params,
|
|
31
|
+
get query() {
|
|
32
|
+
const value = deserializeSearchParams();
|
|
33
|
+
Object.defineProperty(this, "query", { value, writable: true });
|
|
34
|
+
return value;
|
|
35
|
+
},
|
|
36
|
+
set query(value) {
|
|
37
|
+
Object.defineProperty(this, "query", { value, writable: true });
|
|
38
|
+
},
|
|
39
|
+
headers: request.headers,
|
|
40
|
+
body: this.serializer.deserialize(await request.body())
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
encode(output, procedure) {
|
|
44
|
+
const successStatus = fallbackContractConfig("defaultSuccessStatus", procedure["~orpc"].route.successStatus);
|
|
45
|
+
const outputStructure = fallbackContractConfig("defaultOutputStructure", procedure["~orpc"].route.outputStructure);
|
|
46
|
+
if (outputStructure === "compact") {
|
|
47
|
+
return {
|
|
48
|
+
status: successStatus,
|
|
49
|
+
headers: {},
|
|
50
|
+
body: this.serializer.serialize(output)
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
if (!isObject(output)) {
|
|
54
|
+
throw new Error(
|
|
55
|
+
'Invalid output structure for "detailed" output. Expected format: { body: any, headers?: Record<string, string | string[] | undefined> }'
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
return {
|
|
59
|
+
status: successStatus,
|
|
60
|
+
headers: output.headers ?? {},
|
|
61
|
+
body: this.serializer.serialize(output.body)
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
encodeError(error) {
|
|
65
|
+
return {
|
|
66
|
+
status: error.status,
|
|
67
|
+
headers: {},
|
|
68
|
+
body: this.serializer.serialize(error.toJSON())
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function toRou3Pattern(path) {
|
|
74
|
+
return standardizeHTTPPath(path).replace(/\/\{\+([^}]+)\}/g, "/**:$1").replace(/\/\{([^}]+)\}/g, "/:$1");
|
|
75
|
+
}
|
|
76
|
+
function decodeParams(params) {
|
|
77
|
+
return Object.fromEntries(Object.entries(params).map(([key, value]) => [key, decodeURIComponent(value)]));
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
class OpenAPIMatcher {
|
|
81
|
+
tree = createRouter();
|
|
82
|
+
pendingRouters = [];
|
|
83
|
+
init(router, path = []) {
|
|
84
|
+
const laziedOptions = traverseContractProcedures({ router, path }, ({ path: path2, contract }) => {
|
|
85
|
+
const method = fallbackContractConfig("defaultMethod", contract["~orpc"].route.method);
|
|
86
|
+
const httpPath = toRou3Pattern(contract["~orpc"].route.path ?? toHttpPath(path2));
|
|
87
|
+
if (isProcedure(contract)) {
|
|
88
|
+
addRoute(this.tree, method, httpPath, {
|
|
89
|
+
path: path2,
|
|
90
|
+
contract,
|
|
91
|
+
procedure: contract,
|
|
92
|
+
// this mean dev not used contract-first so we can used contract as procedure directly
|
|
93
|
+
router
|
|
94
|
+
});
|
|
95
|
+
} else {
|
|
96
|
+
addRoute(this.tree, method, httpPath, {
|
|
97
|
+
path: path2,
|
|
98
|
+
contract,
|
|
99
|
+
procedure: void 0,
|
|
100
|
+
router
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
this.pendingRouters.push(...laziedOptions.map((option) => ({
|
|
105
|
+
...option,
|
|
106
|
+
httpPathPrefix: toHttpPath(option.path),
|
|
107
|
+
laziedPrefix: getLazyMeta(option.router).prefix
|
|
108
|
+
})));
|
|
109
|
+
}
|
|
110
|
+
async match(method, pathname) {
|
|
111
|
+
if (this.pendingRouters.length) {
|
|
112
|
+
const newPendingRouters = [];
|
|
113
|
+
for (const pendingRouter of this.pendingRouters) {
|
|
114
|
+
if (!pendingRouter.laziedPrefix || pathname.startsWith(pendingRouter.laziedPrefix) || pathname.startsWith(pendingRouter.httpPathPrefix)) {
|
|
115
|
+
const { default: router } = await unlazy(pendingRouter.router);
|
|
116
|
+
this.init(router, pendingRouter.path);
|
|
117
|
+
} else {
|
|
118
|
+
newPendingRouters.push(pendingRouter);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
this.pendingRouters = newPendingRouters;
|
|
122
|
+
}
|
|
123
|
+
const match = findRoute(this.tree, method, pathname);
|
|
124
|
+
if (!match) {
|
|
125
|
+
return void 0;
|
|
126
|
+
}
|
|
127
|
+
if (!match.data.procedure) {
|
|
128
|
+
const { default: maybeProcedure } = await unlazy(getRouter(match.data.router, match.data.path));
|
|
129
|
+
if (!isProcedure(maybeProcedure)) {
|
|
130
|
+
throw new Error(`
|
|
131
|
+
[Contract-First] Missing or invalid implementation for procedure at path: ${toHttpPath(match.data.path)}.
|
|
132
|
+
Ensure that the procedure is correctly defined and matches the expected contract.
|
|
133
|
+
`);
|
|
134
|
+
}
|
|
135
|
+
match.data.procedure = createContractedProcedure(maybeProcedure, match.data.contract);
|
|
136
|
+
}
|
|
137
|
+
return {
|
|
138
|
+
path: match.data.path,
|
|
139
|
+
procedure: match.data.procedure,
|
|
140
|
+
params: match.params ? decodeParams(match.params) : void 0
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export { OpenAPICodec as O, OpenAPIMatcher as a, decodeParams as d, toRou3Pattern as t };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { OpenAPISerializer } from '@orpc/openapi-client/standard';
|
|
2
|
+
import { StandardHandler } from '@orpc/server/standard';
|
|
3
|
+
import { toStandardLazyRequest, toFetchResponse } from '@orpc/standard-server-fetch';
|
|
4
|
+
import { a as OpenAPIMatcher, O as OpenAPICodec } from './openapi.CJTe38Ya.mjs';
|
|
5
|
+
import '@orpc/shared';
|
|
6
|
+
import 'json-schema-typed/draft-2020-12';
|
|
7
|
+
|
|
8
|
+
class OpenAPIHandler {
|
|
9
|
+
standardHandler;
|
|
10
|
+
constructor(router, options = {}) {
|
|
11
|
+
const serializer = new OpenAPISerializer();
|
|
12
|
+
const matcher = new OpenAPIMatcher();
|
|
13
|
+
const codec = new OpenAPICodec(serializer);
|
|
14
|
+
this.standardHandler = new StandardHandler(router, matcher, codec, options);
|
|
15
|
+
}
|
|
16
|
+
async handle(request, ...[
|
|
17
|
+
options = {}
|
|
18
|
+
]) {
|
|
19
|
+
const standardRequest = toStandardLazyRequest(request);
|
|
20
|
+
const result = await this.standardHandler.handle(standardRequest, options);
|
|
21
|
+
if (!result.matched) {
|
|
22
|
+
return result;
|
|
23
|
+
}
|
|
24
|
+
return {
|
|
25
|
+
matched: true,
|
|
26
|
+
response: toFetchResponse(result.response, options)
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export { OpenAPIHandler as O };
|