@orpc/server 0.0.0-next.6c5bfe4 → 0.0.0-next.6d75718

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 (37) hide show
  1. package/README.md +13 -1
  2. package/dist/adapters/fetch/index.d.mts +26 -10
  3. package/dist/adapters/fetch/index.d.ts +26 -10
  4. package/dist/adapters/fetch/index.mjs +5 -5
  5. package/dist/adapters/hono/index.d.mts +6 -3
  6. package/dist/adapters/hono/index.d.ts +6 -3
  7. package/dist/adapters/hono/index.mjs +5 -5
  8. package/dist/adapters/next/index.d.mts +6 -3
  9. package/dist/adapters/next/index.d.ts +6 -3
  10. package/dist/adapters/next/index.mjs +5 -5
  11. package/dist/adapters/node/index.d.mts +28 -21
  12. package/dist/adapters/node/index.d.ts +28 -21
  13. package/dist/adapters/node/index.mjs +45 -23
  14. package/dist/adapters/standard/index.d.mts +9 -8
  15. package/dist/adapters/standard/index.d.ts +9 -8
  16. package/dist/adapters/standard/index.mjs +2 -3
  17. package/dist/index.d.mts +58 -50
  18. package/dist/index.d.ts +58 -50
  19. package/dist/index.mjs +29 -26
  20. package/dist/plugins/index.d.mts +14 -15
  21. package/dist/plugins/index.d.ts +14 -15
  22. package/dist/plugins/index.mjs +10 -6
  23. package/dist/shared/{server.BZRSVRDu.d.ts → server.23VRZIfj.d.ts} +15 -26
  24. package/dist/shared/server.BG5ftPWa.d.ts +10 -0
  25. package/dist/shared/server.BVwwTHyO.mjs +9 -0
  26. package/dist/shared/{server.kPUGzsdw.mjs → server.BtxZnWJ9.mjs} +32 -15
  27. package/dist/shared/server.C-CCcROC.d.mts +10 -0
  28. package/dist/shared/server.CIbpOMZX.d.mts +8 -0
  29. package/dist/shared/server.CTt4UYhI.mjs +51 -0
  30. package/dist/shared/{server.N2ttmv6r.mjs → server.CVNC_Jz8.mjs} +28 -29
  31. package/dist/shared/server.DCcCuA52.d.ts +8 -0
  32. package/dist/shared/{server.iM8li30u.d.mts → server.DJqfB27m.d.mts} +15 -26
  33. package/dist/shared/{server.CPteJIPP.d.mts → server.MZvbGc3n.d.mts} +11 -11
  34. package/dist/shared/{server.CPteJIPP.d.ts → server.MZvbGc3n.d.ts} +11 -11
  35. package/package.json +8 -8
  36. package/dist/shared/server.CXu_v2vM.mjs +0 -28
  37. package/dist/shared/server.Q6ZmnTgO.mjs +0 -12
@@ -1,7 +1,7 @@
1
1
  import { HTTPPath, AnySchema, Meta, InferSchemaOutput, ErrorFromErrorMap } from '@orpc/contract';
2
- import { Interceptor, MaybeOptionalOptions } from '@orpc/shared';
2
+ import { Interceptor } 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.CPteJIPP.js';
4
+ import { a as AnyRouter, A as AnyProcedure, C as Context, P as ProcedureClientInterceptorOptions, R as Router } from './server.MZvbGc3n.js';
5
5
  import { ORPCError } from '@orpc/client';
6
6
 
7
7
  type StandardParams = Record<string, string>;
@@ -20,16 +20,10 @@ interface StandardCodec {
20
20
  decode(request: StandardLazyRequest, params: StandardParams | undefined, procedure: AnyProcedure): Promise<unknown>;
21
21
  }
22
22
 
23
- type StandardHandleOptions<T extends Context> = {
23
+ interface StandardHandleOptions<T extends Context> {
24
24
  prefix?: HTTPPath;
25
- } & (Record<never, never> extends T ? {
26
- context?: T;
27
- } : {
28
25
  context: T;
29
- });
30
- type WellStandardHandleOptions<T extends Context> = StandardHandleOptions<T> & {
31
- context: T;
32
- };
26
+ }
33
27
  type StandardHandleResult = {
34
28
  matched: true;
35
29
  response: StandardResponse;
@@ -37,11 +31,14 @@ type StandardHandleResult = {
37
31
  matched: false;
38
32
  response: undefined;
39
33
  };
40
- type StandardHandlerInterceptorOptions<TContext extends Context> = WellStandardHandleOptions<TContext> & {
34
+ interface StandardHandlerPlugin<TContext extends Context> {
35
+ init?(options: StandardHandlerOptions<TContext>): void;
36
+ }
37
+ interface StandardHandlerInterceptorOptions<T extends Context> extends StandardHandleOptions<T> {
41
38
  request: StandardLazyRequest;
42
- };
39
+ }
43
40
  interface StandardHandlerOptions<TContext extends Context> {
44
- plugins?: HandlerPlugin<TContext>[];
41
+ plugins?: StandardHandlerPlugin<TContext>[];
45
42
  /**
46
43
  * Interceptors at the request level, helpful when you want catch errors
47
44
  */
@@ -59,19 +56,11 @@ interface StandardHandlerOptions<TContext extends Context> {
59
56
  declare class StandardHandler<T extends Context> {
60
57
  private readonly matcher;
61
58
  private readonly codec;
62
- private readonly options;
63
- private readonly plugin;
59
+ private readonly interceptors;
60
+ private readonly clientInterceptors;
61
+ private readonly rootInterceptors;
64
62
  constructor(router: Router<any, T>, matcher: StandardMatcher, codec: StandardCodec, options: NoInfer<StandardHandlerOptions<T>>);
65
- handle(request: StandardLazyRequest, ...[options]: MaybeOptionalOptions<StandardHandleOptions<T>>): Promise<StandardHandleResult>;
66
- }
67
-
68
- interface HandlerPlugin<TContext extends Context> {
69
- init?(options: StandardHandlerOptions<TContext>): void;
70
- }
71
- declare class CompositePlugin<TContext extends Context> implements HandlerPlugin<TContext> {
72
- private readonly plugins;
73
- constructor(plugins?: HandlerPlugin<TContext>[]);
74
- init(options: StandardHandlerOptions<TContext>): void;
63
+ handle(request: StandardLazyRequest, options: StandardHandleOptions<T>): Promise<StandardHandleResult>;
75
64
  }
76
65
 
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 };
66
+ export { type StandardHandleOptions as S, type StandardHandlerOptions as a, type StandardHandlerInterceptorOptions as b, type StandardHandlerPlugin as c, type StandardCodec as d, type StandardParams as e, type StandardMatcher as f, type StandardMatchResult as g, type StandardHandleResult as h, StandardHandler as i };
@@ -0,0 +1,10 @@
1
+ import { C as Context } from './server.MZvbGc3n.js';
2
+ import { S as StandardHandleOptions } from './server.23VRZIfj.js';
3
+
4
+ type FriendlyStandardHandleOptions<T extends Context> = Omit<StandardHandleOptions<T>, 'context'> & (Record<never, never> extends T ? {
5
+ context?: T;
6
+ } : {
7
+ context: T;
8
+ });
9
+
10
+ export type { FriendlyStandardHandleOptions as F };
@@ -0,0 +1,9 @@
1
+ function resolveFriendlyStandardHandleOptions(options) {
2
+ return {
3
+ ...options,
4
+ context: options?.context ?? {}
5
+ // Context only optional if all fields are optional
6
+ };
7
+ }
8
+
9
+ export { resolveFriendlyStandardHandleOptions as r };
@@ -21,19 +21,25 @@ function unlazy(lazied) {
21
21
  return isLazy(lazied) ? lazied[LAZY_SYMBOL].loader() : Promise.resolve({ default: lazied });
22
22
  }
23
23
 
24
- function dedupeMiddlewares(compare, middlewares) {
25
- let min = 0;
24
+ function isStartWithMiddlewares(middlewares, compare) {
25
+ if (compare.length > middlewares.length) {
26
+ return false;
27
+ }
26
28
  for (let i = 0; i < middlewares.length; i++) {
27
- const index = compare.indexOf(middlewares[i], min);
28
- if (index === -1) {
29
- return middlewares.slice(i);
29
+ if (compare[i] === void 0) {
30
+ return true;
31
+ }
32
+ if (middlewares[i] !== compare[i]) {
33
+ return false;
30
34
  }
31
- min = index + 1;
32
35
  }
33
- return [];
36
+ return true;
34
37
  }
35
- function mergeMiddlewares(first, second) {
36
- return [...first, ...dedupeMiddlewares(first, second)];
38
+ function mergeMiddlewares(first, second, options) {
39
+ if (options.dedupeLeading && isStartWithMiddlewares(second, first)) {
40
+ return second;
41
+ }
42
+ return [...first, ...second];
37
43
  }
38
44
  function addMiddleware(middlewares, addition) {
39
45
  return [...middlewares, addition];
@@ -52,6 +58,10 @@ function isProcedure(item) {
52
58
  return isContractProcedure(item) && "middlewares" in item["~orpc"] && "inputValidationIndex" in item["~orpc"] && "outputValidationIndex" in item["~orpc"] && "handler" in item["~orpc"];
53
59
  }
54
60
 
61
+ function mergeCurrentContext(context, other) {
62
+ return { ...context, ...other };
63
+ }
64
+
55
65
  function createORPCErrorConstructorMap(errors) {
56
66
  const proxy = new Proxy(errors, {
57
67
  get(target, code) {
@@ -164,23 +174,30 @@ async function executeProcedureInternal(procedure, options) {
164
174
  let currentInput = options.input;
165
175
  const next = async (...[nextOptions]) => {
166
176
  const index = currentIndex;
177
+ const midContext = nextOptions?.context ?? {};
167
178
  currentIndex += 1;
168
- currentContext = { ...currentContext, ...nextOptions?.context };
179
+ currentContext = mergeCurrentContext(currentContext, midContext);
169
180
  if (index === inputValidationIndex) {
170
181
  currentInput = await validateInput(procedure, currentInput);
171
182
  }
172
183
  const mid = middlewares[index];
173
- 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
+ };
174
191
  if (index === outputValidationIndex) {
175
192
  const validatedOutput = await validateOutput(procedure, result.output);
176
193
  return {
177
- ...result,
194
+ context: result.context,
178
195
  output: validatedOutput
179
196
  };
180
197
  }
181
198
  return result;
182
199
  };
183
- return (await next({})).output;
200
+ return (await next()).output;
184
201
  }
185
202
 
186
203
  const HIDDEN_ROUTER_CONTRACT_SYMBOL = Symbol("ORPC_HIDDEN_ROUTER_CONTRACT");
@@ -250,7 +267,7 @@ function enhanceRouter(router, options) {
250
267
  return accessible;
251
268
  }
252
269
  if (isProcedure(router)) {
253
- const newMiddlewares = mergeMiddlewares(options.middlewares, router["~orpc"].middlewares);
270
+ const newMiddlewares = mergeMiddlewares(options.middlewares, router["~orpc"].middlewares, { dedupeLeading: options.dedupeLeadingMiddlewares });
254
271
  const newMiddlewareAdded = newMiddlewares.length - router["~orpc"].middlewares.length;
255
272
  const enhanced2 = new Procedure({
256
273
  ...router["~orpc"],
@@ -354,4 +371,4 @@ function toHttpPath(path) {
354
371
  return `/${path.map(encodeURIComponent).join("/")}`;
355
372
  }
356
373
 
357
- export { LAZY_SYMBOL as L, Procedure as P, toHttpPath as a, createContractedProcedure as b, createProcedureClient as c, addMiddleware as d, enhanceRouter as e, isLazy as f, getRouter as g, createAssertedLazyProcedure as h, isProcedure as i, getLazyMeta as j, call as k, lazy as l, middlewareOutputFn as m, getHiddenRouterContract as n, createAccessibleLazyRouter as o, unlazyRouter as p, resolveContractProcedures as r, setHiddenRouterContract as s, traverseContractProcedures as t, unlazy as u };
374
+ 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, mergeCurrentContext as m, middlewareOutputFn as n, isStartWithMiddlewares as o, mergeMiddlewares as p, call as q, getHiddenRouterContract as r, setHiddenRouterContract as s, traverseContractProcedures as t, unlazy as u, validateORPCError as v, createAccessibleLazyRouter as w, resolveContractProcedures as x, unlazyRouter as y };
@@ -0,0 +1,10 @@
1
+ import { C as Context } from './server.MZvbGc3n.mjs';
2
+ import { S as StandardHandleOptions } from './server.DJqfB27m.mjs';
3
+
4
+ type FriendlyStandardHandleOptions<T extends Context> = Omit<StandardHandleOptions<T>, 'context'> & (Record<never, never> extends T ? {
5
+ context?: T;
6
+ } : {
7
+ context: T;
8
+ });
9
+
10
+ export type { FriendlyStandardHandleOptions as F };
@@ -0,0 +1,8 @@
1
+ import { StandardRPCJsonSerializerOptions } from '@orpc/client/standard';
2
+ import { C as Context } from './server.MZvbGc3n.mjs';
3
+ import { a as StandardHandlerOptions } from './server.DJqfB27m.mjs';
4
+
5
+ interface StandardRPCHandlerOptions<T extends Context> extends StandardHandlerOptions<T>, StandardRPCJsonSerializerOptions {
6
+ }
7
+
8
+ export type { StandardRPCHandlerOptions as S };
@@ -0,0 +1,51 @@
1
+ import { StandardRPCJsonSerializer, StandardRPCSerializer } from '@orpc/client/standard';
2
+ import { S as StandardHandler, b as StandardRPCMatcher, a as StandardRPCCodec } from './server.CVNC_Jz8.mjs';
3
+ import { toArray, intercept, resolveMaybeOptionalOptions } from '@orpc/shared';
4
+ import { toStandardLazyRequest, toFetchResponse } from '@orpc/standard-server-fetch';
5
+ import { r as resolveFriendlyStandardHandleOptions } from './server.BVwwTHyO.mjs';
6
+
7
+ class FetchHandler {
8
+ constructor(standardHandler, options = {}) {
9
+ this.standardHandler = standardHandler;
10
+ for (const plugin of toArray(options.plugins)) {
11
+ plugin.initRuntimeAdapter?.(options);
12
+ }
13
+ this.adapterInterceptors = toArray(options.adapterInterceptors);
14
+ this.toFetchResponseOptions = options;
15
+ }
16
+ toFetchResponseOptions;
17
+ adapterInterceptors;
18
+ async handle(request, ...rest) {
19
+ return intercept(
20
+ this.adapterInterceptors,
21
+ {
22
+ ...resolveFriendlyStandardHandleOptions(resolveMaybeOptionalOptions(rest)),
23
+ request,
24
+ toFetchResponseOptions: this.toFetchResponseOptions
25
+ },
26
+ async ({ request: request2, toFetchResponseOptions, ...options }) => {
27
+ const standardRequest = toStandardLazyRequest(request2);
28
+ const result = await this.standardHandler.handle(standardRequest, options);
29
+ if (!result.matched) {
30
+ return result;
31
+ }
32
+ return {
33
+ matched: true,
34
+ response: toFetchResponse(result.response, toFetchResponseOptions)
35
+ };
36
+ }
37
+ );
38
+ }
39
+ }
40
+
41
+ class RPCHandler extends FetchHandler {
42
+ constructor(router, options = {}) {
43
+ const jsonSerializer = new StandardRPCJsonSerializer(options);
44
+ const serializer = new StandardRPCSerializer(jsonSerializer);
45
+ const matcher = new StandardRPCMatcher();
46
+ const codec = new StandardRPCCodec(serializer);
47
+ super(new StandardHandler(router, matcher, codec, options), options);
48
+ }
49
+ }
50
+
51
+ export { FetchHandler as F, RPCHandler as R };
@@ -1,51 +1,50 @@
1
1
  import { ORPCError, toORPCError } from '@orpc/client';
2
- import { intercept, trim, parseEmptyableJSON } from '@orpc/shared';
3
- import { C as CompositePlugin } from './server.Q6ZmnTgO.mjs';
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.kPUGzsdw.mjs';
2
+ import { toArray, intercept, trim, parseEmptyableJSON } from '@orpc/shared';
3
+ import { c as createProcedureClient, t as traverseContractProcedures, a as toHttpPath, i as isProcedure, u as unlazy, g as getRouter, b as createContractedProcedure } from './server.BtxZnWJ9.mjs';
5
4
 
6
5
  class StandardHandler {
7
6
  constructor(router, matcher, codec, options) {
8
7
  this.matcher = matcher;
9
8
  this.codec = codec;
10
- this.options = options;
11
- this.plugin = new CompositePlugin(options.plugins);
12
- this.plugin.init(this.options);
9
+ for (const plugin of toArray(options.plugins)) {
10
+ plugin.init?.(options);
11
+ }
12
+ this.interceptors = toArray(options.interceptors);
13
+ this.clientInterceptors = toArray(options.clientInterceptors);
14
+ this.rootInterceptors = toArray(options.rootInterceptors);
13
15
  this.matcher.init(router);
14
16
  }
15
- plugin;
16
- handle(request, ...[options]) {
17
+ interceptors;
18
+ clientInterceptors;
19
+ rootInterceptors;
20
+ handle(request, options) {
17
21
  return intercept(
18
- this.options.rootInterceptors ?? [],
19
- {
20
- request,
21
- ...options,
22
- context: options?.context ?? {}
23
- // context is optional only when all fields are optional so we can safely force it to have a context
24
- },
22
+ this.rootInterceptors,
23
+ { ...options, request },
25
24
  async (interceptorOptions) => {
26
25
  let isDecoding = false;
27
26
  try {
28
27
  return await intercept(
29
- this.options.interceptors ?? [],
28
+ this.interceptors,
30
29
  interceptorOptions,
31
- async (interceptorOptions2) => {
32
- const method = interceptorOptions2.request.method;
33
- const url = interceptorOptions2.request.url;
34
- const pathname = `/${trim(url.pathname.replace(interceptorOptions2.prefix ?? "", ""), "/")}`;
30
+ async ({ request: request2, context, prefix }) => {
31
+ const method = request2.method;
32
+ const url = request2.url;
33
+ const pathname = `/${trim(url.pathname.replace(prefix ?? "", ""), "/")}`;
35
34
  const match = await this.matcher.match(method, pathname);
36
35
  if (!match) {
37
36
  return { matched: false, response: void 0 };
38
37
  }
39
38
  const client = createProcedureClient(match.procedure, {
40
- context: interceptorOptions2.context,
39
+ context,
41
40
  path: match.path,
42
- interceptors: this.options.clientInterceptors
41
+ interceptors: this.clientInterceptors
43
42
  });
44
43
  isDecoding = true;
45
- const input = await this.codec.decode(request, match.params, match.procedure);
44
+ const input = await this.codec.decode(request2, match.params, match.procedure);
46
45
  isDecoding = false;
47
- const lastEventId = Array.isArray(request.headers["last-event-id"]) ? request.headers["last-event-id"].at(-1) : request.headers["last-event-id"];
48
- const output = await client(input, { signal: request.signal, lastEventId });
46
+ const lastEventId = Array.isArray(request2.headers["last-event-id"]) ? request2.headers["last-event-id"].at(-1) : request2.headers["last-event-id"];
47
+ const output = await client(input, { signal: request2.signal, lastEventId });
49
48
  const response = this.codec.encode(output, match.procedure);
50
49
  return {
51
50
  matched: true,
@@ -54,7 +53,7 @@ class StandardHandler {
54
53
  }
55
54
  );
56
55
  } catch (e) {
57
- const error = isDecoding ? new ORPCError("BAD_REQUEST", {
56
+ const error = isDecoding && !(e instanceof ORPCError) ? new ORPCError("BAD_REQUEST", {
58
57
  message: `Malformed request. Ensure the request body is properly formatted and the 'Content-Type' header is set correctly.`,
59
58
  cause: e
60
59
  }) : toORPCError(e);
@@ -69,7 +68,7 @@ class StandardHandler {
69
68
  }
70
69
  }
71
70
 
72
- class RPCCodec {
71
+ class StandardRPCCodec {
73
72
  constructor(serializer) {
74
73
  this.serializer = serializer;
75
74
  }
@@ -93,7 +92,7 @@ class RPCCodec {
93
92
  }
94
93
  }
95
94
 
96
- class RPCMatcher {
95
+ class StandardRPCMatcher {
97
96
  tree = {};
98
97
  pendingRouters = [];
99
98
  init(router, path = []) {
@@ -155,4 +154,4 @@ class RPCMatcher {
155
154
  }
156
155
  }
157
156
 
158
- export { RPCCodec as R, StandardHandler as S, RPCMatcher as a };
157
+ export { StandardHandler as S, StandardRPCCodec as a, StandardRPCMatcher as b };
@@ -0,0 +1,8 @@
1
+ import { StandardRPCJsonSerializerOptions } from '@orpc/client/standard';
2
+ import { C as Context } from './server.MZvbGc3n.js';
3
+ import { a as StandardHandlerOptions } from './server.23VRZIfj.js';
4
+
5
+ interface StandardRPCHandlerOptions<T extends Context> extends StandardHandlerOptions<T>, StandardRPCJsonSerializerOptions {
6
+ }
7
+
8
+ export type { StandardRPCHandlerOptions as S };
@@ -1,7 +1,7 @@
1
1
  import { HTTPPath, AnySchema, Meta, InferSchemaOutput, ErrorFromErrorMap } from '@orpc/contract';
2
- import { Interceptor, MaybeOptionalOptions } from '@orpc/shared';
2
+ import { Interceptor } 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.CPteJIPP.mjs';
4
+ import { a as AnyRouter, A as AnyProcedure, C as Context, P as ProcedureClientInterceptorOptions, R as Router } from './server.MZvbGc3n.mjs';
5
5
  import { ORPCError } from '@orpc/client';
6
6
 
7
7
  type StandardParams = Record<string, string>;
@@ -20,16 +20,10 @@ interface StandardCodec {
20
20
  decode(request: StandardLazyRequest, params: StandardParams | undefined, procedure: AnyProcedure): Promise<unknown>;
21
21
  }
22
22
 
23
- type StandardHandleOptions<T extends Context> = {
23
+ interface StandardHandleOptions<T extends Context> {
24
24
  prefix?: HTTPPath;
25
- } & (Record<never, never> extends T ? {
26
- context?: T;
27
- } : {
28
25
  context: T;
29
- });
30
- type WellStandardHandleOptions<T extends Context> = StandardHandleOptions<T> & {
31
- context: T;
32
- };
26
+ }
33
27
  type StandardHandleResult = {
34
28
  matched: true;
35
29
  response: StandardResponse;
@@ -37,11 +31,14 @@ type StandardHandleResult = {
37
31
  matched: false;
38
32
  response: undefined;
39
33
  };
40
- type StandardHandlerInterceptorOptions<TContext extends Context> = WellStandardHandleOptions<TContext> & {
34
+ interface StandardHandlerPlugin<TContext extends Context> {
35
+ init?(options: StandardHandlerOptions<TContext>): void;
36
+ }
37
+ interface StandardHandlerInterceptorOptions<T extends Context> extends StandardHandleOptions<T> {
41
38
  request: StandardLazyRequest;
42
- };
39
+ }
43
40
  interface StandardHandlerOptions<TContext extends Context> {
44
- plugins?: HandlerPlugin<TContext>[];
41
+ plugins?: StandardHandlerPlugin<TContext>[];
45
42
  /**
46
43
  * Interceptors at the request level, helpful when you want catch errors
47
44
  */
@@ -59,19 +56,11 @@ interface StandardHandlerOptions<TContext extends Context> {
59
56
  declare class StandardHandler<T extends Context> {
60
57
  private readonly matcher;
61
58
  private readonly codec;
62
- private readonly options;
63
- private readonly plugin;
59
+ private readonly interceptors;
60
+ private readonly clientInterceptors;
61
+ private readonly rootInterceptors;
64
62
  constructor(router: Router<any, T>, matcher: StandardMatcher, codec: StandardCodec, options: NoInfer<StandardHandlerOptions<T>>);
65
- handle(request: StandardLazyRequest, ...[options]: MaybeOptionalOptions<StandardHandleOptions<T>>): Promise<StandardHandleResult>;
66
- }
67
-
68
- interface HandlerPlugin<TContext extends Context> {
69
- init?(options: StandardHandlerOptions<TContext>): void;
70
- }
71
- declare class CompositePlugin<TContext extends Context> implements HandlerPlugin<TContext> {
72
- private readonly plugins;
73
- constructor(plugins?: HandlerPlugin<TContext>[]);
74
- init(options: StandardHandlerOptions<TContext>): void;
63
+ handle(request: StandardLazyRequest, options: StandardHandleOptions<T>): Promise<StandardHandleResult>;
75
64
  }
76
65
 
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 };
66
+ export { type StandardHandleOptions as S, type StandardHandlerOptions as a, type StandardHandlerInterceptorOptions as b, type StandardHandlerPlugin as c, type StandardCodec as d, type StandardParams as e, type StandardMatcher as f, type StandardMatchResult as g, type StandardHandleResult as h, StandardHandler as i };
@@ -1,19 +1,19 @@
1
1
  import { ORPCErrorCode, ORPCErrorOptions, ORPCError, ClientContext, Client } from '@orpc/client';
2
- import { IsNever, MaybeOptionalOptions, Promisable, Interceptor, Value } from '@orpc/shared';
2
+ import { MaybeOptionalOptions, Promisable, Interceptor, Value } from '@orpc/shared';
3
3
  import { ErrorMap, ErrorMapItem, InferSchemaInput, HTTPPath, AnySchema, Meta, ContractProcedureDef, InferSchemaOutput, ErrorFromErrorMap, AnyContractRouter, ContractProcedure } from '@orpc/contract';
4
4
 
5
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;
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>;
11
9
 
12
10
  type ORPCErrorConstructorMapItemOptions<TData> = Omit<ORPCErrorOptions<TData>, 'defined' | 'status'>;
13
11
  type ORPCErrorConstructorMapItem<TCode extends ORPCErrorCode, TInData> = (...rest: MaybeOptionalOptions<ORPCErrorConstructorMapItemOptions<TInData>>) => ORPCError<TCode, TInData>;
14
12
  type ORPCErrorConstructorMap<T extends ErrorMap> = {
15
13
  [K in keyof T]: K extends ORPCErrorCode ? T[K] extends ErrorMapItem<infer UInputSchema> ? ORPCErrorConstructorMapItem<K, InferSchemaInput<UInputSchema>> : never : never;
16
14
  };
15
+ declare function createORPCErrorConstructorMap<T extends ErrorMap>(errors: T): ORPCErrorConstructorMap<T>;
16
+ declare function validateORPCError(map: ErrorMap, error: ORPCError<any, any>): Promise<ORPCError<string, unknown>>;
17
17
 
18
18
  declare const LAZY_SYMBOL: unique symbol;
19
19
  interface LazyMeta {
@@ -51,7 +51,7 @@ interface ProcedureHandler<TCurrentContext extends Context, TInput, THandlerOutp
51
51
  }
52
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
53
  __initialContext?: (type: TInitialContext) => unknown;
54
- middlewares: AnyMiddleware[];
54
+ middlewares: readonly AnyMiddleware[];
55
55
  inputValidationIndex: number;
56
56
  outputValidationIndex: number;
57
57
  handler: ProcedureHandler<TCurrentContext, any, any, any, any>;
@@ -72,8 +72,8 @@ type MiddlewareNextFnOptions<TOutContext extends Context> = Record<never, never>
72
72
  } : {
73
73
  context: TOutContext;
74
74
  };
75
- interface MiddlewareNextFn<TInContext extends Context, TOutput> {
76
- <U extends Context & Partial<TInContext> = Record<never, never>>(...rest: MaybeOptionalOptions<MiddlewareNextFnOptions<U>>): MiddlewareResult<U, TOutput>;
75
+ interface MiddlewareNextFn<TOutput> {
76
+ <U extends Context = Record<never, never>>(...rest: MaybeOptionalOptions<MiddlewareNextFnOptions<U>>): MiddlewareResult<U, TOutput>;
77
77
  }
78
78
  interface MiddlewareOutputFn<TOutput> {
79
79
  (output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
@@ -84,7 +84,7 @@ interface MiddlewareOptions<TInContext extends Context, TOutput, TErrorConstruct
84
84
  procedure: Procedure<Context, Context, AnySchema, AnySchema, ErrorMap, TMeta>;
85
85
  signal?: AbortSignal;
86
86
  lastEventId: string | undefined;
87
- next: MiddlewareNextFn<TInContext, TOutput>;
87
+ next: MiddlewareNextFn<TOutput>;
88
88
  errors: TErrorConstructorMap;
89
89
  }
90
90
  interface Middleware<TInContext extends Context, TOutContext extends Context, TInput, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
@@ -140,4 +140,4 @@ type InferRouterOutputs<T extends AnyRouter> = T extends Procedure<any, any, any
140
140
  [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterOutputs<U> : never;
141
141
  };
142
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 };
143
+ export { type AnyProcedure as A, middlewareOutputFn as B, type Context as C, type ProcedureHandlerOptions as D, type ProcedureDef as E, isProcedure as F, createProcedureClient as G, type InferRouterInitialContexts as H, type InferRouterInitialContext as I, type InferRouterCurrentContexts as J, type InferRouterInputs as K, type Lazyable as L, type Middleware as M, type InferRouterOutputs as N, type ORPCErrorConstructorMap as O, type ProcedureClientInterceptorOptions as P, type Router as R, type AnyRouter as a, Procedure as b, type MergedInitialContext as c, type MergedCurrentContext 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, type ORPCErrorConstructorMapItemOptions as k, type ORPCErrorConstructorMapItem as l, mergeCurrentContext as m, createORPCErrorConstructorMap as n, LAZY_SYMBOL as o, type LazyMeta as p, lazy as q, isLazy as r, getLazyMeta as s, type MiddlewareResult as t, unlazy as u, validateORPCError as v, type MiddlewareNextFnOptions as w, type MiddlewareNextFn as x, type MiddlewareOutputFn as y, type MiddlewareOptions as z };
@@ -1,19 +1,19 @@
1
1
  import { ORPCErrorCode, ORPCErrorOptions, ORPCError, ClientContext, Client } from '@orpc/client';
2
- import { IsNever, MaybeOptionalOptions, Promisable, Interceptor, Value } from '@orpc/shared';
2
+ import { MaybeOptionalOptions, Promisable, Interceptor, Value } from '@orpc/shared';
3
3
  import { ErrorMap, ErrorMapItem, InferSchemaInput, HTTPPath, AnySchema, Meta, ContractProcedureDef, InferSchemaOutput, ErrorFromErrorMap, AnyContractRouter, ContractProcedure } from '@orpc/contract';
4
4
 
5
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;
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>;
11
9
 
12
10
  type ORPCErrorConstructorMapItemOptions<TData> = Omit<ORPCErrorOptions<TData>, 'defined' | 'status'>;
13
11
  type ORPCErrorConstructorMapItem<TCode extends ORPCErrorCode, TInData> = (...rest: MaybeOptionalOptions<ORPCErrorConstructorMapItemOptions<TInData>>) => ORPCError<TCode, TInData>;
14
12
  type ORPCErrorConstructorMap<T extends ErrorMap> = {
15
13
  [K in keyof T]: K extends ORPCErrorCode ? T[K] extends ErrorMapItem<infer UInputSchema> ? ORPCErrorConstructorMapItem<K, InferSchemaInput<UInputSchema>> : never : never;
16
14
  };
15
+ declare function createORPCErrorConstructorMap<T extends ErrorMap>(errors: T): ORPCErrorConstructorMap<T>;
16
+ declare function validateORPCError(map: ErrorMap, error: ORPCError<any, any>): Promise<ORPCError<string, unknown>>;
17
17
 
18
18
  declare const LAZY_SYMBOL: unique symbol;
19
19
  interface LazyMeta {
@@ -51,7 +51,7 @@ interface ProcedureHandler<TCurrentContext extends Context, TInput, THandlerOutp
51
51
  }
52
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
53
  __initialContext?: (type: TInitialContext) => unknown;
54
- middlewares: AnyMiddleware[];
54
+ middlewares: readonly AnyMiddleware[];
55
55
  inputValidationIndex: number;
56
56
  outputValidationIndex: number;
57
57
  handler: ProcedureHandler<TCurrentContext, any, any, any, any>;
@@ -72,8 +72,8 @@ type MiddlewareNextFnOptions<TOutContext extends Context> = Record<never, never>
72
72
  } : {
73
73
  context: TOutContext;
74
74
  };
75
- interface MiddlewareNextFn<TInContext extends Context, TOutput> {
76
- <U extends Context & Partial<TInContext> = Record<never, never>>(...rest: MaybeOptionalOptions<MiddlewareNextFnOptions<U>>): MiddlewareResult<U, TOutput>;
75
+ interface MiddlewareNextFn<TOutput> {
76
+ <U extends Context = Record<never, never>>(...rest: MaybeOptionalOptions<MiddlewareNextFnOptions<U>>): MiddlewareResult<U, TOutput>;
77
77
  }
78
78
  interface MiddlewareOutputFn<TOutput> {
79
79
  (output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
@@ -84,7 +84,7 @@ interface MiddlewareOptions<TInContext extends Context, TOutput, TErrorConstruct
84
84
  procedure: Procedure<Context, Context, AnySchema, AnySchema, ErrorMap, TMeta>;
85
85
  signal?: AbortSignal;
86
86
  lastEventId: string | undefined;
87
- next: MiddlewareNextFn<TInContext, TOutput>;
87
+ next: MiddlewareNextFn<TOutput>;
88
88
  errors: TErrorConstructorMap;
89
89
  }
90
90
  interface Middleware<TInContext extends Context, TOutContext extends Context, TInput, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
@@ -140,4 +140,4 @@ type InferRouterOutputs<T extends AnyRouter> = T extends Procedure<any, any, any
140
140
  [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterOutputs<U> : never;
141
141
  };
142
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 };
143
+ export { type AnyProcedure as A, middlewareOutputFn as B, type Context as C, type ProcedureHandlerOptions as D, type ProcedureDef as E, isProcedure as F, createProcedureClient as G, type InferRouterInitialContexts as H, type InferRouterInitialContext as I, type InferRouterCurrentContexts as J, type InferRouterInputs as K, type Lazyable as L, type Middleware as M, type InferRouterOutputs as N, type ORPCErrorConstructorMap as O, type ProcedureClientInterceptorOptions as P, type Router as R, type AnyRouter as a, Procedure as b, type MergedInitialContext as c, type MergedCurrentContext 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, type ORPCErrorConstructorMapItemOptions as k, type ORPCErrorConstructorMapItem as l, mergeCurrentContext as m, createORPCErrorConstructorMap as n, LAZY_SYMBOL as o, type LazyMeta as p, lazy as q, isLazy as r, getLazyMeta as s, type MiddlewareResult as t, unlazy as u, validateORPCError as v, type MiddlewareNextFnOptions as w, type MiddlewareNextFn as x, type MiddlewareOutputFn as y, type MiddlewareOptions as z };
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.6c5bfe4",
4
+ "version": "0.0.0-next.6d75718",
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.6c5bfe4",
62
- "@orpc/contract": "0.0.0-next.6c5bfe4",
63
- "@orpc/standard-server": "0.0.0-next.6c5bfe4",
64
- "@orpc/shared": "0.0.0-next.6c5bfe4",
65
- "@orpc/standard-server-node": "0.0.0-next.6c5bfe4",
66
- "@orpc/standard-server-fetch": "0.0.0-next.6c5bfe4"
61
+ "@orpc/client": "0.0.0-next.6d75718",
62
+ "@orpc/shared": "0.0.0-next.6d75718",
63
+ "@orpc/contract": "0.0.0-next.6d75718",
64
+ "@orpc/standard-server": "0.0.0-next.6d75718",
65
+ "@orpc/standard-server-fetch": "0.0.0-next.6d75718",
66
+ "@orpc/standard-server-node": "0.0.0-next.6d75718"
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",