@orpc/server 0.0.0-next.3826d73 → 0.0.0-next.3afb521

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.
Files changed (80) hide show
  1. package/README.md +118 -0
  2. package/dist/adapters/fetch/index.d.mts +26 -0
  3. package/dist/adapters/fetch/index.d.ts +26 -0
  4. package/dist/adapters/fetch/index.mjs +9 -0
  5. package/dist/adapters/hono/index.d.mts +19 -0
  6. package/dist/adapters/hono/index.d.ts +19 -0
  7. package/dist/{hono.js → adapters/hono/index.mjs} +11 -13
  8. package/dist/adapters/next/index.d.mts +26 -0
  9. package/dist/adapters/next/index.d.ts +26 -0
  10. package/dist/{next.js → adapters/next/index.mjs} +11 -13
  11. package/dist/adapters/node/index.d.mts +34 -0
  12. package/dist/adapters/node/index.d.ts +34 -0
  13. package/dist/adapters/node/index.mjs +31 -0
  14. package/dist/adapters/standard/index.d.mts +25 -0
  15. package/dist/adapters/standard/index.d.ts +25 -0
  16. package/dist/adapters/standard/index.mjs +6 -0
  17. package/dist/index.d.mts +264 -0
  18. package/dist/index.d.ts +264 -0
  19. package/dist/{index.js → index.mjs} +54 -131
  20. package/dist/plugins/index.d.mts +31 -0
  21. package/dist/plugins/index.d.ts +31 -0
  22. package/dist/{chunk-XI6WGCB3.js → plugins/index.mjs} +16 -38
  23. package/dist/shared/server.3cSam35R.mjs +158 -0
  24. package/dist/{chunk-XXBE6CYD.js → shared/server.BFBhsdJr.mjs} +159 -193
  25. package/dist/shared/server.C99bm-AA.d.mts +143 -0
  26. package/dist/shared/server.C99bm-AA.d.ts +143 -0
  27. package/dist/shared/server.CjB_m7jG.mjs +28 -0
  28. package/dist/shared/server.Q6ZmnTgO.mjs +12 -0
  29. package/dist/shared/server.SegZfF38.d.ts +77 -0
  30. package/dist/shared/server.rcYTv2pz.d.mts +77 -0
  31. package/package.json +29 -33
  32. package/dist/chunk-L4ESXQX7.js +0 -32
  33. package/dist/chunk-NXEANHUK.js +0 -351
  34. package/dist/fetch.js +0 -10
  35. package/dist/node.js +0 -31
  36. package/dist/plugins.js +0 -11
  37. package/dist/src/adapters/fetch/index.d.ts +0 -3
  38. package/dist/src/adapters/fetch/rpc-handler.d.ts +0 -11
  39. package/dist/src/adapters/fetch/types.d.ts +0 -14
  40. package/dist/src/adapters/hono/index.d.ts +0 -3
  41. package/dist/src/adapters/hono/middleware.d.ts +0 -12
  42. package/dist/src/adapters/next/index.d.ts +0 -3
  43. package/dist/src/adapters/next/serve.d.ts +0 -19
  44. package/dist/src/adapters/node/index.d.ts +0 -3
  45. package/dist/src/adapters/node/rpc-handler.d.ts +0 -11
  46. package/dist/src/adapters/node/types.d.ts +0 -22
  47. package/dist/src/adapters/standard/handler.d.ts +0 -51
  48. package/dist/src/adapters/standard/index.d.ts +0 -7
  49. package/dist/src/adapters/standard/rpc-codec.d.ts +0 -16
  50. package/dist/src/adapters/standard/rpc-handler.d.ts +0 -8
  51. package/dist/src/adapters/standard/rpc-matcher.d.ts +0 -10
  52. package/dist/src/adapters/standard/rpc-serializer.d.ts +0 -22
  53. package/dist/src/adapters/standard/types.d.ts +0 -20
  54. package/dist/src/builder-variants.d.ts +0 -74
  55. package/dist/src/builder.d.ts +0 -57
  56. package/dist/src/config.d.ts +0 -6
  57. package/dist/src/context.d.ts +0 -8
  58. package/dist/src/hidden.d.ts +0 -8
  59. package/dist/src/implementer-procedure.d.ts +0 -31
  60. package/dist/src/implementer-variants.d.ts +0 -17
  61. package/dist/src/implementer.d.ts +0 -28
  62. package/dist/src/index.d.ts +0 -24
  63. package/dist/src/lazy-utils.d.ts +0 -6
  64. package/dist/src/lazy.d.ts +0 -22
  65. package/dist/src/middleware-decorated.d.ts +0 -10
  66. package/dist/src/middleware-utils.d.ts +0 -5
  67. package/dist/src/middleware.d.ts +0 -37
  68. package/dist/src/plugins/base.d.ts +0 -13
  69. package/dist/src/plugins/cors.d.ts +0 -19
  70. package/dist/src/plugins/index.d.ts +0 -4
  71. package/dist/src/plugins/response-headers.d.ts +0 -10
  72. package/dist/src/procedure-client.d.ts +0 -31
  73. package/dist/src/procedure-decorated.d.ts +0 -22
  74. package/dist/src/procedure-utils.d.ts +0 -18
  75. package/dist/src/procedure.d.ts +0 -30
  76. package/dist/src/router-accessible-lazy.d.ts +0 -8
  77. package/dist/src/router-client.d.ts +0 -11
  78. package/dist/src/router.d.ts +0 -30
  79. package/dist/src/utils.d.ts +0 -24
  80. package/dist/standard.js +0 -17
@@ -1,25 +1,50 @@
1
- // src/lazy.ts
2
- var LAZY_LOADER_SYMBOL = Symbol("ORPC_LAZY_LOADER");
3
- function lazy(loader) {
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
- [LAZY_LOADER_SYMBOL]: loader
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 && LAZY_LOADER_SYMBOL in item && typeof item[LAZY_LOADER_SYMBOL] === "function";
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[LAZY_LOADER_SYMBOL]() : Promise.resolve({ default: lazied });
21
+ return isLazy(lazied) ? lazied[LAZY_SYMBOL].loader() : Promise.resolve({ default: lazied });
13
22
  }
14
23
 
15
- // src/procedure.ts
16
- import { isContractProcedure } from "@orpc/contract";
17
- var Procedure = class {
24
+ function dedupeMiddlewares(compare, middlewares) {
25
+ let min = 0;
26
+ for (let i = 0; i < middlewares.length; i++) {
27
+ const index = compare.indexOf(middlewares[i], min);
28
+ if (index === -1) {
29
+ return middlewares.slice(i);
30
+ }
31
+ min = index + 1;
32
+ }
33
+ return [];
34
+ }
35
+ function mergeMiddlewares(first, second) {
36
+ return [...first, ...dedupeMiddlewares(first, second)];
37
+ }
38
+ function addMiddleware(middlewares, addition) {
39
+ return [...middlewares, addition];
40
+ }
41
+
42
+ class Procedure {
18
43
  "~orpc";
19
44
  constructor(def) {
20
45
  this["~orpc"] = def;
21
46
  }
22
- };
47
+ }
23
48
  function isProcedure(item) {
24
49
  if (item instanceof Procedure) {
25
50
  return true;
@@ -27,43 +52,47 @@ function isProcedure(item) {
27
52
  return isContractProcedure(item) && "middlewares" in item["~orpc"] && "inputValidationIndex" in item["~orpc"] && "outputValidationIndex" in item["~orpc"] && "handler" in item["~orpc"];
28
53
  }
29
54
 
30
- // src/lazy-utils.ts
31
- function flatLazy(lazied) {
32
- const flattenLoader = async () => {
33
- let current = await unlazy(lazied);
34
- while (true) {
35
- if (!isLazy(current.default)) {
36
- break;
55
+ function createORPCErrorConstructorMap(errors) {
56
+ const proxy = new Proxy(errors, {
57
+ get(target, code) {
58
+ if (typeof code !== "string") {
59
+ return Reflect.get(target, code);
37
60
  }
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
- `);
61
+ const item = (...[options]) => {
62
+ const config = errors[code];
63
+ return new ORPCError(code, {
64
+ defined: Boolean(config),
65
+ status: config?.status,
66
+ message: options?.message ?? config?.message,
67
+ data: options?.data,
68
+ cause: options?.cause
69
+ });
70
+ };
71
+ return item;
53
72
  }
54
- return { default: maybeProcedure };
55
73
  });
56
- return lazyProcedure;
74
+ return proxy;
75
+ }
76
+ async function validateORPCError(map, error) {
77
+ const { code, status, message, data, cause, defined } = error;
78
+ const config = map?.[error.code];
79
+ if (!config || fallbackORPCErrorStatus(error.code, config.status) !== error.status) {
80
+ return defined ? new ORPCError(code, { defined: false, status, message, data, cause }) : error;
81
+ }
82
+ if (!config.data) {
83
+ return defined ? error : new ORPCError(code, { defined: true, status, message, data, cause });
84
+ }
85
+ const validated = await config.data["~standard"].validate(error.data);
86
+ if (validated.issues) {
87
+ return defined ? new ORPCError(code, { defined: false, status, message, data, cause }) : error;
88
+ }
89
+ return new ORPCError(code, { defined: true, status, message, data: validated.value, cause });
57
90
  }
58
91
 
59
- // src/middleware.ts
60
92
  function middlewareOutputFn(output) {
61
93
  return { output, context: {} };
62
94
  }
63
95
 
64
- // src/procedure-client.ts
65
- import { createORPCErrorConstructorMap, ORPCError, validateORPCError, ValidationError } from "@orpc/contract";
66
- import { intercept, toError, value } from "@orpc/shared";
67
96
  function createProcedureClient(lazyableProcedure, ...[options]) {
68
97
  return async (...[input, callerOptions]) => {
69
98
  const path = options?.path ?? [];
@@ -154,98 +183,22 @@ async function executeProcedureInternal(procedure, options) {
154
183
  return (await next({})).output;
155
184
  }
156
185
 
157
- // src/hidden.ts
158
- var ROUTER_CONTRACT_SYMBOL = Symbol("ORPC_ROUTER_CONTRACT");
159
- function setRouterContract(obj, contract) {
160
- return new Proxy(obj, {
186
+ const HIDDEN_ROUTER_CONTRACT_SYMBOL = Symbol("ORPC_HIDDEN_ROUTER_CONTRACT");
187
+ function setHiddenRouterContract(router, contract) {
188
+ return new Proxy(router, {
161
189
  get(target, key) {
162
- if (key === ROUTER_CONTRACT_SYMBOL) {
190
+ if (key === HIDDEN_ROUTER_CONTRACT_SYMBOL) {
163
191
  return contract;
164
192
  }
165
193
  return Reflect.get(target, key);
166
194
  }
167
195
  });
168
196
  }
169
- function getRouterContract(obj) {
170
- return obj[ROUTER_CONTRACT_SYMBOL];
171
- }
172
- var LAZY_ROUTER_PREFIX_SYMBOL = Symbol("ORPC_LAZY_ROUTER_PREFIX");
173
- function deepSetLazyRouterPrefix(router, prefix) {
174
- return new Proxy(router, {
175
- get(target, key) {
176
- if (key !== LAZY_ROUTER_PREFIX_SYMBOL) {
177
- const val = Reflect.get(target, key);
178
- if (isLazy(val)) {
179
- return deepSetLazyRouterPrefix(val, prefix);
180
- }
181
- return val;
182
- }
183
- return prefix;
184
- }
185
- });
186
- }
187
- function getLazyRouterPrefix(obj) {
188
- return obj[LAZY_ROUTER_PREFIX_SYMBOL];
189
- }
190
-
191
- // src/router.ts
192
- import { adaptRoute, mergeErrorMap, mergePrefix } from "@orpc/contract";
193
-
194
- // src/middleware-utils.ts
195
- function dedupeMiddlewares(compare, middlewares) {
196
- let min = 0;
197
- for (let i = 0; i < middlewares.length; i++) {
198
- const index = compare.indexOf(middlewares[i], min);
199
- if (index === -1) {
200
- return middlewares.slice(i);
201
- }
202
- min = index + 1;
203
- }
204
- return [];
205
- }
206
- function mergeMiddlewares(first, second) {
207
- return [...first, ...dedupeMiddlewares(first, second)];
208
- }
209
- function addMiddleware(middlewares, addition) {
210
- return [...middlewares, addition];
197
+ function getHiddenRouterContract(router) {
198
+ return router[HIDDEN_ROUTER_CONTRACT_SYMBOL];
211
199
  }
212
200
 
213
- // src/router.ts
214
- function adaptRouter(router, options) {
215
- if (isLazy(router)) {
216
- const adapted2 = lazy(async () => {
217
- const unlaziedRouter = (await unlazy(router)).default;
218
- const adapted3 = adaptRouter(unlaziedRouter, options);
219
- return { default: adapted3 };
220
- });
221
- const accessible = createAccessibleLazyRouter(adapted2);
222
- const currentPrefix = getLazyRouterPrefix(router);
223
- const prefix = currentPrefix ? mergePrefix(options.prefix, currentPrefix) : options.prefix;
224
- if (prefix) {
225
- return deepSetLazyRouterPrefix(accessible, prefix);
226
- }
227
- return accessible;
228
- }
229
- if (isProcedure(router)) {
230
- const newMiddlewares = mergeMiddlewares(options.middlewares, router["~orpc"].middlewares);
231
- const newMiddlewareAdded = newMiddlewares.length - router["~orpc"].middlewares.length;
232
- const adapted2 = new Procedure({
233
- ...router["~orpc"],
234
- route: adaptRoute(router["~orpc"].route, options),
235
- errorMap: mergeErrorMap(options.errorMap, router["~orpc"].errorMap),
236
- middlewares: newMiddlewares,
237
- inputValidationIndex: router["~orpc"].inputValidationIndex + newMiddlewareAdded,
238
- outputValidationIndex: router["~orpc"].outputValidationIndex + newMiddlewareAdded
239
- });
240
- return adapted2;
241
- }
242
- const adapted = {};
243
- for (const key in router) {
244
- adapted[key] = adaptRouter(router[key], options);
245
- }
246
- return adapted;
247
- }
248
- function getRouterChild(router, ...path) {
201
+ function getRouter(router, path) {
249
202
  let current = router;
250
203
  for (let i = 0; i < path.length; i++) {
251
204
  const segment = path[i];
@@ -261,89 +214,119 @@ function getRouterChild(router, ...path) {
261
214
  }
262
215
  const lazied = current;
263
216
  const rest = path.slice(i);
264
- const newLazy = lazy(async () => {
217
+ return lazy(async () => {
265
218
  const unwrapped = await unlazy(lazied);
266
- if (!unwrapped.default) {
267
- return unwrapped;
268
- }
269
- const next = getRouterChild(unwrapped.default, ...rest);
270
- return { default: next };
271
- });
272
- return flatLazy(newLazy);
219
+ const next = getRouter(unwrapped.default, rest);
220
+ return unlazy(next);
221
+ }, getLazyMeta(lazied));
273
222
  }
274
223
  return current;
275
224
  }
276
-
277
- // src/router-accessible-lazy.ts
278
225
  function createAccessibleLazyRouter(lazied) {
279
- const flattenLazy = flatLazy(lazied);
280
- const recursive = new Proxy(flattenLazy, {
226
+ const recursive = new Proxy(lazied, {
281
227
  get(target, key) {
282
228
  if (typeof key !== "string") {
283
229
  return Reflect.get(target, key);
284
230
  }
285
- const next = getRouterChild(flattenLazy, key);
231
+ const next = getRouter(lazied, [key]);
286
232
  return createAccessibleLazyRouter(next);
287
233
  }
288
234
  });
289
235
  return recursive;
290
236
  }
291
-
292
- // src/utils.ts
293
- import { isContractProcedure as isContractProcedure2 } from "@orpc/contract";
294
- function eachContractProcedure(options, callback, laziedOptions = []) {
295
- const hiddenContract = getRouterContract(options.router);
296
- if (hiddenContract) {
297
- return eachContractProcedure(
298
- {
299
- router: hiddenContract,
300
- path: options.path
301
- },
302
- callback,
303
- laziedOptions
304
- );
237
+ function enhanceRouter(router, options) {
238
+ if (isLazy(router)) {
239
+ const laziedMeta = getLazyMeta(router);
240
+ const enhancedPrefix = laziedMeta?.prefix ? mergePrefix(options.prefix, laziedMeta?.prefix) : options.prefix;
241
+ const enhanced2 = lazy(async () => {
242
+ const { default: unlaziedRouter } = await unlazy(router);
243
+ const enhanced3 = enhanceRouter(unlaziedRouter, options);
244
+ return unlazy(enhanced3);
245
+ }, {
246
+ ...laziedMeta,
247
+ prefix: enhancedPrefix
248
+ });
249
+ const accessible = createAccessibleLazyRouter(enhanced2);
250
+ return accessible;
305
251
  }
306
- if (isLazy(options.router)) {
307
- laziedOptions.push({
308
- lazied: options.router,
252
+ if (isProcedure(router)) {
253
+ const newMiddlewares = mergeMiddlewares(options.middlewares, router["~orpc"].middlewares);
254
+ const newMiddlewareAdded = newMiddlewares.length - router["~orpc"].middlewares.length;
255
+ const enhanced2 = new Procedure({
256
+ ...router["~orpc"],
257
+ route: enhanceRoute(router["~orpc"].route, options),
258
+ errorMap: mergeErrorMap(options.errorMap, router["~orpc"].errorMap),
259
+ middlewares: newMiddlewares,
260
+ inputValidationIndex: router["~orpc"].inputValidationIndex + newMiddlewareAdded,
261
+ outputValidationIndex: router["~orpc"].outputValidationIndex + newMiddlewareAdded
262
+ });
263
+ return enhanced2;
264
+ }
265
+ const enhanced = {};
266
+ for (const key in router) {
267
+ enhanced[key] = enhanceRouter(router[key], options);
268
+ }
269
+ return enhanced;
270
+ }
271
+ function traverseContractProcedures(options, callback, lazyOptions = []) {
272
+ let currentRouter = options.router;
273
+ const hiddenContract = getHiddenRouterContract(options.router);
274
+ if (hiddenContract !== void 0) {
275
+ currentRouter = hiddenContract;
276
+ }
277
+ if (isLazy(currentRouter)) {
278
+ lazyOptions.push({
279
+ router: currentRouter,
309
280
  path: options.path
310
281
  });
311
- } else if (isContractProcedure2(options.router)) {
282
+ } else if (isContractProcedure(currentRouter)) {
312
283
  callback({
313
- contract: options.router,
284
+ contract: currentRouter,
314
285
  path: options.path
315
286
  });
316
287
  } else {
317
- for (const key in options.router) {
318
- eachContractProcedure(
288
+ for (const key in currentRouter) {
289
+ traverseContractProcedures(
319
290
  {
320
- router: options.router[key],
291
+ router: currentRouter[key],
321
292
  path: [...options.path, key]
322
293
  },
323
294
  callback,
324
- laziedOptions
295
+ lazyOptions
325
296
  );
326
297
  }
327
298
  }
328
- return laziedOptions;
299
+ return lazyOptions;
329
300
  }
330
- async function eachAllContractProcedure(options, callback) {
301
+ async function resolveContractProcedures(options, callback) {
331
302
  const pending = [options];
332
- for (const item of pending) {
333
- const lazies = eachContractProcedure(item, callback);
334
- for (const lazy2 of lazies) {
335
- const { default: router } = await unlazy(lazy2.lazied);
303
+ for (const options2 of pending) {
304
+ const lazyOptions = traverseContractProcedures(options2, callback);
305
+ for (const options3 of lazyOptions) {
306
+ const { default: router } = await unlazy(options3.router);
336
307
  pending.push({
337
- path: lazy2.path,
338
- router
308
+ router,
309
+ path: options3.path
339
310
  });
340
311
  }
341
312
  }
342
313
  }
343
- function convertPathToHttpPath(path) {
344
- return `/${path.map(encodeURIComponent).join("/")}`;
314
+
315
+ function createAssertedLazyProcedure(lazied) {
316
+ const lazyProcedure = lazy(async () => {
317
+ const { default: maybeProcedure } = await unlazy(lazied);
318
+ if (!isProcedure(maybeProcedure)) {
319
+ throw new Error(`
320
+ Expected a lazy<procedure> but got lazy<unknown>.
321
+ This should be caught by TypeScript compilation.
322
+ Please report this issue if this makes you feel uncomfortable.
323
+ `);
324
+ }
325
+ return { default: maybeProcedure };
326
+ }, getLazyMeta(lazied));
327
+ return lazyProcedure;
345
328
  }
346
- function createContractedProcedure(contract, procedure) {
329
+ function createContractedProcedure(procedure, contract) {
347
330
  return new Procedure({
348
331
  ...procedure["~orpc"],
349
332
  errorMap: contract["~orpc"].errorMap,
@@ -351,29 +334,12 @@ function createContractedProcedure(contract, procedure) {
351
334
  meta: contract["~orpc"].meta
352
335
  });
353
336
  }
337
+ function call(procedure, input, ...rest) {
338
+ return createProcedureClient(procedure, ...rest)(input);
339
+ }
340
+
341
+ function toHttpPath(path) {
342
+ return `/${path.map(encodeURIComponent).join("/")}`;
343
+ }
354
344
 
355
- export {
356
- LAZY_LOADER_SYMBOL,
357
- lazy,
358
- isLazy,
359
- unlazy,
360
- Procedure,
361
- isProcedure,
362
- flatLazy,
363
- createLazyProcedureFormAnyLazy,
364
- addMiddleware,
365
- middlewareOutputFn,
366
- createProcedureClient,
367
- setRouterContract,
368
- getRouterContract,
369
- deepSetLazyRouterPrefix,
370
- getLazyRouterPrefix,
371
- createAccessibleLazyRouter,
372
- adaptRouter,
373
- getRouterChild,
374
- eachContractProcedure,
375
- eachAllContractProcedure,
376
- convertPathToHttpPath,
377
- createContractedProcedure
378
- };
379
- //# sourceMappingURL=chunk-XXBE6CYD.js.map
345
+ 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, getLazyMeta as j, call as k, lazy as l, middlewareOutputFn as m, getHiddenRouterContract as n, createAccessibleLazyRouter as o, resolveContractProcedures as r, setHiddenRouterContract as s, traverseContractProcedures as t, unlazy as u };
@@ -0,0 +1,143 @@
1
+ import { ORPCErrorCode, ORPCErrorOptions, ORPCError, ClientContext, Client } from '@orpc/client';
2
+ import { IsNever, MaybeOptionalOptions, Promisable, Interceptor, Value } from '@orpc/shared';
3
+ import { ErrorMap, ErrorMapItem, SchemaInput, HTTPPath, Schema, Meta, ContractProcedureDef, SchemaOutput, ErrorFromErrorMap, AnyContractRouter, ContractProcedure } from '@orpc/contract';
4
+
5
+ type Context = Record<string, any>;
6
+ type MergedContext<T extends Context, U extends Context> = T & U;
7
+ declare function mergeContext<T extends Context, U extends Context>(context: T, other: U): MergedContext<T, U>;
8
+ type ConflictContextGuard<T extends Context> = true extends IsNever<T> | {
9
+ [K in keyof T]: IsNever<T[K]>;
10
+ }[keyof T] ? never : unknown;
11
+
12
+ type ORPCErrorConstructorMapItemOptions<TData> = Omit<ORPCErrorOptions<TData>, 'defined' | 'status'>;
13
+ type ORPCErrorConstructorMapItem<TCode extends ORPCErrorCode, TInData> = (...rest: MaybeOptionalOptions<ORPCErrorConstructorMapItemOptions<TInData>>) => ORPCError<TCode, TInData>;
14
+ type ORPCErrorConstructorMap<T extends ErrorMap> = {
15
+ [K in keyof T]: K extends ORPCErrorCode ? T[K] extends ErrorMapItem<infer UInputSchema> ? ORPCErrorConstructorMapItem<K, SchemaInput<UInputSchema>> : never : never;
16
+ };
17
+
18
+ declare const LAZY_SYMBOL: unique symbol;
19
+ interface LazyMeta {
20
+ prefix?: HTTPPath;
21
+ }
22
+ interface Lazy<T> {
23
+ [LAZY_SYMBOL]: {
24
+ loader: () => Promise<{
25
+ default: T;
26
+ }>;
27
+ meta: LazyMeta;
28
+ };
29
+ }
30
+ type Lazyable<T> = T | Lazy<T>;
31
+ declare function lazy<T>(loader: () => Promise<{
32
+ default: T;
33
+ }>, meta?: LazyMeta): Lazy<T>;
34
+ declare function isLazy(item: unknown): item is Lazy<any>;
35
+ declare function getLazyMeta(lazied: Lazy<any>): LazyMeta;
36
+ declare function unlazy<T extends Lazyable<any>>(lazied: T): Promise<{
37
+ default: T extends Lazy<infer U> ? U : T;
38
+ }>;
39
+
40
+ interface ProcedureHandlerOptions<TCurrentContext extends Context, TInput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
41
+ context: TCurrentContext;
42
+ input: TInput;
43
+ path: readonly string[];
44
+ procedure: Procedure<Context, Context, Schema, Schema, unknown, ErrorMap, TMeta>;
45
+ signal?: AbortSignal;
46
+ lastEventId: string | undefined;
47
+ errors: TErrorConstructorMap;
48
+ }
49
+ interface ProcedureHandler<TCurrentContext extends Context, TInput, THandlerOutput, TErrorMap extends ErrorMap, TMeta extends Meta> {
50
+ (opt: ProcedureHandlerOptions<TCurrentContext, TInput, ORPCErrorConstructorMap<TErrorMap>, TMeta>): Promisable<THandlerOutput>;
51
+ }
52
+ interface ProcedureDef<TInitialContext extends Context, TCurrentContext extends Context, TInputSchema extends Schema, TOutputSchema extends Schema, THandlerOutput, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap, TMeta> {
53
+ __initialContext?: (type: TInitialContext) => unknown;
54
+ middlewares: AnyMiddleware[];
55
+ inputValidationIndex: number;
56
+ outputValidationIndex: number;
57
+ handler: ProcedureHandler<TCurrentContext, any, THandlerOutput, any, any>;
58
+ }
59
+ declare class Procedure<TInitialContext extends Context, TCurrentContext extends Context, TInputSchema extends Schema, TOutputSchema extends Schema, THandlerOutput, TErrorMap extends ErrorMap, TMeta extends Meta> {
60
+ '~orpc': ProcedureDef<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap, TMeta>;
61
+ constructor(def: ProcedureDef<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap, TMeta>);
62
+ }
63
+ type AnyProcedure = Procedure<any, any, any, any, any, any, any>;
64
+ declare function isProcedure(item: unknown): item is AnyProcedure;
65
+
66
+ type MiddlewareResult<TOutContext extends Context, TOutput> = Promisable<{
67
+ output: TOutput;
68
+ context: TOutContext;
69
+ }>;
70
+ type MiddlewareNextFnOptions<TOutContext extends Context> = Record<never, never> extends TOutContext ? {
71
+ context?: TOutContext;
72
+ } : {
73
+ context: TOutContext;
74
+ };
75
+ interface MiddlewareNextFn<TInContext extends Context, TOutput> {
76
+ <U extends Context & Partial<TInContext> = Record<never, never>>(...rest: MaybeOptionalOptions<MiddlewareNextFnOptions<U>>): MiddlewareResult<U, TOutput>;
77
+ }
78
+ interface MiddlewareOutputFn<TOutput> {
79
+ (output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
80
+ }
81
+ interface MiddlewareOptions<TInContext extends Context, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
82
+ context: TInContext;
83
+ path: readonly string[];
84
+ procedure: Procedure<Context, Context, Schema, Schema, unknown, ErrorMap, TMeta>;
85
+ signal?: AbortSignal;
86
+ lastEventId: string | undefined;
87
+ next: MiddlewareNextFn<TInContext, TOutput>;
88
+ errors: TErrorConstructorMap;
89
+ }
90
+ interface Middleware<TInContext extends Context, TOutContext extends Context, TInput, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
91
+ (options: MiddlewareOptions<TInContext, TOutput, TErrorConstructorMap, TMeta>, input: TInput, output: MiddlewareOutputFn<TOutput>): Promisable<MiddlewareResult<TOutContext, TOutput>>;
92
+ }
93
+ type AnyMiddleware = Middleware<any, any, any, any, any, any>;
94
+ interface MapInputMiddleware<TInput, TMappedInput> {
95
+ (input: TInput): TMappedInput;
96
+ }
97
+ declare function middlewareOutputFn<TOutput>(output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
98
+
99
+ type ProcedureClient<TClientContext extends ClientContext, TInputSchema extends Schema, TOutputSchema extends Schema, THandlerOutput, TErrorMap extends ErrorMap> = Client<TClientContext, SchemaInput<TInputSchema>, SchemaOutput<TOutputSchema, THandlerOutput>, ErrorFromErrorMap<TErrorMap>>;
100
+ interface ProcedureClientInterceptorOptions<TInitialContext extends Context, TInputSchema extends Schema, TErrorMap extends ErrorMap, TMeta extends Meta> {
101
+ context: TInitialContext;
102
+ input: SchemaInput<TInputSchema>;
103
+ errors: ORPCErrorConstructorMap<TErrorMap>;
104
+ path: readonly string[];
105
+ procedure: Procedure<Context, Context, Schema, Schema, unknown, ErrorMap, TMeta>;
106
+ signal?: AbortSignal;
107
+ lastEventId: string | undefined;
108
+ }
109
+ /**
110
+ * Options for creating a procedure caller with comprehensive type safety
111
+ */
112
+ type CreateProcedureClientOptions<TInitialContext extends Context, TInputSchema extends Schema, TOutputSchema extends Schema, THandlerOutput, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext> = {
113
+ /**
114
+ * This is helpful for logging and analytics.
115
+ */
116
+ path?: readonly string[];
117
+ interceptors?: Interceptor<ProcedureClientInterceptorOptions<TInitialContext, TInputSchema, TErrorMap, TMeta>, SchemaOutput<TOutputSchema, THandlerOutput>, ErrorFromErrorMap<TErrorMap>>[];
118
+ } & (Record<never, never> extends TInitialContext ? {
119
+ context?: Value<TInitialContext, [clientContext: TClientContext]>;
120
+ } : {
121
+ context: Value<TInitialContext, [clientContext: TClientContext]>;
122
+ });
123
+ declare function createProcedureClient<TInitialContext extends Context, TInputSchema extends Schema, TOutputSchema extends Schema, THandlerOutput, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext>(lazyableProcedure: Lazyable<Procedure<TInitialContext, any, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap, TMeta>>, ...[options]: MaybeOptionalOptions<CreateProcedureClientOptions<TInitialContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap, TMeta, TClientContext>>): ProcedureClient<TClientContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap>;
124
+
125
+ type Router<T extends AnyContractRouter, TInitialContext extends Context> = T extends ContractProcedure<infer UInputSchema, infer UOutputSchema, infer UErrorMap, infer UMeta> ? Procedure<TInitialContext, any, UInputSchema, UOutputSchema, any, UErrorMap, UMeta> : {
126
+ [K in keyof T]: T[K] extends AnyContractRouter ? Lazyable<Router<T[K], TInitialContext>> : never;
127
+ };
128
+ type AnyRouter = Router<any, any>;
129
+ type InferRouterInitialContext<T extends AnyRouter> = T extends Router<any, infer UInitialContext> ? UInitialContext : never;
130
+ type InferRouterInitialContexts<T extends AnyRouter> = T extends Procedure<infer UInitialContext, any, any, any, any, any, any> ? UInitialContext : {
131
+ [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInitialContexts<U> : never;
132
+ };
133
+ type InferRouterCurrentContexts<T extends AnyRouter> = T extends Procedure<any, infer UCurrentContext, any, any, any, any, any> ? UCurrentContext : {
134
+ [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterCurrentContexts<U> : never;
135
+ };
136
+ type InferRouterInputs<T extends AnyRouter> = T extends Procedure<any, any, infer UInputSchema, any, any, any, any> ? SchemaInput<UInputSchema> : {
137
+ [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInputs<U> : never;
138
+ };
139
+ type InferRouterOutputs<T extends AnyRouter> = T extends Procedure<any, any, any, infer UOutputSchema, infer UFuncOutput, any, any> ? SchemaOutput<UOutputSchema, UFuncOutput> : {
140
+ [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterOutputs<U> : never;
141
+ };
142
+
143
+ export { type AnyProcedure as A, createProcedureClient as B, type Context as C, type InferRouterInitialContexts as D, type InferRouterCurrentContexts as E, type InferRouterInputs as F, type InferRouterOutputs as G, type InferRouterInitialContext as I, type Lazyable as L, type Middleware as M, type ORPCErrorConstructorMap as O, type ProcedureClientInterceptorOptions as P, type Router as R, type AnyRouter as a, Procedure as b, type ConflictContextGuard as c, type MergedContext as d, type MapInputMiddleware as e, type CreateProcedureClientOptions as f, type ProcedureClient as g, type AnyMiddleware as h, type Lazy as i, type ProcedureHandler as j, LAZY_SYMBOL as k, type LazyMeta as l, mergeContext as m, lazy as n, isLazy as o, getLazyMeta as p, type MiddlewareResult as q, type MiddlewareNextFnOptions as r, type MiddlewareNextFn as s, type MiddlewareOutputFn as t, unlazy as u, type MiddlewareOptions as v, middlewareOutputFn as w, type ProcedureHandlerOptions as x, type ProcedureDef as y, isProcedure as z };