@orpc/server 0.0.0-next.9fe2a8b → 0.0.0-next.a05fbfd
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 +118 -0
- package/dist/adapters/fetch/index.d.mts +27 -0
- package/dist/adapters/fetch/index.d.ts +27 -0
- package/dist/adapters/fetch/index.mjs +9 -0
- package/dist/adapters/hono/index.d.mts +20 -0
- package/dist/adapters/hono/index.d.ts +20 -0
- package/dist/adapters/hono/index.mjs +32 -0
- package/dist/adapters/next/index.d.mts +27 -0
- package/dist/adapters/next/index.d.ts +27 -0
- package/dist/{next.js → adapters/next/index.mjs} +11 -18
- package/dist/adapters/node/index.d.mts +35 -0
- package/dist/adapters/node/index.d.ts +35 -0
- package/dist/adapters/node/index.mjs +28 -0
- package/dist/adapters/standard/index.d.mts +29 -0
- package/dist/adapters/standard/index.d.ts +29 -0
- package/dist/adapters/standard/index.mjs +7 -0
- package/dist/index.d.mts +253 -0
- package/dist/index.d.ts +253 -0
- package/dist/{index.js → index.mjs} +41 -99
- package/dist/plugins/index.d.mts +31 -0
- package/dist/plugins/index.d.ts +31 -0
- package/dist/plugins/index.mjs +103 -0
- package/dist/shared/server.BBGuTxHE.mjs +163 -0
- package/dist/shared/server.BqEaivV1.d.ts +9 -0
- package/dist/shared/server.CHpDfeOK.d.mts +77 -0
- package/dist/shared/server.CI7U5gRZ.d.mts +152 -0
- package/dist/shared/server.CI7U5gRZ.d.ts +152 -0
- package/dist/shared/server.CUE4Aija.mjs +24 -0
- package/dist/shared/server.DSZ2XY8G.d.ts +77 -0
- package/dist/shared/server.MnOqRlBp.d.mts +9 -0
- package/dist/shared/server.Q6ZmnTgO.mjs +12 -0
- package/dist/{chunk-KK4SDLC7.js → shared/server.V6zT5iYQ.mjs} +148 -89
- package/package.json +36 -24
- package/dist/chunk-ESTRJAOX.js +0 -299
- package/dist/chunk-WUOGVGWG.js +0 -1
- package/dist/fetch.js +0 -15
- package/dist/hono.js +0 -30
- package/dist/node.js +0 -87
- package/dist/src/adapters/fetch/index.d.ts +0 -6
- package/dist/src/adapters/fetch/orpc-handler.d.ts +0 -20
- package/dist/src/adapters/fetch/orpc-payload-codec.d.ts +0 -16
- package/dist/src/adapters/fetch/orpc-procedure-matcher.d.ts +0 -12
- package/dist/src/adapters/fetch/super-json.d.ts +0 -12
- package/dist/src/adapters/fetch/types.d.ts +0 -21
- package/dist/src/adapters/hono/index.d.ts +0 -3
- package/dist/src/adapters/hono/middleware.d.ts +0 -12
- package/dist/src/adapters/next/index.d.ts +0 -3
- package/dist/src/adapters/next/serve.d.ts +0 -19
- package/dist/src/adapters/node/index.d.ts +0 -5
- package/dist/src/adapters/node/orpc-handler.d.ts +0 -12
- package/dist/src/adapters/node/request-listener.d.ts +0 -28
- package/dist/src/adapters/node/types.d.ts +0 -22
- package/dist/src/builder-variants.d.ts +0 -74
- package/dist/src/builder.d.ts +0 -57
- package/dist/src/config.d.ts +0 -6
- package/dist/src/context.d.ts +0 -9
- package/dist/src/hidden.d.ts +0 -8
- package/dist/src/implementer-procedure.d.ts +0 -30
- package/dist/src/implementer-variants.d.ts +0 -17
- package/dist/src/implementer.d.ts +0 -28
- package/dist/src/index.d.ts +0 -21
- package/dist/src/lazy-utils.d.ts +0 -6
- package/dist/src/lazy.d.ts +0 -22
- package/dist/src/middleware-decorated.d.ts +0 -10
- package/dist/src/middleware-utils.d.ts +0 -5
- package/dist/src/middleware.d.ts +0 -37
- package/dist/src/procedure-client.d.ts +0 -20
- package/dist/src/procedure-decorated.d.ts +0 -21
- package/dist/src/procedure-utils.d.ts +0 -17
- package/dist/src/procedure.d.ts +0 -29
- package/dist/src/router-accessible-lazy.d.ts +0 -8
- package/dist/src/router-client.d.ts +0 -22
- package/dist/src/router.d.ts +0 -29
@@ -1,11 +1,8 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
5
|
-
};
|
1
|
+
import { fallbackORPCErrorStatus, ORPCError } from '@orpc/client';
|
2
|
+
import { isContractProcedure, ValidationError, mergePrefix, mergeErrorMap, adaptRoute } from '@orpc/contract';
|
3
|
+
import { value, intercept, toError } from '@orpc/shared';
|
6
4
|
|
7
|
-
|
8
|
-
var LAZY_LOADER_SYMBOL = Symbol("ORPC_LAZY_LOADER");
|
5
|
+
const LAZY_LOADER_SYMBOL = Symbol("ORPC_LAZY_LOADER");
|
9
6
|
function lazy(loader) {
|
10
7
|
return {
|
11
8
|
[LAZY_LOADER_SYMBOL]: loader
|
@@ -18,14 +15,12 @@ function unlazy(lazied) {
|
|
18
15
|
return isLazy(lazied) ? lazied[LAZY_LOADER_SYMBOL]() : Promise.resolve({ default: lazied });
|
19
16
|
}
|
20
17
|
|
21
|
-
|
22
|
-
import { isContractProcedure } from "@orpc/contract";
|
23
|
-
var Procedure = class {
|
18
|
+
class Procedure {
|
24
19
|
"~orpc";
|
25
20
|
constructor(def) {
|
26
21
|
this["~orpc"] = def;
|
27
22
|
}
|
28
|
-
}
|
23
|
+
}
|
29
24
|
function isProcedure(item) {
|
30
25
|
if (item instanceof Procedure) {
|
31
26
|
return true;
|
@@ -33,7 +28,6 @@ function isProcedure(item) {
|
|
33
28
|
return isContractProcedure(item) && "middlewares" in item["~orpc"] && "inputValidationIndex" in item["~orpc"] && "outputValidationIndex" in item["~orpc"] && "handler" in item["~orpc"];
|
34
29
|
}
|
35
30
|
|
36
|
-
// src/lazy-utils.ts
|
37
31
|
function flatLazy(lazied) {
|
38
32
|
const flattenLoader = async () => {
|
39
33
|
let current = await unlazy(lazied);
|
@@ -62,37 +56,87 @@ function createLazyProcedureFormAnyLazy(lazied) {
|
|
62
56
|
return lazyProcedure;
|
63
57
|
}
|
64
58
|
|
65
|
-
|
59
|
+
function dedupeMiddlewares(compare, middlewares) {
|
60
|
+
let min = 0;
|
61
|
+
for (let i = 0; i < middlewares.length; i++) {
|
62
|
+
const index = compare.indexOf(middlewares[i], min);
|
63
|
+
if (index === -1) {
|
64
|
+
return middlewares.slice(i);
|
65
|
+
}
|
66
|
+
min = index + 1;
|
67
|
+
}
|
68
|
+
return [];
|
69
|
+
}
|
70
|
+
function mergeMiddlewares(first, second) {
|
71
|
+
return [...first, ...dedupeMiddlewares(first, second)];
|
72
|
+
}
|
73
|
+
function addMiddleware(middlewares, addition) {
|
74
|
+
return [...middlewares, addition];
|
75
|
+
}
|
76
|
+
|
77
|
+
function createORPCErrorConstructorMap(errors) {
|
78
|
+
const proxy = new Proxy(errors, {
|
79
|
+
get(target, code) {
|
80
|
+
if (typeof code !== "string") {
|
81
|
+
return Reflect.get(target, code);
|
82
|
+
}
|
83
|
+
const item = (...[options]) => {
|
84
|
+
const config = errors[code];
|
85
|
+
return new ORPCError(code, {
|
86
|
+
defined: Boolean(config),
|
87
|
+
status: config?.status,
|
88
|
+
message: options?.message ?? config?.message,
|
89
|
+
data: options?.data,
|
90
|
+
cause: options?.cause
|
91
|
+
});
|
92
|
+
};
|
93
|
+
return item;
|
94
|
+
}
|
95
|
+
});
|
96
|
+
return proxy;
|
97
|
+
}
|
98
|
+
async function validateORPCError(map, error) {
|
99
|
+
const { code, status, message, data, cause, defined } = error;
|
100
|
+
const config = map?.[error.code];
|
101
|
+
if (!config || fallbackORPCErrorStatus(error.code, config.status) !== error.status) {
|
102
|
+
return defined ? new ORPCError(code, { defined: false, status, message, data, cause }) : error;
|
103
|
+
}
|
104
|
+
if (!config.data) {
|
105
|
+
return defined ? error : new ORPCError(code, { defined: true, status, message, data, cause });
|
106
|
+
}
|
107
|
+
const validated = await config.data["~standard"].validate(error.data);
|
108
|
+
if (validated.issues) {
|
109
|
+
return defined ? new ORPCError(code, { defined: false, status, message, data, cause }) : error;
|
110
|
+
}
|
111
|
+
return new ORPCError(code, { defined: true, status, message, data: validated.value, cause });
|
112
|
+
}
|
113
|
+
|
66
114
|
function middlewareOutputFn(output) {
|
67
115
|
return { output, context: {} };
|
68
116
|
}
|
69
117
|
|
70
|
-
// src/procedure-client.ts
|
71
|
-
import { createORPCErrorConstructorMap, ORPCError, validateORPCError, ValidationError } from "@orpc/contract";
|
72
|
-
import { executeWithHooks, toError, value } from "@orpc/shared";
|
73
118
|
function createProcedureClient(lazyableProcedure, ...[options]) {
|
74
119
|
return async (...[input, callerOptions]) => {
|
75
120
|
const path = options?.path ?? [];
|
76
121
|
const { default: procedure } = await unlazy(lazyableProcedure);
|
77
|
-
const
|
122
|
+
const clientContext = callerOptions?.context ?? {};
|
123
|
+
const context = await value(options?.context ?? {}, clientContext);
|
78
124
|
const errors = createORPCErrorConstructorMap(procedure["~orpc"].errorMap);
|
79
|
-
const executeOptions = {
|
80
|
-
input,
|
81
|
-
context,
|
82
|
-
errors,
|
83
|
-
path,
|
84
|
-
procedure,
|
85
|
-
signal: callerOptions?.signal
|
86
|
-
};
|
87
125
|
try {
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
126
|
+
return await intercept(
|
127
|
+
options?.interceptors ?? [],
|
128
|
+
{
|
129
|
+
context,
|
130
|
+
input,
|
131
|
+
// input only optional when it undefinable so we can safely cast it
|
132
|
+
errors,
|
133
|
+
path,
|
134
|
+
procedure,
|
135
|
+
signal: callerOptions?.signal,
|
136
|
+
lastEventId: callerOptions?.lastEventId
|
137
|
+
},
|
138
|
+
(interceptorOptions) => executeProcedureInternal(interceptorOptions.procedure, interceptorOptions)
|
139
|
+
);
|
96
140
|
} catch (e) {
|
97
141
|
if (!(e instanceof ORPCError)) {
|
98
142
|
throw toError(e);
|
@@ -161,8 +205,7 @@ async function executeProcedureInternal(procedure, options) {
|
|
161
205
|
return (await next({})).output;
|
162
206
|
}
|
163
207
|
|
164
|
-
|
165
|
-
var ROUTER_CONTRACT_SYMBOL = Symbol("ORPC_ROUTER_CONTRACT");
|
208
|
+
const ROUTER_CONTRACT_SYMBOL = Symbol("ORPC_ROUTER_CONTRACT");
|
166
209
|
function setRouterContract(obj, contract) {
|
167
210
|
return new Proxy(obj, {
|
168
211
|
get(target, key) {
|
@@ -176,7 +219,7 @@ function setRouterContract(obj, contract) {
|
|
176
219
|
function getRouterContract(obj) {
|
177
220
|
return obj[ROUTER_CONTRACT_SYMBOL];
|
178
221
|
}
|
179
|
-
|
222
|
+
const LAZY_ROUTER_PREFIX_SYMBOL = Symbol("ORPC_LAZY_ROUTER_PREFIX");
|
180
223
|
function deepSetLazyRouterPrefix(router, prefix) {
|
181
224
|
return new Proxy(router, {
|
182
225
|
get(target, key) {
|
@@ -195,29 +238,20 @@ function getLazyRouterPrefix(obj) {
|
|
195
238
|
return obj[LAZY_ROUTER_PREFIX_SYMBOL];
|
196
239
|
}
|
197
240
|
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
return middlewares.slice(i);
|
241
|
+
function createAccessibleLazyRouter(lazied) {
|
242
|
+
const flattenLazy = flatLazy(lazied);
|
243
|
+
const recursive = new Proxy(flattenLazy, {
|
244
|
+
get(target, key) {
|
245
|
+
if (typeof key !== "string") {
|
246
|
+
return Reflect.get(target, key);
|
247
|
+
}
|
248
|
+
const next = getRouterChild(flattenLazy, key);
|
249
|
+
return createAccessibleLazyRouter(next);
|
208
250
|
}
|
209
|
-
|
210
|
-
|
211
|
-
return [];
|
212
|
-
}
|
213
|
-
function mergeMiddlewares(first, second) {
|
214
|
-
return [...first, ...dedupeMiddlewares(first, second)];
|
215
|
-
}
|
216
|
-
function addMiddleware(middlewares, addition) {
|
217
|
-
return [...middlewares, addition];
|
251
|
+
});
|
252
|
+
return recursive;
|
218
253
|
}
|
219
254
|
|
220
|
-
// src/router.ts
|
221
255
|
function adaptRouter(router, options) {
|
222
256
|
if (isLazy(router)) {
|
223
257
|
const adapted2 = lazy(async () => {
|
@@ -281,40 +315,65 @@ function getRouterChild(router, ...path) {
|
|
281
315
|
return current;
|
282
316
|
}
|
283
317
|
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
}
|
292
|
-
|
293
|
-
|
318
|
+
function eachContractProcedure(options, callback, laziedOptions = []) {
|
319
|
+
const hiddenContract = getRouterContract(options.router);
|
320
|
+
if (hiddenContract) {
|
321
|
+
return eachContractProcedure(
|
322
|
+
{
|
323
|
+
router: hiddenContract,
|
324
|
+
path: options.path
|
325
|
+
},
|
326
|
+
callback,
|
327
|
+
laziedOptions
|
328
|
+
);
|
329
|
+
}
|
330
|
+
if (isLazy(options.router)) {
|
331
|
+
laziedOptions.push({
|
332
|
+
lazied: options.router,
|
333
|
+
path: options.path
|
334
|
+
});
|
335
|
+
} else if (isContractProcedure(options.router)) {
|
336
|
+
callback({
|
337
|
+
contract: options.router,
|
338
|
+
path: options.path
|
339
|
+
});
|
340
|
+
} else {
|
341
|
+
for (const key in options.router) {
|
342
|
+
eachContractProcedure(
|
343
|
+
{
|
344
|
+
router: options.router[key],
|
345
|
+
path: [...options.path, key]
|
346
|
+
},
|
347
|
+
callback,
|
348
|
+
laziedOptions
|
349
|
+
);
|
350
|
+
}
|
351
|
+
}
|
352
|
+
return laziedOptions;
|
353
|
+
}
|
354
|
+
async function eachAllContractProcedure(options, callback) {
|
355
|
+
const pending = [options];
|
356
|
+
for (const item of pending) {
|
357
|
+
const lazies = eachContractProcedure(item, callback);
|
358
|
+
for (const lazy of lazies) {
|
359
|
+
const { default: router } = await unlazy(lazy.lazied);
|
360
|
+
pending.push({
|
361
|
+
path: lazy.path,
|
362
|
+
router
|
363
|
+
});
|
294
364
|
}
|
365
|
+
}
|
366
|
+
}
|
367
|
+
function convertPathToHttpPath(path) {
|
368
|
+
return `/${path.map(encodeURIComponent).join("/")}`;
|
369
|
+
}
|
370
|
+
function createContractedProcedure(contract, procedure) {
|
371
|
+
return new Procedure({
|
372
|
+
...procedure["~orpc"],
|
373
|
+
errorMap: contract["~orpc"].errorMap,
|
374
|
+
route: contract["~orpc"].route,
|
375
|
+
meta: contract["~orpc"].meta
|
295
376
|
});
|
296
|
-
return recursive;
|
297
377
|
}
|
298
378
|
|
299
|
-
export {
|
300
|
-
__export,
|
301
|
-
LAZY_LOADER_SYMBOL,
|
302
|
-
lazy,
|
303
|
-
isLazy,
|
304
|
-
unlazy,
|
305
|
-
Procedure,
|
306
|
-
isProcedure,
|
307
|
-
flatLazy,
|
308
|
-
createLazyProcedureFormAnyLazy,
|
309
|
-
addMiddleware,
|
310
|
-
middlewareOutputFn,
|
311
|
-
createProcedureClient,
|
312
|
-
setRouterContract,
|
313
|
-
getRouterContract,
|
314
|
-
deepSetLazyRouterPrefix,
|
315
|
-
getLazyRouterPrefix,
|
316
|
-
createAccessibleLazyRouter,
|
317
|
-
adaptRouter,
|
318
|
-
getRouterChild
|
319
|
-
};
|
320
|
-
//# sourceMappingURL=chunk-KK4SDLC7.js.map
|
379
|
+
export { LAZY_LOADER_SYMBOL as L, Procedure as P, convertPathToHttpPath as a, createContractedProcedure as b, createProcedureClient as c, addMiddleware as d, eachContractProcedure as e, adaptRouter as f, getRouterChild as g, flatLazy as h, isProcedure as i, isLazy as j, createLazyProcedureFormAnyLazy as k, lazy as l, getRouterContract as m, deepSetLazyRouterPrefix as n, getLazyRouterPrefix as o, middlewareOutputFn as p, createAccessibleLazyRouter as q, eachAllContractProcedure as r, setRouterContract as s, unlazy as u };
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@orpc/server",
|
3
3
|
"type": "module",
|
4
|
-
"version": "0.0.0-next.
|
4
|
+
"version": "0.0.0-next.a05fbfd",
|
5
5
|
"license": "MIT",
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
7
7
|
"repository": {
|
@@ -15,37 +15,42 @@
|
|
15
15
|
],
|
16
16
|
"exports": {
|
17
17
|
".": {
|
18
|
-
"types": "./dist/
|
19
|
-
"import": "./dist/index.
|
20
|
-
"default": "./dist/index.
|
18
|
+
"types": "./dist/index.d.mts",
|
19
|
+
"import": "./dist/index.mjs",
|
20
|
+
"default": "./dist/index.mjs"
|
21
|
+
},
|
22
|
+
"./plugins": {
|
23
|
+
"types": "./dist/plugins/index.d.mts",
|
24
|
+
"import": "./dist/plugins/index.mjs",
|
25
|
+
"default": "./dist/plugins/index.mjs"
|
26
|
+
},
|
27
|
+
"./standard": {
|
28
|
+
"types": "./dist/adapters/standard/index.d.mts",
|
29
|
+
"import": "./dist/adapters/standard/index.mjs",
|
30
|
+
"default": "./dist/adapters/standard/index.mjs"
|
21
31
|
},
|
22
32
|
"./fetch": {
|
23
|
-
"types": "./dist/
|
24
|
-
"import": "./dist/fetch.
|
25
|
-
"default": "./dist/fetch.
|
33
|
+
"types": "./dist/adapters/fetch/index.d.mts",
|
34
|
+
"import": "./dist/adapters/fetch/index.mjs",
|
35
|
+
"default": "./dist/adapters/fetch/index.mjs"
|
26
36
|
},
|
27
37
|
"./hono": {
|
28
|
-
"types": "./dist/
|
29
|
-
"import": "./dist/hono.
|
30
|
-
"default": "./dist/hono.
|
38
|
+
"types": "./dist/adapters/hono/index.d.mts",
|
39
|
+
"import": "./dist/adapters/hono/index.mjs",
|
40
|
+
"default": "./dist/adapters/hono/index.mjs"
|
31
41
|
},
|
32
42
|
"./next": {
|
33
|
-
"types": "./dist/
|
34
|
-
"import": "./dist/next.
|
35
|
-
"default": "./dist/next.
|
43
|
+
"types": "./dist/adapters/next/index.d.mts",
|
44
|
+
"import": "./dist/adapters/next/index.mjs",
|
45
|
+
"default": "./dist/adapters/next/index.mjs"
|
36
46
|
},
|
37
47
|
"./node": {
|
38
|
-
"types": "./dist/
|
39
|
-
"import": "./dist/node.
|
40
|
-
"default": "./dist/node.
|
41
|
-
},
|
42
|
-
"./🔒/*": {
|
43
|
-
"types": "./dist/src/*.d.ts"
|
48
|
+
"types": "./dist/adapters/node/index.d.mts",
|
49
|
+
"import": "./dist/adapters/node/index.mjs",
|
50
|
+
"default": "./dist/adapters/node/index.mjs"
|
44
51
|
}
|
45
52
|
},
|
46
53
|
"files": [
|
47
|
-
"!**/*.map",
|
48
|
-
"!**/*.tsbuildinfo",
|
49
54
|
"dist"
|
50
55
|
],
|
51
56
|
"peerDependencies": {
|
@@ -53,11 +58,18 @@
|
|
53
58
|
"next": ">=14.0.0"
|
54
59
|
},
|
55
60
|
"dependencies": {
|
56
|
-
"@orpc/
|
57
|
-
"@orpc/shared": "0.0.0-next.
|
61
|
+
"@orpc/client": "0.0.0-next.a05fbfd",
|
62
|
+
"@orpc/shared": "0.0.0-next.a05fbfd",
|
63
|
+
"@orpc/standard-server": "0.0.0-next.a05fbfd",
|
64
|
+
"@orpc/contract": "0.0.0-next.a05fbfd",
|
65
|
+
"@orpc/standard-server-fetch": "0.0.0-next.a05fbfd",
|
66
|
+
"@orpc/standard-server-node": "0.0.0-next.a05fbfd"
|
67
|
+
},
|
68
|
+
"devDependencies": {
|
69
|
+
"light-my-request": "^6.5.1"
|
58
70
|
},
|
59
71
|
"scripts": {
|
60
|
-
"build": "
|
72
|
+
"build": "unbuild",
|
61
73
|
"build:watch": "pnpm run build --watch",
|
62
74
|
"type:check": "tsc -b"
|
63
75
|
}
|
package/dist/chunk-ESTRJAOX.js
DELETED
@@ -1,299 +0,0 @@
|
|
1
|
-
import {
|
2
|
-
__export,
|
3
|
-
createProcedureClient,
|
4
|
-
getRouterChild,
|
5
|
-
isProcedure,
|
6
|
-
unlazy
|
7
|
-
} from "./chunk-KK4SDLC7.js";
|
8
|
-
|
9
|
-
// src/adapters/fetch/super-json.ts
|
10
|
-
var super_json_exports = {};
|
11
|
-
__export(super_json_exports, {
|
12
|
-
deserialize: () => deserialize,
|
13
|
-
serialize: () => serialize
|
14
|
-
});
|
15
|
-
|
16
|
-
// ../../node_modules/.pnpm/is-what@5.0.2/node_modules/is-what/dist/getType.js
|
17
|
-
function getType(payload) {
|
18
|
-
return Object.prototype.toString.call(payload).slice(8, -1);
|
19
|
-
}
|
20
|
-
|
21
|
-
// ../../node_modules/.pnpm/is-what@5.0.2/node_modules/is-what/dist/isPlainObject.js
|
22
|
-
function isPlainObject(payload) {
|
23
|
-
if (getType(payload) !== "Object")
|
24
|
-
return false;
|
25
|
-
const prototype = Object.getPrototypeOf(payload);
|
26
|
-
return !!prototype && prototype.constructor === Object && prototype === Object.prototype;
|
27
|
-
}
|
28
|
-
|
29
|
-
// src/adapters/fetch/super-json.ts
|
30
|
-
function serialize(value, segments = [], meta = []) {
|
31
|
-
if (typeof value === "bigint") {
|
32
|
-
meta.push(["bigint", segments]);
|
33
|
-
return { data: value.toString(), meta };
|
34
|
-
}
|
35
|
-
if (value instanceof Date) {
|
36
|
-
meta.push(["date", segments]);
|
37
|
-
const data = Number.isNaN(value.getTime()) ? "Invalid Date" : value.toISOString();
|
38
|
-
return { data, meta };
|
39
|
-
}
|
40
|
-
if (Number.isNaN(value)) {
|
41
|
-
meta.push(["nan", segments]);
|
42
|
-
return { data: "NaN", meta };
|
43
|
-
}
|
44
|
-
if (value instanceof RegExp) {
|
45
|
-
meta.push(["regexp", segments]);
|
46
|
-
return { data: value.toString(), meta };
|
47
|
-
}
|
48
|
-
if (value instanceof URL) {
|
49
|
-
meta.push(["url", segments]);
|
50
|
-
return { data: value.toString(), meta };
|
51
|
-
}
|
52
|
-
if (isPlainObject(value)) {
|
53
|
-
const data = {};
|
54
|
-
for (const k in value) {
|
55
|
-
data[k] = serialize(value[k], [...segments, k], meta).data;
|
56
|
-
}
|
57
|
-
return { data, meta };
|
58
|
-
}
|
59
|
-
if (Array.isArray(value)) {
|
60
|
-
const data = value.map((v, i) => {
|
61
|
-
if (v === void 0) {
|
62
|
-
meta.push(["undefined", [...segments, i]]);
|
63
|
-
return null;
|
64
|
-
}
|
65
|
-
return serialize(v, [...segments, i], meta).data;
|
66
|
-
});
|
67
|
-
return { data, meta };
|
68
|
-
}
|
69
|
-
if (value instanceof Set) {
|
70
|
-
const result = serialize(Array.from(value), segments, meta);
|
71
|
-
meta.push(["set", segments]);
|
72
|
-
return result;
|
73
|
-
}
|
74
|
-
if (value instanceof Map) {
|
75
|
-
const result = serialize(Array.from(value.entries()), segments, meta);
|
76
|
-
meta.push(["map", segments]);
|
77
|
-
return result;
|
78
|
-
}
|
79
|
-
return { data: value, meta };
|
80
|
-
}
|
81
|
-
function deserialize({
|
82
|
-
data,
|
83
|
-
meta
|
84
|
-
}) {
|
85
|
-
if (meta.length === 0) {
|
86
|
-
return data;
|
87
|
-
}
|
88
|
-
const ref = { data };
|
89
|
-
for (const [type, segments] of meta) {
|
90
|
-
let currentRef = ref;
|
91
|
-
let preSegment = "data";
|
92
|
-
for (let i = 0; i < segments.length; i++) {
|
93
|
-
currentRef = currentRef[preSegment];
|
94
|
-
preSegment = segments[i];
|
95
|
-
}
|
96
|
-
switch (type) {
|
97
|
-
case "nan":
|
98
|
-
currentRef[preSegment] = Number.NaN;
|
99
|
-
break;
|
100
|
-
case "bigint":
|
101
|
-
currentRef[preSegment] = BigInt(currentRef[preSegment]);
|
102
|
-
break;
|
103
|
-
case "date":
|
104
|
-
currentRef[preSegment] = new Date(currentRef[preSegment]);
|
105
|
-
break;
|
106
|
-
case "regexp": {
|
107
|
-
const [, pattern, flags] = currentRef[preSegment].match(/^\/(.*)\/([a-z]*)$/);
|
108
|
-
currentRef[preSegment] = new RegExp(pattern, flags);
|
109
|
-
break;
|
110
|
-
}
|
111
|
-
case "url":
|
112
|
-
currentRef[preSegment] = new URL(currentRef[preSegment]);
|
113
|
-
break;
|
114
|
-
case "undefined":
|
115
|
-
currentRef[preSegment] = void 0;
|
116
|
-
break;
|
117
|
-
case "map":
|
118
|
-
currentRef[preSegment] = new Map(currentRef[preSegment]);
|
119
|
-
break;
|
120
|
-
case "set":
|
121
|
-
currentRef[preSegment] = new Set(currentRef[preSegment]);
|
122
|
-
break;
|
123
|
-
/* v8 ignore next 3 */
|
124
|
-
default: {
|
125
|
-
const _expected = type;
|
126
|
-
}
|
127
|
-
}
|
128
|
-
}
|
129
|
-
return ref.data;
|
130
|
-
}
|
131
|
-
|
132
|
-
// src/adapters/fetch/orpc-payload-codec.ts
|
133
|
-
import { ORPCError } from "@orpc/contract";
|
134
|
-
import { findDeepMatches, set } from "@orpc/shared";
|
135
|
-
var ORPCPayloadCodec = class {
|
136
|
-
/**
|
137
|
-
* If method is GET, the payload will be encoded as query string.
|
138
|
-
* If method is GET and payload contain file, the method will be fallback to fallbackMethod. (fallbackMethod = GET will force to use GET method)
|
139
|
-
*/
|
140
|
-
encode(payload, method = "POST", fallbackMethod = "POST") {
|
141
|
-
const { data, meta } = serialize(payload);
|
142
|
-
const { maps, values } = findDeepMatches((v) => v instanceof Blob, data);
|
143
|
-
if (method === "GET" && (values.length === 0 || fallbackMethod === "GET")) {
|
144
|
-
const query = new URLSearchParams({
|
145
|
-
data: JSON.stringify(data),
|
146
|
-
meta: JSON.stringify(meta)
|
147
|
-
});
|
148
|
-
return {
|
149
|
-
query,
|
150
|
-
method: "GET"
|
151
|
-
};
|
152
|
-
}
|
153
|
-
const nonGETMethod = method === "GET" ? fallbackMethod : method;
|
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 {
|
166
|
-
body: form,
|
167
|
-
method: nonGETMethod
|
168
|
-
};
|
169
|
-
}
|
170
|
-
return {
|
171
|
-
body: JSON.stringify({ data, meta }),
|
172
|
-
headers: new Headers({
|
173
|
-
"content-type": "application/json"
|
174
|
-
}),
|
175
|
-
method: nonGETMethod
|
176
|
-
};
|
177
|
-
}
|
178
|
-
async decode(re) {
|
179
|
-
try {
|
180
|
-
if ("method" in re && re.method === "GET") {
|
181
|
-
const url = new URL(re.url);
|
182
|
-
const query = url.searchParams;
|
183
|
-
const data = JSON.parse(query.getAll("data").at(-1));
|
184
|
-
const meta = JSON.parse(query.getAll("meta").at(-1));
|
185
|
-
return deserialize({
|
186
|
-
data,
|
187
|
-
meta
|
188
|
-
});
|
189
|
-
}
|
190
|
-
if (re.headers.get("content-type")?.startsWith("multipart/form-data")) {
|
191
|
-
const form = await re.formData();
|
192
|
-
const rawData = form.get("data");
|
193
|
-
const rawMeta = form.get("meta");
|
194
|
-
const rawMaps = form.get("maps");
|
195
|
-
let data = JSON.parse(rawData);
|
196
|
-
const meta = JSON.parse(rawMeta);
|
197
|
-
const maps = JSON.parse(rawMaps);
|
198
|
-
for (const i in maps) {
|
199
|
-
data = set(data, maps[i], form.get(i));
|
200
|
-
}
|
201
|
-
return deserialize({
|
202
|
-
data,
|
203
|
-
meta
|
204
|
-
});
|
205
|
-
}
|
206
|
-
const json = await re.json();
|
207
|
-
return deserialize(json);
|
208
|
-
} catch (e) {
|
209
|
-
throw new ORPCError("BAD_REQUEST", {
|
210
|
-
message: "Cannot parse request/response. Please check the request/response body and Content-Type header.",
|
211
|
-
cause: e
|
212
|
-
});
|
213
|
-
}
|
214
|
-
}
|
215
|
-
};
|
216
|
-
|
217
|
-
// src/adapters/fetch/orpc-procedure-matcher.ts
|
218
|
-
import { trim } from "@orpc/shared";
|
219
|
-
var ORPCProcedureMatcher = class {
|
220
|
-
constructor(router) {
|
221
|
-
this.router = router;
|
222
|
-
}
|
223
|
-
async match(pathname) {
|
224
|
-
const path = trim(pathname, "/").split("/").map(decodeURIComponent);
|
225
|
-
const match = getRouterChild(this.router, ...path);
|
226
|
-
const { default: maybeProcedure } = await unlazy(match);
|
227
|
-
if (!isProcedure(maybeProcedure)) {
|
228
|
-
return void 0;
|
229
|
-
}
|
230
|
-
return {
|
231
|
-
procedure: maybeProcedure,
|
232
|
-
path
|
233
|
-
};
|
234
|
-
}
|
235
|
-
};
|
236
|
-
|
237
|
-
// src/adapters/fetch/orpc-handler.ts
|
238
|
-
import { ORPCError as ORPCError2 } from "@orpc/contract";
|
239
|
-
import { executeWithHooks, trim as trim2 } from "@orpc/shared";
|
240
|
-
var RPCHandler = class {
|
241
|
-
constructor(router, options) {
|
242
|
-
this.options = options;
|
243
|
-
this.procedureMatcher = options?.procedureMatcher ?? new ORPCProcedureMatcher(router);
|
244
|
-
this.payloadCodec = options?.payloadCodec ?? new ORPCPayloadCodec();
|
245
|
-
}
|
246
|
-
procedureMatcher;
|
247
|
-
payloadCodec;
|
248
|
-
async handle(request, ...[options]) {
|
249
|
-
const context = options?.context ?? {};
|
250
|
-
const execute = async () => {
|
251
|
-
const url = new URL(request.url);
|
252
|
-
const pathname = `/${trim2(url.pathname.replace(options?.prefix ?? "", ""), "/")}`;
|
253
|
-
const match = await this.procedureMatcher.match(pathname);
|
254
|
-
if (!match) {
|
255
|
-
return { matched: false, response: void 0 };
|
256
|
-
}
|
257
|
-
const input = await this.payloadCodec.decode(request);
|
258
|
-
const client = createProcedureClient(match.procedure, {
|
259
|
-
context,
|
260
|
-
path: match.path
|
261
|
-
});
|
262
|
-
const output = await client(input, { signal: request.signal });
|
263
|
-
const { body, headers } = this.payloadCodec.encode(output);
|
264
|
-
const response = new Response(body, { headers });
|
265
|
-
return { matched: true, response };
|
266
|
-
};
|
267
|
-
try {
|
268
|
-
const result = await executeWithHooks({
|
269
|
-
context,
|
270
|
-
execute,
|
271
|
-
input: request,
|
272
|
-
hooks: this.options,
|
273
|
-
meta: {
|
274
|
-
signal: request.signal
|
275
|
-
}
|
276
|
-
});
|
277
|
-
return result;
|
278
|
-
} catch (e) {
|
279
|
-
const error = e instanceof ORPCError2 ? e : new ORPCError2("INTERNAL_SERVER_ERROR", {
|
280
|
-
message: "Internal server error",
|
281
|
-
cause: e
|
282
|
-
});
|
283
|
-
const { body, headers } = this.payloadCodec.encode(error.toJSON());
|
284
|
-
const response = new Response(body, {
|
285
|
-
headers,
|
286
|
-
status: error.status
|
287
|
-
});
|
288
|
-
return { matched: true, response };
|
289
|
-
}
|
290
|
-
}
|
291
|
-
};
|
292
|
-
|
293
|
-
export {
|
294
|
-
super_json_exports,
|
295
|
-
ORPCPayloadCodec,
|
296
|
-
ORPCProcedureMatcher,
|
297
|
-
RPCHandler
|
298
|
-
};
|
299
|
-
//# sourceMappingURL=chunk-ESTRJAOX.js.map
|