@orpc/server 0.0.0-next.f17a1a0 → 0.0.0-next.f4d410a
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 +4 -2
- 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} +84 -155
- package/dist/plugins/index.d.mts +31 -0
- package/dist/plugins/index.d.ts +31 -0
- package/dist/{chunk-WQNNSBXW.js → plugins/index.mjs} +12 -29
- package/dist/shared/server.B3Tm0IXY.d.ts +75 -0
- package/dist/shared/server.BYTulgUc.d.mts +144 -0
- package/dist/shared/server.BYTulgUc.d.ts +144 -0
- package/dist/shared/server.BeJithK4.d.mts +75 -0
- package/dist/{chunk-MHVECKBC.js → shared/server.BtxZnWJ9.mjs} +165 -212
- package/dist/shared/server.Bz_xNBjz.d.mts +8 -0
- package/dist/shared/server.DoP20NVH.mjs +29 -0
- package/dist/shared/server.EhgR_5_I.d.ts +8 -0
- package/dist/shared/server.Q6ZmnTgO.mjs +12 -0
- package/dist/{chunk-7A2ZRAPK.js → shared/server.jG7ZuX3S.mjs} +21 -45
- package/package.json +29 -34
- package/dist/chunk-MEBJNUSY.js +0 -32
- 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 -53
- package/dist/src/adapters/standard/index.d.ts +0 -6
- 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/types.d.ts +0 -21
- package/dist/src/builder-variants.d.ts +0 -75
- package/dist/src/builder.d.ts +0 -58
- package/dist/src/config.d.ts +0 -6
- package/dist/src/context.d.ts +0 -8
- package/dist/src/error.d.ts +0 -12
- package/dist/src/hidden.d.ts +0 -8
- package/dist/src/implementer-procedure.d.ts +0 -33
- package/dist/src/implementer-variants.d.ts +0 -18
- package/dist/src/implementer.d.ts +0 -29
- package/dist/src/index.d.ts +0 -25
- 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 -11
- package/dist/src/middleware-utils.d.ts +0 -5
- package/dist/src/middleware.d.ts +0 -38
- package/dist/src/plugins/base.d.ts +0 -11
- 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 -33
- package/dist/src/procedure-decorated.d.ts +0 -24
- package/dist/src/procedure-utils.d.ts +0 -19
- package/dist/src/procedure.d.ts +0 -31
- package/dist/src/router-accessible-lazy.d.ts +0 -8
- package/dist/src/router-client.d.ts +0 -12
- package/dist/src/router.d.ts +0 -30
- package/dist/src/utils.d.ts +0 -24
- package/dist/standard.js +0 -13
@@ -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,47 +58,10 @@ 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
|
-
const flattenLoader = async () => {
|
33
|
-
let current = await unlazy(lazied);
|
34
|
-
while (true) {
|
35
|
-
if (!isLazy(current.default)) {
|
36
|
-
break;
|
37
|
-
}
|
38
|
-
current = await unlazy(current.default);
|
39
|
-
}
|
40
|
-
return current;
|
41
|
-
};
|
42
|
-
return lazy(flattenLoader);
|
61
|
+
function mergeCurrentContext(context, other) {
|
62
|
+
return { ...context, ...other };
|
43
63
|
}
|
44
|
-
function createLazyProcedureFormAnyLazy(lazied) {
|
45
|
-
const lazyProcedure = lazy(async () => {
|
46
|
-
const { default: maybeProcedure } = await unlazy(flatLazy(lazied));
|
47
|
-
if (!isProcedure(maybeProcedure)) {
|
48
|
-
throw new Error(`
|
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
|
-
`);
|
53
|
-
}
|
54
|
-
return { default: maybeProcedure };
|
55
|
-
});
|
56
|
-
return lazyProcedure;
|
57
|
-
}
|
58
|
-
|
59
|
-
// src/middleware.ts
|
60
|
-
function middlewareOutputFn(output) {
|
61
|
-
return { output, context: {} };
|
62
|
-
}
|
63
|
-
|
64
|
-
// src/procedure-client.ts
|
65
|
-
import { ORPCError as ORPCError2 } from "@orpc/client";
|
66
|
-
import { ValidationError } from "@orpc/contract";
|
67
|
-
import { intercept, toError, value } from "@orpc/shared";
|
68
64
|
|
69
|
-
// src/error.ts
|
70
|
-
import { fallbackORPCErrorStatus, ORPCError } from "@orpc/client";
|
71
65
|
function createORPCErrorConstructorMap(errors) {
|
72
66
|
const proxy = new Proxy(errors, {
|
73
67
|
get(target, code) {
|
@@ -105,7 +99,10 @@ async function validateORPCError(map, error) {
|
|
105
99
|
return new ORPCError(code, { defined: true, status, message, data: validated.value, cause });
|
106
100
|
}
|
107
101
|
|
108
|
-
|
102
|
+
function middlewareOutputFn(output) {
|
103
|
+
return { output, context: {} };
|
104
|
+
}
|
105
|
+
|
109
106
|
function createProcedureClient(lazyableProcedure, ...[options]) {
|
110
107
|
return async (...[input, callerOptions]) => {
|
111
108
|
const path = options?.path ?? [];
|
@@ -129,7 +126,7 @@ function createProcedureClient(lazyableProcedure, ...[options]) {
|
|
129
126
|
(interceptorOptions) => executeProcedureInternal(interceptorOptions.procedure, interceptorOptions)
|
130
127
|
);
|
131
128
|
} catch (e) {
|
132
|
-
if (!(e instanceof
|
129
|
+
if (!(e instanceof ORPCError)) {
|
133
130
|
throw toError(e);
|
134
131
|
}
|
135
132
|
const validated = await validateORPCError(procedure["~orpc"].errorMap, e);
|
@@ -144,7 +141,7 @@ async function validateInput(procedure, input) {
|
|
144
141
|
}
|
145
142
|
const result = await schema["~standard"].validate(input);
|
146
143
|
if (result.issues) {
|
147
|
-
throw new
|
144
|
+
throw new ORPCError("BAD_REQUEST", {
|
148
145
|
message: "Input validation failed",
|
149
146
|
data: {
|
150
147
|
issues: result.issues
|
@@ -161,7 +158,7 @@ async function validateOutput(procedure, output) {
|
|
161
158
|
}
|
162
159
|
const result = await schema["~standard"].validate(output);
|
163
160
|
if (result.issues) {
|
164
|
-
throw new
|
161
|
+
throw new ORPCError("INTERNAL_SERVER_ERROR", {
|
165
162
|
message: "Output validation failed",
|
166
163
|
cause: new ValidationError({ message: "Output validation failed", issues: result.issues })
|
167
164
|
});
|
@@ -177,117 +174,48 @@ async function executeProcedureInternal(procedure, options) {
|
|
177
174
|
let currentInput = options.input;
|
178
175
|
const next = async (...[nextOptions]) => {
|
179
176
|
const index = currentIndex;
|
177
|
+
const midContext = nextOptions?.context ?? {};
|
180
178
|
currentIndex += 1;
|
181
|
-
currentContext =
|
179
|
+
currentContext = mergeCurrentContext(currentContext, midContext);
|
182
180
|
if (index === inputValidationIndex) {
|
183
181
|
currentInput = await validateInput(procedure, currentInput);
|
184
182
|
}
|
185
183
|
const mid = middlewares[index];
|
186
|
-
const result = mid ?
|
184
|
+
const result = mid ? {
|
185
|
+
context: midContext,
|
186
|
+
output: (await mid({ ...options, context: currentContext, next }, currentInput, middlewareOutputFn)).output
|
187
|
+
} : {
|
188
|
+
context: midContext,
|
189
|
+
output: await procedure["~orpc"].handler({ ...options, context: currentContext, input: currentInput })
|
190
|
+
};
|
187
191
|
if (index === outputValidationIndex) {
|
188
192
|
const validatedOutput = await validateOutput(procedure, result.output);
|
189
193
|
return {
|
190
|
-
|
194
|
+
context: result.context,
|
191
195
|
output: validatedOutput
|
192
196
|
};
|
193
197
|
}
|
194
198
|
return result;
|
195
199
|
};
|
196
|
-
return (await next(
|
200
|
+
return (await next()).output;
|
197
201
|
}
|
198
202
|
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
return new Proxy(obj, {
|
203
|
+
const HIDDEN_ROUTER_CONTRACT_SYMBOL = Symbol("ORPC_HIDDEN_ROUTER_CONTRACT");
|
204
|
+
function setHiddenRouterContract(router, contract) {
|
205
|
+
return new Proxy(router, {
|
203
206
|
get(target, key) {
|
204
|
-
if (key ===
|
207
|
+
if (key === HIDDEN_ROUTER_CONTRACT_SYMBOL) {
|
205
208
|
return contract;
|
206
209
|
}
|
207
210
|
return Reflect.get(target, key);
|
208
211
|
}
|
209
212
|
});
|
210
213
|
}
|
211
|
-
function
|
212
|
-
return
|
213
|
-
}
|
214
|
-
var LAZY_ROUTER_PREFIX_SYMBOL = Symbol("ORPC_LAZY_ROUTER_PREFIX");
|
215
|
-
function deepSetLazyRouterPrefix(router, prefix) {
|
216
|
-
return new Proxy(router, {
|
217
|
-
get(target, key) {
|
218
|
-
if (key !== LAZY_ROUTER_PREFIX_SYMBOL) {
|
219
|
-
const val = Reflect.get(target, key);
|
220
|
-
if (isLazy(val)) {
|
221
|
-
return deepSetLazyRouterPrefix(val, prefix);
|
222
|
-
}
|
223
|
-
return val;
|
224
|
-
}
|
225
|
-
return prefix;
|
226
|
-
}
|
227
|
-
});
|
228
|
-
}
|
229
|
-
function getLazyRouterPrefix(obj) {
|
230
|
-
return obj[LAZY_ROUTER_PREFIX_SYMBOL];
|
231
|
-
}
|
232
|
-
|
233
|
-
// src/router.ts
|
234
|
-
import { adaptRoute, mergeErrorMap, mergePrefix } from "@orpc/contract";
|
235
|
-
|
236
|
-
// src/middleware-utils.ts
|
237
|
-
function dedupeMiddlewares(compare, middlewares) {
|
238
|
-
let min = 0;
|
239
|
-
for (let i = 0; i < middlewares.length; i++) {
|
240
|
-
const index = compare.indexOf(middlewares[i], min);
|
241
|
-
if (index === -1) {
|
242
|
-
return middlewares.slice(i);
|
243
|
-
}
|
244
|
-
min = index + 1;
|
245
|
-
}
|
246
|
-
return [];
|
247
|
-
}
|
248
|
-
function mergeMiddlewares(first, second) {
|
249
|
-
return [...first, ...dedupeMiddlewares(first, second)];
|
250
|
-
}
|
251
|
-
function addMiddleware(middlewares, addition) {
|
252
|
-
return [...middlewares, addition];
|
214
|
+
function getHiddenRouterContract(router) {
|
215
|
+
return router[HIDDEN_ROUTER_CONTRACT_SYMBOL];
|
253
216
|
}
|
254
217
|
|
255
|
-
|
256
|
-
function adaptRouter(router, options) {
|
257
|
-
if (isLazy(router)) {
|
258
|
-
const adapted2 = lazy(async () => {
|
259
|
-
const unlaziedRouter = (await unlazy(router)).default;
|
260
|
-
const adapted3 = adaptRouter(unlaziedRouter, options);
|
261
|
-
return { default: adapted3 };
|
262
|
-
});
|
263
|
-
const accessible = createAccessibleLazyRouter(adapted2);
|
264
|
-
const currentPrefix = getLazyRouterPrefix(router);
|
265
|
-
const prefix = currentPrefix ? mergePrefix(options.prefix, currentPrefix) : options.prefix;
|
266
|
-
if (prefix) {
|
267
|
-
return deepSetLazyRouterPrefix(accessible, prefix);
|
268
|
-
}
|
269
|
-
return accessible;
|
270
|
-
}
|
271
|
-
if (isProcedure(router)) {
|
272
|
-
const newMiddlewares = mergeMiddlewares(options.middlewares, router["~orpc"].middlewares);
|
273
|
-
const newMiddlewareAdded = newMiddlewares.length - router["~orpc"].middlewares.length;
|
274
|
-
const adapted2 = new Procedure({
|
275
|
-
...router["~orpc"],
|
276
|
-
route: adaptRoute(router["~orpc"].route, options),
|
277
|
-
errorMap: mergeErrorMap(options.errorMap, router["~orpc"].errorMap),
|
278
|
-
middlewares: newMiddlewares,
|
279
|
-
inputValidationIndex: router["~orpc"].inputValidationIndex + newMiddlewareAdded,
|
280
|
-
outputValidationIndex: router["~orpc"].outputValidationIndex + newMiddlewareAdded
|
281
|
-
});
|
282
|
-
return adapted2;
|
283
|
-
}
|
284
|
-
const adapted = {};
|
285
|
-
for (const key in router) {
|
286
|
-
adapted[key] = adaptRouter(router[key], options);
|
287
|
-
}
|
288
|
-
return adapted;
|
289
|
-
}
|
290
|
-
function getRouterChild(router, ...path) {
|
218
|
+
function getRouter(router, path) {
|
291
219
|
let current = router;
|
292
220
|
for (let i = 0; i < path.length; i++) {
|
293
221
|
const segment = path[i];
|
@@ -303,89 +231,131 @@ function getRouterChild(router, ...path) {
|
|
303
231
|
}
|
304
232
|
const lazied = current;
|
305
233
|
const rest = path.slice(i);
|
306
|
-
|
234
|
+
return lazy(async () => {
|
307
235
|
const unwrapped = await unlazy(lazied);
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
const next = getRouterChild(unwrapped.default, ...rest);
|
312
|
-
return { default: next };
|
313
|
-
});
|
314
|
-
return flatLazy(newLazy);
|
236
|
+
const next = getRouter(unwrapped.default, rest);
|
237
|
+
return unlazy(next);
|
238
|
+
}, getLazyMeta(lazied));
|
315
239
|
}
|
316
240
|
return current;
|
317
241
|
}
|
318
|
-
|
319
|
-
// src/router-accessible-lazy.ts
|
320
242
|
function createAccessibleLazyRouter(lazied) {
|
321
|
-
const
|
322
|
-
const recursive = new Proxy(flattenLazy, {
|
243
|
+
const recursive = new Proxy(lazied, {
|
323
244
|
get(target, key) {
|
324
245
|
if (typeof key !== "string") {
|
325
246
|
return Reflect.get(target, key);
|
326
247
|
}
|
327
|
-
const next =
|
248
|
+
const next = getRouter(lazied, [key]);
|
328
249
|
return createAccessibleLazyRouter(next);
|
329
250
|
}
|
330
251
|
});
|
331
252
|
return recursive;
|
332
253
|
}
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
254
|
+
function enhanceRouter(router, options) {
|
255
|
+
if (isLazy(router)) {
|
256
|
+
const laziedMeta = getLazyMeta(router);
|
257
|
+
const enhancedPrefix = laziedMeta?.prefix ? mergePrefix(options.prefix, laziedMeta?.prefix) : options.prefix;
|
258
|
+
const enhanced2 = lazy(async () => {
|
259
|
+
const { default: unlaziedRouter } = await unlazy(router);
|
260
|
+
const enhanced3 = enhanceRouter(unlaziedRouter, options);
|
261
|
+
return unlazy(enhanced3);
|
262
|
+
}, {
|
263
|
+
...laziedMeta,
|
264
|
+
prefix: enhancedPrefix
|
265
|
+
});
|
266
|
+
const accessible = createAccessibleLazyRouter(enhanced2);
|
267
|
+
return accessible;
|
268
|
+
}
|
269
|
+
if (isProcedure(router)) {
|
270
|
+
const newMiddlewares = mergeMiddlewares(options.middlewares, router["~orpc"].middlewares, { dedupeLeading: options.dedupeLeadingMiddlewares });
|
271
|
+
const newMiddlewareAdded = newMiddlewares.length - router["~orpc"].middlewares.length;
|
272
|
+
const enhanced2 = new Procedure({
|
273
|
+
...router["~orpc"],
|
274
|
+
route: enhanceRoute(router["~orpc"].route, options),
|
275
|
+
errorMap: mergeErrorMap(options.errorMap, router["~orpc"].errorMap),
|
276
|
+
middlewares: newMiddlewares,
|
277
|
+
inputValidationIndex: router["~orpc"].inputValidationIndex + newMiddlewareAdded,
|
278
|
+
outputValidationIndex: router["~orpc"].outputValidationIndex + newMiddlewareAdded
|
279
|
+
});
|
280
|
+
return enhanced2;
|
281
|
+
}
|
282
|
+
const enhanced = {};
|
283
|
+
for (const key in router) {
|
284
|
+
enhanced[key] = enhanceRouter(router[key], options);
|
285
|
+
}
|
286
|
+
return enhanced;
|
287
|
+
}
|
288
|
+
function traverseContractProcedures(options, callback, lazyOptions = []) {
|
289
|
+
let currentRouter = options.router;
|
290
|
+
const hiddenContract = getHiddenRouterContract(options.router);
|
291
|
+
if (hiddenContract !== void 0) {
|
292
|
+
currentRouter = hiddenContract;
|
347
293
|
}
|
348
|
-
if (isLazy(
|
349
|
-
|
350
|
-
|
294
|
+
if (isLazy(currentRouter)) {
|
295
|
+
lazyOptions.push({
|
296
|
+
router: currentRouter,
|
351
297
|
path: options.path
|
352
298
|
});
|
353
|
-
} else if (
|
299
|
+
} else if (isContractProcedure(currentRouter)) {
|
354
300
|
callback({
|
355
|
-
contract:
|
301
|
+
contract: currentRouter,
|
356
302
|
path: options.path
|
357
303
|
});
|
358
304
|
} else {
|
359
|
-
for (const key in
|
360
|
-
|
305
|
+
for (const key in currentRouter) {
|
306
|
+
traverseContractProcedures(
|
361
307
|
{
|
362
|
-
router:
|
308
|
+
router: currentRouter[key],
|
363
309
|
path: [...options.path, key]
|
364
310
|
},
|
365
311
|
callback,
|
366
|
-
|
312
|
+
lazyOptions
|
367
313
|
);
|
368
314
|
}
|
369
315
|
}
|
370
|
-
return
|
316
|
+
return lazyOptions;
|
371
317
|
}
|
372
|
-
async function
|
318
|
+
async function resolveContractProcedures(options, callback) {
|
373
319
|
const pending = [options];
|
374
|
-
for (const
|
375
|
-
const
|
376
|
-
for (const
|
377
|
-
const { default: router } = await unlazy(
|
320
|
+
for (const options2 of pending) {
|
321
|
+
const lazyOptions = traverseContractProcedures(options2, callback);
|
322
|
+
for (const options3 of lazyOptions) {
|
323
|
+
const { default: router } = await unlazy(options3.router);
|
378
324
|
pending.push({
|
379
|
-
|
380
|
-
|
325
|
+
router,
|
326
|
+
path: options3.path
|
381
327
|
});
|
382
328
|
}
|
383
329
|
}
|
384
330
|
}
|
385
|
-
function
|
386
|
-
|
331
|
+
async function unlazyRouter(router) {
|
332
|
+
if (isProcedure(router)) {
|
333
|
+
return router;
|
334
|
+
}
|
335
|
+
const unlazied = {};
|
336
|
+
for (const key in router) {
|
337
|
+
const item = router[key];
|
338
|
+
const { default: unlaziedRouter } = await unlazy(item);
|
339
|
+
unlazied[key] = await unlazyRouter(unlaziedRouter);
|
340
|
+
}
|
341
|
+
return unlazied;
|
387
342
|
}
|
388
|
-
|
343
|
+
|
344
|
+
function createAssertedLazyProcedure(lazied) {
|
345
|
+
const lazyProcedure = lazy(async () => {
|
346
|
+
const { default: maybeProcedure } = await unlazy(lazied);
|
347
|
+
if (!isProcedure(maybeProcedure)) {
|
348
|
+
throw new Error(`
|
349
|
+
Expected a lazy<procedure> but got lazy<unknown>.
|
350
|
+
This should be caught by TypeScript compilation.
|
351
|
+
Please report this issue if this makes you feel uncomfortable.
|
352
|
+
`);
|
353
|
+
}
|
354
|
+
return { default: maybeProcedure };
|
355
|
+
}, getLazyMeta(lazied));
|
356
|
+
return lazyProcedure;
|
357
|
+
}
|
358
|
+
function createContractedProcedure(procedure, contract) {
|
389
359
|
return new Procedure({
|
390
360
|
...procedure["~orpc"],
|
391
361
|
errorMap: contract["~orpc"].errorMap,
|
@@ -393,29 +363,12 @@ function createContractedProcedure(contract, procedure) {
|
|
393
363
|
meta: contract["~orpc"].meta
|
394
364
|
});
|
395
365
|
}
|
366
|
+
function call(procedure, input, ...rest) {
|
367
|
+
return createProcedureClient(procedure, ...rest)(input);
|
368
|
+
}
|
369
|
+
|
370
|
+
function toHttpPath(path) {
|
371
|
+
return `/${path.map(encodeURIComponent).join("/")}`;
|
372
|
+
}
|
396
373
|
|
397
|
-
export {
|
398
|
-
LAZY_LOADER_SYMBOL,
|
399
|
-
lazy,
|
400
|
-
isLazy,
|
401
|
-
unlazy,
|
402
|
-
Procedure,
|
403
|
-
isProcedure,
|
404
|
-
flatLazy,
|
405
|
-
createLazyProcedureFormAnyLazy,
|
406
|
-
addMiddleware,
|
407
|
-
middlewareOutputFn,
|
408
|
-
createProcedureClient,
|
409
|
-
setRouterContract,
|
410
|
-
getRouterContract,
|
411
|
-
deepSetLazyRouterPrefix,
|
412
|
-
getLazyRouterPrefix,
|
413
|
-
createAccessibleLazyRouter,
|
414
|
-
adaptRouter,
|
415
|
-
getRouterChild,
|
416
|
-
eachContractProcedure,
|
417
|
-
eachAllContractProcedure,
|
418
|
-
convertPathToHttpPath,
|
419
|
-
createContractedProcedure
|
420
|
-
};
|
421
|
-
//# sourceMappingURL=chunk-MHVECKBC.js.map
|
374
|
+
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, mergeCurrentContext as m, middlewareOutputFn as n, isStartWithMiddlewares as o, mergeMiddlewares as p, call as q, getHiddenRouterContract as r, setHiddenRouterContract as s, traverseContractProcedures as t, unlazy as u, validateORPCError as v, createAccessibleLazyRouter as w, resolveContractProcedures as x, unlazyRouter as y };
|
@@ -0,0 +1,8 @@
|
|
1
|
+
import { StandardRPCJsonSerializerOptions } from '@orpc/client/standard';
|
2
|
+
import { C as Context } from './server.BYTulgUc.mjs';
|
3
|
+
import { a as StandardHandlerOptions } from './server.BeJithK4.mjs';
|
4
|
+
|
5
|
+
interface StandardRPCHandlerOptions<T extends Context> extends StandardHandlerOptions<T>, StandardRPCJsonSerializerOptions {
|
6
|
+
}
|
7
|
+
|
8
|
+
export type { StandardRPCHandlerOptions as S };
|
@@ -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.jG7ZuX3S.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.BYTulgUc.js';
|
3
|
+
import { a as StandardHandlerOptions } from './server.B3Tm0IXY.js';
|
4
|
+
|
5
|
+
interface StandardRPCHandlerOptions<T extends Context> extends StandardHandlerOptions<T>, StandardRPCJsonSerializerOptions {
|
6
|
+
}
|
7
|
+
|
8
|
+
export type { StandardRPCHandlerOptions as S };
|
@@ -1,21 +1,10 @@
|
|
1
|
-
import {
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
eachContractProcedure,
|
6
|
-
getRouterChild,
|
7
|
-
isProcedure,
|
8
|
-
unlazy
|
9
|
-
} from "./chunk-MHVECKBC.js";
|
10
|
-
import {
|
11
|
-
CompositePlugin
|
12
|
-
} from "./chunk-WQNNSBXW.js";
|
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.BtxZnWJ9.mjs';
|
13
5
|
|
14
|
-
|
15
|
-
|
16
|
-
import { intercept, trim } from "@orpc/shared";
|
17
|
-
var StandardHandler = class {
|
18
|
-
constructor(router, matcher, codec, options = {}) {
|
6
|
+
class StandardHandler {
|
7
|
+
constructor(router, matcher, codec, options) {
|
19
8
|
this.matcher = matcher;
|
20
9
|
this.codec = codec;
|
21
10
|
this.options = options;
|
@@ -78,15 +67,11 @@ var StandardHandler = class {
|
|
78
67
|
}
|
79
68
|
);
|
80
69
|
}
|
81
|
-
}
|
70
|
+
}
|
82
71
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
var RPCCodec = class {
|
87
|
-
serializer;
|
88
|
-
constructor(options = {}) {
|
89
|
-
this.serializer = options.serializer ?? new RPCSerializer();
|
72
|
+
class StandardRPCCodec {
|
73
|
+
constructor(serializer) {
|
74
|
+
this.serializer = serializer;
|
90
75
|
}
|
91
76
|
async decode(request, _params, _procedure) {
|
92
77
|
const serialized = request.method === "GET" ? parseEmptyableJSON(request.url.searchParams.getAll("data").at(-1)) : await request.body();
|
@@ -106,18 +91,14 @@ var RPCCodec = class {
|
|
106
91
|
body: this.serializer.serialize(error.toJSON())
|
107
92
|
};
|
108
93
|
}
|
109
|
-
}
|
94
|
+
}
|
110
95
|
|
111
|
-
|
112
|
-
var RPCMatcher = class {
|
96
|
+
class StandardRPCMatcher {
|
113
97
|
tree = {};
|
114
98
|
pendingRouters = [];
|
115
99
|
init(router, path = []) {
|
116
|
-
const laziedOptions =
|
117
|
-
|
118
|
-
path
|
119
|
-
}, ({ path: path2, contract }) => {
|
120
|
-
const httpPath = convertPathToHttpPath(path2);
|
100
|
+
const laziedOptions = traverseContractProcedures({ router, path }, ({ path: path2, contract }) => {
|
101
|
+
const httpPath = toHttpPath(path2);
|
121
102
|
if (isProcedure(contract)) {
|
122
103
|
this.tree[httpPath] = {
|
123
104
|
path: path2,
|
@@ -137,7 +118,7 @@ var RPCMatcher = class {
|
|
137
118
|
});
|
138
119
|
this.pendingRouters.push(...laziedOptions.map((option) => ({
|
139
120
|
...option,
|
140
|
-
httpPathPrefix:
|
121
|
+
httpPathPrefix: toHttpPath(option.path)
|
141
122
|
})));
|
142
123
|
}
|
143
124
|
async match(_method, pathname) {
|
@@ -145,7 +126,7 @@ var RPCMatcher = class {
|
|
145
126
|
const newPendingRouters = [];
|
146
127
|
for (const pendingRouter of this.pendingRouters) {
|
147
128
|
if (pathname.startsWith(pendingRouter.httpPathPrefix)) {
|
148
|
-
const { default: router } = await unlazy(pendingRouter.
|
129
|
+
const { default: router } = await unlazy(pendingRouter.router);
|
149
130
|
this.init(router, pendingRouter.path);
|
150
131
|
} else {
|
151
132
|
newPendingRouters.push(pendingRouter);
|
@@ -158,25 +139,20 @@ var RPCMatcher = class {
|
|
158
139
|
return void 0;
|
159
140
|
}
|
160
141
|
if (!match.procedure) {
|
161
|
-
const { default: maybeProcedure } = await unlazy(
|
142
|
+
const { default: maybeProcedure } = await unlazy(getRouter(match.router, match.path));
|
162
143
|
if (!isProcedure(maybeProcedure)) {
|
163
144
|
throw new Error(`
|
164
|
-
[Contract-First] Missing or invalid implementation for procedure at path: ${
|
145
|
+
[Contract-First] Missing or invalid implementation for procedure at path: ${toHttpPath(match.path)}.
|
165
146
|
Ensure that the procedure is correctly defined and matches the expected contract.
|
166
147
|
`);
|
167
148
|
}
|
168
|
-
match.procedure = createContractedProcedure(match.contract
|
149
|
+
match.procedure = createContractedProcedure(maybeProcedure, match.contract);
|
169
150
|
}
|
170
151
|
return {
|
171
152
|
path: match.path,
|
172
153
|
procedure: match.procedure
|
173
154
|
};
|
174
155
|
}
|
175
|
-
}
|
156
|
+
}
|
176
157
|
|
177
|
-
export {
|
178
|
-
StandardHandler,
|
179
|
-
RPCCodec,
|
180
|
-
RPCMatcher
|
181
|
-
};
|
182
|
-
//# sourceMappingURL=chunk-7A2ZRAPK.js.map
|
158
|
+
export { StandardHandler as S, StandardRPCCodec as a, StandardRPCMatcher as b };
|