@orpc/openapi 0.17.0 → 0.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunk-KNYXLM77.js +107 -0
- package/dist/fetch.js +516 -622
- package/dist/index.js +257 -4480
- package/dist/src/fetch/bracket-notation.d.ts +84 -0
- package/dist/src/fetch/index.d.ts +9 -3
- package/dist/src/fetch/input-builder-full.d.ts +11 -0
- package/dist/src/fetch/input-builder-simple.d.ts +6 -0
- package/dist/src/fetch/openapi-handler-server.d.ts +7 -0
- package/dist/src/fetch/openapi-handler-serverless.d.ts +7 -0
- package/dist/src/fetch/openapi-handler.d.ts +30 -0
- package/dist/src/fetch/openapi-payload-codec.d.ts +15 -0
- package/dist/src/fetch/openapi-procedure-matcher.d.ts +19 -0
- package/dist/src/fetch/schema-coercer.d.ts +10 -0
- package/dist/src/index.d.ts +10 -1
- package/dist/src/json-serializer.d.ts +5 -0
- package/dist/src/openapi-content-builder.d.ts +10 -0
- package/dist/src/openapi-generator.d.ts +51 -0
- package/dist/src/openapi-parameters-builder.d.ts +9 -0
- package/dist/src/openapi-path-parser.d.ts +8 -0
- package/dist/src/openapi.d.ts +3 -0
- package/dist/src/schema-converter.d.ts +16 -0
- package/dist/src/schema-utils.d.ts +11 -0
- package/dist/src/schema.d.ts +12 -0
- package/dist/src/utils.d.ts +3 -2
- package/package.json +9 -7
- package/dist/chunk-CMRY2Z4J.js +0 -54
- package/dist/src/fetch/base-handler.d.ts +0 -13
- package/dist/src/fetch/server-handler.d.ts +0 -3
- package/dist/src/fetch/serverless-handler.d.ts +0 -3
- package/dist/src/generator.d.ts +0 -24
- package/dist/src/zod-to-json-schema.d.ts +0 -43
package/dist/fetch.js
CHANGED
|
@@ -1,697 +1,591 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
2
|
+
JSONSerializer,
|
|
3
|
+
forEachContractProcedure,
|
|
3
4
|
standardizeHTTPPath
|
|
4
|
-
} from "./chunk-
|
|
5
|
+
} from "./chunk-KNYXLM77.js";
|
|
5
6
|
|
|
6
|
-
// src/fetch/
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
if (
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const context = await value(options.context);
|
|
17
|
-
const accept = options.request.headers.get("Accept") || void 0;
|
|
18
|
-
const serializer = new OpenAPISerializer({ accept });
|
|
19
|
-
const handler = async () => {
|
|
20
|
-
const url = new URL(options.request.url);
|
|
21
|
-
const pathname = `/${trim(url.pathname.replace(options.prefix ?? "", ""), "/")}`;
|
|
22
|
-
const customMethod = options.request.method === "POST" ? url.searchParams.get("method")?.toUpperCase() : void 0;
|
|
23
|
-
const method = customMethod || options.request.method;
|
|
24
|
-
const match = await resolveRouter(options.router, method, pathname);
|
|
25
|
-
if (!match) {
|
|
26
|
-
throw new ORPCError({ code: "NOT_FOUND", message: "Not found" });
|
|
27
|
-
}
|
|
28
|
-
const { path, procedure } = match;
|
|
29
|
-
const params = procedure["~orpc"].contract["~orpc"].InputSchema ? zodCoerce(
|
|
30
|
-
procedure["~orpc"].contract["~orpc"].InputSchema,
|
|
31
|
-
match.params,
|
|
32
|
-
{ bracketNotation: true }
|
|
33
|
-
) : match.params;
|
|
34
|
-
const input = await deserializeInput(options.request, procedure);
|
|
35
|
-
const mergedInput = mergeParamsAndInput(params, input);
|
|
36
|
-
const caller = createProcedureClient({
|
|
37
|
-
context,
|
|
38
|
-
procedure,
|
|
39
|
-
path
|
|
40
|
-
});
|
|
41
|
-
const output = await caller(mergedInput, { signal: options.signal });
|
|
42
|
-
const { body, headers } = serializer.serialize(output);
|
|
43
|
-
return new Response(body, {
|
|
44
|
-
status: 200,
|
|
45
|
-
headers
|
|
46
|
-
});
|
|
47
|
-
};
|
|
48
|
-
try {
|
|
49
|
-
return await executeWithHooks({
|
|
50
|
-
context,
|
|
51
|
-
hooks: options,
|
|
52
|
-
execute: handler,
|
|
53
|
-
input: options.request,
|
|
54
|
-
meta: {
|
|
55
|
-
signal: options.signal
|
|
56
|
-
}
|
|
7
|
+
// src/fetch/bracket-notation.ts
|
|
8
|
+
import { isPlainObject } from "@orpc/shared";
|
|
9
|
+
function serialize(payload, parentKey = "") {
|
|
10
|
+
if (!Array.isArray(payload) && !isPlainObject(payload))
|
|
11
|
+
return [["", payload]];
|
|
12
|
+
const result = [];
|
|
13
|
+
function helper(value, path) {
|
|
14
|
+
if (Array.isArray(value)) {
|
|
15
|
+
value.forEach((item, index) => {
|
|
16
|
+
helper(item, [...path, String(index)]);
|
|
57
17
|
});
|
|
58
|
-
}
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
const { body, headers } = serializer.serialize(error.toJSON());
|
|
62
|
-
return new Response(body, {
|
|
63
|
-
status: error.status,
|
|
64
|
-
headers
|
|
65
|
-
});
|
|
66
|
-
} catch (e2) {
|
|
67
|
-
const error2 = toORPCError(e2);
|
|
68
|
-
const { body, headers } = new OpenAPISerializer().serialize(
|
|
69
|
-
error2.toJSON()
|
|
70
|
-
);
|
|
71
|
-
return new Response(body, {
|
|
72
|
-
status: error2.status,
|
|
73
|
-
headers
|
|
74
|
-
});
|
|
18
|
+
} else if (isPlainObject(value)) {
|
|
19
|
+
for (const [key, val] of Object.entries(value)) {
|
|
20
|
+
helper(val, [...path, key]);
|
|
75
21
|
}
|
|
22
|
+
} else {
|
|
23
|
+
result.push([stringifyPath(path), value]);
|
|
76
24
|
}
|
|
77
|
-
}
|
|
25
|
+
}
|
|
26
|
+
helper(payload, parentKey ? [parentKey] : []);
|
|
27
|
+
return result;
|
|
78
28
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
29
|
+
function deserialize(entities) {
|
|
30
|
+
if (entities.length === 0) {
|
|
31
|
+
return void 0;
|
|
32
|
+
}
|
|
33
|
+
const isRootArray = entities.every(([path]) => path === "");
|
|
34
|
+
const result = isRootArray ? [] : {};
|
|
35
|
+
const arrayPushPaths = /* @__PURE__ */ new Set();
|
|
36
|
+
for (const [path, _] of entities) {
|
|
37
|
+
const segments = parsePath(path);
|
|
38
|
+
const base = segments.slice(0, -1).join(".");
|
|
39
|
+
const last = segments[segments.length - 1];
|
|
40
|
+
if (last === "") {
|
|
41
|
+
arrayPushPaths.add(base);
|
|
42
|
+
} else {
|
|
43
|
+
arrayPushPaths.delete(base);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
function setValue(obj, segments, value, fullPath) {
|
|
47
|
+
const [first, ...rest_] = segments;
|
|
48
|
+
if (Array.isArray(obj) && first === "") {
|
|
49
|
+
;
|
|
50
|
+
obj.push(value);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
const objAsRecord = obj;
|
|
54
|
+
if (rest_.length === 0) {
|
|
55
|
+
objAsRecord[first] = value;
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
const rest = rest_;
|
|
59
|
+
if (rest[0] === "") {
|
|
60
|
+
const pathToCheck = segments.slice(0, -1).join(".");
|
|
61
|
+
if (rest.length === 1 && arrayPushPaths.has(pathToCheck)) {
|
|
62
|
+
if (!(first in objAsRecord)) {
|
|
63
|
+
objAsRecord[first] = [];
|
|
64
|
+
}
|
|
65
|
+
if (Array.isArray(objAsRecord[first])) {
|
|
66
|
+
;
|
|
67
|
+
objAsRecord[first].push(value);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
105
70
|
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
const newPending = [];
|
|
109
|
-
for (const item of pending.ref) {
|
|
110
|
-
const lazyPrefix = getLazyRouterPrefix(item.lazy);
|
|
111
|
-
if (lazyPrefix && !pathname.startsWith(lazyPrefix) && !pathname.startsWith(`/${item.path.map(encodeURIComponent).join("/")}`)) {
|
|
112
|
-
newPending.push(item);
|
|
113
|
-
continue;
|
|
71
|
+
if (!(first in objAsRecord)) {
|
|
72
|
+
objAsRecord[first] = {};
|
|
114
73
|
}
|
|
115
|
-
const
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
pending.ref = newPending;
|
|
119
|
-
const [matches, params_] = routing.match(method, pathname);
|
|
120
|
-
const [match] = matches.sort((a, b) => {
|
|
121
|
-
return Object.keys(a[1]).length - Object.keys(b[1]).length;
|
|
122
|
-
});
|
|
123
|
-
if (!match) {
|
|
124
|
-
return void 0;
|
|
74
|
+
const target = objAsRecord[first];
|
|
75
|
+
target[""] = value;
|
|
76
|
+
return;
|
|
125
77
|
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
match[1],
|
|
129
|
-
(v) => params_[v]
|
|
130
|
-
) : match[1];
|
|
131
|
-
const { default: maybeProcedure } = await unlazy(getRouterChild(router, ...path));
|
|
132
|
-
if (!isProcedure(maybeProcedure)) {
|
|
133
|
-
return void 0;
|
|
78
|
+
if (!(first in objAsRecord)) {
|
|
79
|
+
objAsRecord[first] = {};
|
|
134
80
|
}
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
};
|
|
142
|
-
}
|
|
143
|
-
function mergeParamsAndInput(coercedParams, input) {
|
|
144
|
-
if (Object.keys(coercedParams).length === 0) {
|
|
145
|
-
return input;
|
|
81
|
+
setValue(
|
|
82
|
+
objAsRecord[first],
|
|
83
|
+
rest,
|
|
84
|
+
value,
|
|
85
|
+
fullPath
|
|
86
|
+
);
|
|
146
87
|
}
|
|
147
|
-
|
|
148
|
-
|
|
88
|
+
for (const [path, value] of entities) {
|
|
89
|
+
const segments = parsePath(path);
|
|
90
|
+
setValue(result, segments, value, path);
|
|
149
91
|
}
|
|
150
|
-
return
|
|
151
|
-
...coercedParams,
|
|
152
|
-
...input
|
|
153
|
-
};
|
|
92
|
+
return result;
|
|
154
93
|
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
function toORPCError(e) {
|
|
170
|
-
return e instanceof ORPCError ? e : new ORPCError({
|
|
171
|
-
code: "INTERNAL_SERVER_ERROR",
|
|
172
|
-
message: "Internal server error",
|
|
173
|
-
cause: e
|
|
94
|
+
function escapeSegment(segment) {
|
|
95
|
+
return segment.replace(/[\\[\]]/g, (match) => {
|
|
96
|
+
switch (match) {
|
|
97
|
+
case "\\":
|
|
98
|
+
return "\\\\";
|
|
99
|
+
case "[":
|
|
100
|
+
return "\\[";
|
|
101
|
+
case "]":
|
|
102
|
+
return "\\]";
|
|
103
|
+
default:
|
|
104
|
+
return match;
|
|
105
|
+
}
|
|
174
106
|
});
|
|
175
107
|
}
|
|
176
|
-
function
|
|
177
|
-
|
|
108
|
+
function stringifyPath(path) {
|
|
109
|
+
const [first, ...rest] = path;
|
|
110
|
+
const firstSegment = escapeSegment(first);
|
|
111
|
+
const base = first === "" ? "" : firstSegment;
|
|
112
|
+
return rest.reduce(
|
|
113
|
+
(result, segment) => `${result}[${escapeSegment(segment)}]`,
|
|
114
|
+
base
|
|
115
|
+
);
|
|
178
116
|
}
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
117
|
+
function parsePath(path) {
|
|
118
|
+
if (path === "")
|
|
119
|
+
return [""];
|
|
120
|
+
const result = [];
|
|
121
|
+
let currentSegment = "";
|
|
122
|
+
let inBracket = false;
|
|
123
|
+
let bracketContent = "";
|
|
124
|
+
let backslashCount = 0;
|
|
125
|
+
for (let i = 0; i < path.length; i++) {
|
|
126
|
+
const char = path[i];
|
|
127
|
+
if (char === "\\") {
|
|
128
|
+
backslashCount++;
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
if (backslashCount > 0) {
|
|
132
|
+
const literalBackslashes = "\\".repeat(Math.floor(backslashCount / 2));
|
|
133
|
+
if (char === "[" || char === "]") {
|
|
134
|
+
if (backslashCount % 2 === 1) {
|
|
135
|
+
if (inBracket) {
|
|
136
|
+
bracketContent += literalBackslashes + char;
|
|
137
|
+
} else {
|
|
138
|
+
currentSegment += literalBackslashes + char;
|
|
139
|
+
}
|
|
201
140
|
} else {
|
|
202
|
-
|
|
141
|
+
if (inBracket) {
|
|
142
|
+
bracketContent += literalBackslashes;
|
|
143
|
+
} else {
|
|
144
|
+
currentSegment += literalBackslashes;
|
|
145
|
+
}
|
|
146
|
+
if (char === "[" && !inBracket) {
|
|
147
|
+
if (currentSegment !== "" || result.length === 0) {
|
|
148
|
+
result.push(currentSegment);
|
|
149
|
+
}
|
|
150
|
+
inBracket = true;
|
|
151
|
+
bracketContent = "";
|
|
152
|
+
currentSegment = "";
|
|
153
|
+
} else if (char === "]" && inBracket) {
|
|
154
|
+
result.push(bracketContent);
|
|
155
|
+
inBracket = false;
|
|
156
|
+
bracketContent = "";
|
|
157
|
+
} else {
|
|
158
|
+
if (inBracket) {
|
|
159
|
+
bracketContent += char;
|
|
160
|
+
} else {
|
|
161
|
+
currentSegment += char;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
203
164
|
}
|
|
204
|
-
const optionalSegment = segment.replace("?", "");
|
|
205
|
-
basePath += "/" + optionalSegment;
|
|
206
|
-
results.push(basePath);
|
|
207
165
|
} else {
|
|
208
|
-
|
|
166
|
+
const allBackslashes = "\\".repeat(backslashCount);
|
|
167
|
+
if (inBracket) {
|
|
168
|
+
bracketContent += allBackslashes + char;
|
|
169
|
+
} else {
|
|
170
|
+
currentSegment += allBackslashes + char;
|
|
171
|
+
}
|
|
209
172
|
}
|
|
173
|
+
backslashCount = 0;
|
|
174
|
+
continue;
|
|
210
175
|
}
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
// ../../node_modules/.pnpm/hono@4.6.13/node_modules/hono/dist/router/reg-exp-router/node.js
|
|
216
|
-
var LABEL_REG_EXP_STR = "[^/]+";
|
|
217
|
-
var ONLY_WILDCARD_REG_EXP_STR = ".*";
|
|
218
|
-
var TAIL_WILDCARD_REG_EXP_STR = "(?:|/.*)";
|
|
219
|
-
var PATH_ERROR = Symbol();
|
|
220
|
-
var regExpMetaChars = new Set(".\\+*[^]$()");
|
|
221
|
-
function compareKey(a, b) {
|
|
222
|
-
if (a.length === 1) {
|
|
223
|
-
return b.length === 1 ? a < b ? -1 : 1 : -1;
|
|
224
|
-
}
|
|
225
|
-
if (b.length === 1) {
|
|
226
|
-
return 1;
|
|
227
|
-
}
|
|
228
|
-
if (a === ONLY_WILDCARD_REG_EXP_STR || a === TAIL_WILDCARD_REG_EXP_STR) {
|
|
229
|
-
return 1;
|
|
230
|
-
} else if (b === ONLY_WILDCARD_REG_EXP_STR || b === TAIL_WILDCARD_REG_EXP_STR) {
|
|
231
|
-
return -1;
|
|
232
|
-
}
|
|
233
|
-
if (a === LABEL_REG_EXP_STR) {
|
|
234
|
-
return 1;
|
|
235
|
-
} else if (b === LABEL_REG_EXP_STR) {
|
|
236
|
-
return -1;
|
|
237
|
-
}
|
|
238
|
-
return a.length === b.length ? a < b ? -1 : 1 : b.length - a.length;
|
|
239
|
-
}
|
|
240
|
-
var Node = class {
|
|
241
|
-
#index;
|
|
242
|
-
#varIndex;
|
|
243
|
-
#children = /* @__PURE__ */ Object.create(null);
|
|
244
|
-
insert(tokens, index, paramMap, context, pathErrorCheckOnly) {
|
|
245
|
-
if (tokens.length === 0) {
|
|
246
|
-
if (this.#index !== void 0) {
|
|
247
|
-
throw PATH_ERROR;
|
|
248
|
-
}
|
|
249
|
-
if (pathErrorCheckOnly) {
|
|
250
|
-
return;
|
|
176
|
+
if (char === "[" && !inBracket) {
|
|
177
|
+
if (currentSegment !== "" || result.length === 0) {
|
|
178
|
+
result.push(currentSegment);
|
|
251
179
|
}
|
|
252
|
-
|
|
253
|
-
|
|
180
|
+
inBracket = true;
|
|
181
|
+
bracketContent = "";
|
|
182
|
+
currentSegment = "";
|
|
183
|
+
continue;
|
|
254
184
|
}
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
if (/\((?!\?:)/.test(regexpStr)) {
|
|
264
|
-
throw PATH_ERROR;
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
node = this.#children[regexpStr];
|
|
268
|
-
if (!node) {
|
|
269
|
-
if (Object.keys(this.#children).some(
|
|
270
|
-
(k) => k !== ONLY_WILDCARD_REG_EXP_STR && k !== TAIL_WILDCARD_REG_EXP_STR
|
|
271
|
-
)) {
|
|
272
|
-
throw PATH_ERROR;
|
|
273
|
-
}
|
|
274
|
-
if (pathErrorCheckOnly) {
|
|
275
|
-
return;
|
|
276
|
-
}
|
|
277
|
-
node = this.#children[regexpStr] = new Node();
|
|
278
|
-
if (name !== "") {
|
|
279
|
-
node.#varIndex = context.varIndex++;
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
if (!pathErrorCheckOnly && name !== "") {
|
|
283
|
-
paramMap.push([name, node.#varIndex]);
|
|
284
|
-
}
|
|
185
|
+
if (char === "]" && inBracket) {
|
|
186
|
+
result.push(bracketContent);
|
|
187
|
+
inBracket = false;
|
|
188
|
+
bracketContent = "";
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
if (inBracket) {
|
|
192
|
+
bracketContent += char;
|
|
285
193
|
} else {
|
|
286
|
-
|
|
287
|
-
if (!node) {
|
|
288
|
-
if (Object.keys(this.#children).some(
|
|
289
|
-
(k) => k.length > 1 && k !== ONLY_WILDCARD_REG_EXP_STR && k !== TAIL_WILDCARD_REG_EXP_STR
|
|
290
|
-
)) {
|
|
291
|
-
throw PATH_ERROR;
|
|
292
|
-
}
|
|
293
|
-
if (pathErrorCheckOnly) {
|
|
294
|
-
return;
|
|
295
|
-
}
|
|
296
|
-
node = this.#children[token] = new Node();
|
|
297
|
-
}
|
|
194
|
+
currentSegment += char;
|
|
298
195
|
}
|
|
299
|
-
node.insert(restTokens, index, paramMap, context, pathErrorCheckOnly);
|
|
300
196
|
}
|
|
301
|
-
|
|
302
|
-
const
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
if (typeof this.#index === "number") {
|
|
308
|
-
strList.unshift(`#${this.#index}`);
|
|
309
|
-
}
|
|
310
|
-
if (strList.length === 0) {
|
|
311
|
-
return "";
|
|
197
|
+
if (backslashCount > 0) {
|
|
198
|
+
const remainingBackslashes = "\\".repeat(backslashCount);
|
|
199
|
+
if (inBracket) {
|
|
200
|
+
bracketContent += remainingBackslashes;
|
|
201
|
+
} else {
|
|
202
|
+
currentSegment += remainingBackslashes;
|
|
312
203
|
}
|
|
313
|
-
|
|
314
|
-
|
|
204
|
+
}
|
|
205
|
+
if (inBracket) {
|
|
206
|
+
if (currentSegment !== "" || result.length === 0) {
|
|
207
|
+
result.push(currentSegment);
|
|
315
208
|
}
|
|
316
|
-
|
|
209
|
+
result.push(`[${bracketContent}`);
|
|
210
|
+
} else if (currentSegment !== "" || result.length === 0) {
|
|
211
|
+
result.push(currentSegment);
|
|
212
|
+
}
|
|
213
|
+
return result;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// src/fetch/input-builder-full.ts
|
|
217
|
+
var InputBuilderFull = class {
|
|
218
|
+
build(params, query, headers, body) {
|
|
219
|
+
return {
|
|
220
|
+
params,
|
|
221
|
+
query,
|
|
222
|
+
headers,
|
|
223
|
+
body
|
|
224
|
+
};
|
|
317
225
|
}
|
|
318
226
|
};
|
|
319
227
|
|
|
320
|
-
//
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
const groups = [];
|
|
327
|
-
for (let i = 0; ; ) {
|
|
328
|
-
let replaced = false;
|
|
329
|
-
path = path.replace(/\{[^}]+\}/g, (m) => {
|
|
330
|
-
const mark = `@\\${i}`;
|
|
331
|
-
groups[i] = [mark, m];
|
|
332
|
-
i++;
|
|
333
|
-
replaced = true;
|
|
334
|
-
return mark;
|
|
335
|
-
});
|
|
336
|
-
if (!replaced) {
|
|
337
|
-
break;
|
|
338
|
-
}
|
|
228
|
+
// src/fetch/input-builder-simple.ts
|
|
229
|
+
import { isPlainObject as isPlainObject2 } from "@orpc/shared";
|
|
230
|
+
var InputBuilderSimple = class {
|
|
231
|
+
build(params, payload) {
|
|
232
|
+
if (Object.keys(params).length === 0) {
|
|
233
|
+
return payload;
|
|
339
234
|
}
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
const [mark] = groups[i];
|
|
343
|
-
for (let j = tokens.length - 1; j >= 0; j--) {
|
|
344
|
-
if (tokens[j].indexOf(mark) !== -1) {
|
|
345
|
-
tokens[j] = tokens[j].replace(mark, groups[i][1]);
|
|
346
|
-
break;
|
|
347
|
-
}
|
|
348
|
-
}
|
|
235
|
+
if (!isPlainObject2(payload)) {
|
|
236
|
+
return params;
|
|
349
237
|
}
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
let regexp = this.#root.buildRegExpStr();
|
|
355
|
-
if (regexp === "") {
|
|
356
|
-
return [/^$/, [], []];
|
|
357
|
-
}
|
|
358
|
-
let captureIndex = 0;
|
|
359
|
-
const indexReplacementMap = [];
|
|
360
|
-
const paramReplacementMap = [];
|
|
361
|
-
regexp = regexp.replace(/#(\d+)|@(\d+)|\.\*\$/g, (_, handlerIndex, paramIndex) => {
|
|
362
|
-
if (handlerIndex !== void 0) {
|
|
363
|
-
indexReplacementMap[++captureIndex] = Number(handlerIndex);
|
|
364
|
-
return "$()";
|
|
365
|
-
}
|
|
366
|
-
if (paramIndex !== void 0) {
|
|
367
|
-
paramReplacementMap[Number(paramIndex)] = ++captureIndex;
|
|
368
|
-
return "";
|
|
369
|
-
}
|
|
370
|
-
return "";
|
|
371
|
-
});
|
|
372
|
-
return [new RegExp(`^${regexp}`), indexReplacementMap, paramReplacementMap];
|
|
238
|
+
return {
|
|
239
|
+
...params,
|
|
240
|
+
...payload
|
|
241
|
+
};
|
|
373
242
|
}
|
|
374
243
|
};
|
|
375
244
|
|
|
376
|
-
//
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
wildcardRegExpCache = /* @__PURE__ */ Object.create(null);
|
|
390
|
-
}
|
|
391
|
-
function buildMatcherFromPreprocessedRoutes(routes) {
|
|
392
|
-
const trie = new Trie();
|
|
393
|
-
const handlerData = [];
|
|
394
|
-
if (routes.length === 0) {
|
|
395
|
-
return nullMatcher;
|
|
245
|
+
// src/fetch/openapi-handler.ts
|
|
246
|
+
import { createProcedureClient, ORPCError as ORPCError2 } from "@orpc/server";
|
|
247
|
+
import { executeWithHooks, ORPC_HANDLER_HEADER, trim } from "@orpc/shared";
|
|
248
|
+
|
|
249
|
+
// src/fetch/openapi-payload-codec.ts
|
|
250
|
+
import { ORPCError } from "@orpc/server";
|
|
251
|
+
import { findDeepMatches } from "@orpc/shared";
|
|
252
|
+
import cd from "content-disposition";
|
|
253
|
+
import { safeParse } from "fast-content-type-parse";
|
|
254
|
+
import wcmatch from "wildcard-match";
|
|
255
|
+
var OpenAPIPayloadCodec = class {
|
|
256
|
+
constructor(jsonSerializer) {
|
|
257
|
+
this.jsonSerializer = jsonSerializer;
|
|
396
258
|
}
|
|
397
|
-
|
|
398
|
-
(
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
259
|
+
encode(payload, accept) {
|
|
260
|
+
const typeMatchers = (accept?.split(",").map(safeParse) ?? [{ type: "*/*" }]).map(({ type }) => wcmatch(type));
|
|
261
|
+
if (payload instanceof Blob) {
|
|
262
|
+
const contentType = payload.type || "application/octet-stream";
|
|
263
|
+
if (typeMatchers.some((isMatch) => isMatch(contentType))) {
|
|
264
|
+
const headers = new Headers({
|
|
265
|
+
"Content-Type": contentType
|
|
266
|
+
});
|
|
267
|
+
if (payload instanceof File && payload.name) {
|
|
268
|
+
headers.append("Content-Disposition", cd(payload.name));
|
|
269
|
+
}
|
|
270
|
+
return {
|
|
271
|
+
body: payload,
|
|
272
|
+
headers
|
|
273
|
+
};
|
|
274
|
+
}
|
|
409
275
|
}
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
276
|
+
const handledPayload = this.jsonSerializer.serialize(payload);
|
|
277
|
+
const hasBlobs = findDeepMatches((v) => v instanceof Blob, handledPayload).values.length > 0;
|
|
278
|
+
const isExpectedMultipartFormData = typeMatchers.some(
|
|
279
|
+
(isMatch) => isMatch("multipart/form-data")
|
|
280
|
+
);
|
|
281
|
+
if (hasBlobs && isExpectedMultipartFormData) {
|
|
282
|
+
return this.encodeAsFormData(handledPayload);
|
|
415
283
|
}
|
|
416
|
-
if (
|
|
417
|
-
|
|
284
|
+
if (typeMatchers.some((isMatch) => isMatch("application/json"))) {
|
|
285
|
+
return this.encodeAsJSON(handledPayload);
|
|
418
286
|
}
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
287
|
+
if (typeMatchers.some(
|
|
288
|
+
(isMatch) => isMatch("application/x-www-form-urlencoded")
|
|
289
|
+
)) {
|
|
290
|
+
return this.encodeAsURLSearchParams(handledPayload);
|
|
291
|
+
}
|
|
292
|
+
if (isExpectedMultipartFormData) {
|
|
293
|
+
return this.encodeAsFormData(handledPayload);
|
|
294
|
+
}
|
|
295
|
+
throw new ORPCError({
|
|
296
|
+
code: "NOT_ACCEPTABLE",
|
|
297
|
+
message: `Unsupported content-type: ${accept}`
|
|
427
298
|
});
|
|
428
299
|
}
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
for (let k = 0, len3 = keys.length; k < len3; k++) {
|
|
438
|
-
map[keys[k]] = paramReplacementMap[map[keys[k]]];
|
|
439
|
-
}
|
|
300
|
+
encodeAsJSON(payload) {
|
|
301
|
+
if (payload === void 0) {
|
|
302
|
+
return {
|
|
303
|
+
body: void 0,
|
|
304
|
+
headers: new Headers({
|
|
305
|
+
"content-type": "application/json"
|
|
306
|
+
})
|
|
307
|
+
};
|
|
440
308
|
}
|
|
309
|
+
return {
|
|
310
|
+
body: JSON.stringify(payload),
|
|
311
|
+
headers: new Headers({
|
|
312
|
+
"content-type": "application/json"
|
|
313
|
+
})
|
|
314
|
+
};
|
|
441
315
|
}
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
}
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
316
|
+
encodeAsFormData(payload) {
|
|
317
|
+
const form = new FormData();
|
|
318
|
+
for (const [path, value] of serialize(payload)) {
|
|
319
|
+
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
|
|
320
|
+
form.append(path, value.toString());
|
|
321
|
+
} else if (value === null) {
|
|
322
|
+
form.append(path, "null");
|
|
323
|
+
} else if (value instanceof Date) {
|
|
324
|
+
form.append(
|
|
325
|
+
path,
|
|
326
|
+
Number.isNaN(value.getTime()) ? "Invalid Date" : value.toISOString()
|
|
327
|
+
);
|
|
328
|
+
} else if (value instanceof Blob) {
|
|
329
|
+
form.append(path, value);
|
|
330
|
+
}
|
|
455
331
|
}
|
|
332
|
+
return {
|
|
333
|
+
body: form
|
|
334
|
+
};
|
|
456
335
|
}
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
336
|
+
encodeAsURLSearchParams(payload) {
|
|
337
|
+
const params = new URLSearchParams();
|
|
338
|
+
for (const [path, value] of serialize(payload)) {
|
|
339
|
+
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
|
|
340
|
+
params.append(path, value.toString());
|
|
341
|
+
} else if (value === null) {
|
|
342
|
+
params.append(path, "null");
|
|
343
|
+
} else if (value instanceof Date) {
|
|
344
|
+
params.append(
|
|
345
|
+
path,
|
|
346
|
+
Number.isNaN(value.getTime()) ? "Invalid Date" : value.toISOString()
|
|
347
|
+
);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
return {
|
|
351
|
+
body: params.toString(),
|
|
352
|
+
headers: new Headers({
|
|
353
|
+
"content-type": "application/x-www-form-urlencoded"
|
|
354
|
+
})
|
|
355
|
+
};
|
|
466
356
|
}
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
if (!middleware || !routes) {
|
|
471
|
-
throw new Error(MESSAGE_MATCHER_IS_ALREADY_BUILT);
|
|
357
|
+
async decode(re) {
|
|
358
|
+
if (re instanceof Headers || re instanceof URLSearchParams || re instanceof FormData) {
|
|
359
|
+
return deserialize([...re.entries()]);
|
|
472
360
|
}
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
361
|
+
const contentType = re.headers.get("content-type");
|
|
362
|
+
const contentDisposition = re.headers.get("content-disposition");
|
|
363
|
+
const fileName = contentDisposition ? cd.parse(contentDisposition).parameters.filename : void 0;
|
|
364
|
+
if (fileName) {
|
|
365
|
+
const blob2 = await re.blob();
|
|
366
|
+
const file = new File([blob2], fileName, {
|
|
367
|
+
type: blob2.type
|
|
480
368
|
});
|
|
369
|
+
return file;
|
|
481
370
|
}
|
|
482
|
-
if (
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
const paramCount = (path.match(/\/:/g) || []).length;
|
|
486
|
-
if (/\*$/.test(path)) {
|
|
487
|
-
const re = buildWildcardRegExp(path);
|
|
488
|
-
if (method === METHOD_NAME_ALL) {
|
|
489
|
-
Object.keys(middleware).forEach((m) => {
|
|
490
|
-
middleware[m][path] ||= findMiddleware(middleware[m], path) || findMiddleware(middleware[METHOD_NAME_ALL], path) || [];
|
|
491
|
-
});
|
|
492
|
-
} else {
|
|
493
|
-
middleware[method][path] ||= findMiddleware(middleware[method], path) || findMiddleware(middleware[METHOD_NAME_ALL], path) || [];
|
|
371
|
+
if (!contentType || contentType.startsWith("application/json")) {
|
|
372
|
+
if (!re.body) {
|
|
373
|
+
return void 0;
|
|
494
374
|
}
|
|
495
|
-
|
|
496
|
-
if (method === METHOD_NAME_ALL || method === m) {
|
|
497
|
-
Object.keys(middleware[m]).forEach((p) => {
|
|
498
|
-
re.test(p) && middleware[m][p].push([handler, paramCount]);
|
|
499
|
-
});
|
|
500
|
-
}
|
|
501
|
-
});
|
|
502
|
-
Object.keys(routes).forEach((m) => {
|
|
503
|
-
if (method === METHOD_NAME_ALL || method === m) {
|
|
504
|
-
Object.keys(routes[m]).forEach(
|
|
505
|
-
(p) => re.test(p) && routes[m][p].push([handler, paramCount])
|
|
506
|
-
);
|
|
507
|
-
}
|
|
508
|
-
});
|
|
509
|
-
return;
|
|
375
|
+
return await re.json();
|
|
510
376
|
}
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
377
|
+
if (contentType.startsWith("application/x-www-form-urlencoded")) {
|
|
378
|
+
const params = new URLSearchParams(await re.text());
|
|
379
|
+
return this.decode(params);
|
|
380
|
+
}
|
|
381
|
+
if (contentType.startsWith("text/")) {
|
|
382
|
+
const text = await re.text();
|
|
383
|
+
return text;
|
|
384
|
+
}
|
|
385
|
+
if (contentType.startsWith("multipart/form-data")) {
|
|
386
|
+
const form = await re.formData();
|
|
387
|
+
return this.decode(form);
|
|
522
388
|
}
|
|
389
|
+
const blob = await re.blob();
|
|
390
|
+
return new File([blob], "blob", {
|
|
391
|
+
type: blob.type
|
|
392
|
+
});
|
|
523
393
|
}
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
394
|
+
};
|
|
395
|
+
|
|
396
|
+
// src/fetch/openapi-procedure-matcher.ts
|
|
397
|
+
import { getLazyRouterPrefix, getRouterChild, isProcedure, unlazy } from "@orpc/server";
|
|
398
|
+
import { mapValues } from "@orpc/shared";
|
|
399
|
+
var OpenAPIProcedureMatcher = class {
|
|
400
|
+
constructor(hono, router) {
|
|
401
|
+
this.hono = hono;
|
|
402
|
+
this.router = router;
|
|
403
|
+
this.pendingRouters = [{ path: [], router }];
|
|
404
|
+
}
|
|
405
|
+
pendingRouters;
|
|
406
|
+
async match(method, pathname) {
|
|
407
|
+
await this.handlePendingRouters(pathname);
|
|
408
|
+
const [matches, paramStash] = this.hono.match(method, pathname);
|
|
409
|
+
const [match] = matches.sort((a, b) => {
|
|
410
|
+
const slashCountA = a[0][0].split("/").length;
|
|
411
|
+
const slashCountB = b[0][0].split("/").length;
|
|
412
|
+
if (slashCountA !== slashCountB) {
|
|
413
|
+
return slashCountB - slashCountA;
|
|
536
414
|
}
|
|
537
|
-
const
|
|
538
|
-
|
|
415
|
+
const paramsCountA = Object.keys(a[1]).length;
|
|
416
|
+
const paramsCountB = Object.keys(b[1]).length;
|
|
417
|
+
return paramsCountA - paramsCountB;
|
|
418
|
+
});
|
|
419
|
+
if (!match) {
|
|
420
|
+
return void 0;
|
|
421
|
+
}
|
|
422
|
+
const path = match[0][1];
|
|
423
|
+
const params = paramStash ? mapValues(
|
|
424
|
+
match[1],
|
|
425
|
+
// if paramStash is defined, then match[1] is ParamIndexMap
|
|
426
|
+
(v) => paramStash[v]
|
|
427
|
+
) : match[1];
|
|
428
|
+
const { default: maybeProcedure } = await unlazy(getRouterChild(this.router, ...path));
|
|
429
|
+
if (!isProcedure(maybeProcedure)) {
|
|
430
|
+
return void 0;
|
|
431
|
+
}
|
|
432
|
+
return {
|
|
433
|
+
path,
|
|
434
|
+
procedure: maybeProcedure,
|
|
435
|
+
params: { ...params }
|
|
436
|
+
// normalize params from hono
|
|
539
437
|
};
|
|
540
|
-
return this.match(method, path);
|
|
541
438
|
}
|
|
542
|
-
|
|
543
|
-
const
|
|
544
|
-
|
|
545
|
-
|
|
439
|
+
add(path, router) {
|
|
440
|
+
const lazies = forEachContractProcedure({ path, router }, ({ path: path2, contract }) => {
|
|
441
|
+
const method = contract["~orpc"].route?.method ?? "POST";
|
|
442
|
+
const httpPath = contract["~orpc"].route?.path ? this.convertOpenAPIPathToRouterPath(contract["~orpc"].route?.path) : `/${path2.map(encodeURIComponent).join("/")}`;
|
|
443
|
+
this.hono.add(method, httpPath, [httpPath, path2]);
|
|
546
444
|
});
|
|
547
|
-
this
|
|
548
|
-
return matchers;
|
|
445
|
+
this.pendingRouters.push(...lazies);
|
|
549
446
|
}
|
|
550
|
-
|
|
551
|
-
const
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
routes.push(...ownRoute);
|
|
558
|
-
} else if (method !== METHOD_NAME_ALL) {
|
|
559
|
-
routes.push(
|
|
560
|
-
...Object.keys(r[METHOD_NAME_ALL]).map((path) => [path, r[METHOD_NAME_ALL][path]])
|
|
561
|
-
);
|
|
447
|
+
async handlePendingRouters(pathname) {
|
|
448
|
+
const newPendingLazyRouters = [];
|
|
449
|
+
for (const item of this.pendingRouters) {
|
|
450
|
+
const lazyPrefix = getLazyRouterPrefix(item.router);
|
|
451
|
+
if (lazyPrefix && !pathname.startsWith(lazyPrefix) && !pathname.startsWith(`/${item.path.map(encodeURIComponent).join("/")}`)) {
|
|
452
|
+
newPendingLazyRouters.push(item);
|
|
453
|
+
continue;
|
|
562
454
|
}
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
return null;
|
|
566
|
-
} else {
|
|
567
|
-
return buildMatcherFromPreprocessedRoutes(routes);
|
|
455
|
+
const { default: router } = await unlazy(item.router);
|
|
456
|
+
this.add(item.path, router);
|
|
568
457
|
}
|
|
458
|
+
this.pendingRouters = newPendingLazyRouters;
|
|
459
|
+
}
|
|
460
|
+
convertOpenAPIPathToRouterPath(path) {
|
|
461
|
+
return standardizeHTTPPath(path).replace(/\{([^}]+)\}/g, ":$1");
|
|
569
462
|
}
|
|
570
463
|
};
|
|
571
464
|
|
|
572
|
-
// src/fetch/
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
var LinearRouter = class {
|
|
582
|
-
name = "LinearRouter";
|
|
583
|
-
#routes = [];
|
|
584
|
-
add(method, path, handler) {
|
|
585
|
-
for (let i = 0, paths = checkOptionalParameter(path) || [path], len = paths.length; i < len; i++) {
|
|
586
|
-
this.#routes.push([method, paths[i], handler]);
|
|
465
|
+
// src/fetch/schema-coercer.ts
|
|
466
|
+
var CompositeSchemaCoercer = class {
|
|
467
|
+
constructor(coercers) {
|
|
468
|
+
this.coercers = coercers;
|
|
469
|
+
}
|
|
470
|
+
coerce(schema, value) {
|
|
471
|
+
let current = value;
|
|
472
|
+
for (const coercer of this.coercers) {
|
|
473
|
+
current = coercer.coerce(schema, current);
|
|
587
474
|
}
|
|
475
|
+
return current;
|
|
588
476
|
}
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
const restPath = path.slice(pos + 1);
|
|
645
|
-
const match = new RegExp(pattern, "d").exec(restPath);
|
|
646
|
-
if (!match || match.indices[0][0] !== 0 || match.indices[0][1] === 0) {
|
|
647
|
-
continue ROUTES_LOOP;
|
|
648
|
-
}
|
|
649
|
-
name = name.slice(0, openBracePos);
|
|
650
|
-
value2 = restPath.slice(...match.indices[0]);
|
|
651
|
-
pos += match.indices[0][1] + 1;
|
|
652
|
-
} else {
|
|
653
|
-
let endValuePos = path.indexOf("/", pos + 1);
|
|
654
|
-
if (endValuePos === -1) {
|
|
655
|
-
if (pos + 1 === path.length) {
|
|
656
|
-
continue ROUTES_LOOP;
|
|
657
|
-
}
|
|
658
|
-
endValuePos = path.length;
|
|
659
|
-
}
|
|
660
|
-
value2 = path.slice(pos + 1, endValuePos);
|
|
661
|
-
pos = endValuePos;
|
|
662
|
-
}
|
|
663
|
-
params[name] ||= value2;
|
|
664
|
-
} else {
|
|
665
|
-
const index = path.indexOf(part, pos);
|
|
666
|
-
if (index !== pos) {
|
|
667
|
-
continue ROUTES_LOOP;
|
|
668
|
-
}
|
|
669
|
-
pos += part.length;
|
|
670
|
-
}
|
|
671
|
-
if (j === lastIndex) {
|
|
672
|
-
if (pos !== path.length && !(pos === path.length - 1 && path.charCodeAt(pos) === 47)) {
|
|
673
|
-
continue ROUTES_LOOP;
|
|
674
|
-
}
|
|
675
|
-
}
|
|
676
|
-
}
|
|
677
|
-
handlers.push([handler, params]);
|
|
678
|
-
} else if (hasLabel && hasStar) {
|
|
679
|
-
throw new UnsupportedPathError();
|
|
680
|
-
}
|
|
477
|
+
};
|
|
478
|
+
|
|
479
|
+
// src/fetch/openapi-handler.ts
|
|
480
|
+
var OpenAPIHandler = class {
|
|
481
|
+
constructor(hono, router, options) {
|
|
482
|
+
this.options = options;
|
|
483
|
+
const jsonSerializer = options?.jsonSerializer ?? new JSONSerializer();
|
|
484
|
+
this.procedureMatcher = options?.procedureMatcher ?? new OpenAPIProcedureMatcher(hono, router);
|
|
485
|
+
this.payloadCodec = options?.payloadCodec ?? new OpenAPIPayloadCodec(jsonSerializer);
|
|
486
|
+
this.inputBuilderSimple = options?.inputBuilderSimple ?? new InputBuilderSimple();
|
|
487
|
+
this.inputBuilderFull = options?.inputBuilderFull ?? new InputBuilderFull();
|
|
488
|
+
this.compositeSchemaCoercer = new CompositeSchemaCoercer(options?.schemaCoercers ?? []);
|
|
489
|
+
}
|
|
490
|
+
procedureMatcher;
|
|
491
|
+
payloadCodec;
|
|
492
|
+
inputBuilderSimple;
|
|
493
|
+
inputBuilderFull;
|
|
494
|
+
compositeSchemaCoercer;
|
|
495
|
+
condition(request) {
|
|
496
|
+
return request.headers.get(ORPC_HANDLER_HEADER) === null;
|
|
497
|
+
}
|
|
498
|
+
async fetch(request, ...[options]) {
|
|
499
|
+
const context = options?.context;
|
|
500
|
+
const headers = request.headers;
|
|
501
|
+
const accept = headers.get("Accept") || void 0;
|
|
502
|
+
const execute = async () => {
|
|
503
|
+
const url = new URL(request.url);
|
|
504
|
+
const pathname = `/${trim(url.pathname.replace(options?.prefix ?? "", ""), "/")}`;
|
|
505
|
+
const query = url.searchParams;
|
|
506
|
+
const customMethod = request.method === "POST" ? query.get("method")?.toUpperCase() : void 0;
|
|
507
|
+
const method = customMethod || request.method;
|
|
508
|
+
const match = await this.procedureMatcher.match(method, pathname);
|
|
509
|
+
if (!match) {
|
|
510
|
+
throw new ORPCError2({ code: "NOT_FOUND", message: "Not found" });
|
|
511
|
+
}
|
|
512
|
+
const decodedPayload = request.method === "GET" ? await this.payloadCodec.decode(query) : await this.payloadCodec.decode(request);
|
|
513
|
+
const input = this.inputBuilderSimple.build(match.params, decodedPayload);
|
|
514
|
+
const coercedInput = this.compositeSchemaCoercer.coerce(match.procedure["~orpc"].contract["~orpc"].InputSchema, input);
|
|
515
|
+
const client = createProcedureClient({
|
|
516
|
+
context,
|
|
517
|
+
procedure: match.procedure,
|
|
518
|
+
path: match.path
|
|
519
|
+
});
|
|
520
|
+
const output = await client(coercedInput, { signal: options?.signal });
|
|
521
|
+
const { body, headers: headers2 } = this.payloadCodec.encode(output);
|
|
522
|
+
return new Response(body, { headers: headers2 });
|
|
523
|
+
};
|
|
524
|
+
try {
|
|
525
|
+
return await executeWithHooks({
|
|
526
|
+
context,
|
|
527
|
+
execute,
|
|
528
|
+
input: request,
|
|
529
|
+
hooks: this.options,
|
|
530
|
+
meta: {
|
|
531
|
+
signal: options?.signal
|
|
681
532
|
}
|
|
533
|
+
});
|
|
534
|
+
} catch (e) {
|
|
535
|
+
const error = this.convertToORPCError(e);
|
|
536
|
+
try {
|
|
537
|
+
const { body, headers: headers2 } = this.payloadCodec.encode(error.toJSON(), accept);
|
|
538
|
+
return new Response(body, {
|
|
539
|
+
status: error.status,
|
|
540
|
+
headers: headers2
|
|
541
|
+
});
|
|
542
|
+
} catch (e2) {
|
|
543
|
+
const error2 = this.convertToORPCError(e2);
|
|
544
|
+
const { body, headers: headers2 } = this.payloadCodec.encode(error2.toJSON());
|
|
545
|
+
return new Response(body, {
|
|
546
|
+
status: error2.status,
|
|
547
|
+
headers: headers2
|
|
548
|
+
});
|
|
682
549
|
}
|
|
683
|
-
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
convertToORPCError(e) {
|
|
553
|
+
return e instanceof ORPCError2 ? e : new ORPCError2({
|
|
554
|
+
code: "INTERNAL_SERVER_ERROR",
|
|
555
|
+
message: "Internal server error",
|
|
556
|
+
cause: e
|
|
557
|
+
});
|
|
684
558
|
}
|
|
685
559
|
};
|
|
686
560
|
|
|
687
|
-
// src/fetch/
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
561
|
+
// src/fetch/openapi-handler-server.ts
|
|
562
|
+
import { TrieRouter } from "hono/router/trie-router";
|
|
563
|
+
var OpenAPIServerHandler = class extends OpenAPIHandler {
|
|
564
|
+
constructor(router, options) {
|
|
565
|
+
super(new TrieRouter(), router, options);
|
|
566
|
+
}
|
|
567
|
+
};
|
|
568
|
+
|
|
569
|
+
// src/fetch/openapi-handler-serverless.ts
|
|
570
|
+
import { LinearRouter } from "hono/router/linear-router";
|
|
571
|
+
var OpenAPIServerlessHandler = class extends OpenAPIHandler {
|
|
572
|
+
constructor(router, options) {
|
|
573
|
+
super(new LinearRouter(), router, options);
|
|
574
|
+
}
|
|
575
|
+
};
|
|
691
576
|
export {
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
577
|
+
CompositeSchemaCoercer,
|
|
578
|
+
InputBuilderFull,
|
|
579
|
+
InputBuilderSimple,
|
|
580
|
+
OpenAPIHandler,
|
|
581
|
+
OpenAPIPayloadCodec,
|
|
582
|
+
OpenAPIProcedureMatcher,
|
|
583
|
+
OpenAPIServerHandler,
|
|
584
|
+
OpenAPIServerlessHandler,
|
|
585
|
+
deserialize,
|
|
586
|
+
escapeSegment,
|
|
587
|
+
parsePath,
|
|
588
|
+
serialize,
|
|
589
|
+
stringifyPath
|
|
696
590
|
};
|
|
697
591
|
//# sourceMappingURL=fetch.js.map
|