@orpc/server 0.0.0-next.c099c92 → 0.0.0-next.c0dd7cd
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 +122 -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/adapters/hono/index.mjs +32 -0
- 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 -18
- 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 +277 -0
- package/dist/index.d.ts +277 -0
- package/dist/{index.js → index.mjs} +92 -141
- 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.BY9sDlwl.mjs +158 -0
- package/dist/shared/server.BqBN5WhH.d.mts +8 -0
- package/dist/shared/server.BtxZnWJ9.mjs +374 -0
- package/dist/shared/server.CPqNKiJp.d.ts +75 -0
- package/dist/shared/server.Dba3Iiyp.mjs +12 -0
- package/dist/shared/server.Del5OmaY.mjs +29 -0
- package/dist/shared/server.Dm3ZuTuI.d.ts +8 -0
- package/dist/shared/server.MZvbGc3n.d.mts +143 -0
- package/dist/shared/server.MZvbGc3n.d.ts +143 -0
- package/dist/shared/server.P4_D9lKb.d.mts +75 -0
- package/package.json +36 -24
- package/dist/chunk-ESTRJAOX.js +0 -299
- package/dist/chunk-KK4SDLC7.js +0 -320
- 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
@@ -0,0 +1,158 @@
|
|
1
|
+
import { ORPCError, toORPCError } from '@orpc/client';
|
2
|
+
import { intercept, trim, parseEmptyableJSON } from '@orpc/shared';
|
3
|
+
import { C as CompositeHandlerPlugin } from './server.Dba3Iiyp.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';
|
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 CompositeHandlerPlugin(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 };
|
@@ -0,0 +1,8 @@
|
|
1
|
+
import { StandardRPCJsonSerializerOptions } from '@orpc/client/standard';
|
2
|
+
import { C as Context } from './server.MZvbGc3n.mjs';
|
3
|
+
import { a as StandardHandlerOptions } from './server.P4_D9lKb.mjs';
|
4
|
+
|
5
|
+
interface StandardRPCHandlerOptions<T extends Context> extends StandardHandlerOptions<T>, StandardRPCJsonSerializerOptions {
|
6
|
+
}
|
7
|
+
|
8
|
+
export type { StandardRPCHandlerOptions as S };
|
@@ -0,0 +1,374 @@
|
|
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 = {}) {
|
7
|
+
return {
|
8
|
+
[LAZY_SYMBOL]: {
|
9
|
+
loader,
|
10
|
+
meta
|
11
|
+
}
|
12
|
+
};
|
13
|
+
}
|
14
|
+
function isLazy(item) {
|
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;
|
19
|
+
}
|
20
|
+
function unlazy(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];
|
46
|
+
}
|
47
|
+
|
48
|
+
class Procedure {
|
49
|
+
"~orpc";
|
50
|
+
constructor(def) {
|
51
|
+
this["~orpc"] = def;
|
52
|
+
}
|
53
|
+
}
|
54
|
+
function isProcedure(item) {
|
55
|
+
if (item instanceof Procedure) {
|
56
|
+
return true;
|
57
|
+
}
|
58
|
+
return isContractProcedure(item) && "middlewares" in item["~orpc"] && "inputValidationIndex" in item["~orpc"] && "outputValidationIndex" in item["~orpc"] && "handler" in item["~orpc"];
|
59
|
+
}
|
60
|
+
|
61
|
+
function mergeCurrentContext(context, other) {
|
62
|
+
return { ...context, ...other };
|
63
|
+
}
|
64
|
+
|
65
|
+
function createORPCErrorConstructorMap(errors) {
|
66
|
+
const proxy = new Proxy(errors, {
|
67
|
+
get(target, code) {
|
68
|
+
if (typeof code !== "string") {
|
69
|
+
return Reflect.get(target, code);
|
70
|
+
}
|
71
|
+
const item = (...[options]) => {
|
72
|
+
const config = errors[code];
|
73
|
+
return new ORPCError(code, {
|
74
|
+
defined: Boolean(config),
|
75
|
+
status: config?.status,
|
76
|
+
message: options?.message ?? config?.message,
|
77
|
+
data: options?.data,
|
78
|
+
cause: options?.cause
|
79
|
+
});
|
80
|
+
};
|
81
|
+
return item;
|
82
|
+
}
|
83
|
+
});
|
84
|
+
return proxy;
|
85
|
+
}
|
86
|
+
async function validateORPCError(map, error) {
|
87
|
+
const { code, status, message, data, cause, defined } = error;
|
88
|
+
const config = map?.[error.code];
|
89
|
+
if (!config || fallbackORPCErrorStatus(error.code, config.status) !== error.status) {
|
90
|
+
return defined ? new ORPCError(code, { defined: false, status, message, data, cause }) : error;
|
91
|
+
}
|
92
|
+
if (!config.data) {
|
93
|
+
return defined ? error : new ORPCError(code, { defined: true, status, message, data, cause });
|
94
|
+
}
|
95
|
+
const validated = await config.data["~standard"].validate(error.data);
|
96
|
+
if (validated.issues) {
|
97
|
+
return defined ? new ORPCError(code, { defined: false, status, message, data, cause }) : error;
|
98
|
+
}
|
99
|
+
return new ORPCError(code, { defined: true, status, message, data: validated.value, cause });
|
100
|
+
}
|
101
|
+
|
102
|
+
function middlewareOutputFn(output) {
|
103
|
+
return { output, context: {} };
|
104
|
+
}
|
105
|
+
|
106
|
+
function createProcedureClient(lazyableProcedure, ...[options]) {
|
107
|
+
return async (...[input, callerOptions]) => {
|
108
|
+
const path = options?.path ?? [];
|
109
|
+
const { default: procedure } = await unlazy(lazyableProcedure);
|
110
|
+
const clientContext = callerOptions?.context ?? {};
|
111
|
+
const context = await value(options?.context ?? {}, clientContext);
|
112
|
+
const errors = createORPCErrorConstructorMap(procedure["~orpc"].errorMap);
|
113
|
+
try {
|
114
|
+
return await intercept(
|
115
|
+
options?.interceptors ?? [],
|
116
|
+
{
|
117
|
+
context,
|
118
|
+
input,
|
119
|
+
// input only optional when it undefinable so we can safely cast it
|
120
|
+
errors,
|
121
|
+
path,
|
122
|
+
procedure,
|
123
|
+
signal: callerOptions?.signal,
|
124
|
+
lastEventId: callerOptions?.lastEventId
|
125
|
+
},
|
126
|
+
(interceptorOptions) => executeProcedureInternal(interceptorOptions.procedure, interceptorOptions)
|
127
|
+
);
|
128
|
+
} catch (e) {
|
129
|
+
if (!(e instanceof ORPCError)) {
|
130
|
+
throw toError(e);
|
131
|
+
}
|
132
|
+
const validated = await validateORPCError(procedure["~orpc"].errorMap, e);
|
133
|
+
throw validated;
|
134
|
+
}
|
135
|
+
};
|
136
|
+
}
|
137
|
+
async function validateInput(procedure, input) {
|
138
|
+
const schema = procedure["~orpc"].inputSchema;
|
139
|
+
if (!schema) {
|
140
|
+
return input;
|
141
|
+
}
|
142
|
+
const result = await schema["~standard"].validate(input);
|
143
|
+
if (result.issues) {
|
144
|
+
throw new ORPCError("BAD_REQUEST", {
|
145
|
+
message: "Input validation failed",
|
146
|
+
data: {
|
147
|
+
issues: result.issues
|
148
|
+
},
|
149
|
+
cause: new ValidationError({ message: "Input validation failed", issues: result.issues })
|
150
|
+
});
|
151
|
+
}
|
152
|
+
return result.value;
|
153
|
+
}
|
154
|
+
async function validateOutput(procedure, output) {
|
155
|
+
const schema = procedure["~orpc"].outputSchema;
|
156
|
+
if (!schema) {
|
157
|
+
return output;
|
158
|
+
}
|
159
|
+
const result = await schema["~standard"].validate(output);
|
160
|
+
if (result.issues) {
|
161
|
+
throw new ORPCError("INTERNAL_SERVER_ERROR", {
|
162
|
+
message: "Output validation failed",
|
163
|
+
cause: new ValidationError({ message: "Output validation failed", issues: result.issues })
|
164
|
+
});
|
165
|
+
}
|
166
|
+
return result.value;
|
167
|
+
}
|
168
|
+
async function executeProcedureInternal(procedure, options) {
|
169
|
+
const middlewares = procedure["~orpc"].middlewares;
|
170
|
+
const inputValidationIndex = Math.min(Math.max(0, procedure["~orpc"].inputValidationIndex), middlewares.length);
|
171
|
+
const outputValidationIndex = Math.min(Math.max(0, procedure["~orpc"].outputValidationIndex), middlewares.length);
|
172
|
+
let currentIndex = 0;
|
173
|
+
let currentContext = options.context;
|
174
|
+
let currentInput = options.input;
|
175
|
+
const next = async (...[nextOptions]) => {
|
176
|
+
const index = currentIndex;
|
177
|
+
const midContext = nextOptions?.context ?? {};
|
178
|
+
currentIndex += 1;
|
179
|
+
currentContext = mergeCurrentContext(currentContext, midContext);
|
180
|
+
if (index === inputValidationIndex) {
|
181
|
+
currentInput = await validateInput(procedure, currentInput);
|
182
|
+
}
|
183
|
+
const mid = middlewares[index];
|
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
|
+
};
|
191
|
+
if (index === outputValidationIndex) {
|
192
|
+
const validatedOutput = await validateOutput(procedure, result.output);
|
193
|
+
return {
|
194
|
+
context: result.context,
|
195
|
+
output: validatedOutput
|
196
|
+
};
|
197
|
+
}
|
198
|
+
return result;
|
199
|
+
};
|
200
|
+
return (await next()).output;
|
201
|
+
}
|
202
|
+
|
203
|
+
const HIDDEN_ROUTER_CONTRACT_SYMBOL = Symbol("ORPC_HIDDEN_ROUTER_CONTRACT");
|
204
|
+
function setHiddenRouterContract(router, contract) {
|
205
|
+
return new Proxy(router, {
|
206
|
+
get(target, key) {
|
207
|
+
if (key === HIDDEN_ROUTER_CONTRACT_SYMBOL) {
|
208
|
+
return contract;
|
209
|
+
}
|
210
|
+
return Reflect.get(target, key);
|
211
|
+
}
|
212
|
+
});
|
213
|
+
}
|
214
|
+
function getHiddenRouterContract(router) {
|
215
|
+
return router[HIDDEN_ROUTER_CONTRACT_SYMBOL];
|
216
|
+
}
|
217
|
+
|
218
|
+
function getRouter(router, path) {
|
219
|
+
let current = router;
|
220
|
+
for (let i = 0; i < path.length; i++) {
|
221
|
+
const segment = path[i];
|
222
|
+
if (!current) {
|
223
|
+
return void 0;
|
224
|
+
}
|
225
|
+
if (isProcedure(current)) {
|
226
|
+
return void 0;
|
227
|
+
}
|
228
|
+
if (!isLazy(current)) {
|
229
|
+
current = current[segment];
|
230
|
+
continue;
|
231
|
+
}
|
232
|
+
const lazied = current;
|
233
|
+
const rest = path.slice(i);
|
234
|
+
return lazy(async () => {
|
235
|
+
const unwrapped = await unlazy(lazied);
|
236
|
+
const next = getRouter(unwrapped.default, rest);
|
237
|
+
return unlazy(next);
|
238
|
+
}, getLazyMeta(lazied));
|
239
|
+
}
|
240
|
+
return current;
|
241
|
+
}
|
242
|
+
function createAccessibleLazyRouter(lazied) {
|
243
|
+
const recursive = new Proxy(lazied, {
|
244
|
+
get(target, key) {
|
245
|
+
if (typeof key !== "string") {
|
246
|
+
return Reflect.get(target, key);
|
247
|
+
}
|
248
|
+
const next = getRouter(lazied, [key]);
|
249
|
+
return createAccessibleLazyRouter(next);
|
250
|
+
}
|
251
|
+
});
|
252
|
+
return recursive;
|
253
|
+
}
|
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;
|
293
|
+
}
|
294
|
+
if (isLazy(currentRouter)) {
|
295
|
+
lazyOptions.push({
|
296
|
+
router: currentRouter,
|
297
|
+
path: options.path
|
298
|
+
});
|
299
|
+
} else if (isContractProcedure(currentRouter)) {
|
300
|
+
callback({
|
301
|
+
contract: currentRouter,
|
302
|
+
path: options.path
|
303
|
+
});
|
304
|
+
} else {
|
305
|
+
for (const key in currentRouter) {
|
306
|
+
traverseContractProcedures(
|
307
|
+
{
|
308
|
+
router: currentRouter[key],
|
309
|
+
path: [...options.path, key]
|
310
|
+
},
|
311
|
+
callback,
|
312
|
+
lazyOptions
|
313
|
+
);
|
314
|
+
}
|
315
|
+
}
|
316
|
+
return lazyOptions;
|
317
|
+
}
|
318
|
+
async function resolveContractProcedures(options, callback) {
|
319
|
+
const pending = [options];
|
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);
|
324
|
+
pending.push({
|
325
|
+
router,
|
326
|
+
path: options3.path
|
327
|
+
});
|
328
|
+
}
|
329
|
+
}
|
330
|
+
}
|
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;
|
342
|
+
}
|
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) {
|
359
|
+
return new Procedure({
|
360
|
+
...procedure["~orpc"],
|
361
|
+
errorMap: contract["~orpc"].errorMap,
|
362
|
+
route: contract["~orpc"].route,
|
363
|
+
meta: contract["~orpc"].meta
|
364
|
+
});
|
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
|
+
}
|
373
|
+
|
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,75 @@
|
|
1
|
+
import { HTTPPath, AnySchema, Meta, InferSchemaOutput, ErrorFromErrorMap } from '@orpc/contract';
|
2
|
+
import { Interceptor, MaybeOptionalOptions } from '@orpc/shared';
|
3
|
+
import { StandardResponse, StandardLazyRequest } from '@orpc/standard-server';
|
4
|
+
import { a as AnyRouter, A as AnyProcedure, C as Context, P as ProcedureClientInterceptorOptions, R as Router } from './server.MZvbGc3n.js';
|
5
|
+
import { ORPCError } from '@orpc/client';
|
6
|
+
|
7
|
+
type StandardParams = Record<string, string>;
|
8
|
+
type StandardMatchResult = {
|
9
|
+
path: readonly string[];
|
10
|
+
procedure: AnyProcedure;
|
11
|
+
params?: StandardParams;
|
12
|
+
} | undefined;
|
13
|
+
interface StandardMatcher {
|
14
|
+
init(router: AnyRouter): void;
|
15
|
+
match(method: string, pathname: HTTPPath): Promise<StandardMatchResult>;
|
16
|
+
}
|
17
|
+
interface StandardCodec {
|
18
|
+
encode(output: unknown, procedure: AnyProcedure): StandardResponse;
|
19
|
+
encodeError(error: ORPCError<any, any>): StandardResponse;
|
20
|
+
decode(request: StandardLazyRequest, params: StandardParams | undefined, procedure: AnyProcedure): Promise<unknown>;
|
21
|
+
}
|
22
|
+
|
23
|
+
type StandardHandleOptions<T extends Context> = {
|
24
|
+
prefix?: HTTPPath;
|
25
|
+
} & (Record<never, never> extends T ? {
|
26
|
+
context?: T;
|
27
|
+
} : {
|
28
|
+
context: T;
|
29
|
+
});
|
30
|
+
type StandardHandleResult = {
|
31
|
+
matched: true;
|
32
|
+
response: StandardResponse;
|
33
|
+
} | {
|
34
|
+
matched: false;
|
35
|
+
response: undefined;
|
36
|
+
};
|
37
|
+
type StandardHandlerInterceptorOptions<T extends Context> = StandardHandleOptions<T> & {
|
38
|
+
context: T;
|
39
|
+
request: StandardLazyRequest;
|
40
|
+
};
|
41
|
+
interface StandardHandlerOptions<TContext extends Context> {
|
42
|
+
plugins?: HandlerPlugin<TContext>[];
|
43
|
+
/**
|
44
|
+
* Interceptors at the request level, helpful when you want catch errors
|
45
|
+
*/
|
46
|
+
interceptors?: Interceptor<StandardHandlerInterceptorOptions<TContext>, StandardHandleResult, unknown>[];
|
47
|
+
/**
|
48
|
+
* Interceptors at the root level, helpful when you want override the request/response
|
49
|
+
*/
|
50
|
+
rootInterceptors?: Interceptor<StandardHandlerInterceptorOptions<TContext>, StandardHandleResult, unknown>[];
|
51
|
+
/**
|
52
|
+
*
|
53
|
+
* Interceptors for procedure client.
|
54
|
+
*/
|
55
|
+
clientInterceptors?: Interceptor<ProcedureClientInterceptorOptions<TContext, AnySchema, Record<never, never>, Meta>, InferSchemaOutput<AnySchema>, ErrorFromErrorMap<Record<never, never>>>[];
|
56
|
+
}
|
57
|
+
declare class StandardHandler<T extends Context> {
|
58
|
+
private readonly matcher;
|
59
|
+
private readonly codec;
|
60
|
+
private readonly options;
|
61
|
+
private readonly plugin;
|
62
|
+
constructor(router: Router<any, T>, matcher: StandardMatcher, codec: StandardCodec, options: NoInfer<StandardHandlerOptions<T>>);
|
63
|
+
handle(request: StandardLazyRequest, ...[options]: MaybeOptionalOptions<StandardHandleOptions<T>>): Promise<StandardHandleResult>;
|
64
|
+
}
|
65
|
+
|
66
|
+
interface HandlerPlugin<TContext extends Context> {
|
67
|
+
init?(options: StandardHandlerOptions<TContext>): void;
|
68
|
+
}
|
69
|
+
declare class CompositeHandlerPlugin<TContext extends Context> implements HandlerPlugin<TContext> {
|
70
|
+
private readonly plugins;
|
71
|
+
constructor(plugins?: HandlerPlugin<TContext>[]);
|
72
|
+
init(options: StandardHandlerOptions<TContext>): void;
|
73
|
+
}
|
74
|
+
|
75
|
+
export { CompositeHandlerPlugin as C, type HandlerPlugin as H, type StandardHandleOptions as S, type StandardHandlerOptions as a, type StandardHandlerInterceptorOptions as b, type StandardCodec as c, type StandardParams as d, type StandardMatcher as e, type StandardMatchResult as f, type StandardHandleResult as g, StandardHandler as h };
|
@@ -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.BY9sDlwl.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.MZvbGc3n.js';
|
3
|
+
import { a as StandardHandlerOptions } from './server.CPqNKiJp.js';
|
4
|
+
|
5
|
+
interface StandardRPCHandlerOptions<T extends Context> extends StandardHandlerOptions<T>, StandardRPCJsonSerializerOptions {
|
6
|
+
}
|
7
|
+
|
8
|
+
export type { StandardRPCHandlerOptions as S };
|