@temporary-name/server 1.9.3-alpha.e098b3d1c5bffbad2fadfda89ba01f6452db46b5 → 1.9.3-alpha.ec3bfb9dce56198911349c322c970208b21b50db
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/dist/adapters/aws-lambda/index.d.mts +4 -5
- package/dist/adapters/aws-lambda/index.d.ts +4 -5
- package/dist/adapters/aws-lambda/index.mjs +4 -4
- package/dist/adapters/fetch/index.d.mts +8 -85
- package/dist/adapters/fetch/index.d.ts +8 -85
- package/dist/adapters/fetch/index.mjs +16 -155
- package/dist/adapters/node/index.d.mts +8 -62
- package/dist/adapters/node/index.d.ts +8 -62
- package/dist/adapters/node/index.mjs +14 -120
- package/dist/adapters/standard/index.d.mts +5 -6
- package/dist/adapters/standard/index.d.ts +5 -6
- package/dist/adapters/standard/index.mjs +4 -4
- package/dist/helpers/index.mjs +3 -29
- package/dist/index.d.mts +110 -199
- package/dist/index.d.ts +110 -199
- package/dist/index.mjs +140 -153
- package/dist/openapi/index.d.mts +11 -27
- package/dist/openapi/index.d.ts +11 -27
- package/dist/openapi/index.mjs +9 -79
- package/dist/shared/server.C1RJffw4.mjs +30 -0
- package/dist/shared/server.CQIFwyhc.mjs +40 -0
- package/dist/shared/server.CVhIyQ4x.d.mts +41 -0
- package/dist/shared/server.CYa9puL2.mjs +403 -0
- package/dist/shared/server.ChOv1yG3.mjs +319 -0
- package/dist/shared/server.Cj3_Lp61.d.mts +373 -0
- package/dist/shared/server.Cj3_Lp61.d.ts +373 -0
- package/dist/shared/server.Cza0RB3u.mjs +160 -0
- package/dist/shared/server.D8RAzJ_p.d.ts +41 -0
- package/dist/shared/server.YUvuxHty.mjs +48 -0
- package/package.json +10 -28
- package/dist/plugins/index.d.mts +0 -110
- package/dist/plugins/index.d.ts +0 -110
- package/dist/plugins/index.mjs +0 -175
- package/dist/shared/server.7aL9gcoU.d.mts +0 -23
- package/dist/shared/server.BL2R5jcp.d.mts +0 -228
- package/dist/shared/server.BL2R5jcp.d.ts +0 -228
- package/dist/shared/server.CVBLzkro.mjs +0 -255
- package/dist/shared/server.ClhVCxfg.mjs +0 -413
- package/dist/shared/server.D6Qs_UcF.d.mts +0 -55
- package/dist/shared/server.DFptr1Nz.d.ts +0 -23
- package/dist/shared/server.DpoO_ER_.d.ts +0 -55
- package/dist/shared/server.JtIZ8YG7.mjs +0 -237
|
@@ -1,255 +0,0 @@
|
|
|
1
|
-
import { stringifyJSON, isObject, isORPCErrorStatus, tryDecodeURIComponent, toHttpPath, toArray, intercept, runWithSpan, ORPC_NAME, isAsyncIteratorObject, asyncIteratorWithSpan, setSpanError, ORPCError, toORPCError } from '@temporary-name/shared';
|
|
2
|
-
import { flattenHeader } from '@temporary-name/standard-server';
|
|
3
|
-
import { c as createProcedureClient } from './server.ClhVCxfg.mjs';
|
|
4
|
-
import { fallbackContractConfig, standardizeHTTPPath } from '@temporary-name/contract';
|
|
5
|
-
import { d as deserialize, b as bracketNotationDeserialize, s as serialize } from './server.JtIZ8YG7.mjs';
|
|
6
|
-
import { traverseContractProcedures, isProcedure, getLazyMeta, unlazy, getRouter, createContractedProcedure } from '@temporary-name/server';
|
|
7
|
-
import { createRouter, addRoute, findRoute } from 'rou3';
|
|
8
|
-
|
|
9
|
-
async function decode(request, pathParams) {
|
|
10
|
-
return {
|
|
11
|
-
path: pathParams ?? {},
|
|
12
|
-
query: bracketNotationDeserialize(Array.from(request.url.searchParams.entries())),
|
|
13
|
-
headers: request.headers,
|
|
14
|
-
body: deserialize(await request.body()) ?? {}
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
function encode(output, procedure) {
|
|
18
|
-
const successStatus = fallbackContractConfig(
|
|
19
|
-
"defaultSuccessStatus",
|
|
20
|
-
procedure["~orpc"].route.successStatus
|
|
21
|
-
);
|
|
22
|
-
const outputStructure = fallbackContractConfig(
|
|
23
|
-
"defaultOutputStructure",
|
|
24
|
-
procedure["~orpc"].route.outputStructure
|
|
25
|
-
);
|
|
26
|
-
if (outputStructure === "compact") {
|
|
27
|
-
return {
|
|
28
|
-
status: successStatus,
|
|
29
|
-
headers: {},
|
|
30
|
-
body: serialize(output)
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
if (!isDetailedOutput(output)) {
|
|
34
|
-
throw new Error(`
|
|
35
|
-
Invalid "detailed" output structure:
|
|
36
|
-
\u2022 Expected an object with optional properties:
|
|
37
|
-
- status (number 200-399)
|
|
38
|
-
- headers (Record<string, string | string[]>)
|
|
39
|
-
- body (any)
|
|
40
|
-
\u2022 No extra keys allowed.
|
|
41
|
-
|
|
42
|
-
Actual value:
|
|
43
|
-
${stringifyJSON(output)}
|
|
44
|
-
`);
|
|
45
|
-
}
|
|
46
|
-
return {
|
|
47
|
-
status: output.status ?? successStatus,
|
|
48
|
-
headers: output.headers ?? {},
|
|
49
|
-
body: serialize(output.body)
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
function encodeError(error) {
|
|
53
|
-
return {
|
|
54
|
-
status: error.status,
|
|
55
|
-
headers: {},
|
|
56
|
-
body: serialize(error.toJSON(), { outputFormat: "plain" })
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
function isDetailedOutput(output) {
|
|
60
|
-
if (!isObject(output)) {
|
|
61
|
-
return false;
|
|
62
|
-
}
|
|
63
|
-
if (output.headers && !isObject(output.headers)) {
|
|
64
|
-
return false;
|
|
65
|
-
}
|
|
66
|
-
if (output.status !== void 0 && (typeof output.status !== "number" || !Number.isInteger(output.status) || isORPCErrorStatus(output.status))) {
|
|
67
|
-
return false;
|
|
68
|
-
}
|
|
69
|
-
return true;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
function resolveFriendlyStandardHandleOptions(options) {
|
|
73
|
-
return {
|
|
74
|
-
...options,
|
|
75
|
-
context: options.context ?? {}
|
|
76
|
-
// Context only optional if all fields are optional
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
function toRou3Pattern(path) {
|
|
80
|
-
return standardizeHTTPPath(path).replace(/\/\{\+([^}]+)\}/g, "/**:$1").replace(/\/\{([^}]+)\}/g, "/:$1");
|
|
81
|
-
}
|
|
82
|
-
function decodeParams(params) {
|
|
83
|
-
return Object.fromEntries(
|
|
84
|
-
Object.entries(params).map(([key, value]) => [key, tryDecodeURIComponent(value)])
|
|
85
|
-
);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
class StandardOpenAPIMatcher {
|
|
89
|
-
tree = createRouter();
|
|
90
|
-
pendingRouters = [];
|
|
91
|
-
init(router, path = []) {
|
|
92
|
-
const laziedOptions = traverseContractProcedures({ router, path }, (traverseOptions) => {
|
|
93
|
-
const { path: path2, contract } = traverseOptions;
|
|
94
|
-
const method = fallbackContractConfig("defaultMethod", contract["~orpc"].route.method);
|
|
95
|
-
const httpPath = toRou3Pattern(contract["~orpc"].route.path ?? toHttpPath(path2));
|
|
96
|
-
if (isProcedure(contract)) {
|
|
97
|
-
addRoute(this.tree, method, httpPath, {
|
|
98
|
-
path: path2,
|
|
99
|
-
contract,
|
|
100
|
-
procedure: contract,
|
|
101
|
-
// this mean dev not used contract-first so we can used contract as procedure directly
|
|
102
|
-
router
|
|
103
|
-
});
|
|
104
|
-
} else {
|
|
105
|
-
addRoute(this.tree, method, httpPath, {
|
|
106
|
-
path: path2,
|
|
107
|
-
contract,
|
|
108
|
-
procedure: void 0,
|
|
109
|
-
router
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
});
|
|
113
|
-
this.pendingRouters.push(
|
|
114
|
-
...laziedOptions.map((option) => ({
|
|
115
|
-
...option,
|
|
116
|
-
httpPathPrefix: toHttpPath(option.path),
|
|
117
|
-
laziedPrefix: getLazyMeta(option.router).prefix
|
|
118
|
-
}))
|
|
119
|
-
);
|
|
120
|
-
}
|
|
121
|
-
async match(method, pathname) {
|
|
122
|
-
if (this.pendingRouters.length) {
|
|
123
|
-
const newPendingRouters = [];
|
|
124
|
-
for (const pendingRouter of this.pendingRouters) {
|
|
125
|
-
if (!pendingRouter.laziedPrefix || pathname.startsWith(pendingRouter.laziedPrefix) || pathname.startsWith(pendingRouter.httpPathPrefix)) {
|
|
126
|
-
const { default: router } = await unlazy(pendingRouter.router);
|
|
127
|
-
this.init(router, pendingRouter.path);
|
|
128
|
-
} else {
|
|
129
|
-
newPendingRouters.push(pendingRouter);
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
this.pendingRouters = newPendingRouters;
|
|
133
|
-
}
|
|
134
|
-
const match = findRoute(this.tree, method, pathname);
|
|
135
|
-
if (!match) {
|
|
136
|
-
return void 0;
|
|
137
|
-
}
|
|
138
|
-
if (!match.data.procedure) {
|
|
139
|
-
const { default: maybeProcedure } = await unlazy(getRouter(match.data.router, match.data.path));
|
|
140
|
-
if (!isProcedure(maybeProcedure)) {
|
|
141
|
-
throw new Error(`
|
|
142
|
-
[Contract-First] Missing or invalid implementation for procedure at path: ${toHttpPath(match.data.path)}.
|
|
143
|
-
Ensure that the procedure is correctly defined and matches the expected contract.
|
|
144
|
-
`);
|
|
145
|
-
}
|
|
146
|
-
match.data.procedure = createContractedProcedure(maybeProcedure, match.data.contract);
|
|
147
|
-
}
|
|
148
|
-
return {
|
|
149
|
-
path: match.data.path,
|
|
150
|
-
procedure: match.data.procedure,
|
|
151
|
-
params: match.params ? decodeParams(match.params) : void 0
|
|
152
|
-
};
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
class CompositeStandardHandlerPlugin {
|
|
157
|
-
plugins;
|
|
158
|
-
constructor(plugins = []) {
|
|
159
|
-
this.plugins = [...plugins].sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
|
|
160
|
-
}
|
|
161
|
-
init(options, router) {
|
|
162
|
-
for (const plugin of this.plugins) {
|
|
163
|
-
plugin.init?.(options, router);
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
class StandardHandler {
|
|
169
|
-
interceptors;
|
|
170
|
-
clientInterceptors;
|
|
171
|
-
rootInterceptors;
|
|
172
|
-
matcher;
|
|
173
|
-
constructor(router, options) {
|
|
174
|
-
this.matcher = new StandardOpenAPIMatcher();
|
|
175
|
-
const plugins = new CompositeStandardHandlerPlugin(options.plugins);
|
|
176
|
-
plugins.init(options, router);
|
|
177
|
-
this.interceptors = toArray(options.interceptors);
|
|
178
|
-
this.clientInterceptors = toArray(options.clientInterceptors);
|
|
179
|
-
this.rootInterceptors = toArray(options.rootInterceptors);
|
|
180
|
-
this.matcher.init(router);
|
|
181
|
-
}
|
|
182
|
-
async handle(request, options) {
|
|
183
|
-
const prefix = options.prefix?.replace(/\/$/, "") || void 0;
|
|
184
|
-
if (prefix && !request.url.pathname.startsWith(`${prefix}/`) && request.url.pathname !== prefix) {
|
|
185
|
-
return { matched: false, response: void 0 };
|
|
186
|
-
}
|
|
187
|
-
return intercept(this.rootInterceptors, { ...options, request, prefix }, async (interceptorOptions) => {
|
|
188
|
-
return runWithSpan({ name: `${request.method} ${request.url.pathname}` }, async (span) => {
|
|
189
|
-
let step;
|
|
190
|
-
try {
|
|
191
|
-
return await intercept(
|
|
192
|
-
this.interceptors,
|
|
193
|
-
interceptorOptions,
|
|
194
|
-
async ({ request: request2, context, prefix: prefix2 }) => {
|
|
195
|
-
const method = request2.method;
|
|
196
|
-
const url = request2.url;
|
|
197
|
-
const pathname = prefix2 ? url.pathname.replace(prefix2, "") : url.pathname;
|
|
198
|
-
const match = await runWithSpan(
|
|
199
|
-
{ name: "find_procedure" },
|
|
200
|
-
() => this.matcher.match(method, `/${pathname.replace(/^\/|\/$/g, "")}`)
|
|
201
|
-
);
|
|
202
|
-
if (!match) {
|
|
203
|
-
return { matched: false, response: void 0 };
|
|
204
|
-
}
|
|
205
|
-
span?.updateName(`${ORPC_NAME}.${match.path.join("/")}`);
|
|
206
|
-
span?.setAttribute("rpc.system", ORPC_NAME);
|
|
207
|
-
span?.setAttribute("rpc.method", match.path.join("."));
|
|
208
|
-
step = "decode_input";
|
|
209
|
-
const input = await runWithSpan({ name: "decode_input" }, () => decode(request2, match.params));
|
|
210
|
-
step = void 0;
|
|
211
|
-
if (isAsyncIteratorObject(input.body)) {
|
|
212
|
-
input.body = asyncIteratorWithSpan(
|
|
213
|
-
{ name: "consume_event_iterator_input", signal: request2.signal },
|
|
214
|
-
input.body
|
|
215
|
-
);
|
|
216
|
-
}
|
|
217
|
-
const client = createProcedureClient(match.procedure, {
|
|
218
|
-
context,
|
|
219
|
-
path: match.path,
|
|
220
|
-
interceptors: this.clientInterceptors
|
|
221
|
-
});
|
|
222
|
-
step = "call_procedure";
|
|
223
|
-
const output = await client(input, {
|
|
224
|
-
request: request2,
|
|
225
|
-
signal: request2.signal,
|
|
226
|
-
lastEventId: flattenHeader(request2.headers["last-event-id"])
|
|
227
|
-
});
|
|
228
|
-
step = void 0;
|
|
229
|
-
const response = encode(output, match.procedure);
|
|
230
|
-
return {
|
|
231
|
-
matched: true,
|
|
232
|
-
response
|
|
233
|
-
};
|
|
234
|
-
}
|
|
235
|
-
);
|
|
236
|
-
} catch (e) {
|
|
237
|
-
if (step !== "call_procedure") {
|
|
238
|
-
setSpanError(span, e);
|
|
239
|
-
}
|
|
240
|
-
const error = step === "decode_input" && !(e instanceof ORPCError) ? new ORPCError("BAD_REQUEST", {
|
|
241
|
-
message: `Malformed request. Ensure the request body is properly formatted and the 'Content-Type' header is set correctly.`,
|
|
242
|
-
cause: e
|
|
243
|
-
}) : toORPCError(e);
|
|
244
|
-
const response = encodeError(error);
|
|
245
|
-
return {
|
|
246
|
-
matched: true,
|
|
247
|
-
response
|
|
248
|
-
};
|
|
249
|
-
}
|
|
250
|
-
});
|
|
251
|
-
});
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
export { CompositeStandardHandlerPlugin as C, StandardHandler as S, encodeError as a, StandardOpenAPIMatcher as b, decodeParams as c, decode as d, encode as e, resolveFriendlyStandardHandleOptions as r, toRou3Pattern as t };
|
|
@@ -1,413 +0,0 @@
|
|
|
1
|
-
import { isContractProcedure, mergePrefix, mergeErrorMap, enhanceRoute, validateORPCError, ValidationError } from '@temporary-name/contract';
|
|
2
|
-
import { resolveMaybeOptionalOptions, ORPCError, toArray, value, runWithSpan, intercept, isAsyncIteratorObject, overlayProxy, asyncIteratorWithSpan } from '@temporary-name/shared';
|
|
3
|
-
import { HibernationEventIterator, mapEventIterator } from '@temporary-name/standard-server';
|
|
4
|
-
import { safeParseAsync } from '@temporary-name/zod';
|
|
5
|
-
|
|
6
|
-
function isStartWithMiddlewares(middlewares, compare) {
|
|
7
|
-
if (compare.length > middlewares.length) {
|
|
8
|
-
return false;
|
|
9
|
-
}
|
|
10
|
-
for (let i = 0; i < middlewares.length; i++) {
|
|
11
|
-
if (compare[i] === void 0) {
|
|
12
|
-
return true;
|
|
13
|
-
}
|
|
14
|
-
if (middlewares[i] !== compare[i]) {
|
|
15
|
-
return false;
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
return true;
|
|
19
|
-
}
|
|
20
|
-
function mergeMiddlewares(first, second, options) {
|
|
21
|
-
if (options.dedupeLeading && isStartWithMiddlewares(second, first)) {
|
|
22
|
-
return second;
|
|
23
|
-
}
|
|
24
|
-
return [...first, ...second];
|
|
25
|
-
}
|
|
26
|
-
function addMiddleware(middlewares, addition) {
|
|
27
|
-
return [...middlewares, addition];
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
class Procedure {
|
|
31
|
-
/**
|
|
32
|
-
* This property holds the defined options.
|
|
33
|
-
*/
|
|
34
|
-
"~orpc";
|
|
35
|
-
constructor(def) {
|
|
36
|
-
this["~orpc"] = def;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
function isProcedure(item) {
|
|
40
|
-
if (item instanceof Procedure) {
|
|
41
|
-
return true;
|
|
42
|
-
}
|
|
43
|
-
return isContractProcedure(item) && "middlewares" in item["~orpc"] && "handler" in item["~orpc"];
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
function mergeCurrentContext(context, other) {
|
|
47
|
-
return { ...context, ...other };
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
function createORPCErrorConstructorMap(errors) {
|
|
51
|
-
const proxy = new Proxy(errors, {
|
|
52
|
-
get(target, code) {
|
|
53
|
-
if (typeof code !== "string") {
|
|
54
|
-
return Reflect.get(target, code);
|
|
55
|
-
}
|
|
56
|
-
const item = (...rest) => {
|
|
57
|
-
const options = resolveMaybeOptionalOptions(rest);
|
|
58
|
-
const config = errors[code];
|
|
59
|
-
return new ORPCError(code, {
|
|
60
|
-
defined: Boolean(config),
|
|
61
|
-
status: config?.status,
|
|
62
|
-
message: options.message ?? config?.message,
|
|
63
|
-
data: options.data,
|
|
64
|
-
cause: options.cause
|
|
65
|
-
});
|
|
66
|
-
};
|
|
67
|
-
return item;
|
|
68
|
-
}
|
|
69
|
-
});
|
|
70
|
-
return proxy;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
const HIDDEN_ROUTER_CONTRACT_SYMBOL = Symbol("ORPC_HIDDEN_ROUTER_CONTRACT");
|
|
74
|
-
function setHiddenRouterContract(router, contract) {
|
|
75
|
-
return new Proxy(router, {
|
|
76
|
-
get(target, key) {
|
|
77
|
-
if (key === HIDDEN_ROUTER_CONTRACT_SYMBOL) {
|
|
78
|
-
return contract;
|
|
79
|
-
}
|
|
80
|
-
return Reflect.get(target, key);
|
|
81
|
-
}
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
function getHiddenRouterContract(router) {
|
|
85
|
-
return router[HIDDEN_ROUTER_CONTRACT_SYMBOL];
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
function getRouter(router, path) {
|
|
89
|
-
let current = router;
|
|
90
|
-
for (let i = 0; i < path.length; i++) {
|
|
91
|
-
const segment = path[i];
|
|
92
|
-
if (!current) {
|
|
93
|
-
return void 0;
|
|
94
|
-
}
|
|
95
|
-
if (isProcedure(current)) {
|
|
96
|
-
return void 0;
|
|
97
|
-
}
|
|
98
|
-
if (!isLazy(current)) {
|
|
99
|
-
current = current[segment];
|
|
100
|
-
continue;
|
|
101
|
-
}
|
|
102
|
-
const lazied = current;
|
|
103
|
-
const rest = path.slice(i);
|
|
104
|
-
return lazyInternal(async () => {
|
|
105
|
-
const unwrapped = await unlazy(lazied);
|
|
106
|
-
const next = getRouter(unwrapped.default, rest);
|
|
107
|
-
return unlazy(next);
|
|
108
|
-
}, getLazyMeta(lazied));
|
|
109
|
-
}
|
|
110
|
-
return current;
|
|
111
|
-
}
|
|
112
|
-
function createAccessibleLazyRouter(lazied) {
|
|
113
|
-
const recursive = new Proxy(lazied, {
|
|
114
|
-
get(target, key) {
|
|
115
|
-
if (typeof key !== "string") {
|
|
116
|
-
return Reflect.get(target, key);
|
|
117
|
-
}
|
|
118
|
-
const next = getRouter(lazied, [key]);
|
|
119
|
-
return createAccessibleLazyRouter(next);
|
|
120
|
-
}
|
|
121
|
-
});
|
|
122
|
-
return recursive;
|
|
123
|
-
}
|
|
124
|
-
function enhanceRouter(router, options) {
|
|
125
|
-
if (isLazy(router)) {
|
|
126
|
-
const laziedMeta = getLazyMeta(router);
|
|
127
|
-
const enhancedPrefix = laziedMeta?.prefix ? mergePrefix(options.prefix, laziedMeta?.prefix) : options.prefix;
|
|
128
|
-
const enhanced2 = lazyInternal(
|
|
129
|
-
async () => {
|
|
130
|
-
const { default: unlaziedRouter } = await unlazy(router);
|
|
131
|
-
const enhanced3 = enhanceRouter(unlaziedRouter, options);
|
|
132
|
-
return unlazy(enhanced3);
|
|
133
|
-
},
|
|
134
|
-
{
|
|
135
|
-
...laziedMeta,
|
|
136
|
-
prefix: enhancedPrefix
|
|
137
|
-
}
|
|
138
|
-
);
|
|
139
|
-
const accessible = createAccessibleLazyRouter(enhanced2);
|
|
140
|
-
return accessible;
|
|
141
|
-
}
|
|
142
|
-
if (isProcedure(router)) {
|
|
143
|
-
const newMiddlewares = mergeMiddlewares(options.middlewares, router["~orpc"].middlewares, {
|
|
144
|
-
dedupeLeading: options.dedupeLeadingMiddlewares
|
|
145
|
-
});
|
|
146
|
-
const newMiddlewareAdded = newMiddlewares.length - router["~orpc"].middlewares.length;
|
|
147
|
-
const enhanced2 = new Procedure({
|
|
148
|
-
...router["~orpc"],
|
|
149
|
-
route: enhanceRoute(router["~orpc"].route, options),
|
|
150
|
-
errorMap: mergeErrorMap(options.errorMap, router["~orpc"].errorMap),
|
|
151
|
-
middlewares: newMiddlewares,
|
|
152
|
-
inputValidationIndex: router["~orpc"].inputValidationIndex + newMiddlewareAdded,
|
|
153
|
-
outputValidationIndex: router["~orpc"].outputValidationIndex + newMiddlewareAdded
|
|
154
|
-
});
|
|
155
|
-
return enhanced2;
|
|
156
|
-
}
|
|
157
|
-
const enhanced = {};
|
|
158
|
-
for (const key in router) {
|
|
159
|
-
enhanced[key] = enhanceRouter(router[key], options);
|
|
160
|
-
}
|
|
161
|
-
return enhanced;
|
|
162
|
-
}
|
|
163
|
-
function traverseContractProcedures(options, callback, lazyOptions = []) {
|
|
164
|
-
let currentRouter = options.router;
|
|
165
|
-
const hiddenContract = getHiddenRouterContract(options.router);
|
|
166
|
-
if (hiddenContract !== void 0) {
|
|
167
|
-
currentRouter = hiddenContract;
|
|
168
|
-
}
|
|
169
|
-
if (isLazy(currentRouter)) {
|
|
170
|
-
lazyOptions.push({
|
|
171
|
-
router: currentRouter,
|
|
172
|
-
path: options.path
|
|
173
|
-
});
|
|
174
|
-
} else if (isContractProcedure(currentRouter)) {
|
|
175
|
-
callback({
|
|
176
|
-
contract: currentRouter,
|
|
177
|
-
path: options.path
|
|
178
|
-
});
|
|
179
|
-
} else {
|
|
180
|
-
for (const key in currentRouter) {
|
|
181
|
-
traverseContractProcedures(
|
|
182
|
-
{
|
|
183
|
-
router: currentRouter[key],
|
|
184
|
-
path: [...options.path, key]
|
|
185
|
-
},
|
|
186
|
-
callback,
|
|
187
|
-
lazyOptions
|
|
188
|
-
);
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
return lazyOptions;
|
|
192
|
-
}
|
|
193
|
-
async function resolveContractProcedures(options, callback) {
|
|
194
|
-
const pending = [options];
|
|
195
|
-
for (const options2 of pending) {
|
|
196
|
-
const lazyOptions = traverseContractProcedures(options2, callback);
|
|
197
|
-
for (const options3 of lazyOptions) {
|
|
198
|
-
const { default: router } = await unlazy(options3.router);
|
|
199
|
-
pending.push({
|
|
200
|
-
router,
|
|
201
|
-
path: options3.path
|
|
202
|
-
});
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
async function unlazyRouter(router) {
|
|
207
|
-
if (isProcedure(router)) {
|
|
208
|
-
return router;
|
|
209
|
-
}
|
|
210
|
-
const unlazied = {};
|
|
211
|
-
for (const key in router) {
|
|
212
|
-
const item = router[key];
|
|
213
|
-
const { default: unlaziedRouter } = await unlazy(item);
|
|
214
|
-
unlazied[key] = await unlazyRouter(unlaziedRouter);
|
|
215
|
-
}
|
|
216
|
-
return unlazied;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
const LAZY_SYMBOL = Symbol("ORPC_LAZY_SYMBOL");
|
|
220
|
-
function lazyInternal(loader, meta = {}) {
|
|
221
|
-
return {
|
|
222
|
-
[LAZY_SYMBOL]: {
|
|
223
|
-
loader,
|
|
224
|
-
meta
|
|
225
|
-
}
|
|
226
|
-
};
|
|
227
|
-
}
|
|
228
|
-
function lazy(prefix, loader) {
|
|
229
|
-
return enhanceRouter(lazyInternal(loader), {
|
|
230
|
-
middlewares: [],
|
|
231
|
-
errorMap: {},
|
|
232
|
-
dedupeLeadingMiddlewares: true,
|
|
233
|
-
prefix
|
|
234
|
-
});
|
|
235
|
-
}
|
|
236
|
-
function isLazy(item) {
|
|
237
|
-
return (typeof item === "object" || typeof item === "function") && item !== null && LAZY_SYMBOL in item;
|
|
238
|
-
}
|
|
239
|
-
function getLazyMeta(lazied) {
|
|
240
|
-
return lazied[LAZY_SYMBOL].meta;
|
|
241
|
-
}
|
|
242
|
-
function unlazy(lazied) {
|
|
243
|
-
return isLazy(lazied) ? lazied[LAZY_SYMBOL].loader() : Promise.resolve({ default: lazied });
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
function middlewareOutputFn(output) {
|
|
247
|
-
return { output, context: {} };
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
function createProcedureClient(lazyableProcedure, ...rest) {
|
|
251
|
-
const options = resolveMaybeOptionalOptions(rest);
|
|
252
|
-
return async (...[input, callerOptions]) => {
|
|
253
|
-
const path = toArray(options.path);
|
|
254
|
-
const { default: procedure } = await unlazy(lazyableProcedure);
|
|
255
|
-
const clientContext = callerOptions?.context ?? {};
|
|
256
|
-
const context = await value(options.context ?? {}, clientContext);
|
|
257
|
-
const errors = createORPCErrorConstructorMap(procedure["~orpc"].errorMap);
|
|
258
|
-
const validateError = async (e) => {
|
|
259
|
-
if (e instanceof ORPCError) {
|
|
260
|
-
return await validateORPCError(procedure["~orpc"].errorMap, e);
|
|
261
|
-
}
|
|
262
|
-
return e;
|
|
263
|
-
};
|
|
264
|
-
try {
|
|
265
|
-
const output = await runWithSpan({ name: "call_procedure", signal: callerOptions?.signal }, (span) => {
|
|
266
|
-
span?.setAttribute("procedure.path", [...path]);
|
|
267
|
-
return intercept(
|
|
268
|
-
toArray(options.interceptors),
|
|
269
|
-
{
|
|
270
|
-
context,
|
|
271
|
-
input,
|
|
272
|
-
errors,
|
|
273
|
-
path,
|
|
274
|
-
procedure,
|
|
275
|
-
request: callerOptions?.request,
|
|
276
|
-
signal: callerOptions?.signal,
|
|
277
|
-
lastEventId: callerOptions?.lastEventId
|
|
278
|
-
},
|
|
279
|
-
(interceptorOptions) => {
|
|
280
|
-
const { input: input2, ...opts } = interceptorOptions;
|
|
281
|
-
return executeProcedureInternal(interceptorOptions.procedure, input2, opts);
|
|
282
|
-
}
|
|
283
|
-
);
|
|
284
|
-
});
|
|
285
|
-
if (isAsyncIteratorObject(output)) {
|
|
286
|
-
if (output instanceof HibernationEventIterator) {
|
|
287
|
-
return output;
|
|
288
|
-
}
|
|
289
|
-
return overlayProxy(
|
|
290
|
-
output,
|
|
291
|
-
mapEventIterator(
|
|
292
|
-
asyncIteratorWithSpan(
|
|
293
|
-
{ name: "consume_event_iterator_output", signal: callerOptions?.signal },
|
|
294
|
-
output
|
|
295
|
-
),
|
|
296
|
-
{
|
|
297
|
-
value: (v) => v,
|
|
298
|
-
error: (e) => validateError(e)
|
|
299
|
-
}
|
|
300
|
-
)
|
|
301
|
-
);
|
|
302
|
-
}
|
|
303
|
-
return output;
|
|
304
|
-
} catch (e) {
|
|
305
|
-
throw await validateError(e);
|
|
306
|
-
}
|
|
307
|
-
};
|
|
308
|
-
}
|
|
309
|
-
async function validateInput(procedure, input) {
|
|
310
|
-
const schemas = procedure["~orpc"].schemas;
|
|
311
|
-
return runWithSpan({ name: "validate_input" }, async () => {
|
|
312
|
-
const resultBody = await safeParseAsync(schemas.bodySchema, input.body, { parseType: "body" });
|
|
313
|
-
const resultPath = await safeParseAsync(schemas.pathSchema, input.path, { parseType: "path" });
|
|
314
|
-
const resultQuery = await safeParseAsync(schemas.querySchema, input.query, { parseType: "query" });
|
|
315
|
-
const issues = [];
|
|
316
|
-
if (!resultBody.success) {
|
|
317
|
-
issues.push(...resultBody.error.issues.map((i) => ({ ...i, path: ["body", ...i.path] })));
|
|
318
|
-
}
|
|
319
|
-
if (!resultPath.success) {
|
|
320
|
-
issues.push(...resultPath.error.issues.map((i) => ({ ...i, path: ["path", ...i.path] })));
|
|
321
|
-
}
|
|
322
|
-
if (!resultQuery.success) {
|
|
323
|
-
issues.push(...resultQuery.error.issues.map((i) => ({ ...i, path: ["query", ...i.path] })));
|
|
324
|
-
}
|
|
325
|
-
if (issues.length > 0) {
|
|
326
|
-
throw new ORPCError("BAD_REQUEST", {
|
|
327
|
-
message: "Input validation failed",
|
|
328
|
-
data: {
|
|
329
|
-
issues
|
|
330
|
-
},
|
|
331
|
-
cause: new ValidationError({
|
|
332
|
-
message: "Input validation failed",
|
|
333
|
-
issues,
|
|
334
|
-
data: input
|
|
335
|
-
})
|
|
336
|
-
});
|
|
337
|
-
}
|
|
338
|
-
const results = {
|
|
339
|
-
body: resultBody.data,
|
|
340
|
-
path: resultPath.data,
|
|
341
|
-
query: resultQuery.data
|
|
342
|
-
};
|
|
343
|
-
return results;
|
|
344
|
-
});
|
|
345
|
-
}
|
|
346
|
-
async function validateOutput(procedure, output) {
|
|
347
|
-
const schema = procedure["~orpc"].schemas.outputSchema;
|
|
348
|
-
if (!schema) {
|
|
349
|
-
return output;
|
|
350
|
-
}
|
|
351
|
-
return runWithSpan({ name: "validate_output" }, async () => {
|
|
352
|
-
const result = await safeParseAsync(schema, output, { parseType: "output" });
|
|
353
|
-
if (!result.success) {
|
|
354
|
-
throw new ORPCError("INTERNAL_SERVER_ERROR", {
|
|
355
|
-
message: "Output validation failed",
|
|
356
|
-
cause: new ValidationError({
|
|
357
|
-
message: "Output validation failed",
|
|
358
|
-
issues: result.error.issues,
|
|
359
|
-
data: output
|
|
360
|
-
})
|
|
361
|
-
});
|
|
362
|
-
}
|
|
363
|
-
return result.data;
|
|
364
|
-
});
|
|
365
|
-
}
|
|
366
|
-
async function executeProcedureInternal(procedure, input, options) {
|
|
367
|
-
const middlewares = procedure["~orpc"].middlewares;
|
|
368
|
-
const inputValidationIndex = Math.min(
|
|
369
|
-
Math.max(0, procedure["~orpc"].inputValidationIndex),
|
|
370
|
-
middlewares.length
|
|
371
|
-
);
|
|
372
|
-
const outputValidationIndex = Math.min(
|
|
373
|
-
Math.max(0, procedure["~orpc"].outputValidationIndex),
|
|
374
|
-
middlewares.length
|
|
375
|
-
);
|
|
376
|
-
const next = async (index, context, input2) => {
|
|
377
|
-
let currentInput = input2;
|
|
378
|
-
if (index === inputValidationIndex) {
|
|
379
|
-
currentInput = await validateInput(procedure, currentInput);
|
|
380
|
-
}
|
|
381
|
-
const mid = middlewares[index];
|
|
382
|
-
const output = mid ? await runWithSpan({ name: `middleware.${mid.name}`, signal: options.signal }, async (span) => {
|
|
383
|
-
span?.setAttribute("middleware.index", index);
|
|
384
|
-
span?.setAttribute("middleware.name", mid.name);
|
|
385
|
-
const result = await mid(
|
|
386
|
-
{
|
|
387
|
-
...options,
|
|
388
|
-
context,
|
|
389
|
-
next: async (...[nextOptions]) => {
|
|
390
|
-
const nextContext = nextOptions?.context ?? {};
|
|
391
|
-
return {
|
|
392
|
-
output: await next(index + 1, mergeCurrentContext(context, nextContext), currentInput),
|
|
393
|
-
context: nextContext
|
|
394
|
-
};
|
|
395
|
-
}
|
|
396
|
-
},
|
|
397
|
-
currentInput,
|
|
398
|
-
middlewareOutputFn
|
|
399
|
-
);
|
|
400
|
-
return result.output;
|
|
401
|
-
}) : await runWithSpan(
|
|
402
|
-
{ name: "handler", signal: options.signal },
|
|
403
|
-
() => procedure["~orpc"].handler(currentInput, { ...options, context })
|
|
404
|
-
);
|
|
405
|
-
if (index === outputValidationIndex) {
|
|
406
|
-
return await validateOutput(procedure, output);
|
|
407
|
-
}
|
|
408
|
-
return output;
|
|
409
|
-
};
|
|
410
|
-
return next(0, options.context, input);
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
export { LAZY_SYMBOL as L, Procedure as P, addMiddleware as a, isLazy as b, createProcedureClient as c, getRouter as d, enhanceRouter as e, createORPCErrorConstructorMap as f, getLazyMeta as g, lazy as h, isProcedure as i, middlewareOutputFn as j, isStartWithMiddlewares as k, lazyInternal as l, mergeCurrentContext as m, mergeMiddlewares as n, getHiddenRouterContract as o, createAccessibleLazyRouter as p, unlazyRouter as q, resolveContractProcedures as r, setHiddenRouterContract as s, traverseContractProcedures as t, unlazy as u };
|