@orpc/server 0.0.0-next.df024bb → 0.0.0-next.e000d9a

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 (55) hide show
  1. package/README.md +27 -17
  2. package/dist/adapters/bun-ws/index.d.mts +35 -0
  3. package/dist/adapters/bun-ws/index.d.ts +35 -0
  4. package/dist/adapters/bun-ws/index.mjs +51 -0
  5. package/dist/adapters/crossws/index.d.mts +30 -0
  6. package/dist/adapters/crossws/index.d.ts +30 -0
  7. package/dist/adapters/crossws/index.mjs +51 -0
  8. package/dist/adapters/fetch/index.d.mts +62 -11
  9. package/dist/adapters/fetch/index.d.ts +62 -11
  10. package/dist/adapters/fetch/index.mjs +108 -7
  11. package/dist/adapters/node/index.d.mts +64 -22
  12. package/dist/adapters/node/index.d.ts +64 -22
  13. package/dist/adapters/node/index.mjs +86 -21
  14. package/dist/adapters/standard/index.d.mts +12 -15
  15. package/dist/adapters/standard/index.d.ts +12 -15
  16. package/dist/adapters/standard/index.mjs +4 -4
  17. package/dist/adapters/websocket/index.d.mts +27 -0
  18. package/dist/adapters/websocket/index.d.ts +27 -0
  19. package/dist/adapters/websocket/index.mjs +38 -0
  20. package/dist/adapters/ws/index.d.mts +28 -0
  21. package/dist/adapters/ws/index.d.ts +28 -0
  22. package/dist/adapters/ws/index.mjs +38 -0
  23. package/dist/index.d.mts +701 -127
  24. package/dist/index.d.ts +701 -127
  25. package/dist/index.mjs +193 -52
  26. package/dist/plugins/index.d.mts +143 -18
  27. package/dist/plugins/index.d.ts +143 -18
  28. package/dist/plugins/index.mjs +163 -15
  29. package/dist/shared/{server.BBGuTxHE.mjs → server.4FnxLwwr.mjs} +68 -45
  30. package/dist/shared/server.BRoxSiSC.d.mts +12 -0
  31. package/dist/shared/server.BVwwTHyO.mjs +9 -0
  32. package/dist/shared/server.BW-nUGgA.mjs +36 -0
  33. package/dist/shared/server.BjiJH9Vo.d.ts +10 -0
  34. package/dist/shared/server.Cy1vfSiG.d.ts +12 -0
  35. package/dist/shared/{server.V6zT5iYQ.mjs → server.DG7Tamti.mjs} +161 -173
  36. package/dist/shared/server.DPWk5pjW.d.mts +192 -0
  37. package/dist/shared/server.DPWk5pjW.d.ts +192 -0
  38. package/dist/shared/server.QUe9N8P4.d.mts +10 -0
  39. package/dist/shared/server.YZzrREz9.d.ts +74 -0
  40. package/dist/shared/server.eWLxY3lq.d.mts +74 -0
  41. package/package.json +43 -20
  42. package/dist/adapters/hono/index.d.mts +0 -20
  43. package/dist/adapters/hono/index.d.ts +0 -20
  44. package/dist/adapters/hono/index.mjs +0 -32
  45. package/dist/adapters/next/index.d.mts +0 -27
  46. package/dist/adapters/next/index.d.ts +0 -27
  47. package/dist/adapters/next/index.mjs +0 -29
  48. package/dist/shared/server.BMaJxq9W.d.mts +0 -9
  49. package/dist/shared/server.BT-fqIEm.d.mts +0 -77
  50. package/dist/shared/server.DpdgHO1j.d.ts +0 -9
  51. package/dist/shared/server.KwueCzFr.mjs +0 -26
  52. package/dist/shared/server.Q6ZmnTgO.mjs +0 -12
  53. package/dist/shared/server.ptXwNGQr.d.mts +0 -158
  54. package/dist/shared/server.ptXwNGQr.d.ts +0 -158
  55. package/dist/shared/server.xL87pHsk.d.ts +0 -77
@@ -1,21 +1,54 @@
1
+ import { isContractProcedure, ValidationError, mergePrefix, mergeErrorMap, enhanceRoute } from '@orpc/contract';
1
2
  import { fallbackORPCErrorStatus, ORPCError } from '@orpc/client';
2
- import { isContractProcedure, ValidationError, mergePrefix, mergeErrorMap, adaptRoute } from '@orpc/contract';
3
- import { value, intercept, toError } from '@orpc/shared';
3
+ import { value, intercept } from '@orpc/shared';
4
4
 
5
- const LAZY_LOADER_SYMBOL = Symbol("ORPC_LAZY_LOADER");
6
- function lazy(loader) {
5
+ const LAZY_SYMBOL = Symbol("ORPC_LAZY_SYMBOL");
6
+ function lazy(loader, meta = {}) {
7
7
  return {
8
- [LAZY_LOADER_SYMBOL]: loader
8
+ [LAZY_SYMBOL]: {
9
+ loader,
10
+ meta
11
+ }
9
12
  };
10
13
  }
11
14
  function isLazy(item) {
12
- 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;
13
19
  }
14
20
  function unlazy(lazied) {
15
- return isLazy(lazied) ? lazied[LAZY_LOADER_SYMBOL]() : Promise.resolve({ default: 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];
16
46
  }
17
47
 
18
48
  class Procedure {
49
+ /**
50
+ * This property holds the defined options.
51
+ */
19
52
  "~orpc";
20
53
  constructor(def) {
21
54
  this["~orpc"] = def;
@@ -28,50 +61,8 @@ function isProcedure(item) {
28
61
  return isContractProcedure(item) && "middlewares" in item["~orpc"] && "inputValidationIndex" in item["~orpc"] && "outputValidationIndex" in item["~orpc"] && "handler" in item["~orpc"];
29
62
  }
30
63
 
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];
64
+ function mergeCurrentContext(context, other) {
65
+ return { ...context, ...other };
75
66
  }
76
67
 
77
68
  function createORPCErrorConstructorMap(errors) {
@@ -139,7 +130,7 @@ function createProcedureClient(lazyableProcedure, ...[options]) {
139
130
  );
140
131
  } catch (e) {
141
132
  if (!(e instanceof ORPCError)) {
142
- throw toError(e);
133
+ throw e;
143
134
  }
144
135
  const validated = await validateORPCError(procedure["~orpc"].errorMap, e);
145
136
  throw validated;
@@ -181,193 +172,187 @@ async function executeProcedureInternal(procedure, options) {
181
172
  const middlewares = procedure["~orpc"].middlewares;
182
173
  const inputValidationIndex = Math.min(Math.max(0, procedure["~orpc"].inputValidationIndex), middlewares.length);
183
174
  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 };
175
+ const next = async (index, context, input) => {
176
+ let currentInput = input;
191
177
  if (index === inputValidationIndex) {
192
178
  currentInput = await validateInput(procedure, currentInput);
193
179
  }
194
180
  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 };
181
+ const output = mid ? (await mid({
182
+ ...options,
183
+ context,
184
+ next: async (...[nextOptions]) => {
185
+ const nextContext = nextOptions?.context ?? {};
186
+ return {
187
+ output: await next(index + 1, mergeCurrentContext(context, nextContext), currentInput),
188
+ context: nextContext
189
+ };
190
+ }
191
+ }, currentInput, middlewareOutputFn)).output : await procedure["~orpc"].handler({ ...options, context, input: currentInput });
196
192
  if (index === outputValidationIndex) {
197
- const validatedOutput = await validateOutput(procedure, result.output);
198
- return {
199
- ...result,
200
- output: validatedOutput
201
- };
193
+ return await validateOutput(procedure, output);
202
194
  }
203
- return result;
195
+ return output;
204
196
  };
205
- return (await next({})).output;
197
+ return next(0, options.context, options.input);
206
198
  }
207
199
 
208
- const ROUTER_CONTRACT_SYMBOL = Symbol("ORPC_ROUTER_CONTRACT");
209
- function setRouterContract(obj, contract) {
210
- return new Proxy(obj, {
200
+ const HIDDEN_ROUTER_CONTRACT_SYMBOL = Symbol("ORPC_HIDDEN_ROUTER_CONTRACT");
201
+ function setHiddenRouterContract(router, contract) {
202
+ return new Proxy(router, {
211
203
  get(target, key) {
212
- if (key === ROUTER_CONTRACT_SYMBOL) {
204
+ if (key === HIDDEN_ROUTER_CONTRACT_SYMBOL) {
213
205
  return contract;
214
206
  }
215
207
  return Reflect.get(target, key);
216
208
  }
217
209
  });
218
210
  }
219
- function getRouterContract(obj) {
220
- return obj[ROUTER_CONTRACT_SYMBOL];
211
+ function getHiddenRouterContract(router) {
212
+ return router[HIDDEN_ROUTER_CONTRACT_SYMBOL];
221
213
  }
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;
214
+
215
+ function getRouter(router, path) {
216
+ let current = router;
217
+ for (let i = 0; i < path.length; i++) {
218
+ const segment = path[i];
219
+ if (!current) {
220
+ return void 0;
234
221
  }
235
- });
236
- }
237
- function getLazyRouterPrefix(obj) {
238
- return obj[LAZY_ROUTER_PREFIX_SYMBOL];
222
+ if (isProcedure(current)) {
223
+ return void 0;
224
+ }
225
+ if (!isLazy(current)) {
226
+ current = current[segment];
227
+ continue;
228
+ }
229
+ const lazied = current;
230
+ const rest = path.slice(i);
231
+ return lazy(async () => {
232
+ const unwrapped = await unlazy(lazied);
233
+ const next = getRouter(unwrapped.default, rest);
234
+ return unlazy(next);
235
+ }, getLazyMeta(lazied));
236
+ }
237
+ return current;
239
238
  }
240
-
241
239
  function createAccessibleLazyRouter(lazied) {
242
- const flattenLazy = flatLazy(lazied);
243
- const recursive = new Proxy(flattenLazy, {
240
+ const recursive = new Proxy(lazied, {
244
241
  get(target, key) {
245
242
  if (typeof key !== "string") {
246
243
  return Reflect.get(target, key);
247
244
  }
248
- const next = getRouterChild(flattenLazy, key);
245
+ const next = getRouter(lazied, [key]);
249
246
  return createAccessibleLazyRouter(next);
250
247
  }
251
248
  });
252
249
  return recursive;
253
250
  }
254
-
255
- function adaptRouter(router, options) {
251
+ function enhanceRouter(router, options) {
256
252
  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 };
253
+ const laziedMeta = getLazyMeta(router);
254
+ const enhancedPrefix = laziedMeta?.prefix ? mergePrefix(options.prefix, laziedMeta?.prefix) : options.prefix;
255
+ const enhanced2 = lazy(async () => {
256
+ const { default: unlaziedRouter } = await unlazy(router);
257
+ const enhanced3 = enhanceRouter(unlaziedRouter, options);
258
+ return unlazy(enhanced3);
259
+ }, {
260
+ ...laziedMeta,
261
+ prefix: enhancedPrefix
261
262
  });
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
- }
263
+ const accessible = createAccessibleLazyRouter(enhanced2);
268
264
  return accessible;
269
265
  }
270
266
  if (isProcedure(router)) {
271
- const newMiddlewares = mergeMiddlewares(options.middlewares, router["~orpc"].middlewares);
267
+ const newMiddlewares = mergeMiddlewares(options.middlewares, router["~orpc"].middlewares, { dedupeLeading: options.dedupeLeadingMiddlewares });
272
268
  const newMiddlewareAdded = newMiddlewares.length - router["~orpc"].middlewares.length;
273
- const adapted2 = new Procedure({
269
+ const enhanced2 = new Procedure({
274
270
  ...router["~orpc"],
275
- route: adaptRoute(router["~orpc"].route, options),
271
+ route: enhanceRoute(router["~orpc"].route, options),
276
272
  errorMap: mergeErrorMap(options.errorMap, router["~orpc"].errorMap),
277
273
  middlewares: newMiddlewares,
278
274
  inputValidationIndex: router["~orpc"].inputValidationIndex + newMiddlewareAdded,
279
275
  outputValidationIndex: router["~orpc"].outputValidationIndex + newMiddlewareAdded
280
276
  });
281
- return adapted2;
277
+ return enhanced2;
282
278
  }
283
- const adapted = {};
279
+ const enhanced = {};
284
280
  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);
281
+ enhanced[key] = enhanceRouter(router[key], options);
314
282
  }
315
- return current;
283
+ return enhanced;
316
284
  }
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
- );
285
+ function traverseContractProcedures(options, callback, lazyOptions = []) {
286
+ let currentRouter = options.router;
287
+ const hiddenContract = getHiddenRouterContract(options.router);
288
+ if (hiddenContract !== void 0) {
289
+ currentRouter = hiddenContract;
329
290
  }
330
- if (isLazy(options.router)) {
331
- laziedOptions.push({
332
- lazied: options.router,
291
+ if (isLazy(currentRouter)) {
292
+ lazyOptions.push({
293
+ router: currentRouter,
333
294
  path: options.path
334
295
  });
335
- } else if (isContractProcedure(options.router)) {
296
+ } else if (isContractProcedure(currentRouter)) {
336
297
  callback({
337
- contract: options.router,
298
+ contract: currentRouter,
338
299
  path: options.path
339
300
  });
340
301
  } else {
341
- for (const key in options.router) {
342
- eachContractProcedure(
302
+ for (const key in currentRouter) {
303
+ traverseContractProcedures(
343
304
  {
344
- router: options.router[key],
305
+ router: currentRouter[key],
345
306
  path: [...options.path, key]
346
307
  },
347
308
  callback,
348
- laziedOptions
309
+ lazyOptions
349
310
  );
350
311
  }
351
312
  }
352
- return laziedOptions;
313
+ return lazyOptions;
353
314
  }
354
- async function eachAllContractProcedure(options, callback) {
315
+ async function resolveContractProcedures(options, callback) {
355
316
  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);
317
+ for (const options2 of pending) {
318
+ const lazyOptions = traverseContractProcedures(options2, callback);
319
+ for (const options3 of lazyOptions) {
320
+ const { default: router } = await unlazy(options3.router);
360
321
  pending.push({
361
- path: lazy.path,
362
- router
322
+ router,
323
+ path: options3.path
363
324
  });
364
325
  }
365
326
  }
366
327
  }
367
- function convertPathToHttpPath(path) {
368
- return `/${path.map(encodeURIComponent).join("/")}`;
328
+ async function unlazyRouter(router) {
329
+ if (isProcedure(router)) {
330
+ return router;
331
+ }
332
+ const unlazied = {};
333
+ for (const key in router) {
334
+ const item = router[key];
335
+ const { default: unlaziedRouter } = await unlazy(item);
336
+ unlazied[key] = await unlazyRouter(unlaziedRouter);
337
+ }
338
+ return unlazied;
369
339
  }
370
- function createContractedProcedure(contract, procedure) {
340
+
341
+ function createAssertedLazyProcedure(lazied) {
342
+ const lazyProcedure = lazy(async () => {
343
+ const { default: maybeProcedure } = await unlazy(lazied);
344
+ if (!isProcedure(maybeProcedure)) {
345
+ throw new Error(`
346
+ Expected a lazy<procedure> but got lazy<unknown>.
347
+ This should be caught by TypeScript compilation.
348
+ Please report this issue if this makes you feel uncomfortable.
349
+ `);
350
+ }
351
+ return { default: maybeProcedure };
352
+ }, getLazyMeta(lazied));
353
+ return lazyProcedure;
354
+ }
355
+ function createContractedProcedure(procedure, contract) {
371
356
  return new Procedure({
372
357
  ...procedure["~orpc"],
373
358
  errorMap: contract["~orpc"].errorMap,
@@ -375,5 +360,8 @@ function createContractedProcedure(contract, procedure) {
375
360
  meta: contract["~orpc"].meta
376
361
  });
377
362
  }
363
+ function call(procedure, input, ...rest) {
364
+ return createProcedureClient(procedure, ...rest)(input);
365
+ }
378
366
 
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 };
367
+ export { LAZY_SYMBOL as L, Procedure as P, createContractedProcedure as a, addMiddleware as b, createProcedureClient as c, isLazy as d, enhanceRouter as e, createAssertedLazyProcedure as f, getRouter as g, createORPCErrorConstructorMap as h, isProcedure as i, getLazyMeta as j, middlewareOutputFn as k, lazy as l, mergeCurrentContext as m, isStartWithMiddlewares as n, mergeMiddlewares as o, call as p, getHiddenRouterContract as q, createAccessibleLazyRouter as r, setHiddenRouterContract as s, traverseContractProcedures as t, unlazy as u, validateORPCError as v, resolveContractProcedures as w, unlazyRouter as x };
@@ -0,0 +1,192 @@
1
+ import { ORPCErrorCode, ORPCErrorOptions, ORPCError, HTTPPath, ClientContext, Client } from '@orpc/client';
2
+ import { ErrorMap, ErrorMapItem, InferSchemaInput, AnySchema, Meta, ContractProcedureDef, InferSchemaOutput, ErrorFromErrorMap, AnyContractRouter, ContractProcedure } from '@orpc/contract';
3
+ import { MaybeOptionalOptions, Promisable, Interceptor, Value } from '@orpc/shared';
4
+
5
+ type Context = Record<PropertyKey, any>;
6
+ type MergedInitialContext<TInitial extends Context, TAdditional extends Context, TCurrent extends Context> = TInitial & Omit<TAdditional, keyof TCurrent>;
7
+ type MergedCurrentContext<T extends Context, U extends Context> = Omit<T, keyof U> & U;
8
+ declare function mergeCurrentContext<T extends Context, U extends Context>(context: T, other: U): MergedCurrentContext<T, U>;
9
+
10
+ type ORPCErrorConstructorMapItemOptions<TData> = Omit<ORPCErrorOptions<TData>, 'defined' | 'status'>;
11
+ type ORPCErrorConstructorMapItem<TCode extends ORPCErrorCode, TInData> = (...rest: MaybeOptionalOptions<ORPCErrorConstructorMapItemOptions<TInData>>) => ORPCError<TCode, TInData>;
12
+ type ORPCErrorConstructorMap<T extends ErrorMap> = {
13
+ [K in keyof T]: K extends ORPCErrorCode ? T[K] extends ErrorMapItem<infer UInputSchema> ? ORPCErrorConstructorMapItem<K, InferSchemaInput<UInputSchema>> : never : never;
14
+ };
15
+ declare function createORPCErrorConstructorMap<T extends ErrorMap>(errors: T): ORPCErrorConstructorMap<T>;
16
+ declare function validateORPCError(map: ErrorMap, error: ORPCError<any, any>): Promise<ORPCError<string, unknown>>;
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
+ /**
32
+ * Create a lazy thing.
33
+ */
34
+ declare function lazy<T>(loader: () => Promise<{
35
+ default: T;
36
+ }>, meta?: LazyMeta): Lazy<T>;
37
+ declare function isLazy(item: unknown): item is Lazy<any>;
38
+ declare function getLazyMeta(lazied: Lazy<any>): LazyMeta;
39
+ declare function unlazy<T extends Lazyable<any>>(lazied: T): Promise<{
40
+ default: T extends Lazy<infer U> ? U : T;
41
+ }>;
42
+
43
+ interface ProcedureHandlerOptions<TCurrentContext extends Context, TInput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
44
+ context: TCurrentContext;
45
+ input: TInput;
46
+ path: readonly string[];
47
+ procedure: Procedure<Context, Context, AnySchema, AnySchema, ErrorMap, TMeta>;
48
+ signal?: AbortSignal;
49
+ lastEventId: string | undefined;
50
+ errors: TErrorConstructorMap;
51
+ }
52
+ interface ProcedureHandler<TCurrentContext extends Context, TInput, THandlerOutput, TErrorMap extends ErrorMap, TMeta extends Meta> {
53
+ (opt: ProcedureHandlerOptions<TCurrentContext, TInput, ORPCErrorConstructorMap<TErrorMap>, TMeta>): Promisable<THandlerOutput>;
54
+ }
55
+ interface ProcedureDef<TInitialContext extends Context, TCurrentContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap, TMeta> {
56
+ __initialContext?: (type: TInitialContext) => unknown;
57
+ middlewares: readonly AnyMiddleware[];
58
+ inputValidationIndex: number;
59
+ outputValidationIndex: number;
60
+ handler: ProcedureHandler<TCurrentContext, any, any, any, any>;
61
+ }
62
+ /**
63
+ * This class represents a procedure.
64
+ *
65
+ * @see {@link https://orpc.unnoq.com/docs/procedure Procedure Docs}
66
+ */
67
+ declare class Procedure<TInitialContext extends Context, TCurrentContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> {
68
+ /**
69
+ * This property holds the defined options.
70
+ */
71
+ '~orpc': ProcedureDef<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, TErrorMap, TMeta>;
72
+ constructor(def: ProcedureDef<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, TErrorMap, TMeta>);
73
+ }
74
+ type AnyProcedure = Procedure<any, any, any, any, any, any>;
75
+ declare function isProcedure(item: unknown): item is AnyProcedure;
76
+
77
+ type MiddlewareResult<TOutContext extends Context, TOutput> = Promisable<{
78
+ output: TOutput;
79
+ context: TOutContext;
80
+ }>;
81
+ type MiddlewareNextFnOptions<TOutContext extends Context> = Record<never, never> extends TOutContext ? {
82
+ context?: TOutContext;
83
+ } : {
84
+ context: TOutContext;
85
+ };
86
+ interface MiddlewareNextFn<TOutput> {
87
+ <U extends Context = Record<never, never>>(...rest: MaybeOptionalOptions<MiddlewareNextFnOptions<U>>): MiddlewareResult<U, TOutput>;
88
+ }
89
+ interface MiddlewareOutputFn<TOutput> {
90
+ (output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
91
+ }
92
+ interface MiddlewareOptions<TInContext extends Context, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
93
+ context: TInContext;
94
+ path: readonly string[];
95
+ procedure: Procedure<Context, Context, AnySchema, AnySchema, ErrorMap, TMeta>;
96
+ signal?: AbortSignal;
97
+ lastEventId: string | undefined;
98
+ next: MiddlewareNextFn<TOutput>;
99
+ errors: TErrorConstructorMap;
100
+ }
101
+ /**
102
+ * A function that represents a middleware.
103
+ *
104
+ * @see {@link https://orpc.unnoq.com/docs/middleware Middleware Docs}
105
+ */
106
+ interface Middleware<TInContext extends Context, TOutContext extends Context, TInput, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
107
+ (options: MiddlewareOptions<TInContext, TOutput, TErrorConstructorMap, TMeta>, input: TInput, output: MiddlewareOutputFn<TOutput>): Promisable<MiddlewareResult<TOutContext, TOutput>>;
108
+ }
109
+ type AnyMiddleware = Middleware<any, any, any, any, any, any>;
110
+ interface MapInputMiddleware<TInput, TMappedInput> {
111
+ (input: TInput): TMappedInput;
112
+ }
113
+ declare function middlewareOutputFn<TOutput>(output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
114
+
115
+ type ProcedureClient<TClientContext extends ClientContext, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> = Client<TClientContext, InferSchemaInput<TInputSchema>, InferSchemaOutput<TOutputSchema>, ErrorFromErrorMap<TErrorMap>>;
116
+ interface ProcedureClientInterceptorOptions<TInitialContext extends Context, TErrorMap extends ErrorMap, TMeta extends Meta> {
117
+ context: TInitialContext;
118
+ input: unknown;
119
+ errors: ORPCErrorConstructorMap<TErrorMap>;
120
+ path: readonly string[];
121
+ procedure: Procedure<Context, Context, AnySchema, AnySchema, ErrorMap, TMeta>;
122
+ signal?: AbortSignal;
123
+ lastEventId: string | undefined;
124
+ }
125
+ type CreateProcedureClientOptions<TInitialContext extends Context, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext> = {
126
+ /**
127
+ * This is helpful for logging and analytics.
128
+ */
129
+ path?: readonly string[];
130
+ interceptors?: Interceptor<ProcedureClientInterceptorOptions<TInitialContext, TErrorMap, TMeta>, InferSchemaOutput<TOutputSchema>, ErrorFromErrorMap<TErrorMap>>[];
131
+ } & (Record<never, never> extends TInitialContext ? {
132
+ context?: Value<TInitialContext, [clientContext: TClientContext]>;
133
+ } : {
134
+ context: Value<TInitialContext, [clientContext: TClientContext]>;
135
+ });
136
+ /**
137
+ * Create Server-side client from a procedure.
138
+ *
139
+ * @see {@link https://orpc.unnoq.com/docs/client/server-side Server-side Client Docs}
140
+ */
141
+ declare function createProcedureClient<TInitialContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext>(lazyableProcedure: Lazyable<Procedure<TInitialContext, any, TInputSchema, TOutputSchema, TErrorMap, TMeta>>, ...[options]: MaybeOptionalOptions<CreateProcedureClientOptions<TInitialContext, TOutputSchema, TErrorMap, TMeta, TClientContext>>): ProcedureClient<TClientContext, TInputSchema, TOutputSchema, TErrorMap>;
142
+
143
+ /**
144
+ * Represents a router, which defines a hierarchical structure of procedures.
145
+ *
146
+ * @info A procedure is a router too.
147
+ * @see {@link https://orpc.unnoq.com/docs/contract-first/define-contract#contract-router Contract Router Docs}
148
+ */
149
+ type Router<T extends AnyContractRouter, TInitialContext extends Context> = T extends ContractProcedure<infer UInputSchema, infer UOutputSchema, infer UErrorMap, infer UMeta> ? Procedure<TInitialContext, any, UInputSchema, UOutputSchema, UErrorMap, UMeta> : {
150
+ [K in keyof T]: T[K] extends AnyContractRouter ? Lazyable<Router<T[K], TInitialContext>> : never;
151
+ };
152
+ type AnyRouter = Router<any, any>;
153
+ type InferRouterInitialContext<T extends AnyRouter> = T extends Router<any, infer UInitialContext> ? UInitialContext : never;
154
+ /**
155
+ * Infer all initial context of the router.
156
+ *
157
+ * @info A procedure is a router too.
158
+ * @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
159
+ */
160
+ type InferRouterInitialContexts<T extends AnyRouter> = T extends Procedure<infer UInitialContext, any, any, any, any, any> ? UInitialContext : {
161
+ [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInitialContexts<U> : never;
162
+ };
163
+ /**
164
+ * Infer all current context of the router.
165
+ *
166
+ * @info A procedure is a router too.
167
+ * @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
168
+ */
169
+ type InferRouterCurrentContexts<T extends AnyRouter> = T extends Procedure<any, infer UCurrentContext, any, any, any, any> ? UCurrentContext : {
170
+ [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterCurrentContexts<U> : never;
171
+ };
172
+ /**
173
+ * Infer all router inputs
174
+ *
175
+ * @info A procedure is a router too.
176
+ * @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
177
+ */
178
+ type InferRouterInputs<T extends AnyRouter> = T extends Procedure<any, any, infer UInputSchema, any, any, any> ? InferSchemaInput<UInputSchema> : {
179
+ [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInputs<U> : never;
180
+ };
181
+ /**
182
+ * Infer all router outputs
183
+ *
184
+ * @info A procedure is a router too.
185
+ * @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
186
+ */
187
+ type InferRouterOutputs<T extends AnyRouter> = T extends Procedure<any, any, any, infer UOutputSchema, any, any> ? InferSchemaOutput<UOutputSchema> : {
188
+ [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterOutputs<U> : never;
189
+ };
190
+
191
+ export { isProcedure as E, createProcedureClient as G, Procedure as P, createORPCErrorConstructorMap as l, mergeCurrentContext as m, LAZY_SYMBOL as n, lazy as p, isLazy as q, getLazyMeta as r, unlazy as u, validateORPCError as v, middlewareOutputFn as z };
192
+ export type { AnyMiddleware as A, ProcedureHandlerOptions as B, Context as C, ProcedureDef as D, ProcedureClientInterceptorOptions as F, InferRouterInitialContexts as H, InferRouterInitialContext as I, InferRouterCurrentContexts as J, InferRouterInputs as K, Lazyable as L, Middleware as M, InferRouterOutputs as N, ORPCErrorConstructorMap as O, Router as R, MergedInitialContext as a, MergedCurrentContext as b, MapInputMiddleware as c, CreateProcedureClientOptions as d, ProcedureClient as e, AnyRouter as f, Lazy as g, AnyProcedure as h, ProcedureHandler as i, ORPCErrorConstructorMapItemOptions as j, ORPCErrorConstructorMapItem as k, LazyMeta as o, MiddlewareResult as s, MiddlewareNextFnOptions as t, MiddlewareNextFn as w, MiddlewareOutputFn as x, MiddlewareOptions as y };