@orpc/server 0.0.0-next.bc9d3dd → 0.0.0-next.bf49833

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 (82) 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 -19
  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 -19
  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 +269 -0
  18. package/dist/index.d.ts +269 -0
  19. package/dist/{index.js → index.mjs} +75 -136
  20. package/dist/plugins/index.d.mts +31 -0
  21. package/dist/plugins/index.d.ts +31 -0
  22. package/dist/{chunk-XFBAK67J.js → plugins/index.mjs} +18 -43
  23. package/dist/shared/server.CM3tWr3C.d.mts +75 -0
  24. package/dist/{chunk-SV6DBVXJ.js → shared/server.CMrS28Go.mjs} +164 -195
  25. package/dist/shared/server.CPteJIPP.d.mts +143 -0
  26. package/dist/shared/server.CPteJIPP.d.ts +143 -0
  27. package/dist/shared/server.CSZRzcSW.mjs +158 -0
  28. package/dist/shared/server.Cq3B6PoL.mjs +28 -0
  29. package/dist/shared/server.DmW25ynm.d.ts +75 -0
  30. package/dist/shared/server.Q6ZmnTgO.mjs +12 -0
  31. package/package.json +29 -31
  32. package/dist/chunk-4IQKTPXM.js +0 -145
  33. package/dist/chunk-SD2T3J2Z.js +0 -319
  34. package/dist/fetch.js +0 -16
  35. package/dist/node.js +0 -170
  36. package/dist/plugins.js +0 -11
  37. package/dist/src/adapters/fetch/index.d.ts +0 -4
  38. package/dist/src/adapters/fetch/rpc-handler.d.ts +0 -10
  39. package/dist/src/adapters/fetch/types.d.ts +0 -13
  40. package/dist/src/adapters/fetch/utils.d.ts +0 -5
  41. package/dist/src/adapters/hono/index.d.ts +0 -3
  42. package/dist/src/adapters/hono/middleware.d.ts +0 -13
  43. package/dist/src/adapters/next/index.d.ts +0 -3
  44. package/dist/src/adapters/next/serve.d.ts +0 -20
  45. package/dist/src/adapters/node/index.d.ts +0 -4
  46. package/dist/src/adapters/node/rpc-handler.d.ts +0 -10
  47. package/dist/src/adapters/node/types.d.ts +0 -21
  48. package/dist/src/adapters/node/utils.d.ts +0 -5
  49. package/dist/src/adapters/standard/handler.d.ts +0 -51
  50. package/dist/src/adapters/standard/index.d.ts +0 -7
  51. package/dist/src/adapters/standard/rpc-codec.d.ts +0 -15
  52. package/dist/src/adapters/standard/rpc-handler.d.ts +0 -8
  53. package/dist/src/adapters/standard/rpc-matcher.d.ts +0 -10
  54. package/dist/src/adapters/standard/rpc-serializer.d.ts +0 -16
  55. package/dist/src/adapters/standard/types.d.ts +0 -44
  56. package/dist/src/builder-variants.d.ts +0 -74
  57. package/dist/src/builder.d.ts +0 -57
  58. package/dist/src/config.d.ts +0 -6
  59. package/dist/src/context.d.ts +0 -9
  60. package/dist/src/hidden.d.ts +0 -8
  61. package/dist/src/implementer-procedure.d.ts +0 -30
  62. package/dist/src/implementer-variants.d.ts +0 -17
  63. package/dist/src/implementer.d.ts +0 -28
  64. package/dist/src/index.d.ts +0 -23
  65. package/dist/src/lazy-utils.d.ts +0 -6
  66. package/dist/src/lazy.d.ts +0 -22
  67. package/dist/src/middleware-decorated.d.ts +0 -10
  68. package/dist/src/middleware-utils.d.ts +0 -5
  69. package/dist/src/middleware.d.ts +0 -37
  70. package/dist/src/plugins/base.d.ts +0 -13
  71. package/dist/src/plugins/cors.d.ts +0 -19
  72. package/dist/src/plugins/index.d.ts +0 -4
  73. package/dist/src/plugins/response-headers.d.ts +0 -10
  74. package/dist/src/procedure-client.d.ts +0 -31
  75. package/dist/src/procedure-decorated.d.ts +0 -21
  76. package/dist/src/procedure-utils.d.ts +0 -17
  77. package/dist/src/procedure.d.ts +0 -29
  78. package/dist/src/router-accessible-lazy.d.ts +0 -8
  79. package/dist/src/router-client.d.ts +0 -11
  80. package/dist/src/router.d.ts +0 -29
  81. package/dist/src/utils.d.ts +0 -24
  82. package/dist/standard.js +0 -17
@@ -1,25 +1,39 @@
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 });
22
+ }
23
+
24
+ function mergeMiddlewares(first, second) {
25
+ return [...first, ...second];
26
+ }
27
+ function addMiddleware(middlewares, addition) {
28
+ return [...middlewares, addition];
13
29
  }
14
30
 
15
- // src/procedure.ts
16
- import { isContractProcedure } from "@orpc/contract";
17
- var Procedure = class {
31
+ class Procedure {
18
32
  "~orpc";
19
33
  constructor(def) {
20
34
  this["~orpc"] = def;
21
35
  }
22
- };
36
+ }
23
37
  function isProcedure(item) {
24
38
  if (item instanceof Procedure) {
25
39
  return true;
@@ -27,48 +41,53 @@ function isProcedure(item) {
27
41
  return isContractProcedure(item) && "middlewares" in item["~orpc"] && "inputValidationIndex" in item["~orpc"] && "outputValidationIndex" in item["~orpc"] && "handler" in item["~orpc"];
28
42
  }
29
43
 
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;
44
+ function createORPCErrorConstructorMap(errors) {
45
+ const proxy = new Proxy(errors, {
46
+ get(target, code) {
47
+ if (typeof code !== "string") {
48
+ return Reflect.get(target, code);
37
49
  }
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
- `);
50
+ const item = (...[options]) => {
51
+ const config = errors[code];
52
+ return new ORPCError(code, {
53
+ defined: Boolean(config),
54
+ status: config?.status,
55
+ message: options?.message ?? config?.message,
56
+ data: options?.data,
57
+ cause: options?.cause
58
+ });
59
+ };
60
+ return item;
53
61
  }
54
- return { default: maybeProcedure };
55
62
  });
56
- return lazyProcedure;
63
+ return proxy;
64
+ }
65
+ async function validateORPCError(map, error) {
66
+ const { code, status, message, data, cause, defined } = error;
67
+ const config = map?.[error.code];
68
+ if (!config || fallbackORPCErrorStatus(error.code, config.status) !== error.status) {
69
+ return defined ? new ORPCError(code, { defined: false, status, message, data, cause }) : error;
70
+ }
71
+ if (!config.data) {
72
+ return defined ? error : new ORPCError(code, { defined: true, status, message, data, cause });
73
+ }
74
+ const validated = await config.data["~standard"].validate(error.data);
75
+ if (validated.issues) {
76
+ return defined ? new ORPCError(code, { defined: false, status, message, data, cause }) : error;
77
+ }
78
+ return new ORPCError(code, { defined: true, status, message, data: validated.value, cause });
57
79
  }
58
80
 
59
- // src/middleware.ts
60
81
  function middlewareOutputFn(output) {
61
82
  return { output, context: {} };
62
83
  }
63
84
 
64
- // src/procedure-client.ts
65
- import { createORPCErrorConstructorMap, ORPCError, validateORPCError, ValidationError } from "@orpc/contract";
66
- import { intercept, toError, value } from "@orpc/shared";
67
85
  function createProcedureClient(lazyableProcedure, ...[options]) {
68
86
  return async (...[input, callerOptions]) => {
69
87
  const path = options?.path ?? [];
70
88
  const { default: procedure } = await unlazy(lazyableProcedure);
71
- const context = await value(options?.context ?? {}, callerOptions?.context);
89
+ const clientContext = callerOptions?.context ?? {};
90
+ const context = await value(options?.context ?? {}, clientContext);
72
91
  const errors = createORPCErrorConstructorMap(procedure["~orpc"].errorMap);
73
92
  try {
74
93
  return await intercept(
@@ -80,7 +99,8 @@ function createProcedureClient(lazyableProcedure, ...[options]) {
80
99
  errors,
81
100
  path,
82
101
  procedure,
83
- signal: callerOptions?.signal
102
+ signal: callerOptions?.signal,
103
+ lastEventId: callerOptions?.lastEventId
84
104
  },
85
105
  (interceptorOptions) => executeProcedureInternal(interceptorOptions.procedure, interceptorOptions)
86
106
  );
@@ -152,98 +172,22 @@ async function executeProcedureInternal(procedure, options) {
152
172
  return (await next({})).output;
153
173
  }
154
174
 
155
- // src/hidden.ts
156
- var ROUTER_CONTRACT_SYMBOL = Symbol("ORPC_ROUTER_CONTRACT");
157
- function setRouterContract(obj, contract) {
158
- return new Proxy(obj, {
175
+ const HIDDEN_ROUTER_CONTRACT_SYMBOL = Symbol("ORPC_HIDDEN_ROUTER_CONTRACT");
176
+ function setHiddenRouterContract(router, contract) {
177
+ return new Proxy(router, {
159
178
  get(target, key) {
160
- if (key === ROUTER_CONTRACT_SYMBOL) {
179
+ if (key === HIDDEN_ROUTER_CONTRACT_SYMBOL) {
161
180
  return contract;
162
181
  }
163
182
  return Reflect.get(target, key);
164
183
  }
165
184
  });
166
185
  }
167
- function getRouterContract(obj) {
168
- return obj[ROUTER_CONTRACT_SYMBOL];
169
- }
170
- var LAZY_ROUTER_PREFIX_SYMBOL = Symbol("ORPC_LAZY_ROUTER_PREFIX");
171
- function deepSetLazyRouterPrefix(router, prefix) {
172
- return new Proxy(router, {
173
- get(target, key) {
174
- if (key !== LAZY_ROUTER_PREFIX_SYMBOL) {
175
- const val = Reflect.get(target, key);
176
- if (isLazy(val)) {
177
- return deepSetLazyRouterPrefix(val, prefix);
178
- }
179
- return val;
180
- }
181
- return prefix;
182
- }
183
- });
184
- }
185
- function getLazyRouterPrefix(obj) {
186
- return obj[LAZY_ROUTER_PREFIX_SYMBOL];
187
- }
188
-
189
- // src/router.ts
190
- import { adaptRoute, mergeErrorMap, mergePrefix } from "@orpc/contract";
191
-
192
- // src/middleware-utils.ts
193
- function dedupeMiddlewares(compare, middlewares) {
194
- let min = 0;
195
- for (let i = 0; i < middlewares.length; i++) {
196
- const index = compare.indexOf(middlewares[i], min);
197
- if (index === -1) {
198
- return middlewares.slice(i);
199
- }
200
- min = index + 1;
201
- }
202
- return [];
203
- }
204
- function mergeMiddlewares(first, second) {
205
- return [...first, ...dedupeMiddlewares(first, second)];
206
- }
207
- function addMiddleware(middlewares, addition) {
208
- return [...middlewares, addition];
186
+ function getHiddenRouterContract(router) {
187
+ return router[HIDDEN_ROUTER_CONTRACT_SYMBOL];
209
188
  }
210
189
 
211
- // src/router.ts
212
- function adaptRouter(router, options) {
213
- if (isLazy(router)) {
214
- const adapted2 = lazy(async () => {
215
- const unlaziedRouter = (await unlazy(router)).default;
216
- const adapted3 = adaptRouter(unlaziedRouter, options);
217
- return { default: adapted3 };
218
- });
219
- const accessible = createAccessibleLazyRouter(adapted2);
220
- const currentPrefix = getLazyRouterPrefix(router);
221
- const prefix = currentPrefix ? mergePrefix(options.prefix, currentPrefix) : options.prefix;
222
- if (prefix) {
223
- return deepSetLazyRouterPrefix(accessible, prefix);
224
- }
225
- return accessible;
226
- }
227
- if (isProcedure(router)) {
228
- const newMiddlewares = mergeMiddlewares(options.middlewares, router["~orpc"].middlewares);
229
- const newMiddlewareAdded = newMiddlewares.length - router["~orpc"].middlewares.length;
230
- const adapted2 = new Procedure({
231
- ...router["~orpc"],
232
- route: adaptRoute(router["~orpc"].route, options),
233
- errorMap: mergeErrorMap(options.errorMap, router["~orpc"].errorMap),
234
- middlewares: newMiddlewares,
235
- inputValidationIndex: router["~orpc"].inputValidationIndex + newMiddlewareAdded,
236
- outputValidationIndex: router["~orpc"].outputValidationIndex + newMiddlewareAdded
237
- });
238
- return adapted2;
239
- }
240
- const adapted = {};
241
- for (const key in router) {
242
- adapted[key] = adaptRouter(router[key], options);
243
- }
244
- return adapted;
245
- }
246
- function getRouterChild(router, ...path) {
190
+ function getRouter(router, path) {
247
191
  let current = router;
248
192
  for (let i = 0; i < path.length; i++) {
249
193
  const segment = path[i];
@@ -259,89 +203,131 @@ function getRouterChild(router, ...path) {
259
203
  }
260
204
  const lazied = current;
261
205
  const rest = path.slice(i);
262
- const newLazy = lazy(async () => {
206
+ return lazy(async () => {
263
207
  const unwrapped = await unlazy(lazied);
264
- if (!unwrapped.default) {
265
- return unwrapped;
266
- }
267
- const next = getRouterChild(unwrapped.default, ...rest);
268
- return { default: next };
269
- });
270
- return flatLazy(newLazy);
208
+ const next = getRouter(unwrapped.default, rest);
209
+ return unlazy(next);
210
+ }, getLazyMeta(lazied));
271
211
  }
272
212
  return current;
273
213
  }
274
-
275
- // src/router-accessible-lazy.ts
276
214
  function createAccessibleLazyRouter(lazied) {
277
- const flattenLazy = flatLazy(lazied);
278
- const recursive = new Proxy(flattenLazy, {
215
+ const recursive = new Proxy(lazied, {
279
216
  get(target, key) {
280
217
  if (typeof key !== "string") {
281
218
  return Reflect.get(target, key);
282
219
  }
283
- const next = getRouterChild(flattenLazy, key);
220
+ const next = getRouter(lazied, [key]);
284
221
  return createAccessibleLazyRouter(next);
285
222
  }
286
223
  });
287
224
  return recursive;
288
225
  }
289
-
290
- // src/utils.ts
291
- import { isContractProcedure as isContractProcedure2 } from "@orpc/contract";
292
- function eachContractProcedure(options, callback, laziedOptions = []) {
293
- const hiddenContract = getRouterContract(options.router);
294
- if (hiddenContract) {
295
- return eachContractProcedure(
296
- {
297
- router: hiddenContract,
298
- path: options.path
299
- },
300
- callback,
301
- laziedOptions
302
- );
226
+ function enhanceRouter(router, options) {
227
+ if (isLazy(router)) {
228
+ const laziedMeta = getLazyMeta(router);
229
+ const enhancedPrefix = laziedMeta?.prefix ? mergePrefix(options.prefix, laziedMeta?.prefix) : options.prefix;
230
+ const enhanced2 = lazy(async () => {
231
+ const { default: unlaziedRouter } = await unlazy(router);
232
+ const enhanced3 = enhanceRouter(unlaziedRouter, options);
233
+ return unlazy(enhanced3);
234
+ }, {
235
+ ...laziedMeta,
236
+ prefix: enhancedPrefix
237
+ });
238
+ const accessible = createAccessibleLazyRouter(enhanced2);
239
+ return accessible;
303
240
  }
304
- if (isLazy(options.router)) {
305
- laziedOptions.push({
306
- lazied: options.router,
241
+ if (isProcedure(router)) {
242
+ const newMiddlewares = mergeMiddlewares(options.middlewares, router["~orpc"].middlewares);
243
+ const newMiddlewareAdded = newMiddlewares.length - router["~orpc"].middlewares.length;
244
+ const enhanced2 = new Procedure({
245
+ ...router["~orpc"],
246
+ route: enhanceRoute(router["~orpc"].route, options),
247
+ errorMap: mergeErrorMap(options.errorMap, router["~orpc"].errorMap),
248
+ middlewares: newMiddlewares,
249
+ inputValidationIndex: router["~orpc"].inputValidationIndex + newMiddlewareAdded,
250
+ outputValidationIndex: router["~orpc"].outputValidationIndex + newMiddlewareAdded
251
+ });
252
+ return enhanced2;
253
+ }
254
+ const enhanced = {};
255
+ for (const key in router) {
256
+ enhanced[key] = enhanceRouter(router[key], options);
257
+ }
258
+ return enhanced;
259
+ }
260
+ function traverseContractProcedures(options, callback, lazyOptions = []) {
261
+ let currentRouter = options.router;
262
+ const hiddenContract = getHiddenRouterContract(options.router);
263
+ if (hiddenContract !== void 0) {
264
+ currentRouter = hiddenContract;
265
+ }
266
+ if (isLazy(currentRouter)) {
267
+ lazyOptions.push({
268
+ router: currentRouter,
307
269
  path: options.path
308
270
  });
309
- } else if (isContractProcedure2(options.router)) {
271
+ } else if (isContractProcedure(currentRouter)) {
310
272
  callback({
311
- contract: options.router,
273
+ contract: currentRouter,
312
274
  path: options.path
313
275
  });
314
276
  } else {
315
- for (const key in options.router) {
316
- eachContractProcedure(
277
+ for (const key in currentRouter) {
278
+ traverseContractProcedures(
317
279
  {
318
- router: options.router[key],
280
+ router: currentRouter[key],
319
281
  path: [...options.path, key]
320
282
  },
321
283
  callback,
322
- laziedOptions
284
+ lazyOptions
323
285
  );
324
286
  }
325
287
  }
326
- return laziedOptions;
288
+ return lazyOptions;
327
289
  }
328
- async function eachAllContractProcedure(options, callback) {
290
+ async function resolveContractProcedures(options, callback) {
329
291
  const pending = [options];
330
- for (const item of pending) {
331
- const lazies = eachContractProcedure(item, callback);
332
- for (const lazy2 of lazies) {
333
- const { default: router } = await unlazy(lazy2.lazied);
292
+ for (const options2 of pending) {
293
+ const lazyOptions = traverseContractProcedures(options2, callback);
294
+ for (const options3 of lazyOptions) {
295
+ const { default: router } = await unlazy(options3.router);
334
296
  pending.push({
335
- path: lazy2.path,
336
- router
297
+ router,
298
+ path: options3.path
337
299
  });
338
300
  }
339
301
  }
340
302
  }
341
- function convertPathToHttpPath(path) {
342
- return `/${path.map(encodeURIComponent).join("/")}`;
303
+ async function unlazyRouter(router) {
304
+ if (isProcedure(router)) {
305
+ return router;
306
+ }
307
+ const unlazied = {};
308
+ for (const key in router) {
309
+ const item = router[key];
310
+ const { default: unlaziedRouter } = await unlazy(item);
311
+ unlazied[key] = await unlazyRouter(unlaziedRouter);
312
+ }
313
+ return unlazied;
314
+ }
315
+
316
+ function createAssertedLazyProcedure(lazied) {
317
+ const lazyProcedure = lazy(async () => {
318
+ const { default: maybeProcedure } = await unlazy(lazied);
319
+ if (!isProcedure(maybeProcedure)) {
320
+ throw new Error(`
321
+ Expected a lazy<procedure> but got lazy<unknown>.
322
+ This should be caught by TypeScript compilation.
323
+ Please report this issue if this makes you feel uncomfortable.
324
+ `);
325
+ }
326
+ return { default: maybeProcedure };
327
+ }, getLazyMeta(lazied));
328
+ return lazyProcedure;
343
329
  }
344
- function createContractedProcedure(contract, procedure) {
330
+ function createContractedProcedure(procedure, contract) {
345
331
  return new Procedure({
346
332
  ...procedure["~orpc"],
347
333
  errorMap: contract["~orpc"].errorMap,
@@ -349,29 +335,12 @@ function createContractedProcedure(contract, procedure) {
349
335
  meta: contract["~orpc"].meta
350
336
  });
351
337
  }
338
+ function call(procedure, input, ...rest) {
339
+ return createProcedureClient(procedure, ...rest)(input);
340
+ }
341
+
342
+ function toHttpPath(path) {
343
+ return `/${path.map(encodeURIComponent).join("/")}`;
344
+ }
352
345
 
353
- export {
354
- LAZY_LOADER_SYMBOL,
355
- lazy,
356
- isLazy,
357
- unlazy,
358
- Procedure,
359
- isProcedure,
360
- flatLazy,
361
- createLazyProcedureFormAnyLazy,
362
- addMiddleware,
363
- middlewareOutputFn,
364
- createProcedureClient,
365
- setRouterContract,
366
- getRouterContract,
367
- deepSetLazyRouterPrefix,
368
- getLazyRouterPrefix,
369
- createAccessibleLazyRouter,
370
- adaptRouter,
371
- getRouterChild,
372
- eachContractProcedure,
373
- eachAllContractProcedure,
374
- convertPathToHttpPath,
375
- createContractedProcedure
376
- };
377
- //# sourceMappingURL=chunk-SV6DBVXJ.js.map
346
+ 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, unlazyRouter as p, 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, InferSchemaInput, HTTPPath, AnySchema, Meta, ContractProcedureDef, InferSchemaOutput, 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, InferSchemaInput<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, AnySchema, AnySchema, 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 AnySchema, TOutputSchema extends AnySchema, 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, any, any, any>;
58
+ }
59
+ declare class Procedure<TInitialContext extends Context, TCurrentContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> {
60
+ '~orpc': ProcedureDef<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, TErrorMap, TMeta>;
61
+ constructor(def: ProcedureDef<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, TErrorMap, TMeta>);
62
+ }
63
+ type AnyProcedure = Procedure<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, AnySchema, AnySchema, 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 AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> = Client<TClientContext, InferSchemaInput<TInputSchema>, InferSchemaOutput<TOutputSchema>, ErrorFromErrorMap<TErrorMap>>;
100
+ interface ProcedureClientInterceptorOptions<TInitialContext extends Context, TInputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> {
101
+ context: TInitialContext;
102
+ input: InferSchemaInput<TInputSchema>;
103
+ errors: ORPCErrorConstructorMap<TErrorMap>;
104
+ path: readonly string[];
105
+ procedure: Procedure<Context, Context, AnySchema, AnySchema, 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 AnySchema, TOutputSchema extends AnySchema, 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>, InferSchemaOutput<TOutputSchema>, 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 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, TInputSchema, TOutputSchema, TErrorMap, TMeta, TClientContext>>): ProcedureClient<TClientContext, TInputSchema, TOutputSchema, 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, 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> ? 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> ? 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> ? InferSchemaInput<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, any, any> ? InferSchemaOutput<UOutputSchema> : {
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 };