@orpc/server 0.0.0-next.0165ee0 → 0.0.0-next.05a8f88

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 (43) hide show
  1. package/README.md +14 -1
  2. package/dist/adapters/fetch/index.d.mts +38 -11
  3. package/dist/adapters/fetch/index.d.ts +38 -11
  4. package/dist/adapters/fetch/index.mjs +6 -6
  5. package/dist/adapters/hono/index.d.mts +6 -4
  6. package/dist/adapters/hono/index.d.ts +6 -4
  7. package/dist/adapters/hono/index.mjs +6 -6
  8. package/dist/adapters/next/index.d.mts +6 -4
  9. package/dist/adapters/next/index.d.ts +6 -4
  10. package/dist/adapters/next/index.mjs +6 -6
  11. package/dist/adapters/node/index.d.mts +40 -22
  12. package/dist/adapters/node/index.d.ts +40 -22
  13. package/dist/adapters/node/index.mjs +78 -21
  14. package/dist/adapters/standard/index.d.mts +14 -17
  15. package/dist/adapters/standard/index.d.ts +14 -17
  16. package/dist/adapters/standard/index.mjs +2 -3
  17. package/dist/index.d.mts +160 -124
  18. package/dist/index.d.ts +160 -124
  19. package/dist/index.mjs +78 -48
  20. package/dist/plugins/index.d.mts +15 -16
  21. package/dist/plugins/index.d.ts +15 -16
  22. package/dist/plugins/index.mjs +10 -6
  23. package/dist/shared/server.B77ImKAP.d.mts +8 -0
  24. package/dist/shared/server.BHlRCrf_.d.ts +66 -0
  25. package/dist/shared/server.BVwwTHyO.mjs +9 -0
  26. package/dist/shared/server.Cud5qk0c.d.ts +10 -0
  27. package/dist/shared/server.CzxlqYZL.d.mts +66 -0
  28. package/dist/shared/server.DGnN7q3R.d.mts +10 -0
  29. package/dist/shared/server.DLt5njUb.d.mts +143 -0
  30. package/dist/shared/server.DLt5njUb.d.ts +143 -0
  31. package/dist/shared/server.DUF89eb-.d.ts +8 -0
  32. package/dist/shared/{server.BBGuTxHE.mjs → server.Dfx1jV-K.mjs} +43 -45
  33. package/dist/shared/server.T5WmDoWQ.mjs +98 -0
  34. package/dist/shared/{server.V6zT5iYQ.mjs → server.e3W6AG3-.mjs} +153 -162
  35. package/package.json +8 -8
  36. package/dist/shared/server.B_cAGti1.d.mts +0 -9
  37. package/dist/shared/server.CUE4Aija.mjs +0 -24
  38. package/dist/shared/server.Cn9ybJtE.d.mts +0 -152
  39. package/dist/shared/server.Cn9ybJtE.d.ts +0 -152
  40. package/dist/shared/server.CynXWJja.d.ts +0 -9
  41. package/dist/shared/server.D86dtDX_.d.mts +0 -77
  42. package/dist/shared/server.Q6ZmnTgO.mjs +0 -12
  43. package/dist/shared/server.dPmfqnQI.d.ts +0 -77
@@ -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
- 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 {
@@ -28,50 +58,8 @@ 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];
61
+ function mergeCurrentContext(context, other) {
62
+ return { ...context, ...other };
75
63
  }
76
64
 
77
65
  function createORPCErrorConstructorMap(errors) {
@@ -139,7 +127,7 @@ function createProcedureClient(lazyableProcedure, ...[options]) {
139
127
  );
140
128
  } catch (e) {
141
129
  if (!(e instanceof ORPCError)) {
142
- throw toError(e);
130
+ throw e;
143
131
  }
144
132
  const validated = await validateORPCError(procedure["~orpc"].errorMap, e);
145
133
  throw validated;
@@ -186,188 +174,188 @@ async function executeProcedureInternal(procedure, options) {
186
174
  let currentInput = options.input;
187
175
  const next = async (...[nextOptions]) => {
188
176
  const index = currentIndex;
177
+ const midContext = nextOptions?.context ?? {};
189
178
  currentIndex += 1;
190
- currentContext = { ...currentContext, ...nextOptions?.context };
179
+ currentContext = mergeCurrentContext(currentContext, midContext);
191
180
  if (index === inputValidationIndex) {
192
181
  currentInput = await validateInput(procedure, currentInput);
193
182
  }
194
183
  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 };
184
+ const result = mid ? {
185
+ context: midContext,
186
+ output: (await mid({ ...options, context: currentContext, next }, currentInput, middlewareOutputFn)).output
187
+ } : {
188
+ context: midContext,
189
+ output: await procedure["~orpc"].handler({ ...options, context: currentContext, input: currentInput })
190
+ };
196
191
  if (index === outputValidationIndex) {
197
192
  const validatedOutput = await validateOutput(procedure, result.output);
198
193
  return {
199
- ...result,
194
+ context: result.context,
200
195
  output: validatedOutput
201
196
  };
202
197
  }
203
198
  return result;
204
199
  };
205
- return (await next({})).output;
200
+ return (await next()).output;
206
201
  }
207
202
 
208
- const ROUTER_CONTRACT_SYMBOL = Symbol("ORPC_ROUTER_CONTRACT");
209
- function setRouterContract(obj, contract) {
210
- return new Proxy(obj, {
203
+ const HIDDEN_ROUTER_CONTRACT_SYMBOL = Symbol("ORPC_HIDDEN_ROUTER_CONTRACT");
204
+ function setHiddenRouterContract(router, contract) {
205
+ return new Proxy(router, {
211
206
  get(target, key) {
212
- if (key === ROUTER_CONTRACT_SYMBOL) {
207
+ if (key === HIDDEN_ROUTER_CONTRACT_SYMBOL) {
213
208
  return contract;
214
209
  }
215
210
  return Reflect.get(target, key);
216
211
  }
217
212
  });
218
213
  }
219
- function getRouterContract(obj) {
220
- return obj[ROUTER_CONTRACT_SYMBOL];
214
+ function getHiddenRouterContract(router) {
215
+ return router[HIDDEN_ROUTER_CONTRACT_SYMBOL];
221
216
  }
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;
217
+
218
+ function getRouter(router, path) {
219
+ let current = router;
220
+ for (let i = 0; i < path.length; i++) {
221
+ const segment = path[i];
222
+ if (!current) {
223
+ return void 0;
234
224
  }
235
- });
236
- }
237
- function getLazyRouterPrefix(obj) {
238
- return obj[LAZY_ROUTER_PREFIX_SYMBOL];
225
+ if (isProcedure(current)) {
226
+ return void 0;
227
+ }
228
+ if (!isLazy(current)) {
229
+ current = current[segment];
230
+ continue;
231
+ }
232
+ const lazied = current;
233
+ const rest = path.slice(i);
234
+ return lazy(async () => {
235
+ const unwrapped = await unlazy(lazied);
236
+ const next = getRouter(unwrapped.default, rest);
237
+ return unlazy(next);
238
+ }, getLazyMeta(lazied));
239
+ }
240
+ return current;
239
241
  }
240
-
241
242
  function createAccessibleLazyRouter(lazied) {
242
- const flattenLazy = flatLazy(lazied);
243
- const recursive = new Proxy(flattenLazy, {
243
+ const recursive = new Proxy(lazied, {
244
244
  get(target, key) {
245
245
  if (typeof key !== "string") {
246
246
  return Reflect.get(target, key);
247
247
  }
248
- const next = getRouterChild(flattenLazy, key);
248
+ const next = getRouter(lazied, [key]);
249
249
  return createAccessibleLazyRouter(next);
250
250
  }
251
251
  });
252
252
  return recursive;
253
253
  }
254
-
255
- function adaptRouter(router, options) {
254
+ function enhanceRouter(router, options) {
256
255
  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 };
256
+ const laziedMeta = getLazyMeta(router);
257
+ const enhancedPrefix = laziedMeta?.prefix ? mergePrefix(options.prefix, laziedMeta?.prefix) : options.prefix;
258
+ const enhanced2 = lazy(async () => {
259
+ const { default: unlaziedRouter } = await unlazy(router);
260
+ const enhanced3 = enhanceRouter(unlaziedRouter, options);
261
+ return unlazy(enhanced3);
262
+ }, {
263
+ ...laziedMeta,
264
+ prefix: enhancedPrefix
261
265
  });
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
- }
266
+ const accessible = createAccessibleLazyRouter(enhanced2);
268
267
  return accessible;
269
268
  }
270
269
  if (isProcedure(router)) {
271
- const newMiddlewares = mergeMiddlewares(options.middlewares, router["~orpc"].middlewares);
270
+ const newMiddlewares = mergeMiddlewares(options.middlewares, router["~orpc"].middlewares, { dedupeLeading: options.dedupeLeadingMiddlewares });
272
271
  const newMiddlewareAdded = newMiddlewares.length - router["~orpc"].middlewares.length;
273
- const adapted2 = new Procedure({
272
+ const enhanced2 = new Procedure({
274
273
  ...router["~orpc"],
275
- route: adaptRoute(router["~orpc"].route, options),
274
+ route: enhanceRoute(router["~orpc"].route, options),
276
275
  errorMap: mergeErrorMap(options.errorMap, router["~orpc"].errorMap),
277
276
  middlewares: newMiddlewares,
278
277
  inputValidationIndex: router["~orpc"].inputValidationIndex + newMiddlewareAdded,
279
278
  outputValidationIndex: router["~orpc"].outputValidationIndex + newMiddlewareAdded
280
279
  });
281
- return adapted2;
280
+ return enhanced2;
282
281
  }
283
- const adapted = {};
282
+ const enhanced = {};
284
283
  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);
284
+ enhanced[key] = enhanceRouter(router[key], options);
314
285
  }
315
- return current;
286
+ return enhanced;
316
287
  }
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
- );
288
+ function traverseContractProcedures(options, callback, lazyOptions = []) {
289
+ let currentRouter = options.router;
290
+ const hiddenContract = getHiddenRouterContract(options.router);
291
+ if (hiddenContract !== void 0) {
292
+ currentRouter = hiddenContract;
329
293
  }
330
- if (isLazy(options.router)) {
331
- laziedOptions.push({
332
- lazied: options.router,
294
+ if (isLazy(currentRouter)) {
295
+ lazyOptions.push({
296
+ router: currentRouter,
333
297
  path: options.path
334
298
  });
335
- } else if (isContractProcedure(options.router)) {
299
+ } else if (isContractProcedure(currentRouter)) {
336
300
  callback({
337
- contract: options.router,
301
+ contract: currentRouter,
338
302
  path: options.path
339
303
  });
340
304
  } else {
341
- for (const key in options.router) {
342
- eachContractProcedure(
305
+ for (const key in currentRouter) {
306
+ traverseContractProcedures(
343
307
  {
344
- router: options.router[key],
308
+ router: currentRouter[key],
345
309
  path: [...options.path, key]
346
310
  },
347
311
  callback,
348
- laziedOptions
312
+ lazyOptions
349
313
  );
350
314
  }
351
315
  }
352
- return laziedOptions;
316
+ return lazyOptions;
353
317
  }
354
- async function eachAllContractProcedure(options, callback) {
318
+ async function resolveContractProcedures(options, callback) {
355
319
  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);
320
+ for (const options2 of pending) {
321
+ const lazyOptions = traverseContractProcedures(options2, callback);
322
+ for (const options3 of lazyOptions) {
323
+ const { default: router } = await unlazy(options3.router);
360
324
  pending.push({
361
- path: lazy.path,
362
- router
325
+ router,
326
+ path: options3.path
363
327
  });
364
328
  }
365
329
  }
366
330
  }
367
- function convertPathToHttpPath(path) {
368
- return `/${path.map(encodeURIComponent).join("/")}`;
331
+ async function unlazyRouter(router) {
332
+ if (isProcedure(router)) {
333
+ return router;
334
+ }
335
+ const unlazied = {};
336
+ for (const key in router) {
337
+ const item = router[key];
338
+ const { default: unlaziedRouter } = await unlazy(item);
339
+ unlazied[key] = await unlazyRouter(unlaziedRouter);
340
+ }
341
+ return unlazied;
342
+ }
343
+
344
+ function createAssertedLazyProcedure(lazied) {
345
+ const lazyProcedure = lazy(async () => {
346
+ const { default: maybeProcedure } = await unlazy(lazied);
347
+ if (!isProcedure(maybeProcedure)) {
348
+ throw new Error(`
349
+ Expected a lazy<procedure> but got lazy<unknown>.
350
+ This should be caught by TypeScript compilation.
351
+ Please report this issue if this makes you feel uncomfortable.
352
+ `);
353
+ }
354
+ return { default: maybeProcedure };
355
+ }, getLazyMeta(lazied));
356
+ return lazyProcedure;
369
357
  }
370
- function createContractedProcedure(contract, procedure) {
358
+ function createContractedProcedure(procedure, contract) {
371
359
  return new Procedure({
372
360
  ...procedure["~orpc"],
373
361
  errorMap: contract["~orpc"].errorMap,
@@ -375,5 +363,8 @@ function createContractedProcedure(contract, procedure) {
375
363
  meta: contract["~orpc"].meta
376
364
  });
377
365
  }
366
+ function call(procedure, input, ...rest) {
367
+ return createProcedureClient(procedure, ...rest)(input);
368
+ }
378
369
 
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 };
370
+ 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 };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@orpc/server",
3
3
  "type": "module",
4
- "version": "0.0.0-next.0165ee0",
4
+ "version": "0.0.0-next.05a8f88",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -58,15 +58,15 @@
58
58
  "next": ">=14.0.0"
59
59
  },
60
60
  "dependencies": {
61
- "@orpc/client": "0.0.0-next.0165ee0",
62
- "@orpc/shared": "0.0.0-next.0165ee0",
63
- "@orpc/contract": "0.0.0-next.0165ee0",
64
- "@orpc/standard-server-fetch": "0.0.0-next.0165ee0",
65
- "@orpc/standard-server": "0.0.0-next.0165ee0",
66
- "@orpc/standard-server-node": "0.0.0-next.0165ee0"
61
+ "@orpc/client": "0.0.0-next.05a8f88",
62
+ "@orpc/contract": "0.0.0-next.05a8f88",
63
+ "@orpc/standard-server": "0.0.0-next.05a8f88",
64
+ "@orpc/shared": "0.0.0-next.05a8f88",
65
+ "@orpc/standard-server-node": "0.0.0-next.05a8f88",
66
+ "@orpc/standard-server-fetch": "0.0.0-next.05a8f88"
67
67
  },
68
68
  "devDependencies": {
69
- "light-my-request": "^6.5.1"
69
+ "supertest": "^7.0.0"
70
70
  },
71
71
  "scripts": {
72
72
  "build": "unbuild",
@@ -1,9 +0,0 @@
1
- import { C as Context } from './server.Cn9ybJtE.mjs';
2
- import { a as StandardHandlerOptions, b as StandardMatcher, c as StandardCodec } from './server.D86dtDX_.mjs';
3
-
4
- interface RPCHandlerOptions<T extends Context> extends StandardHandlerOptions<T> {
5
- matcher?: StandardMatcher;
6
- codec?: StandardCodec;
7
- }
8
-
9
- export type { RPCHandlerOptions as R };
@@ -1,24 +0,0 @@
1
- import { toStandardRequest, toFetchResponse } from '@orpc/standard-server-fetch';
2
- import { a as RPCMatcher, R as RPCCodec, S as StandardHandler } from './server.BBGuTxHE.mjs';
3
-
4
- class RPCHandler {
5
- standardHandler;
6
- constructor(router, options) {
7
- const matcher = options?.matcher ?? new RPCMatcher();
8
- const codec = options?.codec ?? new RPCCodec();
9
- this.standardHandler = new StandardHandler(router, matcher, codec, options);
10
- }
11
- async handle(request, ...[options]) {
12
- const standardRequest = toStandardRequest(request);
13
- const result = await this.standardHandler.handle(standardRequest, options);
14
- if (!result.matched) {
15
- return result;
16
- }
17
- return {
18
- matched: true,
19
- response: toFetchResponse(result.response, options)
20
- };
21
- }
22
- }
23
-
24
- export { RPCHandler as R };
@@ -1,152 +0,0 @@
1
- import { ORPCErrorCode, ORPCErrorOptions, ORPCError, ClientContext, Client } from '@orpc/client';
2
- import { IsNever, MaybeOptionalOptions, Promisable, Interceptor, Value } from '@orpc/shared';
3
- import { ErrorMap, ErrorMapItem, SchemaInput, HTTPPath, Meta, Schema, ContractProcedureDef, SchemaOutput, ErrorFromErrorMap, AnyContractRouter, ContractProcedure, MergedErrorMap } from '@orpc/contract';
4
-
5
- type Context = Record<string, any>;
6
- type MergedContext<T extends Context, U extends Context> = T & U;
7
- declare function mergeContext<T extends Context, U extends Context>(context: T, other: U): MergedContext<T, U>;
8
- type ConflictContextGuard<T extends Context> = true extends IsNever<T> | {
9
- [K in keyof T]: IsNever<T[K]>;
10
- }[keyof T] ? never : unknown;
11
-
12
- type ORPCErrorConstructorMapItemOptions<TData> = Omit<ORPCErrorOptions<TData>, 'defined' | 'status'>;
13
- type ORPCErrorConstructorMapItem<TCode extends ORPCErrorCode, TInData> = (...rest: MaybeOptionalOptions<ORPCErrorConstructorMapItemOptions<TInData>>) => ORPCError<TCode, TInData>;
14
- type ORPCErrorConstructorMap<T extends ErrorMap> = {
15
- [K in keyof T]: K extends ORPCErrorCode ? T[K] extends ErrorMapItem<infer UInputSchema> ? ORPCErrorConstructorMapItem<K, SchemaInput<UInputSchema>> : never : never;
16
- };
17
-
18
- declare const LAZY_LOADER_SYMBOL: unique symbol;
19
- interface LazyMeta {
20
- prefix?: HTTPPath;
21
- }
22
- interface Lazy<T> {
23
- [LAZY_LOADER_SYMBOL](): Promise<{
24
- default: T;
25
- }>;
26
- }
27
- type Lazyable<T> = T | Lazy<T>;
28
- interface LazyOptions {
29
- prefix?: HTTPPath;
30
- }
31
- declare function lazy<T>(loader: () => Promise<{
32
- default: T;
33
- }>): Lazy<T>;
34
- declare function isLazy(item: unknown): item is Lazy<any>;
35
- declare function unlazy<T extends Lazyable<any>>(lazied: T): Promise<{
36
- default: T extends Lazy<infer U> ? U : T;
37
- }>;
38
-
39
- type MiddlewareResult<TOutContext extends Context, TOutput> = Promisable<{
40
- output: TOutput;
41
- context: TOutContext;
42
- }>;
43
- type MiddlewareNextFnOptions<TOutContext extends Context> = Record<never, never> extends TOutContext ? {
44
- context?: TOutContext;
45
- } : {
46
- context: TOutContext;
47
- };
48
- interface MiddlewareNextFn<TInContext extends Context, TOutput> {
49
- <U extends Context & Partial<TInContext> = Record<never, never>>(...rest: MaybeOptionalOptions<MiddlewareNextFnOptions<U>>): MiddlewareResult<U, TOutput>;
50
- }
51
- interface MiddlewareOutputFn<TOutput> {
52
- (output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
53
- }
54
- interface MiddlewareOptions<TInContext extends Context, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
55
- context: TInContext;
56
- path: string[];
57
- procedure: Procedure<Context, Context, Schema, Schema, unknown, ErrorMap, TMeta>;
58
- signal?: AbortSignal;
59
- lastEventId: string | undefined;
60
- next: MiddlewareNextFn<TInContext, TOutput>;
61
- errors: TErrorConstructorMap;
62
- }
63
- interface Middleware<TInContext extends Context, TOutContext extends Context, TInput, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
64
- (options: MiddlewareOptions<TInContext, TOutput, TErrorConstructorMap, TMeta>, input: TInput, output: MiddlewareOutputFn<TOutput>): Promisable<MiddlewareResult<TOutContext, TOutput>>;
65
- }
66
- type AnyMiddleware = Middleware<any, any, any, any, any, any>;
67
- interface MapInputMiddleware<TInput, TMappedInput> {
68
- (input: TInput): TMappedInput;
69
- }
70
- declare function middlewareOutputFn<TOutput>(output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
71
-
72
- interface ProcedureHandlerOptions<TCurrentContext extends Context, TInput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
73
- context: TCurrentContext;
74
- input: TInput;
75
- path: string[];
76
- procedure: Procedure<Context, Context, Schema, Schema, unknown, ErrorMap, TMeta>;
77
- signal?: AbortSignal;
78
- lastEventId: string | undefined;
79
- errors: TErrorConstructorMap;
80
- }
81
- interface ProcedureHandler<TCurrentContext extends Context, TInput, THandlerOutput, TErrorMap extends ErrorMap, TMeta extends Meta> {
82
- (opt: ProcedureHandlerOptions<TCurrentContext, TInput, ORPCErrorConstructorMap<TErrorMap>, TMeta>): Promisable<THandlerOutput>;
83
- }
84
- interface ProcedureDef<TInitialContext extends Context, TCurrentContext extends Context, TInputSchema extends Schema, TOutputSchema extends Schema, THandlerOutput, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap, TMeta> {
85
- __initialContext?: (type: TInitialContext) => unknown;
86
- middlewares: AnyMiddleware[];
87
- inputValidationIndex: number;
88
- outputValidationIndex: number;
89
- handler: ProcedureHandler<TCurrentContext, any, THandlerOutput, any, any>;
90
- }
91
- declare class Procedure<TInitialContext extends Context, TCurrentContext extends Context, TInputSchema extends Schema, TOutputSchema extends Schema, THandlerOutput, TErrorMap extends ErrorMap, TMeta extends Meta> {
92
- '~orpc': ProcedureDef<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap, TMeta>;
93
- constructor(def: ProcedureDef<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap, TMeta>);
94
- }
95
- type AnyProcedure = Procedure<any, any, any, any, any, any, any>;
96
- declare function isProcedure(item: unknown): item is AnyProcedure;
97
-
98
- type ProcedureClient<TClientContext extends ClientContext, TInputSchema extends Schema, TOutputSchema extends Schema, THandlerOutput, TErrorMap extends ErrorMap> = Client<TClientContext, SchemaInput<TInputSchema>, SchemaOutput<TOutputSchema, THandlerOutput>, ErrorFromErrorMap<TErrorMap>>;
99
- interface ProcedureClientInterceptorOptions<TInitialContext extends Context, TInputSchema extends Schema, TErrorMap extends ErrorMap, TMeta extends Meta> {
100
- context: TInitialContext;
101
- input: SchemaInput<TInputSchema>;
102
- errors: ORPCErrorConstructorMap<TErrorMap>;
103
- path: string[];
104
- procedure: Procedure<Context, Context, Schema, Schema, unknown, ErrorMap, TMeta>;
105
- signal?: AbortSignal;
106
- lastEventId: string | undefined;
107
- }
108
- /**
109
- * Options for creating a procedure caller with comprehensive type safety
110
- */
111
- type CreateProcedureClientOptions<TInitialContext extends Context, TInputSchema extends Schema, TOutputSchema extends Schema, THandlerOutput, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext> = {
112
- /**
113
- * This is helpful for logging and analytics.
114
- */
115
- path?: string[];
116
- interceptors?: Interceptor<ProcedureClientInterceptorOptions<TInitialContext, TInputSchema, TErrorMap, TMeta>, SchemaOutput<TOutputSchema, THandlerOutput>, ErrorFromErrorMap<TErrorMap>>[];
117
- } & (Record<never, never> extends TInitialContext ? {
118
- context?: Value<TInitialContext, [clientContext: TClientContext]>;
119
- } : {
120
- context: Value<TInitialContext, [clientContext: TClientContext]>;
121
- });
122
- declare function createProcedureClient<TInitialContext extends Context, TInputSchema extends Schema, TOutputSchema extends Schema, THandlerOutput, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext>(lazyableProcedure: Lazyable<Procedure<TInitialContext, any, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap, TMeta>>, ...[options]: MaybeOptionalOptions<CreateProcedureClientOptions<TInitialContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap, TMeta, TClientContext>>): ProcedureClient<TClientContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap>;
123
-
124
- type AccessibleLazyRouter<T extends AnyRouter | undefined | Lazy<AnyRouter | undefined>> = T extends Lazy<infer U extends AnyRouter | undefined | Lazy<AnyRouter | undefined>> ? AccessibleLazyRouter<U> : Lazy<T> & (T extends AnyProcedure | undefined ? unknown : {
125
- [K in keyof T]: T[K] extends AnyRouter ? AccessibleLazyRouter<T[K]> : never;
126
- });
127
- declare function createAccessibleLazyRouter<T extends Lazy<AnyRouter | undefined>>(lazied: T): AccessibleLazyRouter<T>;
128
-
129
- type Router<TInitialContext extends Context, TContract extends AnyContractRouter> = Lazyable<TContract extends ContractProcedure<infer UInputSchema, infer UOutputSchema, infer UErrorMap, infer UMeta> ? Procedure<TInitialContext, any, UInputSchema, UOutputSchema, any, UErrorMap, UMeta> : {
130
- [K in keyof TContract]: TContract[K] extends AnyContractRouter ? Router<TInitialContext, TContract[K]> : never;
131
- }>;
132
- type AnyRouter = Router<any, any>;
133
- type InferRouterInitialContext<T extends AnyRouter> = T extends Router<infer UInitialContext, any> ? UInitialContext : never;
134
- type InferRouterInputs<T extends AnyRouter> = T extends Lazy<infer U extends AnyRouter> ? InferRouterInputs<U> : T extends Procedure<any, any, infer UInputSchema, any, any, any, any> ? SchemaInput<UInputSchema> : {
135
- [K in keyof T]: T[K] extends AnyRouter ? InferRouterInputs<T[K]> : never;
136
- };
137
- type InferRouterOutputs<T extends AnyRouter> = T extends Lazy<infer U extends AnyRouter> ? InferRouterOutputs<U> : T extends Procedure<any, any, any, infer UOutputSchema, infer UFuncOutput, any, any> ? SchemaOutput<UOutputSchema, UFuncOutput> : {
138
- [K in keyof T]: T[K] extends AnyRouter ? InferRouterOutputs<T[K]> : never;
139
- };
140
- type AdaptedRouter<TRouter extends AnyRouter, TInitialContext extends Context, TErrorMap extends ErrorMap> = TRouter extends Lazy<infer U extends AnyRouter> ? AccessibleLazyRouter<AdaptedRouter<U, TInitialContext, TErrorMap>> : TRouter extends Procedure<any, infer UCurrentContext, infer UInputSchema, infer UOutputSchema, infer UFuncOutput, infer UErrorMap, infer UMeta> ? Procedure<TInitialContext, UCurrentContext, UInputSchema, UOutputSchema, UFuncOutput, MergedErrorMap<TErrorMap, UErrorMap>, UMeta> : {
141
- [K in keyof TRouter]: TRouter[K] extends AnyRouter ? AdaptedRouter<TRouter[K], TInitialContext, TErrorMap> : never;
142
- };
143
- interface AdaptRouterOptions<TErrorMap extends ErrorMap> {
144
- middlewares: AnyMiddleware[];
145
- tags?: readonly string[];
146
- prefix?: HTTPPath;
147
- errorMap: TErrorMap;
148
- }
149
- declare function adaptRouter<TRouter extends AnyRouter, TInitialContext extends Context, TErrorMap extends ErrorMap>(router: TRouter, options: AdaptRouterOptions<TErrorMap>): AdaptedRouter<TRouter, TInitialContext, TErrorMap>;
150
- declare function getRouterChild<T extends AnyRouter | Lazy<undefined>>(router: T, ...path: string[]): T extends Lazy<any> ? Lazy<AnyProcedure> | Lazy<Record<string, AnyRouter>> | Lazy<undefined> : AnyRouter | Lazy<undefined> | undefined;
151
-
152
- export { type AnyProcedure as A, type ProcedureDef as B, type Context as C, isProcedure as D, createProcedureClient as E, type InferRouterInputs as F, type InferRouterOutputs as G, adaptRouter as H, type InferRouterInitialContext as I, getRouterChild as J, type AccessibleLazyRouter as K, type Lazy as L, type Middleware as M, createAccessibleLazyRouter as N, 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 ProcedureHandler as h, type AdaptRouterOptions as i, type AdaptedRouter as j, type AnyMiddleware as k, type Lazyable as l, mergeContext as m, LAZY_LOADER_SYMBOL as n, type LazyMeta as o, type LazyOptions as p, lazy as q, isLazy as r, type MiddlewareResult as s, type MiddlewareNextFnOptions as t, unlazy as u, type MiddlewareNextFn as v, type MiddlewareOutputFn as w, type MiddlewareOptions as x, middlewareOutputFn as y, type ProcedureHandlerOptions as z };