@orpc/server 0.0.0-next.1d55ec0 → 0.0.0-next.2a66984
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/chunk-FN62GL22.js +182 -0
- package/dist/fetch.js +238 -632
- package/dist/index.js +403 -278
- package/dist/src/builder.d.ts +27 -40
- package/dist/src/fetch/composite-handler.d.ts +8 -0
- package/dist/src/fetch/index.d.ts +6 -0
- package/dist/src/fetch/orpc-handler.d.ts +20 -0
- package/dist/src/fetch/orpc-payload-codec.d.ts +9 -0
- package/dist/src/fetch/orpc-procedure-matcher.d.ts +12 -0
- package/dist/src/fetch/super-json.d.ts +12 -0
- package/dist/src/fetch/types.d.ts +16 -0
- package/dist/src/hidden.d.ts +6 -0
- package/dist/src/implementer-chainable.d.ts +10 -0
- package/dist/src/index.d.ts +12 -3
- package/dist/src/lazy-decorated.d.ts +10 -0
- package/dist/src/lazy-utils.d.ts +4 -0
- package/dist/src/lazy.d.ts +18 -0
- package/dist/src/middleware-decorated.d.ts +8 -0
- package/dist/src/middleware.d.ts +4 -6
- package/dist/src/procedure-builder.d.ts +16 -24
- package/dist/src/procedure-client.d.ts +29 -0
- package/dist/src/procedure-decorated.d.ts +14 -0
- package/dist/src/procedure-implementer.d.ts +14 -13
- package/dist/src/procedure.d.ts +19 -24
- package/dist/src/router-builder.d.ts +25 -17
- package/dist/src/router-client.d.ts +25 -0
- package/dist/src/router-implementer.d.ts +19 -17
- package/dist/src/router.d.ts +12 -15
- package/dist/src/types.d.ts +9 -4
- package/dist/src/utils.d.ts +1 -0
- package/package.json +8 -12
- package/dist/chunk-CVLK2PBB.js +0 -189
- package/dist/src/adapters/fetch.d.ts +0 -41
- package/dist/src/procedure-caller.d.ts +0 -18
- package/dist/src/router-caller.d.ts +0 -21
package/dist/fetch.js
CHANGED
@@ -1,680 +1,286 @@
|
|
1
1
|
import {
|
2
|
-
|
3
|
-
|
4
|
-
|
2
|
+
createProcedureClient,
|
3
|
+
getRouterChild,
|
4
|
+
isProcedure,
|
5
|
+
unlazy
|
6
|
+
} from "./chunk-FN62GL22.js";
|
5
7
|
|
6
|
-
// src/
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
standardizeHTTPPath
|
11
|
-
} from "@orpc/contract";
|
12
|
-
import {
|
13
|
-
get,
|
14
|
-
isPlainObject,
|
15
|
-
mapValues,
|
16
|
-
trim,
|
17
|
-
value
|
18
|
-
} from "@orpc/shared";
|
19
|
-
import { ORPCError } from "@orpc/shared/error";
|
20
|
-
import {
|
21
|
-
OpenAPIDeserializer,
|
22
|
-
OpenAPISerializer,
|
23
|
-
ORPCDeserializer,
|
24
|
-
ORPCSerializer,
|
25
|
-
zodCoerce
|
26
|
-
} from "@orpc/transformer";
|
27
|
-
|
28
|
-
// ../../node_modules/.pnpm/hono@4.6.6/node_modules/hono/dist/router.js
|
29
|
-
var METHOD_NAME_ALL = "ALL";
|
30
|
-
var MESSAGE_MATCHER_IS_ALREADY_BUILT = "Can not add a route since the matcher is already built.";
|
31
|
-
var UnsupportedPathError = class extends Error {
|
32
|
-
};
|
33
|
-
|
34
|
-
// ../../node_modules/.pnpm/hono@4.6.6/node_modules/hono/dist/utils/url.js
|
35
|
-
var checkOptionalParameter = (path) => {
|
36
|
-
if (!path.match(/\:.+\?$/)) {
|
37
|
-
return null;
|
8
|
+
// src/fetch/composite-handler.ts
|
9
|
+
var CompositeHandler = class {
|
10
|
+
constructor(handlers) {
|
11
|
+
this.handlers = handlers;
|
38
12
|
}
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
if (segment !== "" && !/\:/.test(segment)) {
|
44
|
-
basePath += "/" + segment;
|
45
|
-
} else if (/\:/.test(segment)) {
|
46
|
-
if (/\?/.test(segment)) {
|
47
|
-
if (results.length === 0 && basePath === "") {
|
48
|
-
results.push("/");
|
49
|
-
} else {
|
50
|
-
results.push(basePath);
|
51
|
-
}
|
52
|
-
const optionalSegment = segment.replace("?", "");
|
53
|
-
basePath += "/" + optionalSegment;
|
54
|
-
results.push(basePath);
|
55
|
-
} else {
|
56
|
-
basePath += "/" + segment;
|
13
|
+
async fetch(request, ...opt) {
|
14
|
+
for (const handler of this.handlers) {
|
15
|
+
if (handler.condition(request)) {
|
16
|
+
return handler.fetch(request, ...opt);
|
57
17
|
}
|
58
18
|
}
|
59
|
-
|
60
|
-
|
61
|
-
};
|
62
|
-
|
63
|
-
// ../../node_modules/.pnpm/hono@4.6.6/node_modules/hono/dist/router/linear-router/router.js
|
64
|
-
var emptyParams = /* @__PURE__ */ Object.create(null);
|
65
|
-
var splitPathRe = /\/(:\w+(?:{(?:(?:{[\d,]+})|[^}])+})?)|\/[^\/\?]+|(\?)/g;
|
66
|
-
var splitByStarRe = /\*/;
|
67
|
-
var LinearRouter = class {
|
68
|
-
name = "LinearRouter";
|
69
|
-
routes = [];
|
70
|
-
add(method, path, handler) {
|
71
|
-
;
|
72
|
-
(checkOptionalParameter(path) || [path]).forEach((p) => {
|
73
|
-
this.routes.push([method, p, handler]);
|
19
|
+
return new Response("None of the handlers can handle the request.", {
|
20
|
+
status: 404
|
74
21
|
});
|
75
22
|
}
|
76
|
-
match(method, path) {
|
77
|
-
const handlers = [];
|
78
|
-
ROUTES_LOOP:
|
79
|
-
for (let i = 0, len = this.routes.length; i < len; i++) {
|
80
|
-
const [routeMethod, routePath, handler] = this.routes[i];
|
81
|
-
if (routeMethod !== method && routeMethod !== METHOD_NAME_ALL) {
|
82
|
-
continue;
|
83
|
-
}
|
84
|
-
if (routePath === "*" || routePath === "/*") {
|
85
|
-
handlers.push([handler, emptyParams]);
|
86
|
-
continue;
|
87
|
-
}
|
88
|
-
const hasStar = routePath.indexOf("*") !== -1;
|
89
|
-
const hasLabel = routePath.indexOf(":") !== -1;
|
90
|
-
if (!hasStar && !hasLabel) {
|
91
|
-
if (routePath === path || routePath + "/" === path) {
|
92
|
-
handlers.push([handler, emptyParams]);
|
93
|
-
}
|
94
|
-
} else if (hasStar && !hasLabel) {
|
95
|
-
const endsWithStar = routePath.charCodeAt(routePath.length - 1) === 42;
|
96
|
-
const parts = (endsWithStar ? routePath.slice(0, -2) : routePath).split(splitByStarRe);
|
97
|
-
const lastIndex = parts.length - 1;
|
98
|
-
for (let j = 0, pos = 0, len2 = parts.length; j < len2; j++) {
|
99
|
-
const part = parts[j];
|
100
|
-
const index = path.indexOf(part, pos);
|
101
|
-
if (index !== pos) {
|
102
|
-
continue ROUTES_LOOP;
|
103
|
-
}
|
104
|
-
pos += part.length;
|
105
|
-
if (j === lastIndex) {
|
106
|
-
if (!endsWithStar && pos !== path.length && !(pos === path.length - 1 && path.charCodeAt(pos) === 47)) {
|
107
|
-
continue ROUTES_LOOP;
|
108
|
-
}
|
109
|
-
} else {
|
110
|
-
const index2 = path.indexOf("/", pos);
|
111
|
-
if (index2 === -1) {
|
112
|
-
continue ROUTES_LOOP;
|
113
|
-
}
|
114
|
-
pos = index2;
|
115
|
-
}
|
116
|
-
}
|
117
|
-
handlers.push([handler, emptyParams]);
|
118
|
-
} else if (hasLabel && !hasStar) {
|
119
|
-
const params = /* @__PURE__ */ Object.create(null);
|
120
|
-
const parts = routePath.match(splitPathRe);
|
121
|
-
const lastIndex = parts.length - 1;
|
122
|
-
for (let j = 0, pos = 0, len2 = parts.length; j < len2; j++) {
|
123
|
-
if (pos === -1 || pos >= path.length) {
|
124
|
-
continue ROUTES_LOOP;
|
125
|
-
}
|
126
|
-
const part = parts[j];
|
127
|
-
if (part.charCodeAt(1) === 58) {
|
128
|
-
let name = part.slice(2);
|
129
|
-
let value2;
|
130
|
-
if (name.charCodeAt(name.length - 1) === 125) {
|
131
|
-
const openBracePos = name.indexOf("{");
|
132
|
-
const pattern = name.slice(openBracePos + 1, -1);
|
133
|
-
const restPath = path.slice(pos + 1);
|
134
|
-
const match = new RegExp(pattern, "d").exec(restPath);
|
135
|
-
if (!match || match.indices[0][0] !== 0 || match.indices[0][1] === 0) {
|
136
|
-
continue ROUTES_LOOP;
|
137
|
-
}
|
138
|
-
name = name.slice(0, openBracePos);
|
139
|
-
value2 = restPath.slice(...match.indices[0]);
|
140
|
-
pos += match.indices[0][1] + 1;
|
141
|
-
} else {
|
142
|
-
let endValuePos = path.indexOf("/", pos + 1);
|
143
|
-
if (endValuePos === -1) {
|
144
|
-
if (pos + 1 === path.length) {
|
145
|
-
continue ROUTES_LOOP;
|
146
|
-
}
|
147
|
-
endValuePos = path.length;
|
148
|
-
}
|
149
|
-
value2 = path.slice(pos + 1, endValuePos);
|
150
|
-
pos = endValuePos;
|
151
|
-
}
|
152
|
-
params[name] ||= value2;
|
153
|
-
} else {
|
154
|
-
const index = path.indexOf(part, pos);
|
155
|
-
if (index !== pos) {
|
156
|
-
continue ROUTES_LOOP;
|
157
|
-
}
|
158
|
-
pos += part.length;
|
159
|
-
}
|
160
|
-
if (j === lastIndex) {
|
161
|
-
if (pos !== path.length && !(pos === path.length - 1 && path.charCodeAt(pos) === 47)) {
|
162
|
-
continue ROUTES_LOOP;
|
163
|
-
}
|
164
|
-
}
|
165
|
-
}
|
166
|
-
handlers.push([handler, params]);
|
167
|
-
} else if (hasLabel && hasStar) {
|
168
|
-
throw new UnsupportedPathError();
|
169
|
-
}
|
170
|
-
}
|
171
|
-
return [handlers];
|
172
|
-
}
|
173
23
|
};
|
174
24
|
|
175
|
-
//
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
25
|
+
// src/fetch/orpc-handler.ts
|
26
|
+
import { executeWithHooks, ORPC_HANDLER_HEADER, ORPC_HANDLER_VALUE, trim as trim2 } from "@orpc/shared";
|
27
|
+
import { ORPCError as ORPCError2 } from "@orpc/shared/error";
|
28
|
+
|
29
|
+
// src/fetch/orpc-payload-codec.ts
|
30
|
+
import { findDeepMatches, set } from "@orpc/shared";
|
31
|
+
import { ORPCError } from "@orpc/shared/error";
|
32
|
+
|
33
|
+
// ../../node_modules/.pnpm/is-what@5.0.2/node_modules/is-what/dist/getType.js
|
34
|
+
function getType(payload) {
|
35
|
+
return Object.prototype.toString.call(payload).slice(8, -1);
|
36
|
+
}
|
37
|
+
|
38
|
+
// ../../node_modules/.pnpm/is-what@5.0.2/node_modules/is-what/dist/isPlainObject.js
|
39
|
+
function isPlainObject(payload) {
|
40
|
+
if (getType(payload) !== "Object")
|
41
|
+
return false;
|
42
|
+
const prototype = Object.getPrototypeOf(payload);
|
43
|
+
return !!prototype && prototype.constructor === Object && prototype === Object.prototype;
|
44
|
+
}
|
45
|
+
|
46
|
+
// src/fetch/super-json.ts
|
47
|
+
function serialize(value, segments = [], meta = []) {
|
48
|
+
if (typeof value === "bigint") {
|
49
|
+
meta.push(["bigint", segments]);
|
50
|
+
return { data: value.toString(), meta };
|
187
51
|
}
|
188
|
-
if (
|
189
|
-
|
190
|
-
|
191
|
-
return
|
52
|
+
if (value instanceof Date) {
|
53
|
+
meta.push(["date", segments]);
|
54
|
+
const data = Number.isNaN(value.getTime()) ? "Invalid Date" : value.toISOString();
|
55
|
+
return { data, meta };
|
192
56
|
}
|
193
|
-
if (
|
194
|
-
|
195
|
-
|
196
|
-
return -1;
|
57
|
+
if (Number.isNaN(value)) {
|
58
|
+
meta.push(["nan", segments]);
|
59
|
+
return { data: "NaN", meta };
|
197
60
|
}
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
index;
|
202
|
-
varIndex;
|
203
|
-
children = /* @__PURE__ */ Object.create(null);
|
204
|
-
insert(tokens, index, paramMap, context, pathErrorCheckOnly) {
|
205
|
-
if (tokens.length === 0) {
|
206
|
-
if (this.index !== void 0) {
|
207
|
-
throw PATH_ERROR;
|
208
|
-
}
|
209
|
-
if (pathErrorCheckOnly) {
|
210
|
-
return;
|
211
|
-
}
|
212
|
-
this.index = index;
|
213
|
-
return;
|
214
|
-
}
|
215
|
-
const [token, ...restTokens] = tokens;
|
216
|
-
const pattern = token === "*" ? restTokens.length === 0 ? ["", "", ONLY_WILDCARD_REG_EXP_STR] : ["", "", LABEL_REG_EXP_STR] : token === "/*" ? ["", "", TAIL_WILDCARD_REG_EXP_STR] : token.match(/^\:([^\{\}]+)(?:\{(.+)\})?$/);
|
217
|
-
let node;
|
218
|
-
if (pattern) {
|
219
|
-
const name = pattern[1];
|
220
|
-
let regexpStr = pattern[2] || LABEL_REG_EXP_STR;
|
221
|
-
if (name && pattern[2]) {
|
222
|
-
regexpStr = regexpStr.replace(/^\((?!\?:)(?=[^)]+\)$)/, "(?:");
|
223
|
-
if (/\((?!\?:)/.test(regexpStr)) {
|
224
|
-
throw PATH_ERROR;
|
225
|
-
}
|
226
|
-
}
|
227
|
-
node = this.children[regexpStr];
|
228
|
-
if (!node) {
|
229
|
-
if (Object.keys(this.children).some(
|
230
|
-
(k) => k !== ONLY_WILDCARD_REG_EXP_STR && k !== TAIL_WILDCARD_REG_EXP_STR
|
231
|
-
)) {
|
232
|
-
throw PATH_ERROR;
|
233
|
-
}
|
234
|
-
if (pathErrorCheckOnly) {
|
235
|
-
return;
|
236
|
-
}
|
237
|
-
node = this.children[regexpStr] = new Node();
|
238
|
-
if (name !== "") {
|
239
|
-
node.varIndex = context.varIndex++;
|
240
|
-
}
|
241
|
-
}
|
242
|
-
if (!pathErrorCheckOnly && name !== "") {
|
243
|
-
paramMap.push([name, node.varIndex]);
|
244
|
-
}
|
245
|
-
} else {
|
246
|
-
node = this.children[token];
|
247
|
-
if (!node) {
|
248
|
-
if (Object.keys(this.children).some(
|
249
|
-
(k) => k.length > 1 && k !== ONLY_WILDCARD_REG_EXP_STR && k !== TAIL_WILDCARD_REG_EXP_STR
|
250
|
-
)) {
|
251
|
-
throw PATH_ERROR;
|
252
|
-
}
|
253
|
-
if (pathErrorCheckOnly) {
|
254
|
-
return;
|
255
|
-
}
|
256
|
-
node = this.children[token] = new Node();
|
257
|
-
}
|
258
|
-
}
|
259
|
-
node.insert(restTokens, index, paramMap, context, pathErrorCheckOnly);
|
61
|
+
if (value instanceof RegExp) {
|
62
|
+
meta.push(["regexp", segments]);
|
63
|
+
return { data: value.toString(), meta };
|
260
64
|
}
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
const c = this.children[k];
|
265
|
-
return (typeof c.varIndex === "number" ? `(${k})@${c.varIndex}` : regExpMetaChars.has(k) ? `\\${k}` : k) + c.buildRegExpStr();
|
266
|
-
});
|
267
|
-
if (typeof this.index === "number") {
|
268
|
-
strList.unshift(`#${this.index}`);
|
269
|
-
}
|
270
|
-
if (strList.length === 0) {
|
271
|
-
return "";
|
272
|
-
}
|
273
|
-
if (strList.length === 1) {
|
274
|
-
return strList[0];
|
275
|
-
}
|
276
|
-
return "(?:" + strList.join("|") + ")";
|
65
|
+
if (value instanceof URL) {
|
66
|
+
meta.push(["url", segments]);
|
67
|
+
return { data: value.toString(), meta };
|
277
68
|
}
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
context = { varIndex: 0 };
|
283
|
-
root = new Node();
|
284
|
-
insert(path, index, pathErrorCheckOnly) {
|
285
|
-
const paramAssoc = [];
|
286
|
-
const groups = [];
|
287
|
-
for (let i = 0; ; ) {
|
288
|
-
let replaced = false;
|
289
|
-
path = path.replace(/\{[^}]+\}/g, (m) => {
|
290
|
-
const mark = `@\\${i}`;
|
291
|
-
groups[i] = [mark, m];
|
292
|
-
i++;
|
293
|
-
replaced = true;
|
294
|
-
return mark;
|
295
|
-
});
|
296
|
-
if (!replaced) {
|
297
|
-
break;
|
298
|
-
}
|
69
|
+
if (isPlainObject(value)) {
|
70
|
+
const data = {};
|
71
|
+
for (const k in value) {
|
72
|
+
data[k] = serialize(value[k], [...segments, k], meta).data;
|
299
73
|
}
|
300
|
-
|
301
|
-
for (let i = groups.length - 1; i >= 0; i--) {
|
302
|
-
const [mark] = groups[i];
|
303
|
-
for (let j = tokens.length - 1; j >= 0; j--) {
|
304
|
-
if (tokens[j].indexOf(mark) !== -1) {
|
305
|
-
tokens[j] = tokens[j].replace(mark, groups[i][1]);
|
306
|
-
break;
|
307
|
-
}
|
308
|
-
}
|
309
|
-
}
|
310
|
-
this.root.insert(tokens, index, paramAssoc, this.context, pathErrorCheckOnly);
|
311
|
-
return paramAssoc;
|
74
|
+
return { data, meta };
|
312
75
|
}
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
let captureIndex = 0;
|
319
|
-
const indexReplacementMap = [];
|
320
|
-
const paramReplacementMap = [];
|
321
|
-
regexp = regexp.replace(/#(\d+)|@(\d+)|\.\*\$/g, (_, handlerIndex, paramIndex) => {
|
322
|
-
if (typeof handlerIndex !== "undefined") {
|
323
|
-
indexReplacementMap[++captureIndex] = Number(handlerIndex);
|
324
|
-
return "$()";
|
76
|
+
if (Array.isArray(value)) {
|
77
|
+
const data = value.map((v, i) => {
|
78
|
+
if (v === void 0) {
|
79
|
+
meta.push(["undefined", [...segments, i]]);
|
80
|
+
return null;
|
325
81
|
}
|
326
|
-
|
327
|
-
paramReplacementMap[Number(paramIndex)] = ++captureIndex;
|
328
|
-
return "";
|
329
|
-
}
|
330
|
-
return "";
|
82
|
+
return serialize(v, [...segments, i], meta).data;
|
331
83
|
});
|
332
|
-
return
|
84
|
+
return { data, meta };
|
333
85
|
}
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
)}$`
|
346
|
-
);
|
347
|
-
}
|
348
|
-
function clearWildcardRegExpCache() {
|
349
|
-
wildcardRegExpCache = /* @__PURE__ */ Object.create(null);
|
86
|
+
if (value instanceof Set) {
|
87
|
+
const result = serialize(Array.from(value), segments, meta);
|
88
|
+
meta.push(["set", segments]);
|
89
|
+
return result;
|
90
|
+
}
|
91
|
+
if (value instanceof Map) {
|
92
|
+
const result = serialize(Array.from(value.entries()), segments, meta);
|
93
|
+
meta.push(["map", segments]);
|
94
|
+
return result;
|
95
|
+
}
|
96
|
+
return { data: value, meta };
|
350
97
|
}
|
351
|
-
function
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
98
|
+
function deserialize({
|
99
|
+
data,
|
100
|
+
meta
|
101
|
+
}) {
|
102
|
+
if (meta.length === 0) {
|
103
|
+
return data;
|
356
104
|
}
|
357
|
-
const
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
const [pathErrorCheckOnly, path, handlers] = routesWithStaticPathFlag[i];
|
365
|
-
if (pathErrorCheckOnly) {
|
366
|
-
staticMap[path] = [handlers.map(([h]) => [h, /* @__PURE__ */ Object.create(null)]), emptyParam];
|
367
|
-
} else {
|
368
|
-
j++;
|
369
|
-
}
|
370
|
-
let paramAssoc;
|
371
|
-
try {
|
372
|
-
paramAssoc = trie.insert(path, j, pathErrorCheckOnly);
|
373
|
-
} catch (e) {
|
374
|
-
throw e === PATH_ERROR ? new UnsupportedPathError(path) : e;
|
105
|
+
const ref = { data };
|
106
|
+
for (const [type, segments] of meta) {
|
107
|
+
let currentRef = ref;
|
108
|
+
let preSegment = "data";
|
109
|
+
for (let i = 0; i < segments.length; i++) {
|
110
|
+
currentRef = currentRef[preSegment];
|
111
|
+
preSegment = segments[i];
|
375
112
|
}
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
for (let i = 0, len = handlerData.length; i < len; i++) {
|
391
|
-
for (let j = 0, len2 = handlerData[i].length; j < len2; j++) {
|
392
|
-
const map = handlerData[i][j]?.[1];
|
393
|
-
if (!map) {
|
394
|
-
continue;
|
113
|
+
switch (type) {
|
114
|
+
case "nan":
|
115
|
+
currentRef[preSegment] = Number.NaN;
|
116
|
+
break;
|
117
|
+
case "bigint":
|
118
|
+
currentRef[preSegment] = BigInt(currentRef[preSegment]);
|
119
|
+
break;
|
120
|
+
case "date":
|
121
|
+
currentRef[preSegment] = new Date(currentRef[preSegment]);
|
122
|
+
break;
|
123
|
+
case "regexp": {
|
124
|
+
const [, pattern, flags] = currentRef[preSegment].match(/^\/(.*)\/([a-z]*)$/);
|
125
|
+
currentRef[preSegment] = new RegExp(pattern, flags);
|
126
|
+
break;
|
395
127
|
}
|
396
|
-
|
397
|
-
|
398
|
-
|
128
|
+
case "url":
|
129
|
+
currentRef[preSegment] = new URL(currentRef[preSegment]);
|
130
|
+
break;
|
131
|
+
case "undefined":
|
132
|
+
currentRef[preSegment] = void 0;
|
133
|
+
break;
|
134
|
+
case "map":
|
135
|
+
currentRef[preSegment] = new Map(currentRef[preSegment]);
|
136
|
+
break;
|
137
|
+
case "set":
|
138
|
+
currentRef[preSegment] = new Set(currentRef[preSegment]);
|
139
|
+
break;
|
140
|
+
/* v8 ignore next 3 */
|
141
|
+
default: {
|
142
|
+
const _expected = type;
|
399
143
|
}
|
400
144
|
}
|
401
145
|
}
|
402
|
-
|
403
|
-
for (const i in indexReplacementMap) {
|
404
|
-
handlerMap[i] = handlerData[indexReplacementMap[i]];
|
405
|
-
}
|
406
|
-
return [regexp, handlerMap, staticMap];
|
146
|
+
return ref.data;
|
407
147
|
}
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
148
|
+
|
149
|
+
// src/fetch/orpc-payload-codec.ts
|
150
|
+
var ORPCPayloadCodec = class {
|
151
|
+
encode(payload) {
|
152
|
+
const { data, meta } = serialize(payload);
|
153
|
+
const { maps, values } = findDeepMatches((v) => v instanceof Blob, data);
|
154
|
+
if (values.length > 0) {
|
155
|
+
const form = new FormData();
|
156
|
+
if (data !== void 0) {
|
157
|
+
form.append("data", JSON.stringify(data));
|
158
|
+
}
|
159
|
+
form.append("meta", JSON.stringify(meta));
|
160
|
+
form.append("maps", JSON.stringify(maps));
|
161
|
+
for (const i in values) {
|
162
|
+
const value = values[i];
|
163
|
+
form.append(i, value);
|
164
|
+
}
|
165
|
+
return { body: form };
|
415
166
|
}
|
167
|
+
return {
|
168
|
+
body: JSON.stringify({ data, meta }),
|
169
|
+
headers: new Headers({
|
170
|
+
"content-type": "application/json"
|
171
|
+
})
|
172
|
+
};
|
416
173
|
}
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
;
|
434
|
-
[middleware, routes].forEach((handlerMap) => {
|
435
|
-
handlerMap[method] = /* @__PURE__ */ Object.create(null);
|
436
|
-
Object.keys(handlerMap[METHOD_NAME_ALL]).forEach((p) => {
|
437
|
-
handlerMap[method][p] = [...handlerMap[METHOD_NAME_ALL][p]];
|
438
|
-
});
|
439
|
-
});
|
440
|
-
}
|
441
|
-
if (path === "/*") {
|
442
|
-
path = "*";
|
443
|
-
}
|
444
|
-
const paramCount = (path.match(/\/:/g) || []).length;
|
445
|
-
if (/\*$/.test(path)) {
|
446
|
-
const re = buildWildcardRegExp(path);
|
447
|
-
if (method === METHOD_NAME_ALL) {
|
448
|
-
Object.keys(middleware).forEach((m) => {
|
449
|
-
middleware[m][path] ||= findMiddleware(middleware[m], path) || findMiddleware(middleware[METHOD_NAME_ALL], path) || [];
|
174
|
+
async decode(re) {
|
175
|
+
try {
|
176
|
+
if (re.headers.get("content-type")?.startsWith("multipart/form-data")) {
|
177
|
+
const form = await re.formData();
|
178
|
+
const rawData = form.get("data");
|
179
|
+
const rawMeta = form.get("meta");
|
180
|
+
const rawMaps = form.get("maps");
|
181
|
+
let data = JSON.parse(rawData);
|
182
|
+
const meta = JSON.parse(rawMeta);
|
183
|
+
const maps = JSON.parse(rawMaps);
|
184
|
+
for (const i in maps) {
|
185
|
+
data = set(data, maps[i], form.get(i));
|
186
|
+
}
|
187
|
+
return deserialize({
|
188
|
+
data,
|
189
|
+
meta
|
450
190
|
});
|
451
|
-
} else {
|
452
|
-
middleware[method][path] ||= findMiddleware(middleware[method], path) || findMiddleware(middleware[METHOD_NAME_ALL], path) || [];
|
453
191
|
}
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
Object.keys(routes).forEach((m) => {
|
462
|
-
if (method === METHOD_NAME_ALL || method === m) {
|
463
|
-
Object.keys(routes[m]).forEach(
|
464
|
-
(p) => re.test(p) && routes[m][p].push([handler, paramCount])
|
465
|
-
);
|
466
|
-
}
|
467
|
-
});
|
468
|
-
return;
|
469
|
-
}
|
470
|
-
const paths = checkOptionalParameter(path) || [path];
|
471
|
-
for (let i = 0, len = paths.length; i < len; i++) {
|
472
|
-
const path2 = paths[i];
|
473
|
-
Object.keys(routes).forEach((m) => {
|
474
|
-
if (method === METHOD_NAME_ALL || method === m) {
|
475
|
-
routes[m][path2] ||= [
|
476
|
-
...findMiddleware(middleware[m], path2) || findMiddleware(middleware[METHOD_NAME_ALL], path2) || []
|
477
|
-
];
|
478
|
-
routes[m][path2].push([handler, paramCount - len + i + 1]);
|
479
|
-
}
|
192
|
+
const json = await re.json();
|
193
|
+
return deserialize(json);
|
194
|
+
} catch (e) {
|
195
|
+
throw new ORPCError({
|
196
|
+
code: "BAD_REQUEST",
|
197
|
+
message: "Cannot parse request/response. Please check the request/response body and Content-Type header.",
|
198
|
+
cause: e
|
480
199
|
});
|
481
200
|
}
|
482
201
|
}
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
return staticMatch;
|
491
|
-
}
|
492
|
-
const match = path2.match(matcher[0]);
|
493
|
-
if (!match) {
|
494
|
-
return [[], emptyParam];
|
495
|
-
}
|
496
|
-
const index = match.indexOf("", 1);
|
497
|
-
return [matcher[1][index], match];
|
498
|
-
};
|
499
|
-
return this.match(method, path);
|
500
|
-
}
|
501
|
-
buildAllMatchers() {
|
502
|
-
const matchers = /* @__PURE__ */ Object.create(null);
|
503
|
-
[...Object.keys(this.routes), ...Object.keys(this.middleware)].forEach((method) => {
|
504
|
-
matchers[method] ||= this.buildMatcher(method);
|
505
|
-
});
|
506
|
-
this.middleware = this.routes = void 0;
|
507
|
-
return matchers;
|
202
|
+
};
|
203
|
+
|
204
|
+
// src/fetch/orpc-procedure-matcher.ts
|
205
|
+
import { trim } from "@orpc/shared";
|
206
|
+
var ORPCProcedureMatcher = class {
|
207
|
+
constructor(router) {
|
208
|
+
this.router = router;
|
508
209
|
}
|
509
|
-
|
510
|
-
const
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
hasOwnRoute ||= true;
|
516
|
-
routes.push(...ownRoute);
|
517
|
-
} else if (method !== METHOD_NAME_ALL) {
|
518
|
-
routes.push(
|
519
|
-
...Object.keys(r[METHOD_NAME_ALL]).map((path) => [path, r[METHOD_NAME_ALL][path]])
|
520
|
-
);
|
521
|
-
}
|
522
|
-
});
|
523
|
-
if (!hasOwnRoute) {
|
524
|
-
return null;
|
525
|
-
} else {
|
526
|
-
return buildMatcherFromPreprocessedRoutes(routes);
|
210
|
+
async match(pathname) {
|
211
|
+
const path = trim(pathname, "/").split("/").map(decodeURIComponent);
|
212
|
+
const match = getRouterChild(this.router, ...path);
|
213
|
+
const { default: maybeProcedure } = await unlazy(match);
|
214
|
+
if (!isProcedure(maybeProcedure)) {
|
215
|
+
return void 0;
|
527
216
|
}
|
217
|
+
return {
|
218
|
+
procedure: maybeProcedure,
|
219
|
+
path
|
220
|
+
};
|
528
221
|
}
|
529
222
|
};
|
530
223
|
|
531
|
-
// src/
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
const accept = requestOptions.request.headers.get("Accept") || void 0;
|
553
|
-
const serializer = isORPCTransformer ? new ORPCSerializer() : new OpenAPISerializer({ accept });
|
554
|
-
const context = await value(requestOptions.context);
|
555
|
-
const handler = async () => {
|
556
|
-
const url = new URL(requestOptions.request.url);
|
557
|
-
const pathname = `/${trim(url.pathname.replace(requestOptions.prefix ?? "", ""), "/")}`;
|
558
|
-
let path;
|
559
|
-
let procedure;
|
560
|
-
let params;
|
561
|
-
if (isORPCTransformer) {
|
562
|
-
path = trim(pathname, "/").split("/").map(decodeURIComponent);
|
563
|
-
const val = get(options.router, path);
|
564
|
-
if (isProcedure(val)) {
|
565
|
-
procedure = val;
|
566
|
-
}
|
567
|
-
} else {
|
568
|
-
const customMethod = requestOptions.request.method === "POST" ? url.searchParams.get("method")?.toUpperCase() : void 0;
|
569
|
-
const method = customMethod || requestOptions.request.method;
|
570
|
-
const [matches, params_] = routing.match(method, pathname);
|
571
|
-
const [match] = matches.sort((a, b) => {
|
572
|
-
return Object.keys(a[1]).length - Object.keys(b[1]).length;
|
573
|
-
});
|
574
|
-
if (match) {
|
575
|
-
path = match[0][0];
|
576
|
-
procedure = match[0][1];
|
577
|
-
if (params_) {
|
578
|
-
params = mapValues(
|
579
|
-
match[1],
|
580
|
-
(v) => params_[v]
|
581
|
-
);
|
582
|
-
} else {
|
583
|
-
params = match[1];
|
584
|
-
}
|
585
|
-
}
|
586
|
-
if (!path || !procedure) {
|
587
|
-
path = trim(pathname, "/").split("/").map(decodeURIComponent);
|
588
|
-
const val = get(options.router, path);
|
589
|
-
if (isProcedure(val)) {
|
590
|
-
procedure = val;
|
591
|
-
}
|
592
|
-
}
|
593
|
-
}
|
594
|
-
if (!path || !procedure) {
|
595
|
-
throw new ORPCError({ code: "NOT_FOUND", message: "Not found" });
|
224
|
+
// src/fetch/orpc-handler.ts
|
225
|
+
var ORPCHandler = class {
|
226
|
+
constructor(router, options) {
|
227
|
+
this.router = router;
|
228
|
+
this.options = options;
|
229
|
+
this.procedureMatcher = options?.procedureMatcher ?? new ORPCProcedureMatcher(router);
|
230
|
+
this.payloadCodec = options?.payloadCodec ?? new ORPCPayloadCodec();
|
231
|
+
}
|
232
|
+
procedureMatcher;
|
233
|
+
payloadCodec;
|
234
|
+
condition(request) {
|
235
|
+
return Boolean(request.headers.get(ORPC_HANDLER_HEADER)?.includes(ORPC_HANDLER_VALUE));
|
236
|
+
}
|
237
|
+
async fetch(request, ...[options]) {
|
238
|
+
const context = options?.context;
|
239
|
+
const execute = async () => {
|
240
|
+
const url = new URL(request.url);
|
241
|
+
const pathname = `/${trim2(url.pathname.replace(options?.prefix ?? "", ""), "/")}`;
|
242
|
+
const match = await this.procedureMatcher.match(pathname);
|
243
|
+
if (!match) {
|
244
|
+
throw new ORPCError2({ code: "NOT_FOUND", message: "Not found" });
|
596
245
|
}
|
597
|
-
const
|
598
|
-
|
599
|
-
});
|
600
|
-
const input_ = await (async () => {
|
601
|
-
try {
|
602
|
-
return await deserializer.deserialize(requestOptions.request);
|
603
|
-
} catch (e) {
|
604
|
-
throw new ORPCError({
|
605
|
-
code: "BAD_REQUEST",
|
606
|
-
message: "Cannot parse request. Please check the request body and Content-Type header.",
|
607
|
-
cause: e
|
608
|
-
});
|
609
|
-
}
|
610
|
-
})();
|
611
|
-
const input = (() => {
|
612
|
-
if (!params || Object.keys(params).length === 0) {
|
613
|
-
return input_;
|
614
|
-
}
|
615
|
-
const coercedParams = procedure.zz$p.contract.zz$cp.InputSchema ? zodCoerce(
|
616
|
-
procedure.zz$p.contract.zz$cp.InputSchema,
|
617
|
-
{ ...params },
|
618
|
-
{
|
619
|
-
bracketNotation: true
|
620
|
-
}
|
621
|
-
) : params;
|
622
|
-
if (!isPlainObject(input_)) {
|
623
|
-
return coercedParams;
|
624
|
-
}
|
625
|
-
return {
|
626
|
-
...coercedParams,
|
627
|
-
...input_
|
628
|
-
};
|
629
|
-
})();
|
630
|
-
const caller = createProcedureCaller({
|
246
|
+
const input = await this.payloadCodec.decode(request);
|
247
|
+
const client = createProcedureClient({
|
631
248
|
context,
|
632
|
-
procedure,
|
633
|
-
path
|
634
|
-
});
|
635
|
-
const output = await caller(input);
|
636
|
-
const { body, headers } = serializer.serialize(output);
|
637
|
-
return new Response(body, {
|
638
|
-
status: 200,
|
639
|
-
headers
|
249
|
+
procedure: match.procedure,
|
250
|
+
path: match.path
|
640
251
|
});
|
252
|
+
const output = await client(input, { signal: options?.signal });
|
253
|
+
const { body, headers } = this.payloadCodec.encode(output);
|
254
|
+
return new Response(body, { headers });
|
641
255
|
};
|
642
256
|
try {
|
643
|
-
return await
|
644
|
-
|
645
|
-
|
646
|
-
|
257
|
+
return await executeWithHooks({
|
258
|
+
context,
|
259
|
+
execute,
|
260
|
+
input: request,
|
261
|
+
hooks: this.options,
|
262
|
+
meta: {
|
263
|
+
signal: options?.signal
|
264
|
+
}
|
265
|
+
});
|
647
266
|
} catch (e) {
|
648
|
-
const error =
|
649
|
-
|
650
|
-
|
651
|
-
|
652
|
-
|
653
|
-
|
654
|
-
|
655
|
-
|
656
|
-
|
657
|
-
|
658
|
-
error2.toJSON()
|
659
|
-
);
|
660
|
-
return new Response(body, {
|
661
|
-
status: error2.status,
|
662
|
-
headers
|
663
|
-
});
|
664
|
-
}
|
267
|
+
const error = e instanceof ORPCError2 ? e : new ORPCError2({
|
268
|
+
code: "INTERNAL_SERVER_ERROR",
|
269
|
+
message: "Internal server error",
|
270
|
+
cause: e
|
271
|
+
});
|
272
|
+
const { body, headers } = this.payloadCodec.encode(error.toJSON());
|
273
|
+
return new Response(body, {
|
274
|
+
headers,
|
275
|
+
status: error.status
|
276
|
+
});
|
665
277
|
}
|
666
|
-
}
|
667
|
-
}
|
668
|
-
function openAPIPathToRouterPath(path) {
|
669
|
-
return standardizeHTTPPath(path).replace(/\{([^}]+)\}/g, ":$1");
|
670
|
-
}
|
671
|
-
function toORPCError(e) {
|
672
|
-
return e instanceof ORPCError ? e : new ORPCError({
|
673
|
-
code: "INTERNAL_SERVER_ERROR",
|
674
|
-
message: "Internal server error",
|
675
|
-
cause: e
|
676
|
-
});
|
677
|
-
}
|
278
|
+
}
|
279
|
+
};
|
678
280
|
export {
|
679
|
-
|
281
|
+
CompositeHandler,
|
282
|
+
ORPCHandler,
|
283
|
+
ORPCPayloadCodec,
|
284
|
+
ORPCProcedureMatcher
|
680
285
|
};
|
286
|
+
//# sourceMappingURL=fetch.js.map
|