@orpc/server 0.0.0-next.9125edb → 0.0.0-next.924a598
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 +120 -0
- package/dist/adapters/fetch/index.d.mts +28 -0
- package/dist/adapters/fetch/index.d.ts +28 -0
- package/dist/adapters/fetch/index.mjs +9 -0
- package/dist/adapters/hono/index.d.mts +21 -0
- package/dist/adapters/hono/index.d.ts +21 -0
- package/dist/{hono.js → adapters/hono/index.mjs} +11 -13
- package/dist/adapters/next/index.d.mts +28 -0
- package/dist/adapters/next/index.d.ts +28 -0
- package/dist/{next.js → adapters/next/index.mjs} +11 -13
- package/dist/adapters/node/index.d.mts +36 -0
- package/dist/adapters/node/index.d.ts +36 -0
- package/dist/adapters/node/index.mjs +32 -0
- package/dist/adapters/standard/index.d.mts +26 -0
- package/dist/adapters/standard/index.d.ts +26 -0
- package/dist/adapters/standard/index.mjs +6 -0
- package/dist/index.d.mts +278 -0
- package/dist/index.d.ts +278 -0
- package/dist/{index.js → index.mjs} +93 -151
- package/dist/plugins/index.d.mts +31 -0
- package/dist/plugins/index.d.ts +31 -0
- package/dist/{chunk-XI6WGCB3.js → plugins/index.mjs} +17 -39
- package/dist/shared/server.BXfgvxK8.d.ts +8 -0
- package/dist/{chunk-XP6YRLY2.js → shared/server.B_5ZADvP.mjs} +179 -194
- package/dist/shared/server.D1vPuPwc.mjs +29 -0
- package/dist/shared/server.DKCNBSd-.d.mts +8 -0
- package/dist/shared/server.DisswUB5.mjs +158 -0
- package/dist/shared/server.Dj89-yLZ.d.ts +75 -0
- package/dist/shared/server.DnmJuN02.d.mts +144 -0
- package/dist/shared/server.DnmJuN02.d.ts +144 -0
- package/dist/shared/server.DpIMjOfC.d.mts +75 -0
- package/dist/shared/server.Q6ZmnTgO.mjs +12 -0
- package/package.json +29 -33
- package/dist/chunk-GBEB77SU.js +0 -32
- package/dist/chunk-GBL3M2PB.js +0 -325
- package/dist/fetch.js +0 -10
- package/dist/node.js +0 -31
- package/dist/plugins.js +0 -11
- package/dist/src/adapters/fetch/index.d.ts +0 -3
- package/dist/src/adapters/fetch/rpc-handler.d.ts +0 -11
- package/dist/src/adapters/fetch/types.d.ts +0 -14
- 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 -3
- package/dist/src/adapters/node/rpc-handler.d.ts +0 -11
- package/dist/src/adapters/node/types.d.ts +0 -22
- package/dist/src/adapters/standard/handler.d.ts +0 -51
- package/dist/src/adapters/standard/index.d.ts +0 -7
- package/dist/src/adapters/standard/rpc-codec.d.ts +0 -16
- package/dist/src/adapters/standard/rpc-handler.d.ts +0 -8
- package/dist/src/adapters/standard/rpc-matcher.d.ts +0 -10
- package/dist/src/adapters/standard/rpc-serializer.d.ts +0 -16
- package/dist/src/adapters/standard/types.d.ts +0 -20
- 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 -31
- package/dist/src/implementer-variants.d.ts +0 -17
- package/dist/src/implementer.d.ts +0 -28
- package/dist/src/index.d.ts +0 -23
- 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 -36
- package/dist/src/plugins/base.d.ts +0 -13
- package/dist/src/plugins/cors.d.ts +0 -19
- package/dist/src/plugins/index.d.ts +0 -4
- package/dist/src/plugins/response-headers.d.ts +0 -10
- package/dist/src/procedure-client.d.ts +0 -30
- package/dist/src/procedure-decorated.d.ts +0 -22
- package/dist/src/procedure-utils.d.ts +0 -18
- 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 -11
- package/dist/src/router.d.ts +0 -30
- package/dist/src/utils.d.ts +0 -24
- package/dist/standard.js +0 -17
@@ -1,25 +1,56 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
import { isContractProcedure, ValidationError, mergePrefix, mergeErrorMap, enhanceRoute } from '@orpc/contract';
|
2
|
+
import { fallbackORPCErrorStatus, ORPCError } from '@orpc/client';
|
3
|
+
import { value, intercept, toError } from '@orpc/shared';
|
4
|
+
|
5
|
+
const LAZY_SYMBOL = Symbol("ORPC_LAZY_SYMBOL");
|
6
|
+
function lazy(loader, meta = {}) {
|
4
7
|
return {
|
5
|
-
[
|
8
|
+
[LAZY_SYMBOL]: {
|
9
|
+
loader,
|
10
|
+
meta
|
11
|
+
}
|
6
12
|
};
|
7
13
|
}
|
8
14
|
function isLazy(item) {
|
9
|
-
return (typeof item === "object" || typeof item === "function") && item !== null &&
|
15
|
+
return (typeof item === "object" || typeof item === "function") && item !== null && LAZY_SYMBOL in item;
|
16
|
+
}
|
17
|
+
function getLazyMeta(lazied) {
|
18
|
+
return lazied[LAZY_SYMBOL].meta;
|
10
19
|
}
|
11
20
|
function unlazy(lazied) {
|
12
|
-
return isLazy(lazied) ? lazied[
|
21
|
+
return isLazy(lazied) ? lazied[LAZY_SYMBOL].loader() : Promise.resolve({ default: lazied });
|
22
|
+
}
|
23
|
+
|
24
|
+
function isStartWithMiddlewares(middlewares, compare) {
|
25
|
+
if (compare.length > middlewares.length) {
|
26
|
+
return false;
|
27
|
+
}
|
28
|
+
for (let i = 0; i < middlewares.length; i++) {
|
29
|
+
if (compare[i] === void 0) {
|
30
|
+
return true;
|
31
|
+
}
|
32
|
+
if (middlewares[i] !== compare[i]) {
|
33
|
+
return false;
|
34
|
+
}
|
35
|
+
}
|
36
|
+
return true;
|
37
|
+
}
|
38
|
+
function mergeMiddlewares(first, second, options) {
|
39
|
+
if (options.dedupeLeading && isStartWithMiddlewares(second, first)) {
|
40
|
+
return second;
|
41
|
+
}
|
42
|
+
return [...first, ...second];
|
43
|
+
}
|
44
|
+
function addMiddleware(middlewares, addition) {
|
45
|
+
return [...middlewares, addition];
|
13
46
|
}
|
14
47
|
|
15
|
-
|
16
|
-
import { isContractProcedure } from "@orpc/contract";
|
17
|
-
var Procedure = class {
|
48
|
+
class Procedure {
|
18
49
|
"~orpc";
|
19
50
|
constructor(def) {
|
20
51
|
this["~orpc"] = def;
|
21
52
|
}
|
22
|
-
}
|
53
|
+
}
|
23
54
|
function isProcedure(item) {
|
24
55
|
if (item instanceof Procedure) {
|
25
56
|
return true;
|
@@ -27,43 +58,47 @@ function isProcedure(item) {
|
|
27
58
|
return isContractProcedure(item) && "middlewares" in item["~orpc"] && "inputValidationIndex" in item["~orpc"] && "outputValidationIndex" in item["~orpc"] && "handler" in item["~orpc"];
|
28
59
|
}
|
29
60
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
if (!isLazy(current.default)) {
|
36
|
-
break;
|
61
|
+
function createORPCErrorConstructorMap(errors) {
|
62
|
+
const proxy = new Proxy(errors, {
|
63
|
+
get(target, code) {
|
64
|
+
if (typeof code !== "string") {
|
65
|
+
return Reflect.get(target, code);
|
37
66
|
}
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
Expected a lazy<procedure> but got lazy<unknown>.
|
50
|
-
This should be caught by TypeScript compilation.
|
51
|
-
Please report this issue if this makes you feel uncomfortable.
|
52
|
-
`);
|
67
|
+
const item = (...[options]) => {
|
68
|
+
const config = errors[code];
|
69
|
+
return new ORPCError(code, {
|
70
|
+
defined: Boolean(config),
|
71
|
+
status: config?.status,
|
72
|
+
message: options?.message ?? config?.message,
|
73
|
+
data: options?.data,
|
74
|
+
cause: options?.cause
|
75
|
+
});
|
76
|
+
};
|
77
|
+
return item;
|
53
78
|
}
|
54
|
-
return { default: maybeProcedure };
|
55
79
|
});
|
56
|
-
return
|
80
|
+
return proxy;
|
81
|
+
}
|
82
|
+
async function validateORPCError(map, error) {
|
83
|
+
const { code, status, message, data, cause, defined } = error;
|
84
|
+
const config = map?.[error.code];
|
85
|
+
if (!config || fallbackORPCErrorStatus(error.code, config.status) !== error.status) {
|
86
|
+
return defined ? new ORPCError(code, { defined: false, status, message, data, cause }) : error;
|
87
|
+
}
|
88
|
+
if (!config.data) {
|
89
|
+
return defined ? error : new ORPCError(code, { defined: true, status, message, data, cause });
|
90
|
+
}
|
91
|
+
const validated = await config.data["~standard"].validate(error.data);
|
92
|
+
if (validated.issues) {
|
93
|
+
return defined ? new ORPCError(code, { defined: false, status, message, data, cause }) : error;
|
94
|
+
}
|
95
|
+
return new ORPCError(code, { defined: true, status, message, data: validated.value, cause });
|
57
96
|
}
|
58
97
|
|
59
|
-
// src/middleware.ts
|
60
98
|
function middlewareOutputFn(output) {
|
61
99
|
return { output, context: {} };
|
62
100
|
}
|
63
101
|
|
64
|
-
// src/procedure-client.ts
|
65
|
-
import { createORPCErrorConstructorMap, ORPCError, validateORPCError, ValidationError } from "@orpc/contract";
|
66
|
-
import { intercept, toError, value } from "@orpc/shared";
|
67
102
|
function createProcedureClient(lazyableProcedure, ...[options]) {
|
68
103
|
return async (...[input, callerOptions]) => {
|
69
104
|
const path = options?.path ?? [];
|
@@ -81,7 +116,8 @@ function createProcedureClient(lazyableProcedure, ...[options]) {
|
|
81
116
|
errors,
|
82
117
|
path,
|
83
118
|
procedure,
|
84
|
-
signal: callerOptions?.signal
|
119
|
+
signal: callerOptions?.signal,
|
120
|
+
lastEventId: callerOptions?.lastEventId
|
85
121
|
},
|
86
122
|
(interceptorOptions) => executeProcedureInternal(interceptorOptions.procedure, interceptorOptions)
|
87
123
|
);
|
@@ -153,98 +189,22 @@ async function executeProcedureInternal(procedure, options) {
|
|
153
189
|
return (await next({})).output;
|
154
190
|
}
|
155
191
|
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
return new Proxy(obj, {
|
192
|
+
const HIDDEN_ROUTER_CONTRACT_SYMBOL = Symbol("ORPC_HIDDEN_ROUTER_CONTRACT");
|
193
|
+
function setHiddenRouterContract(router, contract) {
|
194
|
+
return new Proxy(router, {
|
160
195
|
get(target, key) {
|
161
|
-
if (key ===
|
196
|
+
if (key === HIDDEN_ROUTER_CONTRACT_SYMBOL) {
|
162
197
|
return contract;
|
163
198
|
}
|
164
199
|
return Reflect.get(target, key);
|
165
200
|
}
|
166
201
|
});
|
167
202
|
}
|
168
|
-
function
|
169
|
-
return
|
170
|
-
}
|
171
|
-
var LAZY_ROUTER_PREFIX_SYMBOL = Symbol("ORPC_LAZY_ROUTER_PREFIX");
|
172
|
-
function deepSetLazyRouterPrefix(router, prefix) {
|
173
|
-
return new Proxy(router, {
|
174
|
-
get(target, key) {
|
175
|
-
if (key !== LAZY_ROUTER_PREFIX_SYMBOL) {
|
176
|
-
const val = Reflect.get(target, key);
|
177
|
-
if (isLazy(val)) {
|
178
|
-
return deepSetLazyRouterPrefix(val, prefix);
|
179
|
-
}
|
180
|
-
return val;
|
181
|
-
}
|
182
|
-
return prefix;
|
183
|
-
}
|
184
|
-
});
|
185
|
-
}
|
186
|
-
function getLazyRouterPrefix(obj) {
|
187
|
-
return obj[LAZY_ROUTER_PREFIX_SYMBOL];
|
203
|
+
function getHiddenRouterContract(router) {
|
204
|
+
return router[HIDDEN_ROUTER_CONTRACT_SYMBOL];
|
188
205
|
}
|
189
206
|
|
190
|
-
|
191
|
-
import { adaptRoute, mergeErrorMap, mergePrefix } from "@orpc/contract";
|
192
|
-
|
193
|
-
// src/middleware-utils.ts
|
194
|
-
function dedupeMiddlewares(compare, middlewares) {
|
195
|
-
let min = 0;
|
196
|
-
for (let i = 0; i < middlewares.length; i++) {
|
197
|
-
const index = compare.indexOf(middlewares[i], min);
|
198
|
-
if (index === -1) {
|
199
|
-
return middlewares.slice(i);
|
200
|
-
}
|
201
|
-
min = index + 1;
|
202
|
-
}
|
203
|
-
return [];
|
204
|
-
}
|
205
|
-
function mergeMiddlewares(first, second) {
|
206
|
-
return [...first, ...dedupeMiddlewares(first, second)];
|
207
|
-
}
|
208
|
-
function addMiddleware(middlewares, addition) {
|
209
|
-
return [...middlewares, addition];
|
210
|
-
}
|
211
|
-
|
212
|
-
// src/router.ts
|
213
|
-
function adaptRouter(router, options) {
|
214
|
-
if (isLazy(router)) {
|
215
|
-
const adapted2 = lazy(async () => {
|
216
|
-
const unlaziedRouter = (await unlazy(router)).default;
|
217
|
-
const adapted3 = adaptRouter(unlaziedRouter, options);
|
218
|
-
return { default: adapted3 };
|
219
|
-
});
|
220
|
-
const accessible = createAccessibleLazyRouter(adapted2);
|
221
|
-
const currentPrefix = getLazyRouterPrefix(router);
|
222
|
-
const prefix = currentPrefix ? mergePrefix(options.prefix, currentPrefix) : options.prefix;
|
223
|
-
if (prefix) {
|
224
|
-
return deepSetLazyRouterPrefix(accessible, prefix);
|
225
|
-
}
|
226
|
-
return accessible;
|
227
|
-
}
|
228
|
-
if (isProcedure(router)) {
|
229
|
-
const newMiddlewares = mergeMiddlewares(options.middlewares, router["~orpc"].middlewares);
|
230
|
-
const newMiddlewareAdded = newMiddlewares.length - router["~orpc"].middlewares.length;
|
231
|
-
const adapted2 = new Procedure({
|
232
|
-
...router["~orpc"],
|
233
|
-
route: adaptRoute(router["~orpc"].route, options),
|
234
|
-
errorMap: mergeErrorMap(options.errorMap, router["~orpc"].errorMap),
|
235
|
-
middlewares: newMiddlewares,
|
236
|
-
inputValidationIndex: router["~orpc"].inputValidationIndex + newMiddlewareAdded,
|
237
|
-
outputValidationIndex: router["~orpc"].outputValidationIndex + newMiddlewareAdded
|
238
|
-
});
|
239
|
-
return adapted2;
|
240
|
-
}
|
241
|
-
const adapted = {};
|
242
|
-
for (const key in router) {
|
243
|
-
adapted[key] = adaptRouter(router[key], options);
|
244
|
-
}
|
245
|
-
return adapted;
|
246
|
-
}
|
247
|
-
function getRouterChild(router, ...path) {
|
207
|
+
function getRouter(router, path) {
|
248
208
|
let current = router;
|
249
209
|
for (let i = 0; i < path.length; i++) {
|
250
210
|
const segment = path[i];
|
@@ -260,89 +220,131 @@ function getRouterChild(router, ...path) {
|
|
260
220
|
}
|
261
221
|
const lazied = current;
|
262
222
|
const rest = path.slice(i);
|
263
|
-
|
223
|
+
return lazy(async () => {
|
264
224
|
const unwrapped = await unlazy(lazied);
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
const next = getRouterChild(unwrapped.default, ...rest);
|
269
|
-
return { default: next };
|
270
|
-
});
|
271
|
-
return flatLazy(newLazy);
|
225
|
+
const next = getRouter(unwrapped.default, rest);
|
226
|
+
return unlazy(next);
|
227
|
+
}, getLazyMeta(lazied));
|
272
228
|
}
|
273
229
|
return current;
|
274
230
|
}
|
275
|
-
|
276
|
-
// src/router-accessible-lazy.ts
|
277
231
|
function createAccessibleLazyRouter(lazied) {
|
278
|
-
const
|
279
|
-
const recursive = new Proxy(flattenLazy, {
|
232
|
+
const recursive = new Proxy(lazied, {
|
280
233
|
get(target, key) {
|
281
234
|
if (typeof key !== "string") {
|
282
235
|
return Reflect.get(target, key);
|
283
236
|
}
|
284
|
-
const next =
|
237
|
+
const next = getRouter(lazied, [key]);
|
285
238
|
return createAccessibleLazyRouter(next);
|
286
239
|
}
|
287
240
|
});
|
288
241
|
return recursive;
|
289
242
|
}
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
243
|
+
function enhanceRouter(router, options) {
|
244
|
+
if (isLazy(router)) {
|
245
|
+
const laziedMeta = getLazyMeta(router);
|
246
|
+
const enhancedPrefix = laziedMeta?.prefix ? mergePrefix(options.prefix, laziedMeta?.prefix) : options.prefix;
|
247
|
+
const enhanced2 = lazy(async () => {
|
248
|
+
const { default: unlaziedRouter } = await unlazy(router);
|
249
|
+
const enhanced3 = enhanceRouter(unlaziedRouter, options);
|
250
|
+
return unlazy(enhanced3);
|
251
|
+
}, {
|
252
|
+
...laziedMeta,
|
253
|
+
prefix: enhancedPrefix
|
254
|
+
});
|
255
|
+
const accessible = createAccessibleLazyRouter(enhanced2);
|
256
|
+
return accessible;
|
304
257
|
}
|
305
|
-
if (
|
306
|
-
|
307
|
-
|
258
|
+
if (isProcedure(router)) {
|
259
|
+
const newMiddlewares = mergeMiddlewares(options.middlewares, router["~orpc"].middlewares, { dedupeLeading: options.dedupeLeadingMiddlewares });
|
260
|
+
const newMiddlewareAdded = newMiddlewares.length - router["~orpc"].middlewares.length;
|
261
|
+
const enhanced2 = new Procedure({
|
262
|
+
...router["~orpc"],
|
263
|
+
route: enhanceRoute(router["~orpc"].route, options),
|
264
|
+
errorMap: mergeErrorMap(options.errorMap, router["~orpc"].errorMap),
|
265
|
+
middlewares: newMiddlewares,
|
266
|
+
inputValidationIndex: router["~orpc"].inputValidationIndex + newMiddlewareAdded,
|
267
|
+
outputValidationIndex: router["~orpc"].outputValidationIndex + newMiddlewareAdded
|
268
|
+
});
|
269
|
+
return enhanced2;
|
270
|
+
}
|
271
|
+
const enhanced = {};
|
272
|
+
for (const key in router) {
|
273
|
+
enhanced[key] = enhanceRouter(router[key], options);
|
274
|
+
}
|
275
|
+
return enhanced;
|
276
|
+
}
|
277
|
+
function traverseContractProcedures(options, callback, lazyOptions = []) {
|
278
|
+
let currentRouter = options.router;
|
279
|
+
const hiddenContract = getHiddenRouterContract(options.router);
|
280
|
+
if (hiddenContract !== void 0) {
|
281
|
+
currentRouter = hiddenContract;
|
282
|
+
}
|
283
|
+
if (isLazy(currentRouter)) {
|
284
|
+
lazyOptions.push({
|
285
|
+
router: currentRouter,
|
308
286
|
path: options.path
|
309
287
|
});
|
310
|
-
} else if (
|
288
|
+
} else if (isContractProcedure(currentRouter)) {
|
311
289
|
callback({
|
312
|
-
contract:
|
290
|
+
contract: currentRouter,
|
313
291
|
path: options.path
|
314
292
|
});
|
315
293
|
} else {
|
316
|
-
for (const key in
|
317
|
-
|
294
|
+
for (const key in currentRouter) {
|
295
|
+
traverseContractProcedures(
|
318
296
|
{
|
319
|
-
router:
|
297
|
+
router: currentRouter[key],
|
320
298
|
path: [...options.path, key]
|
321
299
|
},
|
322
300
|
callback,
|
323
|
-
|
301
|
+
lazyOptions
|
324
302
|
);
|
325
303
|
}
|
326
304
|
}
|
327
|
-
return
|
305
|
+
return lazyOptions;
|
328
306
|
}
|
329
|
-
async function
|
307
|
+
async function resolveContractProcedures(options, callback) {
|
330
308
|
const pending = [options];
|
331
|
-
for (const
|
332
|
-
const
|
333
|
-
for (const
|
334
|
-
const { default: router } = await unlazy(
|
309
|
+
for (const options2 of pending) {
|
310
|
+
const lazyOptions = traverseContractProcedures(options2, callback);
|
311
|
+
for (const options3 of lazyOptions) {
|
312
|
+
const { default: router } = await unlazy(options3.router);
|
335
313
|
pending.push({
|
336
|
-
|
337
|
-
|
314
|
+
router,
|
315
|
+
path: options3.path
|
338
316
|
});
|
339
317
|
}
|
340
318
|
}
|
341
319
|
}
|
342
|
-
function
|
343
|
-
|
320
|
+
async function unlazyRouter(router) {
|
321
|
+
if (isProcedure(router)) {
|
322
|
+
return router;
|
323
|
+
}
|
324
|
+
const unlazied = {};
|
325
|
+
for (const key in router) {
|
326
|
+
const item = router[key];
|
327
|
+
const { default: unlaziedRouter } = await unlazy(item);
|
328
|
+
unlazied[key] = await unlazyRouter(unlaziedRouter);
|
329
|
+
}
|
330
|
+
return unlazied;
|
344
331
|
}
|
345
|
-
|
332
|
+
|
333
|
+
function createAssertedLazyProcedure(lazied) {
|
334
|
+
const lazyProcedure = lazy(async () => {
|
335
|
+
const { default: maybeProcedure } = await unlazy(lazied);
|
336
|
+
if (!isProcedure(maybeProcedure)) {
|
337
|
+
throw new Error(`
|
338
|
+
Expected a lazy<procedure> but got lazy<unknown>.
|
339
|
+
This should be caught by TypeScript compilation.
|
340
|
+
Please report this issue if this makes you feel uncomfortable.
|
341
|
+
`);
|
342
|
+
}
|
343
|
+
return { default: maybeProcedure };
|
344
|
+
}, getLazyMeta(lazied));
|
345
|
+
return lazyProcedure;
|
346
|
+
}
|
347
|
+
function createContractedProcedure(procedure, contract) {
|
346
348
|
return new Procedure({
|
347
349
|
...procedure["~orpc"],
|
348
350
|
errorMap: contract["~orpc"].errorMap,
|
@@ -350,29 +352,12 @@ function createContractedProcedure(contract, procedure) {
|
|
350
352
|
meta: contract["~orpc"].meta
|
351
353
|
});
|
352
354
|
}
|
355
|
+
function call(procedure, input, ...rest) {
|
356
|
+
return createProcedureClient(procedure, ...rest)(input);
|
357
|
+
}
|
358
|
+
|
359
|
+
function toHttpPath(path) {
|
360
|
+
return `/${path.map(encodeURIComponent).join("/")}`;
|
361
|
+
}
|
353
362
|
|
354
|
-
export {
|
355
|
-
LAZY_LOADER_SYMBOL,
|
356
|
-
lazy,
|
357
|
-
isLazy,
|
358
|
-
unlazy,
|
359
|
-
Procedure,
|
360
|
-
isProcedure,
|
361
|
-
flatLazy,
|
362
|
-
createLazyProcedureFormAnyLazy,
|
363
|
-
addMiddleware,
|
364
|
-
middlewareOutputFn,
|
365
|
-
createProcedureClient,
|
366
|
-
setRouterContract,
|
367
|
-
getRouterContract,
|
368
|
-
deepSetLazyRouterPrefix,
|
369
|
-
getLazyRouterPrefix,
|
370
|
-
createAccessibleLazyRouter,
|
371
|
-
adaptRouter,
|
372
|
-
getRouterChild,
|
373
|
-
eachContractProcedure,
|
374
|
-
eachAllContractProcedure,
|
375
|
-
convertPathToHttpPath,
|
376
|
-
createContractedProcedure
|
377
|
-
};
|
378
|
-
//# sourceMappingURL=chunk-XP6YRLY2.js.map
|
363
|
+
export { LAZY_SYMBOL as L, Procedure as P, toHttpPath as a, createContractedProcedure as b, createProcedureClient as c, addMiddleware as d, enhanceRouter as e, isLazy as f, getRouter as g, createAssertedLazyProcedure as h, isProcedure as i, createORPCErrorConstructorMap as j, getLazyMeta as k, lazy as l, middlewareOutputFn as m, isStartWithMiddlewares as n, mergeMiddlewares as o, call as p, getHiddenRouterContract as q, createAccessibleLazyRouter as r, setHiddenRouterContract as s, traverseContractProcedures as t, unlazy as u, validateORPCError as v, resolveContractProcedures as w, unlazyRouter as x };
|
@@ -0,0 +1,29 @@
|
|
1
|
+
import { StandardRPCJsonSerializer, StandardRPCSerializer } from '@orpc/client/standard';
|
2
|
+
import { toStandardLazyRequest, toFetchResponse } from '@orpc/standard-server-fetch';
|
3
|
+
import { S as StandardHandler, b as StandardRPCMatcher, a as StandardRPCCodec } from './server.DisswUB5.mjs';
|
4
|
+
|
5
|
+
class RPCHandler {
|
6
|
+
standardHandler;
|
7
|
+
constructor(router, options = {}) {
|
8
|
+
const jsonSerializer = new StandardRPCJsonSerializer(options);
|
9
|
+
const serializer = new StandardRPCSerializer(jsonSerializer);
|
10
|
+
const matcher = new StandardRPCMatcher();
|
11
|
+
const codec = new StandardRPCCodec(serializer);
|
12
|
+
this.standardHandler = new StandardHandler(router, matcher, codec, options);
|
13
|
+
}
|
14
|
+
async handle(request, ...[
|
15
|
+
options = {}
|
16
|
+
]) {
|
17
|
+
const standardRequest = toStandardLazyRequest(request);
|
18
|
+
const result = await this.standardHandler.handle(standardRequest, options);
|
19
|
+
if (!result.matched) {
|
20
|
+
return result;
|
21
|
+
}
|
22
|
+
return {
|
23
|
+
matched: true,
|
24
|
+
response: toFetchResponse(result.response, options)
|
25
|
+
};
|
26
|
+
}
|
27
|
+
}
|
28
|
+
|
29
|
+
export { RPCHandler as R };
|
@@ -0,0 +1,8 @@
|
|
1
|
+
import { StandardRPCJsonSerializerOptions } from '@orpc/client/standard';
|
2
|
+
import { C as Context } from './server.DnmJuN02.mjs';
|
3
|
+
import { a as StandardHandlerOptions } from './server.DpIMjOfC.mjs';
|
4
|
+
|
5
|
+
interface StandardRPCHandlerOptions<T extends Context> extends StandardHandlerOptions<T>, StandardRPCJsonSerializerOptions {
|
6
|
+
}
|
7
|
+
|
8
|
+
export type { StandardRPCHandlerOptions as S };
|
@@ -0,0 +1,158 @@
|
|
1
|
+
import { ORPCError, toORPCError } from '@orpc/client';
|
2
|
+
import { intercept, trim, parseEmptyableJSON } from '@orpc/shared';
|
3
|
+
import { C as CompositePlugin } from './server.Q6ZmnTgO.mjs';
|
4
|
+
import { c as createProcedureClient, t as traverseContractProcedures, a as toHttpPath, i as isProcedure, u as unlazy, g as getRouter, b as createContractedProcedure } from './server.B_5ZADvP.mjs';
|
5
|
+
|
6
|
+
class StandardHandler {
|
7
|
+
constructor(router, matcher, codec, options) {
|
8
|
+
this.matcher = matcher;
|
9
|
+
this.codec = codec;
|
10
|
+
this.options = options;
|
11
|
+
this.plugin = new CompositePlugin(options.plugins);
|
12
|
+
this.plugin.init(this.options);
|
13
|
+
this.matcher.init(router);
|
14
|
+
}
|
15
|
+
plugin;
|
16
|
+
handle(request, ...[options]) {
|
17
|
+
return intercept(
|
18
|
+
this.options.rootInterceptors ?? [],
|
19
|
+
{
|
20
|
+
request,
|
21
|
+
...options,
|
22
|
+
context: options?.context ?? {}
|
23
|
+
// context is optional only when all fields are optional so we can safely force it to have a context
|
24
|
+
},
|
25
|
+
async (interceptorOptions) => {
|
26
|
+
let isDecoding = false;
|
27
|
+
try {
|
28
|
+
return await intercept(
|
29
|
+
this.options.interceptors ?? [],
|
30
|
+
interceptorOptions,
|
31
|
+
async (interceptorOptions2) => {
|
32
|
+
const method = interceptorOptions2.request.method;
|
33
|
+
const url = interceptorOptions2.request.url;
|
34
|
+
const pathname = `/${trim(url.pathname.replace(interceptorOptions2.prefix ?? "", ""), "/")}`;
|
35
|
+
const match = await this.matcher.match(method, pathname);
|
36
|
+
if (!match) {
|
37
|
+
return { matched: false, response: void 0 };
|
38
|
+
}
|
39
|
+
const client = createProcedureClient(match.procedure, {
|
40
|
+
context: interceptorOptions2.context,
|
41
|
+
path: match.path,
|
42
|
+
interceptors: this.options.clientInterceptors
|
43
|
+
});
|
44
|
+
isDecoding = true;
|
45
|
+
const input = await this.codec.decode(request, match.params, match.procedure);
|
46
|
+
isDecoding = false;
|
47
|
+
const lastEventId = Array.isArray(request.headers["last-event-id"]) ? request.headers["last-event-id"].at(-1) : request.headers["last-event-id"];
|
48
|
+
const output = await client(input, { signal: request.signal, lastEventId });
|
49
|
+
const response = this.codec.encode(output, match.procedure);
|
50
|
+
return {
|
51
|
+
matched: true,
|
52
|
+
response
|
53
|
+
};
|
54
|
+
}
|
55
|
+
);
|
56
|
+
} catch (e) {
|
57
|
+
const error = isDecoding ? new ORPCError("BAD_REQUEST", {
|
58
|
+
message: `Malformed request. Ensure the request body is properly formatted and the 'Content-Type' header is set correctly.`,
|
59
|
+
cause: e
|
60
|
+
}) : toORPCError(e);
|
61
|
+
const response = this.codec.encodeError(error);
|
62
|
+
return {
|
63
|
+
matched: true,
|
64
|
+
response
|
65
|
+
};
|
66
|
+
}
|
67
|
+
}
|
68
|
+
);
|
69
|
+
}
|
70
|
+
}
|
71
|
+
|
72
|
+
class StandardRPCCodec {
|
73
|
+
constructor(serializer) {
|
74
|
+
this.serializer = serializer;
|
75
|
+
}
|
76
|
+
async decode(request, _params, _procedure) {
|
77
|
+
const serialized = request.method === "GET" ? parseEmptyableJSON(request.url.searchParams.getAll("data").at(-1)) : await request.body();
|
78
|
+
return this.serializer.deserialize(serialized);
|
79
|
+
}
|
80
|
+
encode(output, _procedure) {
|
81
|
+
return {
|
82
|
+
status: 200,
|
83
|
+
headers: {},
|
84
|
+
body: this.serializer.serialize(output)
|
85
|
+
};
|
86
|
+
}
|
87
|
+
encodeError(error) {
|
88
|
+
return {
|
89
|
+
status: error.status,
|
90
|
+
headers: {},
|
91
|
+
body: this.serializer.serialize(error.toJSON())
|
92
|
+
};
|
93
|
+
}
|
94
|
+
}
|
95
|
+
|
96
|
+
class StandardRPCMatcher {
|
97
|
+
tree = {};
|
98
|
+
pendingRouters = [];
|
99
|
+
init(router, path = []) {
|
100
|
+
const laziedOptions = traverseContractProcedures({ router, path }, ({ path: path2, contract }) => {
|
101
|
+
const httpPath = toHttpPath(path2);
|
102
|
+
if (isProcedure(contract)) {
|
103
|
+
this.tree[httpPath] = {
|
104
|
+
path: path2,
|
105
|
+
contract,
|
106
|
+
procedure: contract,
|
107
|
+
// this mean dev not used contract-first so we can used contract as procedure directly
|
108
|
+
router
|
109
|
+
};
|
110
|
+
} else {
|
111
|
+
this.tree[httpPath] = {
|
112
|
+
path: path2,
|
113
|
+
contract,
|
114
|
+
procedure: void 0,
|
115
|
+
router
|
116
|
+
};
|
117
|
+
}
|
118
|
+
});
|
119
|
+
this.pendingRouters.push(...laziedOptions.map((option) => ({
|
120
|
+
...option,
|
121
|
+
httpPathPrefix: toHttpPath(option.path)
|
122
|
+
})));
|
123
|
+
}
|
124
|
+
async match(_method, pathname) {
|
125
|
+
if (this.pendingRouters.length) {
|
126
|
+
const newPendingRouters = [];
|
127
|
+
for (const pendingRouter of this.pendingRouters) {
|
128
|
+
if (pathname.startsWith(pendingRouter.httpPathPrefix)) {
|
129
|
+
const { default: router } = await unlazy(pendingRouter.router);
|
130
|
+
this.init(router, pendingRouter.path);
|
131
|
+
} else {
|
132
|
+
newPendingRouters.push(pendingRouter);
|
133
|
+
}
|
134
|
+
}
|
135
|
+
this.pendingRouters = newPendingRouters;
|
136
|
+
}
|
137
|
+
const match = this.tree[pathname];
|
138
|
+
if (!match) {
|
139
|
+
return void 0;
|
140
|
+
}
|
141
|
+
if (!match.procedure) {
|
142
|
+
const { default: maybeProcedure } = await unlazy(getRouter(match.router, match.path));
|
143
|
+
if (!isProcedure(maybeProcedure)) {
|
144
|
+
throw new Error(`
|
145
|
+
[Contract-First] Missing or invalid implementation for procedure at path: ${toHttpPath(match.path)}.
|
146
|
+
Ensure that the procedure is correctly defined and matches the expected contract.
|
147
|
+
`);
|
148
|
+
}
|
149
|
+
match.procedure = createContractedProcedure(maybeProcedure, match.contract);
|
150
|
+
}
|
151
|
+
return {
|
152
|
+
path: match.path,
|
153
|
+
procedure: match.procedure
|
154
|
+
};
|
155
|
+
}
|
156
|
+
}
|
157
|
+
|
158
|
+
export { StandardHandler as S, StandardRPCCodec as a, StandardRPCMatcher as b };
|