@orpc/server 0.0.0-next.b42ba03 → 0.0.0-next.b45a533
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 +26 -0
- package/dist/adapters/fetch/index.d.ts +26 -0
- package/dist/adapters/fetch/index.mjs +9 -0
- package/dist/adapters/hono/index.d.mts +19 -0
- package/dist/adapters/hono/index.d.ts +19 -0
- package/dist/{hono.js → adapters/hono/index.mjs} +11 -19
- package/dist/adapters/next/index.d.mts +26 -0
- package/dist/adapters/next/index.d.ts +26 -0
- package/dist/{next.js → adapters/next/index.mjs} +11 -19
- package/dist/adapters/node/index.d.mts +34 -0
- package/dist/adapters/node/index.d.ts +34 -0
- package/dist/adapters/node/index.mjs +31 -0
- package/dist/adapters/standard/index.d.mts +25 -0
- package/dist/adapters/standard/index.d.ts +25 -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-XFBAK67J.js → plugins/index.mjs} +18 -43
- package/dist/shared/server.3mOimouH.mjs +158 -0
- package/dist/{chunk-EDQKEHUX.js → shared/server.B_5ZADvP.mjs} +181 -195
- package/dist/shared/server.BgDZnmUZ.mjs +28 -0
- package/dist/shared/server.CL84X8p4.d.mts +75 -0
- package/dist/shared/server.DnmJuN02.d.mts +144 -0
- package/dist/shared/server.DnmJuN02.d.ts +144 -0
- package/dist/shared/server.Q6ZmnTgO.mjs +12 -0
- package/dist/shared/server.hqPWnakL.d.ts +75 -0
- package/package.json +29 -31
- package/dist/chunk-2OH4QMZ4.js +0 -145
- package/dist/chunk-BFGSRNYZ.js +0 -319
- package/dist/fetch.js +0 -16
- package/dist/node.js +0 -170
- package/dist/plugins.js +0 -11
- package/dist/src/adapters/fetch/index.d.ts +0 -4
- package/dist/src/adapters/fetch/rpc-handler.d.ts +0 -10
- package/dist/src/adapters/fetch/types.d.ts +0 -13
- package/dist/src/adapters/fetch/utils.d.ts +0 -5
- package/dist/src/adapters/hono/index.d.ts +0 -3
- package/dist/src/adapters/hono/middleware.d.ts +0 -13
- package/dist/src/adapters/next/index.d.ts +0 -3
- package/dist/src/adapters/next/serve.d.ts +0 -20
- package/dist/src/adapters/node/index.d.ts +0 -4
- package/dist/src/adapters/node/rpc-handler.d.ts +0 -10
- package/dist/src/adapters/node/types.d.ts +0 -21
- package/dist/src/adapters/node/utils.d.ts +0 -5
- 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 -15
- 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 -44
- 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 -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 -37
- 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 -31
- 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 -11
- package/dist/src/router.d.ts +0 -29
- package/dist/src/utils.d.ts +0 -24
- package/dist/standard.js +0 -17
@@ -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 RPCCodec {
|
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 RPCMatcher {
|
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 { RPCCodec as R, StandardHandler as S, RPCMatcher as a };
|
@@ -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,48 +58,53 @@ 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 ?? [];
|
70
105
|
const { default: procedure } = await unlazy(lazyableProcedure);
|
71
|
-
const
|
106
|
+
const clientContext = callerOptions?.context ?? {};
|
107
|
+
const context = await value(options?.context ?? {}, clientContext);
|
72
108
|
const errors = createORPCErrorConstructorMap(procedure["~orpc"].errorMap);
|
73
109
|
try {
|
74
110
|
return await intercept(
|
@@ -80,7 +116,8 @@ function createProcedureClient(lazyableProcedure, ...[options]) {
|
|
80
116
|
errors,
|
81
117
|
path,
|
82
118
|
procedure,
|
83
|
-
signal: callerOptions?.signal
|
119
|
+
signal: callerOptions?.signal,
|
120
|
+
lastEventId: callerOptions?.lastEventId
|
84
121
|
},
|
85
122
|
(interceptorOptions) => executeProcedureInternal(interceptorOptions.procedure, interceptorOptions)
|
86
123
|
);
|
@@ -152,98 +189,22 @@ async function executeProcedureInternal(procedure, options) {
|
|
152
189
|
return (await next({})).output;
|
153
190
|
}
|
154
191
|
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
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, {
|
159
195
|
get(target, key) {
|
160
|
-
if (key ===
|
196
|
+
if (key === HIDDEN_ROUTER_CONTRACT_SYMBOL) {
|
161
197
|
return contract;
|
162
198
|
}
|
163
199
|
return Reflect.get(target, key);
|
164
200
|
}
|
165
201
|
});
|
166
202
|
}
|
167
|
-
function
|
168
|
-
return
|
169
|
-
}
|
170
|
-
var LAZY_ROUTER_PREFIX_SYMBOL = Symbol("ORPC_LAZY_ROUTER_PREFIX");
|
171
|
-
function deepSetLazyRouterPrefix(router, prefix) {
|
172
|
-
return new Proxy(router, {
|
173
|
-
get(target, key) {
|
174
|
-
if (key !== LAZY_ROUTER_PREFIX_SYMBOL) {
|
175
|
-
const val = Reflect.get(target, key);
|
176
|
-
if (isLazy(val)) {
|
177
|
-
return deepSetLazyRouterPrefix(val, prefix);
|
178
|
-
}
|
179
|
-
return val;
|
180
|
-
}
|
181
|
-
return prefix;
|
182
|
-
}
|
183
|
-
});
|
184
|
-
}
|
185
|
-
function getLazyRouterPrefix(obj) {
|
186
|
-
return obj[LAZY_ROUTER_PREFIX_SYMBOL];
|
203
|
+
function getHiddenRouterContract(router) {
|
204
|
+
return router[HIDDEN_ROUTER_CONTRACT_SYMBOL];
|
187
205
|
}
|
188
206
|
|
189
|
-
|
190
|
-
import { adaptRoute, mergeErrorMap, mergePrefix } from "@orpc/contract";
|
191
|
-
|
192
|
-
// src/middleware-utils.ts
|
193
|
-
function dedupeMiddlewares(compare, middlewares) {
|
194
|
-
let min = 0;
|
195
|
-
for (let i = 0; i < middlewares.length; i++) {
|
196
|
-
const index = compare.indexOf(middlewares[i], min);
|
197
|
-
if (index === -1) {
|
198
|
-
return middlewares.slice(i);
|
199
|
-
}
|
200
|
-
min = index + 1;
|
201
|
-
}
|
202
|
-
return [];
|
203
|
-
}
|
204
|
-
function mergeMiddlewares(first, second) {
|
205
|
-
return [...first, ...dedupeMiddlewares(first, second)];
|
206
|
-
}
|
207
|
-
function addMiddleware(middlewares, addition) {
|
208
|
-
return [...middlewares, addition];
|
209
|
-
}
|
210
|
-
|
211
|
-
// src/router.ts
|
212
|
-
function adaptRouter(router, options) {
|
213
|
-
if (isLazy(router)) {
|
214
|
-
const adapted2 = lazy(async () => {
|
215
|
-
const unlaziedRouter = (await unlazy(router)).default;
|
216
|
-
const adapted3 = adaptRouter(unlaziedRouter, options);
|
217
|
-
return { default: adapted3 };
|
218
|
-
});
|
219
|
-
const accessible = createAccessibleLazyRouter(adapted2);
|
220
|
-
const currentPrefix = getLazyRouterPrefix(router);
|
221
|
-
const prefix = currentPrefix ? mergePrefix(options.prefix, currentPrefix) : options.prefix;
|
222
|
-
if (prefix) {
|
223
|
-
return deepSetLazyRouterPrefix(accessible, prefix);
|
224
|
-
}
|
225
|
-
return accessible;
|
226
|
-
}
|
227
|
-
if (isProcedure(router)) {
|
228
|
-
const newMiddlewares = mergeMiddlewares(options.middlewares, router["~orpc"].middlewares);
|
229
|
-
const newMiddlewareAdded = newMiddlewares.length - router["~orpc"].middlewares.length;
|
230
|
-
const adapted2 = new Procedure({
|
231
|
-
...router["~orpc"],
|
232
|
-
route: adaptRoute(router["~orpc"].route, options),
|
233
|
-
errorMap: mergeErrorMap(options.errorMap, router["~orpc"].errorMap),
|
234
|
-
middlewares: newMiddlewares,
|
235
|
-
inputValidationIndex: router["~orpc"].inputValidationIndex + newMiddlewareAdded,
|
236
|
-
outputValidationIndex: router["~orpc"].outputValidationIndex + newMiddlewareAdded
|
237
|
-
});
|
238
|
-
return adapted2;
|
239
|
-
}
|
240
|
-
const adapted = {};
|
241
|
-
for (const key in router) {
|
242
|
-
adapted[key] = adaptRouter(router[key], options);
|
243
|
-
}
|
244
|
-
return adapted;
|
245
|
-
}
|
246
|
-
function getRouterChild(router, ...path) {
|
207
|
+
function getRouter(router, path) {
|
247
208
|
let current = router;
|
248
209
|
for (let i = 0; i < path.length; i++) {
|
249
210
|
const segment = path[i];
|
@@ -259,89 +220,131 @@ function getRouterChild(router, ...path) {
|
|
259
220
|
}
|
260
221
|
const lazied = current;
|
261
222
|
const rest = path.slice(i);
|
262
|
-
|
223
|
+
return lazy(async () => {
|
263
224
|
const unwrapped = await unlazy(lazied);
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
const next = getRouterChild(unwrapped.default, ...rest);
|
268
|
-
return { default: next };
|
269
|
-
});
|
270
|
-
return flatLazy(newLazy);
|
225
|
+
const next = getRouter(unwrapped.default, rest);
|
226
|
+
return unlazy(next);
|
227
|
+
}, getLazyMeta(lazied));
|
271
228
|
}
|
272
229
|
return current;
|
273
230
|
}
|
274
|
-
|
275
|
-
// src/router-accessible-lazy.ts
|
276
231
|
function createAccessibleLazyRouter(lazied) {
|
277
|
-
const
|
278
|
-
const recursive = new Proxy(flattenLazy, {
|
232
|
+
const recursive = new Proxy(lazied, {
|
279
233
|
get(target, key) {
|
280
234
|
if (typeof key !== "string") {
|
281
235
|
return Reflect.get(target, key);
|
282
236
|
}
|
283
|
-
const next =
|
237
|
+
const next = getRouter(lazied, [key]);
|
284
238
|
return createAccessibleLazyRouter(next);
|
285
239
|
}
|
286
240
|
});
|
287
241
|
return recursive;
|
288
242
|
}
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
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;
|
303
257
|
}
|
304
|
-
if (
|
305
|
-
|
306
|
-
|
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,
|
307
286
|
path: options.path
|
308
287
|
});
|
309
|
-
} else if (
|
288
|
+
} else if (isContractProcedure(currentRouter)) {
|
310
289
|
callback({
|
311
|
-
contract:
|
290
|
+
contract: currentRouter,
|
312
291
|
path: options.path
|
313
292
|
});
|
314
293
|
} else {
|
315
|
-
for (const key in
|
316
|
-
|
294
|
+
for (const key in currentRouter) {
|
295
|
+
traverseContractProcedures(
|
317
296
|
{
|
318
|
-
router:
|
297
|
+
router: currentRouter[key],
|
319
298
|
path: [...options.path, key]
|
320
299
|
},
|
321
300
|
callback,
|
322
|
-
|
301
|
+
lazyOptions
|
323
302
|
);
|
324
303
|
}
|
325
304
|
}
|
326
|
-
return
|
305
|
+
return lazyOptions;
|
327
306
|
}
|
328
|
-
async function
|
307
|
+
async function resolveContractProcedures(options, callback) {
|
329
308
|
const pending = [options];
|
330
|
-
for (const
|
331
|
-
const
|
332
|
-
for (const
|
333
|
-
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);
|
334
313
|
pending.push({
|
335
|
-
|
336
|
-
|
314
|
+
router,
|
315
|
+
path: options3.path
|
337
316
|
});
|
338
317
|
}
|
339
318
|
}
|
340
319
|
}
|
341
|
-
function
|
342
|
-
|
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;
|
343
331
|
}
|
344
|
-
|
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) {
|
345
348
|
return new Procedure({
|
346
349
|
...procedure["~orpc"],
|
347
350
|
errorMap: contract["~orpc"].errorMap,
|
@@ -349,29 +352,12 @@ function createContractedProcedure(contract, procedure) {
|
|
349
352
|
meta: contract["~orpc"].meta
|
350
353
|
});
|
351
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
|
+
}
|
352
362
|
|
353
|
-
export {
|
354
|
-
LAZY_LOADER_SYMBOL,
|
355
|
-
lazy,
|
356
|
-
isLazy,
|
357
|
-
unlazy,
|
358
|
-
Procedure,
|
359
|
-
isProcedure,
|
360
|
-
flatLazy,
|
361
|
-
createLazyProcedureFormAnyLazy,
|
362
|
-
addMiddleware,
|
363
|
-
middlewareOutputFn,
|
364
|
-
createProcedureClient,
|
365
|
-
setRouterContract,
|
366
|
-
getRouterContract,
|
367
|
-
deepSetLazyRouterPrefix,
|
368
|
-
getLazyRouterPrefix,
|
369
|
-
createAccessibleLazyRouter,
|
370
|
-
adaptRouter,
|
371
|
-
getRouterChild,
|
372
|
-
eachContractProcedure,
|
373
|
-
eachAllContractProcedure,
|
374
|
-
convertPathToHttpPath,
|
375
|
-
createContractedProcedure
|
376
|
-
};
|
377
|
-
//# sourceMappingURL=chunk-EDQKEHUX.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,28 @@
|
|
1
|
+
import { RPCSerializer } from '@orpc/client/standard';
|
2
|
+
import { toStandardLazyRequest, toFetchResponse } from '@orpc/standard-server-fetch';
|
3
|
+
import { S as StandardHandler, a as RPCMatcher, R as RPCCodec } from './server.3mOimouH.mjs';
|
4
|
+
|
5
|
+
class RPCHandler {
|
6
|
+
standardHandler;
|
7
|
+
constructor(router, options = {}) {
|
8
|
+
const serializer = new RPCSerializer();
|
9
|
+
const matcher = new RPCMatcher();
|
10
|
+
const codec = new RPCCodec(serializer);
|
11
|
+
this.standardHandler = new StandardHandler(router, matcher, codec, options);
|
12
|
+
}
|
13
|
+
async handle(request, ...[
|
14
|
+
options = {}
|
15
|
+
]) {
|
16
|
+
const standardRequest = toStandardLazyRequest(request);
|
17
|
+
const result = await this.standardHandler.handle(standardRequest, options);
|
18
|
+
if (!result.matched) {
|
19
|
+
return result;
|
20
|
+
}
|
21
|
+
return {
|
22
|
+
matched: true,
|
23
|
+
response: toFetchResponse(result.response, options)
|
24
|
+
};
|
25
|
+
}
|
26
|
+
}
|
27
|
+
|
28
|
+
export { RPCHandler as R };
|