@orpc/server 0.0.0-next.1431467 → 0.0.0-next.15d9202

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 (32) hide show
  1. package/README.md +3 -1
  2. package/dist/adapters/fetch/index.d.mts +3 -3
  3. package/dist/adapters/fetch/index.d.ts +3 -3
  4. package/dist/adapters/fetch/index.mjs +3 -3
  5. package/dist/adapters/hono/index.d.mts +2 -2
  6. package/dist/adapters/hono/index.d.ts +2 -2
  7. package/dist/adapters/hono/index.mjs +3 -3
  8. package/dist/adapters/next/index.d.mts +2 -2
  9. package/dist/adapters/next/index.d.ts +2 -2
  10. package/dist/adapters/next/index.mjs +3 -3
  11. package/dist/adapters/node/index.d.mts +3 -3
  12. package/dist/adapters/node/index.d.ts +3 -3
  13. package/dist/adapters/node/index.mjs +2 -2
  14. package/dist/adapters/standard/index.d.mts +4 -4
  15. package/dist/adapters/standard/index.d.ts +4 -4
  16. package/dist/adapters/standard/index.mjs +2 -2
  17. package/dist/index.d.mts +135 -112
  18. package/dist/index.d.ts +135 -112
  19. package/dist/index.mjs +56 -43
  20. package/dist/plugins/index.d.mts +12 -12
  21. package/dist/plugins/index.d.ts +12 -12
  22. package/dist/plugins/index.mjs +1 -1
  23. package/dist/shared/{server.DKrKGnk2.mjs → server.3mOimouH.mjs} +8 -11
  24. package/dist/shared/{server.V6zT5iYQ.mjs → server.B_5ZADvP.mjs} +142 -158
  25. package/dist/shared/{server.BHIDiY4a.mjs → server.BgDZnmUZ.mjs} +1 -1
  26. package/dist/shared/{server.Drm1Lma3.d.ts → server.CL84X8p4.d.mts} +12 -14
  27. package/dist/shared/server.DnmJuN02.d.mts +144 -0
  28. package/dist/shared/server.DnmJuN02.d.ts +144 -0
  29. package/dist/shared/{server.CtBp-i4f.d.mts → server.hqPWnakL.d.ts} +12 -14
  30. package/package.json +7 -7
  31. package/dist/shared/server.ptXwNGQr.d.mts +0 -158
  32. package/dist/shared/server.ptXwNGQr.d.ts +0 -158
@@ -1,18 +1,48 @@
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
3
  import { value, intercept, toError } 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 {
@@ -28,52 +58,6 @@ function isProcedure(item) {
28
58
  return isContractProcedure(item) && "middlewares" in item["~orpc"] && "inputValidationIndex" in item["~orpc"] && "outputValidationIndex" in item["~orpc"] && "handler" in item["~orpc"];
29
59
  }
30
60
 
31
- function flatLazy(lazied) {
32
- const flattenLoader = async () => {
33
- let current = await unlazy(lazied);
34
- while (true) {
35
- if (!isLazy(current.default)) {
36
- break;
37
- }
38
- current = await unlazy(current.default);
39
- }
40
- return current;
41
- };
42
- return lazy(flattenLoader);
43
- }
44
- function createLazyProcedureFormAnyLazy(lazied) {
45
- const lazyProcedure = lazy(async () => {
46
- const { default: maybeProcedure } = await unlazy(flatLazy(lazied));
47
- if (!isProcedure(maybeProcedure)) {
48
- throw new Error(`
49
- Expected a lazy<procedure> but got lazy<unknown>.
50
- This should be caught by TypeScript compilation.
51
- Please report this issue if this makes you feel uncomfortable.
52
- `);
53
- }
54
- return { default: maybeProcedure };
55
- });
56
- return lazyProcedure;
57
- }
58
-
59
- function dedupeMiddlewares(compare, middlewares) {
60
- let min = 0;
61
- for (let i = 0; i < middlewares.length; i++) {
62
- const index = compare.indexOf(middlewares[i], min);
63
- if (index === -1) {
64
- return middlewares.slice(i);
65
- }
66
- min = index + 1;
67
- }
68
- return [];
69
- }
70
- function mergeMiddlewares(first, second) {
71
- return [...first, ...dedupeMiddlewares(first, second)];
72
- }
73
- function addMiddleware(middlewares, addition) {
74
- return [...middlewares, addition];
75
- }
76
-
77
61
  function createORPCErrorConstructorMap(errors) {
78
62
  const proxy = new Proxy(errors, {
79
63
  get(target, code) {
@@ -205,169 +189,162 @@ async function executeProcedureInternal(procedure, options) {
205
189
  return (await next({})).output;
206
190
  }
207
191
 
208
- const ROUTER_CONTRACT_SYMBOL = Symbol("ORPC_ROUTER_CONTRACT");
209
- function setRouterContract(obj, contract) {
210
- return new Proxy(obj, {
192
+ const HIDDEN_ROUTER_CONTRACT_SYMBOL = Symbol("ORPC_HIDDEN_ROUTER_CONTRACT");
193
+ function setHiddenRouterContract(router, contract) {
194
+ return new Proxy(router, {
211
195
  get(target, key) {
212
- if (key === ROUTER_CONTRACT_SYMBOL) {
196
+ if (key === HIDDEN_ROUTER_CONTRACT_SYMBOL) {
213
197
  return contract;
214
198
  }
215
199
  return Reflect.get(target, key);
216
200
  }
217
201
  });
218
202
  }
219
- function getRouterContract(obj) {
220
- return obj[ROUTER_CONTRACT_SYMBOL];
203
+ function getHiddenRouterContract(router) {
204
+ return router[HIDDEN_ROUTER_CONTRACT_SYMBOL];
221
205
  }
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;
206
+
207
+ function getRouter(router, path) {
208
+ let current = router;
209
+ for (let i = 0; i < path.length; i++) {
210
+ const segment = path[i];
211
+ if (!current) {
212
+ return void 0;
234
213
  }
235
- });
236
- }
237
- function getLazyRouterPrefix(obj) {
238
- return obj[LAZY_ROUTER_PREFIX_SYMBOL];
214
+ if (isProcedure(current)) {
215
+ return void 0;
216
+ }
217
+ if (!isLazy(current)) {
218
+ current = current[segment];
219
+ continue;
220
+ }
221
+ const lazied = current;
222
+ const rest = path.slice(i);
223
+ return lazy(async () => {
224
+ const unwrapped = await unlazy(lazied);
225
+ const next = getRouter(unwrapped.default, rest);
226
+ return unlazy(next);
227
+ }, getLazyMeta(lazied));
228
+ }
229
+ return current;
239
230
  }
240
-
241
231
  function createAccessibleLazyRouter(lazied) {
242
- const flattenLazy = flatLazy(lazied);
243
- const recursive = new Proxy(flattenLazy, {
232
+ const recursive = new Proxy(lazied, {
244
233
  get(target, key) {
245
234
  if (typeof key !== "string") {
246
235
  return Reflect.get(target, key);
247
236
  }
248
- const next = getRouterChild(flattenLazy, key);
237
+ const next = getRouter(lazied, [key]);
249
238
  return createAccessibleLazyRouter(next);
250
239
  }
251
240
  });
252
241
  return recursive;
253
242
  }
254
-
255
- function adaptRouter(router, options) {
243
+ function enhanceRouter(router, options) {
256
244
  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 };
245
+ const laziedMeta = getLazyMeta(router);
246
+ const enhancedPrefix = laziedMeta?.prefix ? mergePrefix(options.prefix, laziedMeta?.prefix) : options.prefix;
247
+ const enhanced2 = lazy(async () => {
248
+ const { default: unlaziedRouter } = await unlazy(router);
249
+ const enhanced3 = enhanceRouter(unlaziedRouter, options);
250
+ return unlazy(enhanced3);
251
+ }, {
252
+ ...laziedMeta,
253
+ prefix: enhancedPrefix
261
254
  });
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
- }
255
+ const accessible = createAccessibleLazyRouter(enhanced2);
268
256
  return accessible;
269
257
  }
270
258
  if (isProcedure(router)) {
271
- const newMiddlewares = mergeMiddlewares(options.middlewares, router["~orpc"].middlewares);
259
+ const newMiddlewares = mergeMiddlewares(options.middlewares, router["~orpc"].middlewares, { dedupeLeading: options.dedupeLeadingMiddlewares });
272
260
  const newMiddlewareAdded = newMiddlewares.length - router["~orpc"].middlewares.length;
273
- const adapted2 = new Procedure({
261
+ const enhanced2 = new Procedure({
274
262
  ...router["~orpc"],
275
- route: adaptRoute(router["~orpc"].route, options),
263
+ route: enhanceRoute(router["~orpc"].route, options),
276
264
  errorMap: mergeErrorMap(options.errorMap, router["~orpc"].errorMap),
277
265
  middlewares: newMiddlewares,
278
266
  inputValidationIndex: router["~orpc"].inputValidationIndex + newMiddlewareAdded,
279
267
  outputValidationIndex: router["~orpc"].outputValidationIndex + newMiddlewareAdded
280
268
  });
281
- return adapted2;
269
+ return enhanced2;
282
270
  }
283
- const adapted = {};
271
+ const enhanced = {};
284
272
  for (const key in router) {
285
- adapted[key] = adaptRouter(router[key], options);
273
+ enhanced[key] = enhanceRouter(router[key], options);
286
274
  }
287
- return adapted;
275
+ return enhanced;
288
276
  }
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);
277
+ function traverseContractProcedures(options, callback, lazyOptions = []) {
278
+ let currentRouter = options.router;
279
+ const hiddenContract = getHiddenRouterContract(options.router);
280
+ if (hiddenContract !== void 0) {
281
+ currentRouter = hiddenContract;
314
282
  }
315
- return current;
316
- }
317
-
318
- function eachContractProcedure(options, callback, laziedOptions = []) {
319
- const hiddenContract = getRouterContract(options.router);
320
- if (hiddenContract) {
321
- return eachContractProcedure(
322
- {
323
- router: hiddenContract,
324
- path: options.path
325
- },
326
- callback,
327
- laziedOptions
328
- );
329
- }
330
- if (isLazy(options.router)) {
331
- laziedOptions.push({
332
- lazied: options.router,
283
+ if (isLazy(currentRouter)) {
284
+ lazyOptions.push({
285
+ router: currentRouter,
333
286
  path: options.path
334
287
  });
335
- } else if (isContractProcedure(options.router)) {
288
+ } else if (isContractProcedure(currentRouter)) {
336
289
  callback({
337
- contract: options.router,
290
+ contract: currentRouter,
338
291
  path: options.path
339
292
  });
340
293
  } else {
341
- for (const key in options.router) {
342
- eachContractProcedure(
294
+ for (const key in currentRouter) {
295
+ traverseContractProcedures(
343
296
  {
344
- router: options.router[key],
297
+ router: currentRouter[key],
345
298
  path: [...options.path, key]
346
299
  },
347
300
  callback,
348
- laziedOptions
301
+ lazyOptions
349
302
  );
350
303
  }
351
304
  }
352
- return laziedOptions;
305
+ return lazyOptions;
353
306
  }
354
- async function eachAllContractProcedure(options, callback) {
307
+ async function resolveContractProcedures(options, callback) {
355
308
  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);
309
+ for (const options2 of pending) {
310
+ const lazyOptions = traverseContractProcedures(options2, callback);
311
+ for (const options3 of lazyOptions) {
312
+ const { default: router } = await unlazy(options3.router);
360
313
  pending.push({
361
- path: lazy.path,
362
- router
314
+ router,
315
+ path: options3.path
363
316
  });
364
317
  }
365
318
  }
366
319
  }
367
- function convertPathToHttpPath(path) {
368
- return `/${path.map(encodeURIComponent).join("/")}`;
320
+ async function unlazyRouter(router) {
321
+ if (isProcedure(router)) {
322
+ return router;
323
+ }
324
+ const unlazied = {};
325
+ for (const key in router) {
326
+ const item = router[key];
327
+ const { default: unlaziedRouter } = await unlazy(item);
328
+ unlazied[key] = await unlazyRouter(unlaziedRouter);
329
+ }
330
+ return unlazied;
369
331
  }
370
- function createContractedProcedure(contract, procedure) {
332
+
333
+ function createAssertedLazyProcedure(lazied) {
334
+ const lazyProcedure = lazy(async () => {
335
+ const { default: maybeProcedure } = await unlazy(lazied);
336
+ if (!isProcedure(maybeProcedure)) {
337
+ throw new Error(`
338
+ Expected a lazy<procedure> but got lazy<unknown>.
339
+ This should be caught by TypeScript compilation.
340
+ Please report this issue if this makes you feel uncomfortable.
341
+ `);
342
+ }
343
+ return { default: maybeProcedure };
344
+ }, getLazyMeta(lazied));
345
+ return lazyProcedure;
346
+ }
347
+ function createContractedProcedure(procedure, contract) {
371
348
  return new Procedure({
372
349
  ...procedure["~orpc"],
373
350
  errorMap: contract["~orpc"].errorMap,
@@ -375,5 +352,12 @@ function createContractedProcedure(contract, procedure) {
375
352
  meta: contract["~orpc"].meta
376
353
  });
377
354
  }
355
+ function call(procedure, input, ...rest) {
356
+ return createProcedureClient(procedure, ...rest)(input);
357
+ }
358
+
359
+ function toHttpPath(path) {
360
+ return `/${path.map(encodeURIComponent).join("/")}`;
361
+ }
378
362
 
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 };
363
+ 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, createORPCErrorConstructorMap as j, getLazyMeta as k, lazy as l, middlewareOutputFn 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 };
@@ -1,6 +1,6 @@
1
1
  import { RPCSerializer } from '@orpc/client/standard';
2
2
  import { toStandardLazyRequest, toFetchResponse } from '@orpc/standard-server-fetch';
3
- import { S as StandardHandler, a as RPCMatcher, R as RPCCodec } from './server.DKrKGnk2.mjs';
3
+ import { S as StandardHandler, a as RPCMatcher, R as RPCCodec } from './server.3mOimouH.mjs';
4
4
 
5
5
  class RPCHandler {
6
6
  standardHandler;
@@ -1,12 +1,12 @@
1
- import { HTTPPath, Schema, Meta, SchemaOutput, ErrorFromErrorMap } from '@orpc/contract';
1
+ import { HTTPPath, AnySchema, Meta, InferSchemaOutput, ErrorFromErrorMap } from '@orpc/contract';
2
2
  import { Interceptor, MaybeOptionalOptions } from '@orpc/shared';
3
3
  import { StandardResponse, StandardLazyRequest } from '@orpc/standard-server';
4
- import { a as AnyRouter, A as AnyProcedure, C as Context, P as ProcedureClientInterceptorOptions, R as Router } from './server.ptXwNGQr.js';
4
+ import { a as AnyRouter, A as AnyProcedure, C as Context, P as ProcedureClientInterceptorOptions, R as Router } from './server.DnmJuN02.mjs';
5
5
  import { ORPCError } from '@orpc/client';
6
6
 
7
7
  type StandardParams = Record<string, string>;
8
8
  type StandardMatchResult = {
9
- path: string[];
9
+ path: readonly string[];
10
10
  procedure: AnyProcedure;
11
11
  params?: StandardParams;
12
12
  } | undefined;
@@ -27,9 +27,6 @@ type StandardHandleOptions<T extends Context> = {
27
27
  } : {
28
28
  context: T;
29
29
  });
30
- type WellStandardHandleOptions<T extends Context> = StandardHandleOptions<T> & {
31
- context: T;
32
- };
33
30
  type StandardHandleResult = {
34
31
  matched: true;
35
32
  response: StandardResponse;
@@ -37,11 +34,12 @@ type StandardHandleResult = {
37
34
  matched: false;
38
35
  response: undefined;
39
36
  };
40
- type StandardHandlerInterceptorOptions<TContext extends Context> = WellStandardHandleOptions<TContext> & {
37
+ type StandardHandlerInterceptorOptions<T extends Context> = StandardHandleOptions<T> & {
38
+ context: T;
41
39
  request: StandardLazyRequest;
42
40
  };
43
41
  interface StandardHandlerOptions<TContext extends Context> {
44
- plugins?: Plugin<TContext>[];
42
+ plugins?: HandlerPlugin<TContext>[];
45
43
  /**
46
44
  * Interceptors at the request level, helpful when you want catch errors
47
45
  */
@@ -54,24 +52,24 @@ interface StandardHandlerOptions<TContext extends Context> {
54
52
  *
55
53
  * Interceptors for procedure client.
56
54
  */
57
- clientInterceptors?: Interceptor<ProcedureClientInterceptorOptions<TContext, Schema, Record<never, never>, Meta>, SchemaOutput<Schema, unknown>, ErrorFromErrorMap<Record<never, never>>>[];
55
+ clientInterceptors?: Interceptor<ProcedureClientInterceptorOptions<TContext, AnySchema, Record<never, never>, Meta>, InferSchemaOutput<AnySchema>, ErrorFromErrorMap<Record<never, never>>>[];
58
56
  }
59
57
  declare class StandardHandler<T extends Context> {
60
58
  private readonly matcher;
61
59
  private readonly codec;
62
60
  private readonly options;
63
61
  private readonly plugin;
64
- constructor(router: Router<T, any>, matcher: StandardMatcher, codec: StandardCodec, options: NoInfer<StandardHandlerOptions<T>>);
62
+ constructor(router: Router<any, T>, matcher: StandardMatcher, codec: StandardCodec, options: NoInfer<StandardHandlerOptions<T>>);
65
63
  handle(request: StandardLazyRequest, ...[options]: MaybeOptionalOptions<StandardHandleOptions<T>>): Promise<StandardHandleResult>;
66
64
  }
67
65
 
68
- interface Plugin<TContext extends Context> {
66
+ interface HandlerPlugin<TContext extends Context> {
69
67
  init?(options: StandardHandlerOptions<TContext>): void;
70
68
  }
71
- declare class CompositePlugin<TContext extends Context> implements Plugin<TContext> {
69
+ declare class CompositePlugin<TContext extends Context> implements HandlerPlugin<TContext> {
72
70
  private readonly plugins;
73
- constructor(plugins?: Plugin<TContext>[]);
71
+ constructor(plugins?: HandlerPlugin<TContext>[]);
74
72
  init(options: StandardHandlerOptions<TContext>): void;
75
73
  }
76
74
 
77
- export { CompositePlugin as C, type Plugin as P, type StandardHandleOptions as S, type WellStandardHandleOptions as W, type StandardHandlerInterceptorOptions as a, type StandardHandlerOptions as b, type StandardCodec as c, type StandardParams as d, type StandardMatcher as e, type StandardMatchResult as f, type StandardHandleResult as g, StandardHandler as h };
75
+ export { CompositePlugin as C, type HandlerPlugin as H, type StandardHandleOptions as S, type StandardHandlerInterceptorOptions as a, type StandardHandlerOptions as b, type StandardCodec as c, type StandardParams as d, type StandardMatcher as e, type StandardMatchResult as f, type StandardHandleResult as g, StandardHandler as h };
@@ -0,0 +1,144 @@
1
+ import { ORPCErrorCode, ORPCErrorOptions, ORPCError, ClientContext, Client } from '@orpc/client';
2
+ import { 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 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
+ type ContextExtendsGuard<T extends Context, U extends Context> = T extends T & U ? unknown : never;
10
+
11
+ type ORPCErrorConstructorMapItemOptions<TData> = Omit<ORPCErrorOptions<TData>, 'defined' | 'status'>;
12
+ type ORPCErrorConstructorMapItem<TCode extends ORPCErrorCode, TInData> = (...rest: MaybeOptionalOptions<ORPCErrorConstructorMapItemOptions<TInData>>) => ORPCError<TCode, TInData>;
13
+ type ORPCErrorConstructorMap<T extends ErrorMap> = {
14
+ [K in keyof T]: K extends ORPCErrorCode ? T[K] extends ErrorMapItem<infer UInputSchema> ? ORPCErrorConstructorMapItem<K, InferSchemaInput<UInputSchema>> : never : never;
15
+ };
16
+ declare function createORPCErrorConstructorMap<T extends ErrorMap>(errors: T): ORPCErrorConstructorMap<T>;
17
+ declare function validateORPCError(map: ErrorMap, error: ORPCError<any, any>): Promise<ORPCError<string, unknown>>;
18
+
19
+ declare const LAZY_SYMBOL: unique symbol;
20
+ interface LazyMeta {
21
+ prefix?: HTTPPath;
22
+ }
23
+ interface Lazy<T> {
24
+ [LAZY_SYMBOL]: {
25
+ loader: () => Promise<{
26
+ default: T;
27
+ }>;
28
+ meta: LazyMeta;
29
+ };
30
+ }
31
+ type Lazyable<T> = T | Lazy<T>;
32
+ declare function lazy<T>(loader: () => Promise<{
33
+ default: T;
34
+ }>, meta?: LazyMeta): Lazy<T>;
35
+ declare function isLazy(item: unknown): item is Lazy<any>;
36
+ declare function getLazyMeta(lazied: Lazy<any>): LazyMeta;
37
+ declare function unlazy<T extends Lazyable<any>>(lazied: T): Promise<{
38
+ default: T extends Lazy<infer U> ? U : T;
39
+ }>;
40
+
41
+ interface ProcedureHandlerOptions<TCurrentContext extends Context, TInput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
42
+ context: TCurrentContext;
43
+ input: TInput;
44
+ path: readonly string[];
45
+ procedure: Procedure<Context, Context, AnySchema, AnySchema, ErrorMap, TMeta>;
46
+ signal?: AbortSignal;
47
+ lastEventId: string | undefined;
48
+ errors: TErrorConstructorMap;
49
+ }
50
+ interface ProcedureHandler<TCurrentContext extends Context, TInput, THandlerOutput, TErrorMap extends ErrorMap, TMeta extends Meta> {
51
+ (opt: ProcedureHandlerOptions<TCurrentContext, TInput, ORPCErrorConstructorMap<TErrorMap>, TMeta>): Promisable<THandlerOutput>;
52
+ }
53
+ 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> {
54
+ __initialContext?: (type: TInitialContext) => unknown;
55
+ middlewares: readonly AnyMiddleware[];
56
+ inputValidationIndex: number;
57
+ outputValidationIndex: number;
58
+ handler: ProcedureHandler<TCurrentContext, any, any, any, any>;
59
+ }
60
+ declare class Procedure<TInitialContext extends Context, TCurrentContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> {
61
+ '~orpc': ProcedureDef<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, TErrorMap, TMeta>;
62
+ constructor(def: ProcedureDef<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, TErrorMap, TMeta>);
63
+ }
64
+ type AnyProcedure = Procedure<any, any, any, any, any, any>;
65
+ declare function isProcedure(item: unknown): item is AnyProcedure;
66
+
67
+ type MiddlewareResult<TOutContext extends Context, TOutput> = Promisable<{
68
+ output: TOutput;
69
+ context: TOutContext;
70
+ }>;
71
+ type MiddlewareNextFnOptions<TOutContext extends Context> = Record<never, never> extends TOutContext ? {
72
+ context?: TOutContext;
73
+ } : {
74
+ context: TOutContext;
75
+ };
76
+ interface MiddlewareNextFn<TOutput> {
77
+ <U extends Context = Record<never, never>>(...rest: MaybeOptionalOptions<MiddlewareNextFnOptions<U>>): MiddlewareResult<U, TOutput>;
78
+ }
79
+ interface MiddlewareOutputFn<TOutput> {
80
+ (output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
81
+ }
82
+ interface MiddlewareOptions<TInContext extends Context, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
83
+ context: TInContext;
84
+ path: readonly string[];
85
+ procedure: Procedure<Context, Context, AnySchema, AnySchema, ErrorMap, TMeta>;
86
+ signal?: AbortSignal;
87
+ lastEventId: string | undefined;
88
+ next: MiddlewareNextFn<TOutput>;
89
+ errors: TErrorConstructorMap;
90
+ }
91
+ interface Middleware<TInContext extends Context, TOutContext extends Context, TInput, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
92
+ (options: MiddlewareOptions<TInContext, TOutput, TErrorConstructorMap, TMeta>, input: TInput, output: MiddlewareOutputFn<TOutput>): Promisable<MiddlewareResult<TOutContext, TOutput>>;
93
+ }
94
+ type AnyMiddleware = Middleware<any, any, any, any, any, any>;
95
+ interface MapInputMiddleware<TInput, TMappedInput> {
96
+ (input: TInput): TMappedInput;
97
+ }
98
+ declare function middlewareOutputFn<TOutput>(output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
99
+
100
+ type ProcedureClient<TClientContext extends ClientContext, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> = Client<TClientContext, InferSchemaInput<TInputSchema>, InferSchemaOutput<TOutputSchema>, ErrorFromErrorMap<TErrorMap>>;
101
+ interface ProcedureClientInterceptorOptions<TInitialContext extends Context, TInputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> {
102
+ context: TInitialContext;
103
+ input: InferSchemaInput<TInputSchema>;
104
+ errors: ORPCErrorConstructorMap<TErrorMap>;
105
+ path: readonly string[];
106
+ procedure: Procedure<Context, Context, AnySchema, AnySchema, ErrorMap, TMeta>;
107
+ signal?: AbortSignal;
108
+ lastEventId: string | undefined;
109
+ }
110
+ /**
111
+ * Options for creating a procedure caller with comprehensive type safety
112
+ */
113
+ type CreateProcedureClientOptions<TInitialContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext> = {
114
+ /**
115
+ * This is helpful for logging and analytics.
116
+ */
117
+ path?: readonly string[];
118
+ interceptors?: Interceptor<ProcedureClientInterceptorOptions<TInitialContext, TInputSchema, TErrorMap, TMeta>, InferSchemaOutput<TOutputSchema>, ErrorFromErrorMap<TErrorMap>>[];
119
+ } & (Record<never, never> extends TInitialContext ? {
120
+ context?: Value<TInitialContext, [clientContext: TClientContext]>;
121
+ } : {
122
+ context: Value<TInitialContext, [clientContext: TClientContext]>;
123
+ });
124
+ 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>;
125
+
126
+ 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> : {
127
+ [K in keyof T]: T[K] extends AnyContractRouter ? Lazyable<Router<T[K], TInitialContext>> : never;
128
+ };
129
+ type AnyRouter = Router<any, any>;
130
+ type InferRouterInitialContext<T extends AnyRouter> = T extends Router<any, infer UInitialContext> ? UInitialContext : never;
131
+ type InferRouterInitialContexts<T extends AnyRouter> = T extends Procedure<infer UInitialContext, any, any, any, any, any> ? UInitialContext : {
132
+ [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInitialContexts<U> : never;
133
+ };
134
+ type InferRouterCurrentContexts<T extends AnyRouter> = T extends Procedure<any, infer UCurrentContext, any, any, any, any> ? UCurrentContext : {
135
+ [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterCurrentContexts<U> : never;
136
+ };
137
+ type InferRouterInputs<T extends AnyRouter> = T extends Procedure<any, any, infer UInputSchema, any, any, any> ? InferSchemaInput<UInputSchema> : {
138
+ [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInputs<U> : never;
139
+ };
140
+ type InferRouterOutputs<T extends AnyRouter> = T extends Procedure<any, any, any, infer UOutputSchema, any, any> ? InferSchemaOutput<UOutputSchema> : {
141
+ [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterOutputs<U> : never;
142
+ };
143
+
144
+ export { type AnyProcedure as A, type MiddlewareOptions as B, type Context as C, middlewareOutputFn as D, type ProcedureHandlerOptions as E, type ProcedureDef as F, isProcedure as G, createProcedureClient as H, type InferRouterInitialContext as I, type InferRouterInitialContexts as J, type InferRouterCurrentContexts as K, type Lazyable as L, type Middleware as M, type InferRouterInputs as N, type ORPCErrorConstructorMap as O, type ProcedureClientInterceptorOptions as P, type InferRouterOutputs as Q, type Router as R, type AnyRouter as a, Procedure as b, type ContextExtendsGuard as c, type MergedCurrentContext as d, type MergedInitialContext as e, type MapInputMiddleware as f, type CreateProcedureClientOptions as g, type ProcedureClient as h, type AnyMiddleware as i, type Lazy as j, type ProcedureHandler as k, type ORPCErrorConstructorMapItemOptions as l, mergeCurrentContext as m, type ORPCErrorConstructorMapItem as n, createORPCErrorConstructorMap as o, LAZY_SYMBOL as p, type LazyMeta as q, lazy as r, isLazy as s, getLazyMeta as t, unlazy as u, validateORPCError as v, type MiddlewareResult as w, type MiddlewareNextFnOptions as x, type MiddlewareNextFn as y, type MiddlewareOutputFn as z };