@orpc/server 0.0.0-next.93e7a4c → 0.0.0-next.954e6e2
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 +118 -0
- package/dist/adapters/fetch/index.d.mts +27 -0
- package/dist/adapters/fetch/index.d.ts +27 -0
- package/dist/adapters/fetch/index.mjs +9 -0
- package/dist/adapters/hono/index.d.mts +20 -0
- package/dist/adapters/hono/index.d.ts +20 -0
- package/dist/adapters/hono/index.mjs +32 -0
- package/dist/adapters/next/index.d.mts +27 -0
- package/dist/adapters/next/index.d.ts +27 -0
- package/dist/adapters/next/index.mjs +29 -0
- package/dist/adapters/node/index.d.mts +35 -0
- package/dist/adapters/node/index.d.ts +35 -0
- package/dist/adapters/node/index.mjs +30 -0
- package/dist/adapters/standard/index.d.mts +29 -0
- package/dist/adapters/standard/index.d.ts +29 -0
- package/dist/adapters/standard/index.mjs +7 -0
- package/dist/index.d.mts +255 -0
- package/dist/index.d.ts +255 -0
- package/dist/index.mjs +333 -0
- 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.B-ewprcf.d.ts +77 -0
- package/dist/shared/server.BBGuTxHE.mjs +163 -0
- package/dist/shared/server.CA-o8cUY.d.mts +9 -0
- package/dist/shared/server.Cn9ybJtE.d.mts +152 -0
- package/dist/shared/server.Cn9ybJtE.d.ts +152 -0
- package/dist/shared/server.DJrh0Ceu.d.mts +77 -0
- package/dist/shared/server.DPQt9YYq.d.ts +9 -0
- package/dist/shared/server.KwueCzFr.mjs +26 -0
- package/dist/shared/server.Q6ZmnTgO.mjs +12 -0
- package/dist/shared/server.V6zT5iYQ.mjs +379 -0
- package/package.json +43 -15
- package/dist/chunk-FN62GL22.js +0 -182
- package/dist/fetch.js +0 -286
- package/dist/index.js +0 -518
- package/dist/src/builder.d.ts +0 -35
- package/dist/src/fetch/composite-handler.d.ts +0 -8
- package/dist/src/fetch/index.d.ts +0 -6
- package/dist/src/fetch/orpc-handler.d.ts +0 -20
- package/dist/src/fetch/orpc-payload-codec.d.ts +0 -9
- package/dist/src/fetch/orpc-procedure-matcher.d.ts +0 -12
- package/dist/src/fetch/super-json.d.ts +0 -12
- package/dist/src/fetch/types.d.ts +0 -16
- package/dist/src/hidden.d.ts +0 -6
- package/dist/src/implementer-chainable.d.ts +0 -10
- package/dist/src/index.d.ts +0 -23
- package/dist/src/lazy-decorated.d.ts +0 -10
- package/dist/src/lazy-utils.d.ts +0 -4
- package/dist/src/lazy.d.ts +0 -18
- package/dist/src/middleware-decorated.d.ts +0 -8
- package/dist/src/middleware.d.ts +0 -23
- package/dist/src/procedure-builder.d.ts +0 -22
- package/dist/src/procedure-client.d.ts +0 -29
- package/dist/src/procedure-decorated.d.ts +0 -14
- package/dist/src/procedure-implementer.d.ts +0 -18
- package/dist/src/procedure.d.ts +0 -23
- package/dist/src/router-builder.d.ts +0 -29
- package/dist/src/router-client.d.ts +0 -25
- package/dist/src/router-implementer.d.ts +0 -21
- package/dist/src/router.d.ts +0 -16
- package/dist/src/types.d.ts +0 -12
- package/dist/src/utils.d.ts +0 -3
@@ -0,0 +1,379 @@
|
|
1
|
+
import { fallbackORPCErrorStatus, ORPCError } from '@orpc/client';
|
2
|
+
import { isContractProcedure, ValidationError, mergePrefix, mergeErrorMap, adaptRoute } from '@orpc/contract';
|
3
|
+
import { value, intercept, toError } from '@orpc/shared';
|
4
|
+
|
5
|
+
const LAZY_LOADER_SYMBOL = Symbol("ORPC_LAZY_LOADER");
|
6
|
+
function lazy(loader) {
|
7
|
+
return {
|
8
|
+
[LAZY_LOADER_SYMBOL]: loader
|
9
|
+
};
|
10
|
+
}
|
11
|
+
function isLazy(item) {
|
12
|
+
return (typeof item === "object" || typeof item === "function") && item !== null && LAZY_LOADER_SYMBOL in item && typeof item[LAZY_LOADER_SYMBOL] === "function";
|
13
|
+
}
|
14
|
+
function unlazy(lazied) {
|
15
|
+
return isLazy(lazied) ? lazied[LAZY_LOADER_SYMBOL]() : Promise.resolve({ default: lazied });
|
16
|
+
}
|
17
|
+
|
18
|
+
class Procedure {
|
19
|
+
"~orpc";
|
20
|
+
constructor(def) {
|
21
|
+
this["~orpc"] = def;
|
22
|
+
}
|
23
|
+
}
|
24
|
+
function isProcedure(item) {
|
25
|
+
if (item instanceof Procedure) {
|
26
|
+
return true;
|
27
|
+
}
|
28
|
+
return isContractProcedure(item) && "middlewares" in item["~orpc"] && "inputValidationIndex" in item["~orpc"] && "outputValidationIndex" in item["~orpc"] && "handler" in item["~orpc"];
|
29
|
+
}
|
30
|
+
|
31
|
+
function flatLazy(lazied) {
|
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);
|
43
|
+
}
|
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
|
+
function dedupeMiddlewares(compare, middlewares) {
|
60
|
+
let min = 0;
|
61
|
+
for (let i = 0; i < middlewares.length; i++) {
|
62
|
+
const index = compare.indexOf(middlewares[i], min);
|
63
|
+
if (index === -1) {
|
64
|
+
return middlewares.slice(i);
|
65
|
+
}
|
66
|
+
min = index + 1;
|
67
|
+
}
|
68
|
+
return [];
|
69
|
+
}
|
70
|
+
function mergeMiddlewares(first, second) {
|
71
|
+
return [...first, ...dedupeMiddlewares(first, second)];
|
72
|
+
}
|
73
|
+
function addMiddleware(middlewares, addition) {
|
74
|
+
return [...middlewares, addition];
|
75
|
+
}
|
76
|
+
|
77
|
+
function createORPCErrorConstructorMap(errors) {
|
78
|
+
const proxy = new Proxy(errors, {
|
79
|
+
get(target, code) {
|
80
|
+
if (typeof code !== "string") {
|
81
|
+
return Reflect.get(target, code);
|
82
|
+
}
|
83
|
+
const item = (...[options]) => {
|
84
|
+
const config = errors[code];
|
85
|
+
return new ORPCError(code, {
|
86
|
+
defined: Boolean(config),
|
87
|
+
status: config?.status,
|
88
|
+
message: options?.message ?? config?.message,
|
89
|
+
data: options?.data,
|
90
|
+
cause: options?.cause
|
91
|
+
});
|
92
|
+
};
|
93
|
+
return item;
|
94
|
+
}
|
95
|
+
});
|
96
|
+
return proxy;
|
97
|
+
}
|
98
|
+
async function validateORPCError(map, error) {
|
99
|
+
const { code, status, message, data, cause, defined } = error;
|
100
|
+
const config = map?.[error.code];
|
101
|
+
if (!config || fallbackORPCErrorStatus(error.code, config.status) !== error.status) {
|
102
|
+
return defined ? new ORPCError(code, { defined: false, status, message, data, cause }) : error;
|
103
|
+
}
|
104
|
+
if (!config.data) {
|
105
|
+
return defined ? error : new ORPCError(code, { defined: true, status, message, data, cause });
|
106
|
+
}
|
107
|
+
const validated = await config.data["~standard"].validate(error.data);
|
108
|
+
if (validated.issues) {
|
109
|
+
return defined ? new ORPCError(code, { defined: false, status, message, data, cause }) : error;
|
110
|
+
}
|
111
|
+
return new ORPCError(code, { defined: true, status, message, data: validated.value, cause });
|
112
|
+
}
|
113
|
+
|
114
|
+
function middlewareOutputFn(output) {
|
115
|
+
return { output, context: {} };
|
116
|
+
}
|
117
|
+
|
118
|
+
function createProcedureClient(lazyableProcedure, ...[options]) {
|
119
|
+
return async (...[input, callerOptions]) => {
|
120
|
+
const path = options?.path ?? [];
|
121
|
+
const { default: procedure } = await unlazy(lazyableProcedure);
|
122
|
+
const clientContext = callerOptions?.context ?? {};
|
123
|
+
const context = await value(options?.context ?? {}, clientContext);
|
124
|
+
const errors = createORPCErrorConstructorMap(procedure["~orpc"].errorMap);
|
125
|
+
try {
|
126
|
+
return await intercept(
|
127
|
+
options?.interceptors ?? [],
|
128
|
+
{
|
129
|
+
context,
|
130
|
+
input,
|
131
|
+
// input only optional when it undefinable so we can safely cast it
|
132
|
+
errors,
|
133
|
+
path,
|
134
|
+
procedure,
|
135
|
+
signal: callerOptions?.signal,
|
136
|
+
lastEventId: callerOptions?.lastEventId
|
137
|
+
},
|
138
|
+
(interceptorOptions) => executeProcedureInternal(interceptorOptions.procedure, interceptorOptions)
|
139
|
+
);
|
140
|
+
} catch (e) {
|
141
|
+
if (!(e instanceof ORPCError)) {
|
142
|
+
throw toError(e);
|
143
|
+
}
|
144
|
+
const validated = await validateORPCError(procedure["~orpc"].errorMap, e);
|
145
|
+
throw validated;
|
146
|
+
}
|
147
|
+
};
|
148
|
+
}
|
149
|
+
async function validateInput(procedure, input) {
|
150
|
+
const schema = procedure["~orpc"].inputSchema;
|
151
|
+
if (!schema) {
|
152
|
+
return input;
|
153
|
+
}
|
154
|
+
const result = await schema["~standard"].validate(input);
|
155
|
+
if (result.issues) {
|
156
|
+
throw new ORPCError("BAD_REQUEST", {
|
157
|
+
message: "Input validation failed",
|
158
|
+
data: {
|
159
|
+
issues: result.issues
|
160
|
+
},
|
161
|
+
cause: new ValidationError({ message: "Input validation failed", issues: result.issues })
|
162
|
+
});
|
163
|
+
}
|
164
|
+
return result.value;
|
165
|
+
}
|
166
|
+
async function validateOutput(procedure, output) {
|
167
|
+
const schema = procedure["~orpc"].outputSchema;
|
168
|
+
if (!schema) {
|
169
|
+
return output;
|
170
|
+
}
|
171
|
+
const result = await schema["~standard"].validate(output);
|
172
|
+
if (result.issues) {
|
173
|
+
throw new ORPCError("INTERNAL_SERVER_ERROR", {
|
174
|
+
message: "Output validation failed",
|
175
|
+
cause: new ValidationError({ message: "Output validation failed", issues: result.issues })
|
176
|
+
});
|
177
|
+
}
|
178
|
+
return result.value;
|
179
|
+
}
|
180
|
+
async function executeProcedureInternal(procedure, options) {
|
181
|
+
const middlewares = procedure["~orpc"].middlewares;
|
182
|
+
const inputValidationIndex = Math.min(Math.max(0, procedure["~orpc"].inputValidationIndex), middlewares.length);
|
183
|
+
const outputValidationIndex = Math.min(Math.max(0, procedure["~orpc"].outputValidationIndex), middlewares.length);
|
184
|
+
let currentIndex = 0;
|
185
|
+
let currentContext = options.context;
|
186
|
+
let currentInput = options.input;
|
187
|
+
const next = async (...[nextOptions]) => {
|
188
|
+
const index = currentIndex;
|
189
|
+
currentIndex += 1;
|
190
|
+
currentContext = { ...currentContext, ...nextOptions?.context };
|
191
|
+
if (index === inputValidationIndex) {
|
192
|
+
currentInput = await validateInput(procedure, currentInput);
|
193
|
+
}
|
194
|
+
const mid = middlewares[index];
|
195
|
+
const result = mid ? await mid({ ...options, context: currentContext, next }, currentInput, middlewareOutputFn) : { output: await procedure["~orpc"].handler({ ...options, context: currentContext, input: currentInput }), context: currentContext };
|
196
|
+
if (index === outputValidationIndex) {
|
197
|
+
const validatedOutput = await validateOutput(procedure, result.output);
|
198
|
+
return {
|
199
|
+
...result,
|
200
|
+
output: validatedOutput
|
201
|
+
};
|
202
|
+
}
|
203
|
+
return result;
|
204
|
+
};
|
205
|
+
return (await next({})).output;
|
206
|
+
}
|
207
|
+
|
208
|
+
const ROUTER_CONTRACT_SYMBOL = Symbol("ORPC_ROUTER_CONTRACT");
|
209
|
+
function setRouterContract(obj, contract) {
|
210
|
+
return new Proxy(obj, {
|
211
|
+
get(target, key) {
|
212
|
+
if (key === ROUTER_CONTRACT_SYMBOL) {
|
213
|
+
return contract;
|
214
|
+
}
|
215
|
+
return Reflect.get(target, key);
|
216
|
+
}
|
217
|
+
});
|
218
|
+
}
|
219
|
+
function getRouterContract(obj) {
|
220
|
+
return obj[ROUTER_CONTRACT_SYMBOL];
|
221
|
+
}
|
222
|
+
const LAZY_ROUTER_PREFIX_SYMBOL = Symbol("ORPC_LAZY_ROUTER_PREFIX");
|
223
|
+
function deepSetLazyRouterPrefix(router, prefix) {
|
224
|
+
return new Proxy(router, {
|
225
|
+
get(target, key) {
|
226
|
+
if (key !== LAZY_ROUTER_PREFIX_SYMBOL) {
|
227
|
+
const val = Reflect.get(target, key);
|
228
|
+
if (isLazy(val)) {
|
229
|
+
return deepSetLazyRouterPrefix(val, prefix);
|
230
|
+
}
|
231
|
+
return val;
|
232
|
+
}
|
233
|
+
return prefix;
|
234
|
+
}
|
235
|
+
});
|
236
|
+
}
|
237
|
+
function getLazyRouterPrefix(obj) {
|
238
|
+
return obj[LAZY_ROUTER_PREFIX_SYMBOL];
|
239
|
+
}
|
240
|
+
|
241
|
+
function createAccessibleLazyRouter(lazied) {
|
242
|
+
const flattenLazy = flatLazy(lazied);
|
243
|
+
const recursive = new Proxy(flattenLazy, {
|
244
|
+
get(target, key) {
|
245
|
+
if (typeof key !== "string") {
|
246
|
+
return Reflect.get(target, key);
|
247
|
+
}
|
248
|
+
const next = getRouterChild(flattenLazy, key);
|
249
|
+
return createAccessibleLazyRouter(next);
|
250
|
+
}
|
251
|
+
});
|
252
|
+
return recursive;
|
253
|
+
}
|
254
|
+
|
255
|
+
function adaptRouter(router, options) {
|
256
|
+
if (isLazy(router)) {
|
257
|
+
const adapted2 = lazy(async () => {
|
258
|
+
const unlaziedRouter = (await unlazy(router)).default;
|
259
|
+
const adapted3 = adaptRouter(unlaziedRouter, options);
|
260
|
+
return { default: adapted3 };
|
261
|
+
});
|
262
|
+
const accessible = createAccessibleLazyRouter(adapted2);
|
263
|
+
const currentPrefix = getLazyRouterPrefix(router);
|
264
|
+
const prefix = currentPrefix ? mergePrefix(options.prefix, currentPrefix) : options.prefix;
|
265
|
+
if (prefix) {
|
266
|
+
return deepSetLazyRouterPrefix(accessible, prefix);
|
267
|
+
}
|
268
|
+
return accessible;
|
269
|
+
}
|
270
|
+
if (isProcedure(router)) {
|
271
|
+
const newMiddlewares = mergeMiddlewares(options.middlewares, router["~orpc"].middlewares);
|
272
|
+
const newMiddlewareAdded = newMiddlewares.length - router["~orpc"].middlewares.length;
|
273
|
+
const adapted2 = new Procedure({
|
274
|
+
...router["~orpc"],
|
275
|
+
route: adaptRoute(router["~orpc"].route, options),
|
276
|
+
errorMap: mergeErrorMap(options.errorMap, router["~orpc"].errorMap),
|
277
|
+
middlewares: newMiddlewares,
|
278
|
+
inputValidationIndex: router["~orpc"].inputValidationIndex + newMiddlewareAdded,
|
279
|
+
outputValidationIndex: router["~orpc"].outputValidationIndex + newMiddlewareAdded
|
280
|
+
});
|
281
|
+
return adapted2;
|
282
|
+
}
|
283
|
+
const adapted = {};
|
284
|
+
for (const key in router) {
|
285
|
+
adapted[key] = adaptRouter(router[key], options);
|
286
|
+
}
|
287
|
+
return adapted;
|
288
|
+
}
|
289
|
+
function getRouterChild(router, ...path) {
|
290
|
+
let current = router;
|
291
|
+
for (let i = 0; i < path.length; i++) {
|
292
|
+
const segment = path[i];
|
293
|
+
if (!current) {
|
294
|
+
return void 0;
|
295
|
+
}
|
296
|
+
if (isProcedure(current)) {
|
297
|
+
return void 0;
|
298
|
+
}
|
299
|
+
if (!isLazy(current)) {
|
300
|
+
current = current[segment];
|
301
|
+
continue;
|
302
|
+
}
|
303
|
+
const lazied = current;
|
304
|
+
const rest = path.slice(i);
|
305
|
+
const newLazy = lazy(async () => {
|
306
|
+
const unwrapped = await unlazy(lazied);
|
307
|
+
if (!unwrapped.default) {
|
308
|
+
return unwrapped;
|
309
|
+
}
|
310
|
+
const next = getRouterChild(unwrapped.default, ...rest);
|
311
|
+
return { default: next };
|
312
|
+
});
|
313
|
+
return flatLazy(newLazy);
|
314
|
+
}
|
315
|
+
return current;
|
316
|
+
}
|
317
|
+
|
318
|
+
function eachContractProcedure(options, callback, laziedOptions = []) {
|
319
|
+
const hiddenContract = getRouterContract(options.router);
|
320
|
+
if (hiddenContract) {
|
321
|
+
return eachContractProcedure(
|
322
|
+
{
|
323
|
+
router: hiddenContract,
|
324
|
+
path: options.path
|
325
|
+
},
|
326
|
+
callback,
|
327
|
+
laziedOptions
|
328
|
+
);
|
329
|
+
}
|
330
|
+
if (isLazy(options.router)) {
|
331
|
+
laziedOptions.push({
|
332
|
+
lazied: options.router,
|
333
|
+
path: options.path
|
334
|
+
});
|
335
|
+
} else if (isContractProcedure(options.router)) {
|
336
|
+
callback({
|
337
|
+
contract: options.router,
|
338
|
+
path: options.path
|
339
|
+
});
|
340
|
+
} else {
|
341
|
+
for (const key in options.router) {
|
342
|
+
eachContractProcedure(
|
343
|
+
{
|
344
|
+
router: options.router[key],
|
345
|
+
path: [...options.path, key]
|
346
|
+
},
|
347
|
+
callback,
|
348
|
+
laziedOptions
|
349
|
+
);
|
350
|
+
}
|
351
|
+
}
|
352
|
+
return laziedOptions;
|
353
|
+
}
|
354
|
+
async function eachAllContractProcedure(options, callback) {
|
355
|
+
const pending = [options];
|
356
|
+
for (const item of pending) {
|
357
|
+
const lazies = eachContractProcedure(item, callback);
|
358
|
+
for (const lazy of lazies) {
|
359
|
+
const { default: router } = await unlazy(lazy.lazied);
|
360
|
+
pending.push({
|
361
|
+
path: lazy.path,
|
362
|
+
router
|
363
|
+
});
|
364
|
+
}
|
365
|
+
}
|
366
|
+
}
|
367
|
+
function convertPathToHttpPath(path) {
|
368
|
+
return `/${path.map(encodeURIComponent).join("/")}`;
|
369
|
+
}
|
370
|
+
function createContractedProcedure(contract, procedure) {
|
371
|
+
return new Procedure({
|
372
|
+
...procedure["~orpc"],
|
373
|
+
errorMap: contract["~orpc"].errorMap,
|
374
|
+
route: contract["~orpc"].route,
|
375
|
+
meta: contract["~orpc"].meta
|
376
|
+
});
|
377
|
+
}
|
378
|
+
|
379
|
+
export { LAZY_LOADER_SYMBOL as L, Procedure as P, convertPathToHttpPath as a, createContractedProcedure as b, createProcedureClient as c, addMiddleware as d, eachContractProcedure as e, adaptRouter as f, getRouterChild as g, flatLazy as h, isProcedure as i, isLazy as j, createLazyProcedureFormAnyLazy as k, lazy as l, getRouterContract as m, deepSetLazyRouterPrefix as n, getLazyRouterPrefix as o, middlewareOutputFn as p, createAccessibleLazyRouter as q, eachAllContractProcedure as r, setRouterContract as s, unlazy as u };
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "@orpc/server",
|
3
3
|
"type": "module",
|
4
|
-
"version": "0.0.0-next.
|
4
|
+
"version": "0.0.0-next.954e6e2",
|
5
5
|
"license": "MIT",
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
7
7
|
"repository": {
|
@@ -15,33 +15,61 @@
|
|
15
15
|
],
|
16
16
|
"exports": {
|
17
17
|
".": {
|
18
|
-
"types": "./dist/
|
19
|
-
"import": "./dist/index.
|
20
|
-
"default": "./dist/index.
|
18
|
+
"types": "./dist/index.d.mts",
|
19
|
+
"import": "./dist/index.mjs",
|
20
|
+
"default": "./dist/index.mjs"
|
21
|
+
},
|
22
|
+
"./plugins": {
|
23
|
+
"types": "./dist/plugins/index.d.mts",
|
24
|
+
"import": "./dist/plugins/index.mjs",
|
25
|
+
"default": "./dist/plugins/index.mjs"
|
26
|
+
},
|
27
|
+
"./standard": {
|
28
|
+
"types": "./dist/adapters/standard/index.d.mts",
|
29
|
+
"import": "./dist/adapters/standard/index.mjs",
|
30
|
+
"default": "./dist/adapters/standard/index.mjs"
|
21
31
|
},
|
22
32
|
"./fetch": {
|
23
|
-
"types": "./dist/
|
24
|
-
"import": "./dist/fetch.
|
25
|
-
"default": "./dist/fetch.
|
33
|
+
"types": "./dist/adapters/fetch/index.d.mts",
|
34
|
+
"import": "./dist/adapters/fetch/index.mjs",
|
35
|
+
"default": "./dist/adapters/fetch/index.mjs"
|
36
|
+
},
|
37
|
+
"./hono": {
|
38
|
+
"types": "./dist/adapters/hono/index.d.mts",
|
39
|
+
"import": "./dist/adapters/hono/index.mjs",
|
40
|
+
"default": "./dist/adapters/hono/index.mjs"
|
26
41
|
},
|
27
|
-
"
|
28
|
-
"types": "./dist/
|
42
|
+
"./next": {
|
43
|
+
"types": "./dist/adapters/next/index.d.mts",
|
44
|
+
"import": "./dist/adapters/next/index.mjs",
|
45
|
+
"default": "./dist/adapters/next/index.mjs"
|
46
|
+
},
|
47
|
+
"./node": {
|
48
|
+
"types": "./dist/adapters/node/index.d.mts",
|
49
|
+
"import": "./dist/adapters/node/index.mjs",
|
50
|
+
"default": "./dist/adapters/node/index.mjs"
|
29
51
|
}
|
30
52
|
},
|
31
53
|
"files": [
|
32
|
-
"!**/*.map",
|
33
|
-
"!**/*.tsbuildinfo",
|
34
54
|
"dist"
|
35
55
|
],
|
56
|
+
"peerDependencies": {
|
57
|
+
"hono": ">=4.6.0",
|
58
|
+
"next": ">=14.0.0"
|
59
|
+
},
|
36
60
|
"dependencies": {
|
37
|
-
"@orpc/
|
38
|
-
"@orpc/
|
61
|
+
"@orpc/client": "0.0.0-next.954e6e2",
|
62
|
+
"@orpc/contract": "0.0.0-next.954e6e2",
|
63
|
+
"@orpc/shared": "0.0.0-next.954e6e2",
|
64
|
+
"@orpc/standard-server": "0.0.0-next.954e6e2",
|
65
|
+
"@orpc/standard-server-node": "0.0.0-next.954e6e2",
|
66
|
+
"@orpc/standard-server-fetch": "0.0.0-next.954e6e2"
|
39
67
|
},
|
40
68
|
"devDependencies": {
|
41
|
-
"
|
69
|
+
"light-my-request": "^6.5.1"
|
42
70
|
},
|
43
71
|
"scripts": {
|
44
|
-
"build": "
|
72
|
+
"build": "unbuild",
|
45
73
|
"build:watch": "pnpm run build --watch",
|
46
74
|
"type:check": "tsc -b"
|
47
75
|
}
|
package/dist/chunk-FN62GL22.js
DELETED
@@ -1,182 +0,0 @@
|
|
1
|
-
// src/utils.ts
|
2
|
-
function mergeContext(a, b) {
|
3
|
-
if (!a)
|
4
|
-
return b;
|
5
|
-
if (!b)
|
6
|
-
return a;
|
7
|
-
return {
|
8
|
-
...a,
|
9
|
-
...b
|
10
|
-
};
|
11
|
-
}
|
12
|
-
|
13
|
-
// src/procedure.ts
|
14
|
-
import { isContractProcedure } from "@orpc/contract";
|
15
|
-
var Procedure = class {
|
16
|
-
"~type" = "Procedure";
|
17
|
-
"~orpc";
|
18
|
-
constructor(def) {
|
19
|
-
this["~orpc"] = def;
|
20
|
-
}
|
21
|
-
};
|
22
|
-
function isProcedure(item) {
|
23
|
-
if (item instanceof Procedure) {
|
24
|
-
return true;
|
25
|
-
}
|
26
|
-
return (typeof item === "object" || typeof item === "function") && item !== null && "~type" in item && item["~type"] === "Procedure" && "~orpc" in item && typeof item["~orpc"] === "object" && item["~orpc"] !== null && "contract" in item["~orpc"] && isContractProcedure(item["~orpc"].contract) && "func" in item["~orpc"] && typeof item["~orpc"].func === "function";
|
27
|
-
}
|
28
|
-
|
29
|
-
// src/lazy.ts
|
30
|
-
var LAZY_LOADER_SYMBOL = Symbol("ORPC_LAZY_LOADER");
|
31
|
-
function lazy(loader) {
|
32
|
-
return {
|
33
|
-
[LAZY_LOADER_SYMBOL]: loader
|
34
|
-
};
|
35
|
-
}
|
36
|
-
function isLazy(item) {
|
37
|
-
return (typeof item === "object" || typeof item === "function") && item !== null && LAZY_LOADER_SYMBOL in item && typeof item[LAZY_LOADER_SYMBOL] === "function";
|
38
|
-
}
|
39
|
-
function unlazy(lazied) {
|
40
|
-
return isLazy(lazied) ? lazied[LAZY_LOADER_SYMBOL]() : Promise.resolve({ default: lazied });
|
41
|
-
}
|
42
|
-
function flatLazy(lazied) {
|
43
|
-
const flattenLoader = async () => {
|
44
|
-
let current = await unlazy(lazied);
|
45
|
-
while (true) {
|
46
|
-
if (!isLazy(current.default)) {
|
47
|
-
break;
|
48
|
-
}
|
49
|
-
current = await unlazy(current.default);
|
50
|
-
}
|
51
|
-
return current;
|
52
|
-
};
|
53
|
-
return lazy(flattenLoader);
|
54
|
-
}
|
55
|
-
|
56
|
-
// src/procedure-client.ts
|
57
|
-
import { executeWithHooks, value } from "@orpc/shared";
|
58
|
-
import { ORPCError } from "@orpc/shared/error";
|
59
|
-
function createProcedureClient(options) {
|
60
|
-
return async (...[input, callerOptions]) => {
|
61
|
-
const path = options.path ?? [];
|
62
|
-
const { default: procedure } = await unlazy(options.procedure);
|
63
|
-
const context = await value(options.context);
|
64
|
-
const meta = {
|
65
|
-
path,
|
66
|
-
procedure,
|
67
|
-
signal: callerOptions?.signal
|
68
|
-
};
|
69
|
-
const executeWithValidation = async () => {
|
70
|
-
const validInput = await validateInput(procedure, input);
|
71
|
-
const output = await executeMiddlewareChain(
|
72
|
-
procedure,
|
73
|
-
validInput,
|
74
|
-
context,
|
75
|
-
meta
|
76
|
-
);
|
77
|
-
return validateOutput(procedure, output);
|
78
|
-
};
|
79
|
-
return executeWithHooks({
|
80
|
-
hooks: options,
|
81
|
-
input,
|
82
|
-
context,
|
83
|
-
meta,
|
84
|
-
execute: executeWithValidation
|
85
|
-
});
|
86
|
-
};
|
87
|
-
}
|
88
|
-
async function validateInput(procedure, input) {
|
89
|
-
const schema = procedure["~orpc"].contract["~orpc"].InputSchema;
|
90
|
-
if (!schema)
|
91
|
-
return input;
|
92
|
-
const result = await schema["~standard"].validate(input);
|
93
|
-
if (result.issues) {
|
94
|
-
throw new ORPCError({
|
95
|
-
message: "Input validation failed",
|
96
|
-
code: "BAD_REQUEST",
|
97
|
-
issues: result.issues
|
98
|
-
});
|
99
|
-
}
|
100
|
-
return result.value;
|
101
|
-
}
|
102
|
-
async function validateOutput(procedure, output) {
|
103
|
-
const schema = procedure["~orpc"].contract["~orpc"].OutputSchema;
|
104
|
-
if (!schema)
|
105
|
-
return output;
|
106
|
-
const result = await schema["~standard"].validate(output);
|
107
|
-
if (result.issues) {
|
108
|
-
throw new ORPCError({
|
109
|
-
message: "Output validation failed",
|
110
|
-
code: "INTERNAL_SERVER_ERROR",
|
111
|
-
issues: result.issues
|
112
|
-
});
|
113
|
-
}
|
114
|
-
return result.value;
|
115
|
-
}
|
116
|
-
async function executeMiddlewareChain(procedure, input, context, meta) {
|
117
|
-
const middlewares = procedure["~orpc"].middlewares ?? [];
|
118
|
-
let currentMidIndex = 0;
|
119
|
-
let currentContext = context;
|
120
|
-
const next = async (nextOptions) => {
|
121
|
-
const mid = middlewares[currentMidIndex];
|
122
|
-
currentMidIndex += 1;
|
123
|
-
currentContext = mergeContext(currentContext, nextOptions.context);
|
124
|
-
if (mid) {
|
125
|
-
return await mid(input, currentContext, {
|
126
|
-
...meta,
|
127
|
-
next,
|
128
|
-
output: (output) => ({ output, context: void 0 })
|
129
|
-
});
|
130
|
-
}
|
131
|
-
const result = {
|
132
|
-
output: await procedure["~orpc"].func(input, currentContext, meta),
|
133
|
-
context: currentContext
|
134
|
-
};
|
135
|
-
return result;
|
136
|
-
};
|
137
|
-
return (await next({})).output;
|
138
|
-
}
|
139
|
-
|
140
|
-
// src/router.ts
|
141
|
-
function getRouterChild(router, ...path) {
|
142
|
-
let current = router;
|
143
|
-
for (let i = 0; i < path.length; i++) {
|
144
|
-
const segment = path[i];
|
145
|
-
if (!current) {
|
146
|
-
return void 0;
|
147
|
-
}
|
148
|
-
if (isProcedure(current)) {
|
149
|
-
return void 0;
|
150
|
-
}
|
151
|
-
if (!isLazy(current)) {
|
152
|
-
current = current[segment];
|
153
|
-
continue;
|
154
|
-
}
|
155
|
-
const lazied = current;
|
156
|
-
const rest = path.slice(i);
|
157
|
-
const newLazy = lazy(async () => {
|
158
|
-
const unwrapped = await unlazy(lazied);
|
159
|
-
if (!unwrapped.default) {
|
160
|
-
return unwrapped;
|
161
|
-
}
|
162
|
-
const next = getRouterChild(unwrapped.default, ...rest);
|
163
|
-
return { default: next };
|
164
|
-
});
|
165
|
-
return flatLazy(newLazy);
|
166
|
-
}
|
167
|
-
return current;
|
168
|
-
}
|
169
|
-
|
170
|
-
export {
|
171
|
-
mergeContext,
|
172
|
-
Procedure,
|
173
|
-
isProcedure,
|
174
|
-
LAZY_LOADER_SYMBOL,
|
175
|
-
lazy,
|
176
|
-
isLazy,
|
177
|
-
unlazy,
|
178
|
-
flatLazy,
|
179
|
-
createProcedureClient,
|
180
|
-
getRouterChild
|
181
|
-
};
|
182
|
-
//# sourceMappingURL=chunk-FN62GL22.js.map
|