@orpc/server 0.0.0-next.1b15515 → 0.0.0-next.1d3318c

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 (33) hide show
  1. package/dist/adapters/fetch/index.d.mts +4 -5
  2. package/dist/adapters/fetch/index.d.ts +4 -5
  3. package/dist/adapters/fetch/index.mjs +4 -4
  4. package/dist/adapters/hono/index.d.mts +2 -3
  5. package/dist/adapters/hono/index.d.ts +2 -3
  6. package/dist/adapters/hono/index.mjs +4 -4
  7. package/dist/adapters/next/index.d.mts +2 -3
  8. package/dist/adapters/next/index.d.ts +2 -3
  9. package/dist/adapters/next/index.mjs +4 -4
  10. package/dist/adapters/node/index.d.mts +4 -5
  11. package/dist/adapters/node/index.d.ts +4 -5
  12. package/dist/adapters/node/index.mjs +12 -9
  13. package/dist/adapters/standard/index.d.mts +8 -12
  14. package/dist/adapters/standard/index.d.ts +8 -12
  15. package/dist/adapters/standard/index.mjs +2 -3
  16. package/dist/index.d.mts +113 -103
  17. package/dist/index.d.ts +113 -103
  18. package/dist/index.mjs +31 -25
  19. package/dist/plugins/index.d.mts +6 -6
  20. package/dist/plugins/index.d.ts +6 -6
  21. package/dist/shared/{server.BBGuTxHE.mjs → server.3cSam35R.mjs} +11 -16
  22. package/dist/shared/{server.V6zT5iYQ.mjs → server.BFBhsdJr.mjs} +118 -152
  23. package/dist/shared/{server.D86dtDX_.d.mts → server.BZRSVRDu.d.ts} +14 -14
  24. package/dist/shared/server.CPteJIPP.d.mts +143 -0
  25. package/dist/shared/server.CPteJIPP.d.ts +143 -0
  26. package/dist/shared/server.CjB_m7jG.mjs +28 -0
  27. package/dist/shared/{server.dPmfqnQI.d.ts → server.iM8li30u.d.mts} +14 -14
  28. package/package.json +7 -7
  29. package/dist/shared/server.B_cAGti1.d.mts +0 -9
  30. package/dist/shared/server.CUE4Aija.mjs +0 -24
  31. package/dist/shared/server.Cn9ybJtE.d.mts +0 -152
  32. package/dist/shared/server.Cn9ybJtE.d.ts +0 -152
  33. package/dist/shared/server.CynXWJja.d.ts +0 -9
@@ -1,11 +1,10 @@
1
1
  import { ORPCError, toORPCError } from '@orpc/client';
2
2
  import { intercept, trim, parseEmptyableJSON } from '@orpc/shared';
3
3
  import { C as CompositePlugin } from './server.Q6ZmnTgO.mjs';
4
- import { c as createProcedureClient, e as eachContractProcedure, a as convertPathToHttpPath, i as isProcedure, u as unlazy, g as getRouterChild, b as createContractedProcedure } from './server.V6zT5iYQ.mjs';
5
- import { RPCSerializer } from '@orpc/client/standard';
4
+ import { c as createProcedureClient, t as traverseContractProcedures, a as toHttpPath, i as isProcedure, u as unlazy, g as getRouter, b as createContractedProcedure } from './server.BFBhsdJr.mjs';
6
5
 
7
6
  class StandardHandler {
8
- constructor(router, matcher, codec, options = {}) {
7
+ constructor(router, matcher, codec, options) {
9
8
  this.matcher = matcher;
10
9
  this.codec = codec;
11
10
  this.options = options;
@@ -71,9 +70,8 @@ class StandardHandler {
71
70
  }
72
71
 
73
72
  class RPCCodec {
74
- serializer;
75
- constructor(options = {}) {
76
- this.serializer = options.serializer ?? new RPCSerializer();
73
+ constructor(serializer) {
74
+ this.serializer = serializer;
77
75
  }
78
76
  async decode(request, _params, _procedure) {
79
77
  const serialized = request.method === "GET" ? parseEmptyableJSON(request.url.searchParams.getAll("data").at(-1)) : await request.body();
@@ -99,11 +97,8 @@ class RPCMatcher {
99
97
  tree = {};
100
98
  pendingRouters = [];
101
99
  init(router, path = []) {
102
- const laziedOptions = eachContractProcedure({
103
- router,
104
- path
105
- }, ({ path: path2, contract }) => {
106
- const httpPath = convertPathToHttpPath(path2);
100
+ const laziedOptions = traverseContractProcedures({ router, path }, ({ path: path2, contract }) => {
101
+ const httpPath = toHttpPath(path2);
107
102
  if (isProcedure(contract)) {
108
103
  this.tree[httpPath] = {
109
104
  path: path2,
@@ -123,7 +118,7 @@ class RPCMatcher {
123
118
  });
124
119
  this.pendingRouters.push(...laziedOptions.map((option) => ({
125
120
  ...option,
126
- httpPathPrefix: convertPathToHttpPath(option.path)
121
+ httpPathPrefix: toHttpPath(option.path)
127
122
  })));
128
123
  }
129
124
  async match(_method, pathname) {
@@ -131,7 +126,7 @@ class RPCMatcher {
131
126
  const newPendingRouters = [];
132
127
  for (const pendingRouter of this.pendingRouters) {
133
128
  if (pathname.startsWith(pendingRouter.httpPathPrefix)) {
134
- const { default: router } = await unlazy(pendingRouter.lazied);
129
+ const { default: router } = await unlazy(pendingRouter.router);
135
130
  this.init(router, pendingRouter.path);
136
131
  } else {
137
132
  newPendingRouters.push(pendingRouter);
@@ -144,14 +139,14 @@ class RPCMatcher {
144
139
  return void 0;
145
140
  }
146
141
  if (!match.procedure) {
147
- const { default: maybeProcedure } = await unlazy(getRouterChild(match.router, ...match.path));
142
+ const { default: maybeProcedure } = await unlazy(getRouter(match.router, match.path));
148
143
  if (!isProcedure(maybeProcedure)) {
149
144
  throw new Error(`
150
- [Contract-First] Missing or invalid implementation for procedure at path: ${convertPathToHttpPath(match.path)}.
145
+ [Contract-First] Missing or invalid implementation for procedure at path: ${toHttpPath(match.path)}.
151
146
  Ensure that the procedure is correctly defined and matches the expected contract.
152
147
  `);
153
148
  }
154
- match.procedure = createContractedProcedure(match.contract, maybeProcedure);
149
+ match.procedure = createContractedProcedure(maybeProcedure, match.contract);
155
150
  }
156
151
  return {
157
152
  path: match.path,
@@ -1,59 +1,24 @@
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";
13
- }
14
- function unlazy(lazied) {
15
- return isLazy(lazied) ? lazied[LAZY_LOADER_SYMBOL]() : Promise.resolve({ default: lazied });
16
- }
17
-
18
- class Procedure {
19
- "~orpc";
20
- constructor(def) {
21
- this["~orpc"] = def;
22
- }
23
- }
24
- function isProcedure(item) {
25
- if (item instanceof Procedure) {
26
- return true;
27
- }
28
- return isContractProcedure(item) && "middlewares" in item["~orpc"] && "inputValidationIndex" in item["~orpc"] && "outputValidationIndex" in item["~orpc"] && "handler" in item["~orpc"];
15
+ return (typeof item === "object" || typeof item === "function") && item !== null && LAZY_SYMBOL in item;
29
16
  }
30
-
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);
17
+ function getLazyMeta(lazied) {
18
+ return lazied[LAZY_SYMBOL].meta;
43
19
  }
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;
20
+ function unlazy(lazied) {
21
+ return isLazy(lazied) ? lazied[LAZY_SYMBOL].loader() : Promise.resolve({ default: lazied });
57
22
  }
58
23
 
59
24
  function dedupeMiddlewares(compare, middlewares) {
@@ -74,6 +39,19 @@ function addMiddleware(middlewares, addition) {
74
39
  return [...middlewares, addition];
75
40
  }
76
41
 
42
+ class Procedure {
43
+ "~orpc";
44
+ constructor(def) {
45
+ this["~orpc"] = def;
46
+ }
47
+ }
48
+ function isProcedure(item) {
49
+ if (item instanceof Procedure) {
50
+ return true;
51
+ }
52
+ return isContractProcedure(item) && "middlewares" in item["~orpc"] && "inputValidationIndex" in item["~orpc"] && "outputValidationIndex" in item["~orpc"] && "handler" in item["~orpc"];
53
+ }
54
+
77
55
  function createORPCErrorConstructorMap(errors) {
78
56
  const proxy = new Proxy(errors, {
79
57
  get(target, code) {
@@ -205,169 +183,150 @@ async function executeProcedureInternal(procedure, options) {
205
183
  return (await next({})).output;
206
184
  }
207
185
 
208
- const ROUTER_CONTRACT_SYMBOL = Symbol("ORPC_ROUTER_CONTRACT");
209
- function setRouterContract(obj, contract) {
210
- return new Proxy(obj, {
186
+ const HIDDEN_ROUTER_CONTRACT_SYMBOL = Symbol("ORPC_HIDDEN_ROUTER_CONTRACT");
187
+ function setHiddenRouterContract(router, contract) {
188
+ return new Proxy(router, {
211
189
  get(target, key) {
212
- if (key === ROUTER_CONTRACT_SYMBOL) {
190
+ if (key === HIDDEN_ROUTER_CONTRACT_SYMBOL) {
213
191
  return contract;
214
192
  }
215
193
  return Reflect.get(target, key);
216
194
  }
217
195
  });
218
196
  }
219
- function getRouterContract(obj) {
220
- return obj[ROUTER_CONTRACT_SYMBOL];
197
+ function getHiddenRouterContract(router) {
198
+ return router[HIDDEN_ROUTER_CONTRACT_SYMBOL];
221
199
  }
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;
200
+
201
+ function getRouter(router, path) {
202
+ let current = router;
203
+ for (let i = 0; i < path.length; i++) {
204
+ const segment = path[i];
205
+ if (!current) {
206
+ return void 0;
234
207
  }
235
- });
236
- }
237
- function getLazyRouterPrefix(obj) {
238
- return obj[LAZY_ROUTER_PREFIX_SYMBOL];
208
+ if (isProcedure(current)) {
209
+ return void 0;
210
+ }
211
+ if (!isLazy(current)) {
212
+ current = current[segment];
213
+ continue;
214
+ }
215
+ const lazied = current;
216
+ const rest = path.slice(i);
217
+ return lazy(async () => {
218
+ const unwrapped = await unlazy(lazied);
219
+ const next = getRouter(unwrapped.default, rest);
220
+ return unlazy(next);
221
+ }, getLazyMeta(lazied));
222
+ }
223
+ return current;
239
224
  }
240
-
241
225
  function createAccessibleLazyRouter(lazied) {
242
- const flattenLazy = flatLazy(lazied);
243
- const recursive = new Proxy(flattenLazy, {
226
+ const recursive = new Proxy(lazied, {
244
227
  get(target, key) {
245
228
  if (typeof key !== "string") {
246
229
  return Reflect.get(target, key);
247
230
  }
248
- const next = getRouterChild(flattenLazy, key);
231
+ const next = getRouter(lazied, [key]);
249
232
  return createAccessibleLazyRouter(next);
250
233
  }
251
234
  });
252
235
  return recursive;
253
236
  }
254
-
255
- function adaptRouter(router, options) {
237
+ function enhanceRouter(router, options) {
256
238
  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 };
239
+ const laziedMeta = getLazyMeta(router);
240
+ const enhancedPrefix = laziedMeta?.prefix ? mergePrefix(options.prefix, laziedMeta?.prefix) : options.prefix;
241
+ const enhanced2 = lazy(async () => {
242
+ const { default: unlaziedRouter } = await unlazy(router);
243
+ const enhanced3 = enhanceRouter(unlaziedRouter, options);
244
+ return unlazy(enhanced3);
245
+ }, {
246
+ ...laziedMeta,
247
+ prefix: enhancedPrefix
261
248
  });
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
- }
249
+ const accessible = createAccessibleLazyRouter(enhanced2);
268
250
  return accessible;
269
251
  }
270
252
  if (isProcedure(router)) {
271
253
  const newMiddlewares = mergeMiddlewares(options.middlewares, router["~orpc"].middlewares);
272
254
  const newMiddlewareAdded = newMiddlewares.length - router["~orpc"].middlewares.length;
273
- const adapted2 = new Procedure({
255
+ const enhanced2 = new Procedure({
274
256
  ...router["~orpc"],
275
- route: adaptRoute(router["~orpc"].route, options),
257
+ route: enhanceRoute(router["~orpc"].route, options),
276
258
  errorMap: mergeErrorMap(options.errorMap, router["~orpc"].errorMap),
277
259
  middlewares: newMiddlewares,
278
260
  inputValidationIndex: router["~orpc"].inputValidationIndex + newMiddlewareAdded,
279
261
  outputValidationIndex: router["~orpc"].outputValidationIndex + newMiddlewareAdded
280
262
  });
281
- return adapted2;
263
+ return enhanced2;
282
264
  }
283
- const adapted = {};
265
+ const enhanced = {};
284
266
  for (const key in router) {
285
- adapted[key] = adaptRouter(router[key], options);
267
+ enhanced[key] = enhanceRouter(router[key], options);
286
268
  }
287
- return adapted;
269
+ return enhanced;
288
270
  }
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);
314
- }
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
- );
271
+ function traverseContractProcedures(options, callback, lazyOptions = []) {
272
+ let currentRouter = options.router;
273
+ const hiddenContract = getHiddenRouterContract(options.router);
274
+ if (hiddenContract !== void 0) {
275
+ currentRouter = hiddenContract;
329
276
  }
330
- if (isLazy(options.router)) {
331
- laziedOptions.push({
332
- lazied: options.router,
277
+ if (isLazy(currentRouter)) {
278
+ lazyOptions.push({
279
+ router: currentRouter,
333
280
  path: options.path
334
281
  });
335
- } else if (isContractProcedure(options.router)) {
282
+ } else if (isContractProcedure(currentRouter)) {
336
283
  callback({
337
- contract: options.router,
284
+ contract: currentRouter,
338
285
  path: options.path
339
286
  });
340
287
  } else {
341
- for (const key in options.router) {
342
- eachContractProcedure(
288
+ for (const key in currentRouter) {
289
+ traverseContractProcedures(
343
290
  {
344
- router: options.router[key],
291
+ router: currentRouter[key],
345
292
  path: [...options.path, key]
346
293
  },
347
294
  callback,
348
- laziedOptions
295
+ lazyOptions
349
296
  );
350
297
  }
351
298
  }
352
- return laziedOptions;
299
+ return lazyOptions;
353
300
  }
354
- async function eachAllContractProcedure(options, callback) {
301
+ async function resolveContractProcedures(options, callback) {
355
302
  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);
303
+ for (const options2 of pending) {
304
+ const lazyOptions = traverseContractProcedures(options2, callback);
305
+ for (const options3 of lazyOptions) {
306
+ const { default: router } = await unlazy(options3.router);
360
307
  pending.push({
361
- path: lazy.path,
362
- router
308
+ router,
309
+ path: options3.path
363
310
  });
364
311
  }
365
312
  }
366
313
  }
367
- function convertPathToHttpPath(path) {
368
- return `/${path.map(encodeURIComponent).join("/")}`;
314
+
315
+ function createAssertedLazyProcedure(lazied) {
316
+ const lazyProcedure = lazy(async () => {
317
+ const { default: maybeProcedure } = await unlazy(lazied);
318
+ if (!isProcedure(maybeProcedure)) {
319
+ throw new Error(`
320
+ Expected a lazy<procedure> but got lazy<unknown>.
321
+ This should be caught by TypeScript compilation.
322
+ Please report this issue if this makes you feel uncomfortable.
323
+ `);
324
+ }
325
+ return { default: maybeProcedure };
326
+ }, getLazyMeta(lazied));
327
+ return lazyProcedure;
369
328
  }
370
- function createContractedProcedure(contract, procedure) {
329
+ function createContractedProcedure(procedure, contract) {
371
330
  return new Procedure({
372
331
  ...procedure["~orpc"],
373
332
  errorMap: contract["~orpc"].errorMap,
@@ -375,5 +334,12 @@ function createContractedProcedure(contract, procedure) {
375
334
  meta: contract["~orpc"].meta
376
335
  });
377
336
  }
337
+ function call(procedure, input, ...rest) {
338
+ return createProcedureClient(procedure, ...rest)(input);
339
+ }
340
+
341
+ function toHttpPath(path) {
342
+ return `/${path.map(encodeURIComponent).join("/")}`;
343
+ }
378
344
 
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 };
345
+ export { LAZY_SYMBOL as L, Procedure as P, toHttpPath as a, createContractedProcedure as b, createProcedureClient as c, addMiddleware as d, enhanceRouter as e, isLazy as f, getRouter as g, createAssertedLazyProcedure as h, isProcedure as i, getLazyMeta as j, call as k, lazy as l, middlewareOutputFn as m, getHiddenRouterContract as n, createAccessibleLazyRouter as o, resolveContractProcedures as r, setHiddenRouterContract as s, traverseContractProcedures as t, unlazy as u };
@@ -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
- import { StandardResponse, StandardRequest } from '@orpc/standard-server';
4
- import { a as AnyRouter, A as AnyProcedure, C as Context, P as ProcedureClientInterceptorOptions, R as Router } from './server.Cn9ybJtE.mjs';
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.CPteJIPP.js';
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;
@@ -17,7 +17,7 @@ interface StandardMatcher {
17
17
  interface StandardCodec {
18
18
  encode(output: unknown, procedure: AnyProcedure): StandardResponse;
19
19
  encodeError(error: ORPCError<any, any>): StandardResponse;
20
- decode(request: StandardRequest, params: StandardParams | undefined, procedure: AnyProcedure): Promise<unknown>;
20
+ decode(request: StandardLazyRequest, params: StandardParams | undefined, procedure: AnyProcedure): Promise<unknown>;
21
21
  }
22
22
 
23
23
  type StandardHandleOptions<T extends Context> = {
@@ -38,10 +38,10 @@ type StandardHandleResult = {
38
38
  response: undefined;
39
39
  };
40
40
  type StandardHandlerInterceptorOptions<TContext extends Context> = WellStandardHandleOptions<TContext> & {
41
- request: StandardRequest;
41
+ request: StandardLazyRequest;
42
42
  };
43
43
  interface StandardHandlerOptions<TContext extends Context> {
44
- plugins?: Plugin<TContext>[];
44
+ plugins?: HandlerPlugin<TContext>[];
45
45
  /**
46
46
  * Interceptors at the request level, helpful when you want catch errors
47
47
  */
@@ -54,24 +54,24 @@ interface StandardHandlerOptions<TContext extends Context> {
54
54
  *
55
55
  * Interceptors for procedure client.
56
56
  */
57
- clientInterceptors?: Interceptor<ProcedureClientInterceptorOptions<TContext, Schema, Record<never, never>, Meta>, SchemaOutput<Schema, unknown>, ErrorFromErrorMap<Record<never, never>>>[];
57
+ clientInterceptors?: Interceptor<ProcedureClientInterceptorOptions<TContext, AnySchema, Record<never, never>, Meta>, InferSchemaOutput<AnySchema>, ErrorFromErrorMap<Record<never, never>>>[];
58
58
  }
59
59
  declare class StandardHandler<T extends Context> {
60
60
  private readonly matcher;
61
61
  private readonly codec;
62
62
  private readonly options;
63
63
  private readonly plugin;
64
- constructor(router: Router<T, any>, matcher: StandardMatcher, codec: StandardCodec, options?: NoInfer<StandardHandlerOptions<T>>);
65
- handle(request: StandardRequest, ...[options]: MaybeOptionalOptions<StandardHandleOptions<T>>): Promise<StandardHandleResult>;
64
+ constructor(router: Router<any, T>, matcher: StandardMatcher, codec: StandardCodec, options: NoInfer<StandardHandlerOptions<T>>);
65
+ handle(request: StandardLazyRequest, ...[options]: MaybeOptionalOptions<StandardHandleOptions<T>>): Promise<StandardHandleResult>;
66
66
  }
67
67
 
68
- interface Plugin<TContext extends Context> {
68
+ interface HandlerPlugin<TContext extends Context> {
69
69
  init?(options: StandardHandlerOptions<TContext>): void;
70
70
  }
71
- declare class CompositePlugin<TContext extends Context> implements Plugin<TContext> {
71
+ declare class CompositePlugin<TContext extends Context> implements HandlerPlugin<TContext> {
72
72
  private readonly plugins;
73
- constructor(plugins?: Plugin<TContext>[]);
73
+ constructor(plugins?: HandlerPlugin<TContext>[]);
74
74
  init(options: StandardHandlerOptions<TContext>): void;
75
75
  }
76
76
 
77
- export { CompositePlugin as C, type Plugin as P, type StandardHandleOptions as S, type WellStandardHandleOptions as W, type StandardHandlerOptions as a, type StandardMatcher as b, type StandardCodec as c, type StandardHandlerInterceptorOptions as d, type StandardParams as e, type StandardMatchResult as f, type StandardHandleResult as g, StandardHandler as h };
77
+ export { CompositePlugin as C, type HandlerPlugin as H, 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 };
@@ -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 };