@orpc/server 0.0.0-next.3e1c2e9 → 0.0.0-next.3ee2e95

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 +9 -0
  2. package/dist/adapters/fetch/index.d.mts +41 -11
  3. package/dist/adapters/fetch/index.d.ts +41 -11
  4. package/dist/adapters/fetch/index.mjs +6 -6
  5. package/dist/adapters/hono/index.d.mts +5 -4
  6. package/dist/adapters/hono/index.d.ts +5 -4
  7. package/dist/adapters/hono/index.mjs +6 -6
  8. package/dist/adapters/next/index.d.mts +5 -4
  9. package/dist/adapters/next/index.d.ts +5 -4
  10. package/dist/adapters/next/index.mjs +6 -6
  11. package/dist/adapters/node/index.d.mts +43 -22
  12. package/dist/adapters/node/index.d.ts +43 -22
  13. package/dist/adapters/node/index.mjs +81 -21
  14. package/dist/adapters/standard/index.d.mts +6 -6
  15. package/dist/adapters/standard/index.d.ts +6 -6
  16. package/dist/adapters/standard/index.mjs +3 -3
  17. package/dist/index.d.mts +50 -36
  18. package/dist/index.d.ts +50 -36
  19. package/dist/index.mjs +27 -6
  20. package/dist/plugins/index.d.mts +108 -15
  21. package/dist/plugins/index.d.ts +108 -15
  22. package/dist/plugins/index.mjs +179 -6
  23. package/dist/shared/server.BVwwTHyO.mjs +9 -0
  24. package/dist/shared/{server.BtxZnWJ9.mjs → server.C37gDhSZ.mjs} +19 -29
  25. package/dist/shared/server.C56IpPNj.d.mts +10 -0
  26. package/dist/shared/{server.BY9sDlwl.mjs → server.C78d5yfh.mjs} +43 -28
  27. package/dist/shared/server.CSIDw0mq.mjs +106 -0
  28. package/dist/shared/{server.Dm3ZuTuI.d.ts → server.CyrwhzzU.d.ts} +2 -2
  29. package/dist/shared/{server.MZvbGc3n.d.mts → server.DLt5njUb.d.mts} +8 -8
  30. package/dist/shared/{server.MZvbGc3n.d.ts → server.DLt5njUb.d.ts} +8 -8
  31. package/dist/shared/{server.P4_D9lKb.d.mts → server.DfyOFejj.d.ts} +27 -29
  32. package/dist/shared/{server.BqBN5WhH.d.mts → server.DgrNIRDf.d.mts} +2 -2
  33. package/dist/shared/{server.CPqNKiJp.d.ts → server.Dm5DEJl5.d.mts} +27 -29
  34. package/dist/shared/server.DnuwaDJo.d.ts +10 -0
  35. package/package.json +8 -8
  36. package/dist/shared/server.Dba3Iiyp.mjs +0 -12
  37. package/dist/shared/server.Del5OmaY.mjs +0 -29
@@ -1,51 +1,66 @@
1
1
  import { ORPCError, toORPCError } from '@orpc/client';
2
- import { intercept, trim, parseEmptyableJSON } from '@orpc/shared';
3
- import { C as CompositeHandlerPlugin } from './server.Dba3Iiyp.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.BtxZnWJ9.mjs';
2
+ import { toArray, intercept, parseEmptyableJSON } from '@orpc/shared';
3
+ import { c as createProcedureClient, t as traverseContractProcedures, i as isProcedure, u as unlazy, g as getRouter, a as createContractedProcedure } from './server.C37gDhSZ.mjs';
4
+ import { toHttpPath } from '@orpc/client/standard';
5
+
6
+ class CompositeStandardHandlerPlugin {
7
+ plugins;
8
+ constructor(plugins = []) {
9
+ this.plugins = [...plugins].sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
10
+ }
11
+ init(options) {
12
+ for (const plugin of this.plugins) {
13
+ plugin.init?.(options);
14
+ }
15
+ }
16
+ }
5
17
 
6
18
  class StandardHandler {
7
19
  constructor(router, matcher, codec, options) {
8
20
  this.matcher = matcher;
9
21
  this.codec = codec;
10
- this.options = options;
11
- this.plugin = new CompositeHandlerPlugin(options.plugins);
12
- this.plugin.init(this.options);
22
+ const plugins = new CompositeStandardHandlerPlugin(options.plugins);
23
+ plugins.init(options);
24
+ this.interceptors = toArray(options.interceptors);
25
+ this.clientInterceptors = toArray(options.clientInterceptors);
26
+ this.rootInterceptors = toArray(options.rootInterceptors);
13
27
  this.matcher.init(router);
14
28
  }
15
- plugin;
16
- handle(request, ...[options]) {
29
+ interceptors;
30
+ clientInterceptors;
31
+ rootInterceptors;
32
+ async handle(request, options) {
33
+ const prefix = options.prefix?.replace(/\/$/, "") || void 0;
34
+ if (prefix && !request.url.pathname.startsWith(`${prefix}/`) && request.url.pathname !== prefix) {
35
+ return { matched: false, response: void 0 };
36
+ }
17
37
  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
- },
38
+ this.rootInterceptors,
39
+ { ...options, request, prefix },
25
40
  async (interceptorOptions) => {
26
41
  let isDecoding = false;
27
42
  try {
28
43
  return await intercept(
29
- this.options.interceptors ?? [],
44
+ this.interceptors,
30
45
  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 ?? "", ""), "/")}`;
35
- const match = await this.matcher.match(method, pathname);
46
+ async ({ request: request2, context, prefix: prefix2 }) => {
47
+ const method = request2.method;
48
+ const url = request2.url;
49
+ const pathname = prefix2 ? url.pathname.replace(prefix2, "") : url.pathname;
50
+ const match = await this.matcher.match(method, `/${pathname.replace(/^\/|\/$/g, "")}`);
36
51
  if (!match) {
37
52
  return { matched: false, response: void 0 };
38
53
  }
39
54
  const client = createProcedureClient(match.procedure, {
40
- context: interceptorOptions2.context,
55
+ context,
41
56
  path: match.path,
42
- interceptors: this.options.clientInterceptors
57
+ interceptors: this.clientInterceptors
43
58
  });
44
59
  isDecoding = true;
45
- const input = await this.codec.decode(request, match.params, match.procedure);
60
+ const input = await this.codec.decode(request2, match.params, match.procedure);
46
61
  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 });
62
+ const lastEventId = Array.isArray(request2.headers["last-event-id"]) ? request2.headers["last-event-id"].at(-1) : request2.headers["last-event-id"];
63
+ const output = await client(input, { signal: request2.signal, lastEventId });
49
64
  const response = this.codec.encode(output, match.procedure);
50
65
  return {
51
66
  matched: true,
@@ -54,7 +69,7 @@ class StandardHandler {
54
69
  }
55
70
  );
56
71
  } catch (e) {
57
- const error = isDecoding ? new ORPCError("BAD_REQUEST", {
72
+ const error = isDecoding && !(e instanceof ORPCError) ? new ORPCError("BAD_REQUEST", {
58
73
  message: `Malformed request. Ensure the request body is properly formatted and the 'Content-Type' header is set correctly.`,
59
74
  cause: e
60
75
  }) : toORPCError(e);
@@ -155,4 +170,4 @@ class StandardRPCMatcher {
155
170
  }
156
171
  }
157
172
 
158
- export { StandardHandler as S, StandardRPCCodec as a, StandardRPCMatcher as b };
173
+ export { CompositeStandardHandlerPlugin as C, StandardHandler as S, StandardRPCCodec as a, StandardRPCMatcher as b };
@@ -0,0 +1,106 @@
1
+ import { ORPCError } from '@orpc/client';
2
+ import { StandardRPCJsonSerializer, StandardRPCSerializer } from '@orpc/client/standard';
3
+ import { C as CompositeStandardHandlerPlugin, S as StandardHandler, b as StandardRPCMatcher, a as StandardRPCCodec } from './server.C78d5yfh.mjs';
4
+ import { toArray, intercept, resolveMaybeOptionalOptions } from '@orpc/shared';
5
+ import { toStandardLazyRequest, toFetchResponse } from '@orpc/standard-server-fetch';
6
+ import { r as resolveFriendlyStandardHandleOptions } from './server.BVwwTHyO.mjs';
7
+ import '@orpc/contract';
8
+
9
+ class BodyLimitPlugin {
10
+ maxBodySize;
11
+ constructor(options) {
12
+ this.maxBodySize = options.maxBodySize;
13
+ }
14
+ initRuntimeAdapter(options) {
15
+ options.adapterInterceptors ??= [];
16
+ options.adapterInterceptors.push(async (options2) => {
17
+ if (!options2.request.body) {
18
+ return options2.next();
19
+ }
20
+ let currentBodySize = 0;
21
+ const rawReader = options2.request.body.getReader();
22
+ const reader = new ReadableStream({
23
+ start: async (controller) => {
24
+ try {
25
+ if (Number(options2.request.headers.get("content-length")) > this.maxBodySize) {
26
+ controller.error(new ORPCError("PAYLOAD_TOO_LARGE"));
27
+ return;
28
+ }
29
+ while (true) {
30
+ const { done, value } = await rawReader.read();
31
+ if (done) {
32
+ break;
33
+ }
34
+ currentBodySize += value.length;
35
+ if (currentBodySize > this.maxBodySize) {
36
+ controller.error(new ORPCError("PAYLOAD_TOO_LARGE"));
37
+ break;
38
+ }
39
+ controller.enqueue(value);
40
+ }
41
+ } finally {
42
+ controller.close();
43
+ }
44
+ }
45
+ });
46
+ const requestInit = { body: reader, duplex: "half" };
47
+ return options2.next({
48
+ ...options2,
49
+ request: new Request(options2.request, requestInit)
50
+ });
51
+ });
52
+ }
53
+ }
54
+
55
+ class CompositeFetchHandlerPlugin extends CompositeStandardHandlerPlugin {
56
+ initRuntimeAdapter(options) {
57
+ for (const plugin of this.plugins) {
58
+ plugin.initRuntimeAdapter?.(options);
59
+ }
60
+ }
61
+ }
62
+
63
+ class FetchHandler {
64
+ constructor(standardHandler, options = {}) {
65
+ this.standardHandler = standardHandler;
66
+ const plugin = new CompositeFetchHandlerPlugin(options.plugins);
67
+ plugin.initRuntimeAdapter(options);
68
+ this.adapterInterceptors = toArray(options.adapterInterceptors);
69
+ this.toFetchResponseOptions = options;
70
+ }
71
+ toFetchResponseOptions;
72
+ adapterInterceptors;
73
+ async handle(request, ...rest) {
74
+ return intercept(
75
+ this.adapterInterceptors,
76
+ {
77
+ ...resolveFriendlyStandardHandleOptions(resolveMaybeOptionalOptions(rest)),
78
+ request,
79
+ toFetchResponseOptions: this.toFetchResponseOptions
80
+ },
81
+ async ({ request: request2, toFetchResponseOptions, ...options }) => {
82
+ const standardRequest = toStandardLazyRequest(request2);
83
+ const result = await this.standardHandler.handle(standardRequest, options);
84
+ if (!result.matched) {
85
+ return result;
86
+ }
87
+ return {
88
+ matched: true,
89
+ response: toFetchResponse(result.response, toFetchResponseOptions)
90
+ };
91
+ }
92
+ );
93
+ }
94
+ }
95
+
96
+ class RPCHandler extends FetchHandler {
97
+ constructor(router, options = {}) {
98
+ const jsonSerializer = new StandardRPCJsonSerializer(options);
99
+ const serializer = new StandardRPCSerializer(jsonSerializer);
100
+ const matcher = new StandardRPCMatcher();
101
+ const codec = new StandardRPCCodec(serializer);
102
+ super(new StandardHandler(router, matcher, codec, options), options);
103
+ }
104
+ }
105
+
106
+ export { BodyLimitPlugin as B, CompositeFetchHandlerPlugin as C, FetchHandler as F, RPCHandler as R };
@@ -1,6 +1,6 @@
1
1
  import { StandardRPCJsonSerializerOptions } from '@orpc/client/standard';
2
- import { C as Context } from './server.MZvbGc3n.js';
3
- import { a as StandardHandlerOptions } from './server.CPqNKiJp.js';
2
+ import { C as Context } from './server.DLt5njUb.js';
3
+ import { a as StandardHandlerOptions } from './server.DfyOFejj.js';
4
4
 
5
5
  interface StandardRPCHandlerOptions<T extends Context> extends StandardHandlerOptions<T>, StandardRPCJsonSerializerOptions {
6
6
  }
@@ -1,8 +1,8 @@
1
- import { ORPCErrorCode, ORPCErrorOptions, ORPCError, ClientContext, Client } from '@orpc/client';
1
+ import { ORPCErrorCode, ORPCErrorOptions, ORPCError, HTTPPath, ClientContext, Client } from '@orpc/client';
2
2
  import { MaybeOptionalOptions, Promisable, Interceptor, Value } from '@orpc/shared';
3
- import { ErrorMap, ErrorMapItem, InferSchemaInput, HTTPPath, AnySchema, Meta, ContractProcedureDef, InferSchemaOutput, ErrorFromErrorMap, AnyContractRouter, ContractProcedure } from '@orpc/contract';
3
+ import { ErrorMap, ErrorMapItem, InferSchemaInput, AnySchema, Meta, ContractProcedureDef, InferSchemaOutput, ErrorFromErrorMap, AnyContractRouter, ContractProcedure } from '@orpc/contract';
4
4
 
5
- type Context = Record<string, any>;
5
+ type Context = Record<PropertyKey, any>;
6
6
  type MergedInitialContext<TInitial extends Context, TAdditional extends Context, TCurrent extends Context> = TInitial & Omit<TAdditional, keyof TCurrent>;
7
7
  type MergedCurrentContext<T extends Context, U extends Context> = Omit<T, keyof U> & U;
8
8
  declare function mergeCurrentContext<T extends Context, U extends Context>(context: T, other: U): MergedCurrentContext<T, U>;
@@ -97,9 +97,9 @@ interface MapInputMiddleware<TInput, TMappedInput> {
97
97
  declare function middlewareOutputFn<TOutput>(output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
98
98
 
99
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> {
100
+ interface ProcedureClientInterceptorOptions<TInitialContext extends Context, TErrorMap extends ErrorMap, TMeta extends Meta> {
101
101
  context: TInitialContext;
102
- input: InferSchemaInput<TInputSchema>;
102
+ input: unknown;
103
103
  errors: ORPCErrorConstructorMap<TErrorMap>;
104
104
  path: readonly string[];
105
105
  procedure: Procedure<Context, Context, AnySchema, AnySchema, ErrorMap, TMeta>;
@@ -109,18 +109,18 @@ interface ProcedureClientInterceptorOptions<TInitialContext extends Context, TIn
109
109
  /**
110
110
  * Options for creating a procedure caller with comprehensive type safety
111
111
  */
112
- type CreateProcedureClientOptions<TInitialContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext> = {
112
+ type CreateProcedureClientOptions<TInitialContext extends Context, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext> = {
113
113
  /**
114
114
  * This is helpful for logging and analytics.
115
115
  */
116
116
  path?: readonly string[];
117
- interceptors?: Interceptor<ProcedureClientInterceptorOptions<TInitialContext, TInputSchema, TErrorMap, TMeta>, InferSchemaOutput<TOutputSchema>, ErrorFromErrorMap<TErrorMap>>[];
117
+ interceptors?: Interceptor<ProcedureClientInterceptorOptions<TInitialContext, TErrorMap, TMeta>, InferSchemaOutput<TOutputSchema>, ErrorFromErrorMap<TErrorMap>>[];
118
118
  } & (Record<never, never> extends TInitialContext ? {
119
119
  context?: Value<TInitialContext, [clientContext: TClientContext]>;
120
120
  } : {
121
121
  context: Value<TInitialContext, [clientContext: TClientContext]>;
122
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>;
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, TOutputSchema, TErrorMap, TMeta, TClientContext>>): ProcedureClient<TClientContext, TInputSchema, TOutputSchema, TErrorMap>;
124
124
 
125
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
126
  [K in keyof T]: T[K] extends AnyContractRouter ? Lazyable<Router<T[K], TInitialContext>> : never;
@@ -1,8 +1,8 @@
1
- import { ORPCErrorCode, ORPCErrorOptions, ORPCError, ClientContext, Client } from '@orpc/client';
1
+ import { ORPCErrorCode, ORPCErrorOptions, ORPCError, HTTPPath, ClientContext, Client } from '@orpc/client';
2
2
  import { MaybeOptionalOptions, Promisable, Interceptor, Value } from '@orpc/shared';
3
- import { ErrorMap, ErrorMapItem, InferSchemaInput, HTTPPath, AnySchema, Meta, ContractProcedureDef, InferSchemaOutput, ErrorFromErrorMap, AnyContractRouter, ContractProcedure } from '@orpc/contract';
3
+ import { ErrorMap, ErrorMapItem, InferSchemaInput, AnySchema, Meta, ContractProcedureDef, InferSchemaOutput, ErrorFromErrorMap, AnyContractRouter, ContractProcedure } from '@orpc/contract';
4
4
 
5
- type Context = Record<string, any>;
5
+ type Context = Record<PropertyKey, any>;
6
6
  type MergedInitialContext<TInitial extends Context, TAdditional extends Context, TCurrent extends Context> = TInitial & Omit<TAdditional, keyof TCurrent>;
7
7
  type MergedCurrentContext<T extends Context, U extends Context> = Omit<T, keyof U> & U;
8
8
  declare function mergeCurrentContext<T extends Context, U extends Context>(context: T, other: U): MergedCurrentContext<T, U>;
@@ -97,9 +97,9 @@ interface MapInputMiddleware<TInput, TMappedInput> {
97
97
  declare function middlewareOutputFn<TOutput>(output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
98
98
 
99
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> {
100
+ interface ProcedureClientInterceptorOptions<TInitialContext extends Context, TErrorMap extends ErrorMap, TMeta extends Meta> {
101
101
  context: TInitialContext;
102
- input: InferSchemaInput<TInputSchema>;
102
+ input: unknown;
103
103
  errors: ORPCErrorConstructorMap<TErrorMap>;
104
104
  path: readonly string[];
105
105
  procedure: Procedure<Context, Context, AnySchema, AnySchema, ErrorMap, TMeta>;
@@ -109,18 +109,18 @@ interface ProcedureClientInterceptorOptions<TInitialContext extends Context, TIn
109
109
  /**
110
110
  * Options for creating a procedure caller with comprehensive type safety
111
111
  */
112
- type CreateProcedureClientOptions<TInitialContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext> = {
112
+ type CreateProcedureClientOptions<TInitialContext extends Context, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext> = {
113
113
  /**
114
114
  * This is helpful for logging and analytics.
115
115
  */
116
116
  path?: readonly string[];
117
- interceptors?: Interceptor<ProcedureClientInterceptorOptions<TInitialContext, TInputSchema, TErrorMap, TMeta>, InferSchemaOutput<TOutputSchema>, ErrorFromErrorMap<TErrorMap>>[];
117
+ interceptors?: Interceptor<ProcedureClientInterceptorOptions<TInitialContext, TErrorMap, TMeta>, InferSchemaOutput<TOutputSchema>, ErrorFromErrorMap<TErrorMap>>[];
118
118
  } & (Record<never, never> extends TInitialContext ? {
119
119
  context?: Value<TInitialContext, [clientContext: TClientContext]>;
120
120
  } : {
121
121
  context: Value<TInitialContext, [clientContext: TClientContext]>;
122
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>;
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, TOutputSchema, TErrorMap, TMeta, TClientContext>>): ProcedureClient<TClientContext, TInputSchema, TOutputSchema, TErrorMap>;
124
124
 
125
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
126
  [K in keyof T]: T[K] extends AnyContractRouter ? Lazyable<Router<T[K], TInitialContext>> : never;
@@ -1,8 +1,8 @@
1
- import { HTTPPath, AnySchema, Meta, InferSchemaOutput, ErrorFromErrorMap } from '@orpc/contract';
2
- import { Interceptor, MaybeOptionalOptions } from '@orpc/shared';
1
+ import { HTTPPath, ORPCError } from '@orpc/client';
2
+ import { Meta, InferSchemaOutput, AnySchema, ErrorFromErrorMap } from '@orpc/contract';
3
+ import { Interceptor, ThrowableError } from '@orpc/shared';
3
4
  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.MZvbGc3n.mjs';
5
- import { ORPCError } from '@orpc/client';
5
+ import { a as AnyRouter, A as AnyProcedure, C as Context, P as ProcedureClientInterceptorOptions, R as Router } from './server.DLt5njUb.js';
6
6
 
7
7
  type StandardParams = Record<string, string>;
8
8
  type StandardMatchResult = {
@@ -20,13 +20,20 @@ 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 StandardHandlerPlugin<TContext extends Context> {
24
+ order?: number;
25
+ init?(options: StandardHandlerOptions<TContext>): void;
26
+ }
27
+ declare class CompositeStandardHandlerPlugin<T extends Context, TPlugin extends StandardHandlerPlugin<T>> implements StandardHandlerPlugin<T> {
28
+ protected readonly plugins: TPlugin[];
29
+ constructor(plugins?: readonly TPlugin[]);
30
+ init(options: StandardHandlerOptions<T>): void;
31
+ }
32
+
33
+ interface StandardHandleOptions<T extends Context> {
24
34
  prefix?: HTTPPath;
25
- } & (Record<never, never> extends T ? {
26
- context?: T;
27
- } : {
28
35
  context: T;
29
- });
36
+ }
30
37
  type StandardHandleResult = {
31
38
  matched: true;
32
39
  response: StandardResponse;
@@ -34,42 +41,33 @@ type StandardHandleResult = {
34
41
  matched: false;
35
42
  response: undefined;
36
43
  };
37
- type StandardHandlerInterceptorOptions<T extends Context> = StandardHandleOptions<T> & {
38
- context: T;
44
+ interface StandardHandlerInterceptorOptions<T extends Context> extends StandardHandleOptions<T> {
39
45
  request: StandardLazyRequest;
40
- };
46
+ }
41
47
  interface StandardHandlerOptions<TContext extends Context> {
42
- plugins?: HandlerPlugin<TContext>[];
48
+ plugins?: StandardHandlerPlugin<TContext>[];
43
49
  /**
44
50
  * Interceptors at the request level, helpful when you want catch errors
45
51
  */
46
- interceptors?: Interceptor<StandardHandlerInterceptorOptions<TContext>, StandardHandleResult, unknown>[];
52
+ interceptors?: Interceptor<StandardHandlerInterceptorOptions<TContext>, StandardHandleResult, ThrowableError>[];
47
53
  /**
48
54
  * Interceptors at the root level, helpful when you want override the request/response
49
55
  */
50
- rootInterceptors?: Interceptor<StandardHandlerInterceptorOptions<TContext>, StandardHandleResult, unknown>[];
56
+ rootInterceptors?: Interceptor<StandardHandlerInterceptorOptions<TContext>, StandardHandleResult, ThrowableError>[];
51
57
  /**
52
58
  *
53
59
  * Interceptors for procedure client.
54
60
  */
55
- clientInterceptors?: Interceptor<ProcedureClientInterceptorOptions<TContext, AnySchema, Record<never, never>, Meta>, InferSchemaOutput<AnySchema>, ErrorFromErrorMap<Record<never, never>>>[];
61
+ clientInterceptors?: Interceptor<ProcedureClientInterceptorOptions<TContext, Record<never, never>, Meta>, InferSchemaOutput<AnySchema>, ErrorFromErrorMap<Record<never, never>>>[];
56
62
  }
57
63
  declare class StandardHandler<T extends Context> {
58
64
  private readonly matcher;
59
65
  private readonly codec;
60
- private readonly options;
61
- private readonly plugin;
66
+ private readonly interceptors;
67
+ private readonly clientInterceptors;
68
+ private readonly rootInterceptors;
62
69
  constructor(router: Router<any, T>, matcher: StandardMatcher, codec: StandardCodec, options: NoInfer<StandardHandlerOptions<T>>);
63
- handle(request: StandardLazyRequest, ...[options]: MaybeOptionalOptions<StandardHandleOptions<T>>): Promise<StandardHandleResult>;
64
- }
65
-
66
- interface HandlerPlugin<TContext extends Context> {
67
- init?(options: StandardHandlerOptions<TContext>): void;
68
- }
69
- declare class CompositeHandlerPlugin<TContext extends Context> implements HandlerPlugin<TContext> {
70
- private readonly plugins;
71
- constructor(plugins?: HandlerPlugin<TContext>[]);
72
- init(options: StandardHandlerOptions<TContext>): void;
70
+ handle(request: StandardLazyRequest, options: StandardHandleOptions<T>): Promise<StandardHandleResult>;
73
71
  }
74
72
 
75
- export { CompositeHandlerPlugin as C, type HandlerPlugin as H, type StandardHandleOptions as S, type StandardHandlerOptions as a, type StandardHandlerInterceptorOptions 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 };
73
+ export { CompositeStandardHandlerPlugin as C, type StandardHandleOptions as S, type StandardHandlerOptions as a, type StandardCodec as b, type StandardParams as c, type StandardMatcher as d, type StandardMatchResult as e, type StandardHandleResult as f, type StandardHandlerInterceptorOptions as g, StandardHandler as h, type StandardHandlerPlugin as i };
@@ -1,6 +1,6 @@
1
1
  import { StandardRPCJsonSerializerOptions } from '@orpc/client/standard';
2
- import { C as Context } from './server.MZvbGc3n.mjs';
3
- import { a as StandardHandlerOptions } from './server.P4_D9lKb.mjs';
2
+ import { C as Context } from './server.DLt5njUb.mjs';
3
+ import { a as StandardHandlerOptions } from './server.Dm5DEJl5.mjs';
4
4
 
5
5
  interface StandardRPCHandlerOptions<T extends Context> extends StandardHandlerOptions<T>, StandardRPCJsonSerializerOptions {
6
6
  }
@@ -1,8 +1,8 @@
1
- import { HTTPPath, AnySchema, Meta, InferSchemaOutput, ErrorFromErrorMap } from '@orpc/contract';
2
- import { Interceptor, MaybeOptionalOptions } from '@orpc/shared';
1
+ import { HTTPPath, ORPCError } from '@orpc/client';
2
+ import { Meta, InferSchemaOutput, AnySchema, ErrorFromErrorMap } from '@orpc/contract';
3
+ import { Interceptor, ThrowableError } from '@orpc/shared';
3
4
  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.MZvbGc3n.js';
5
- import { ORPCError } from '@orpc/client';
5
+ import { a as AnyRouter, A as AnyProcedure, C as Context, P as ProcedureClientInterceptorOptions, R as Router } from './server.DLt5njUb.mjs';
6
6
 
7
7
  type StandardParams = Record<string, string>;
8
8
  type StandardMatchResult = {
@@ -20,13 +20,20 @@ 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 StandardHandlerPlugin<TContext extends Context> {
24
+ order?: number;
25
+ init?(options: StandardHandlerOptions<TContext>): void;
26
+ }
27
+ declare class CompositeStandardHandlerPlugin<T extends Context, TPlugin extends StandardHandlerPlugin<T>> implements StandardHandlerPlugin<T> {
28
+ protected readonly plugins: TPlugin[];
29
+ constructor(plugins?: readonly TPlugin[]);
30
+ init(options: StandardHandlerOptions<T>): void;
31
+ }
32
+
33
+ interface StandardHandleOptions<T extends Context> {
24
34
  prefix?: HTTPPath;
25
- } & (Record<never, never> extends T ? {
26
- context?: T;
27
- } : {
28
35
  context: T;
29
- });
36
+ }
30
37
  type StandardHandleResult = {
31
38
  matched: true;
32
39
  response: StandardResponse;
@@ -34,42 +41,33 @@ type StandardHandleResult = {
34
41
  matched: false;
35
42
  response: undefined;
36
43
  };
37
- type StandardHandlerInterceptorOptions<T extends Context> = StandardHandleOptions<T> & {
38
- context: T;
44
+ interface StandardHandlerInterceptorOptions<T extends Context> extends StandardHandleOptions<T> {
39
45
  request: StandardLazyRequest;
40
- };
46
+ }
41
47
  interface StandardHandlerOptions<TContext extends Context> {
42
- plugins?: HandlerPlugin<TContext>[];
48
+ plugins?: StandardHandlerPlugin<TContext>[];
43
49
  /**
44
50
  * Interceptors at the request level, helpful when you want catch errors
45
51
  */
46
- interceptors?: Interceptor<StandardHandlerInterceptorOptions<TContext>, StandardHandleResult, unknown>[];
52
+ interceptors?: Interceptor<StandardHandlerInterceptorOptions<TContext>, StandardHandleResult, ThrowableError>[];
47
53
  /**
48
54
  * Interceptors at the root level, helpful when you want override the request/response
49
55
  */
50
- rootInterceptors?: Interceptor<StandardHandlerInterceptorOptions<TContext>, StandardHandleResult, unknown>[];
56
+ rootInterceptors?: Interceptor<StandardHandlerInterceptorOptions<TContext>, StandardHandleResult, ThrowableError>[];
51
57
  /**
52
58
  *
53
59
  * Interceptors for procedure client.
54
60
  */
55
- clientInterceptors?: Interceptor<ProcedureClientInterceptorOptions<TContext, AnySchema, Record<never, never>, Meta>, InferSchemaOutput<AnySchema>, ErrorFromErrorMap<Record<never, never>>>[];
61
+ clientInterceptors?: Interceptor<ProcedureClientInterceptorOptions<TContext, Record<never, never>, Meta>, InferSchemaOutput<AnySchema>, ErrorFromErrorMap<Record<never, never>>>[];
56
62
  }
57
63
  declare class StandardHandler<T extends Context> {
58
64
  private readonly matcher;
59
65
  private readonly codec;
60
- private readonly options;
61
- private readonly plugin;
66
+ private readonly interceptors;
67
+ private readonly clientInterceptors;
68
+ private readonly rootInterceptors;
62
69
  constructor(router: Router<any, T>, matcher: StandardMatcher, codec: StandardCodec, options: NoInfer<StandardHandlerOptions<T>>);
63
- handle(request: StandardLazyRequest, ...[options]: MaybeOptionalOptions<StandardHandleOptions<T>>): Promise<StandardHandleResult>;
64
- }
65
-
66
- interface HandlerPlugin<TContext extends Context> {
67
- init?(options: StandardHandlerOptions<TContext>): void;
68
- }
69
- declare class CompositeHandlerPlugin<TContext extends Context> implements HandlerPlugin<TContext> {
70
- private readonly plugins;
71
- constructor(plugins?: HandlerPlugin<TContext>[]);
72
- init(options: StandardHandlerOptions<TContext>): void;
70
+ handle(request: StandardLazyRequest, options: StandardHandleOptions<T>): Promise<StandardHandleResult>;
73
71
  }
74
72
 
75
- export { CompositeHandlerPlugin as C, type HandlerPlugin as H, type StandardHandleOptions as S, type StandardHandlerOptions as a, type StandardHandlerInterceptorOptions 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 };
73
+ export { CompositeStandardHandlerPlugin as C, type StandardHandleOptions as S, type StandardHandlerOptions as a, type StandardCodec as b, type StandardParams as c, type StandardMatcher as d, type StandardMatchResult as e, type StandardHandleResult as f, type StandardHandlerInterceptorOptions as g, StandardHandler as h, type StandardHandlerPlugin as i };
@@ -0,0 +1,10 @@
1
+ import { C as Context } from './server.DLt5njUb.js';
2
+ import { S as StandardHandleOptions } from './server.DfyOFejj.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 };
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.3e1c2e9",
4
+ "version": "0.0.0-next.3ee2e95",
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/contract": "0.0.0-next.3e1c2e9",
62
- "@orpc/shared": "0.0.0-next.3e1c2e9",
63
- "@orpc/standard-server-fetch": "0.0.0-next.3e1c2e9",
64
- "@orpc/standard-server": "0.0.0-next.3e1c2e9",
65
- "@orpc/standard-server-node": "0.0.0-next.3e1c2e9",
66
- "@orpc/client": "0.0.0-next.3e1c2e9"
61
+ "@orpc/shared": "0.0.0-next.3ee2e95",
62
+ "@orpc/client": "0.0.0-next.3ee2e95",
63
+ "@orpc/standard-server": "0.0.0-next.3ee2e95",
64
+ "@orpc/standard-server-fetch": "0.0.0-next.3ee2e95",
65
+ "@orpc/standard-server-node": "0.0.0-next.3ee2e95",
66
+ "@orpc/contract": "0.0.0-next.3ee2e95"
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,12 +0,0 @@
1
- class CompositeHandlerPlugin {
2
- constructor(plugins = []) {
3
- this.plugins = plugins;
4
- }
5
- init(options) {
6
- for (const plugin of this.plugins) {
7
- plugin.init?.(options);
8
- }
9
- }
10
- }
11
-
12
- export { CompositeHandlerPlugin as C };
@@ -1,29 +0,0 @@
1
- import { StandardRPCJsonSerializer, StandardRPCSerializer } from '@orpc/client/standard';
2
- import { toStandardLazyRequest, toFetchResponse } from '@orpc/standard-server-fetch';
3
- import { S as StandardHandler, b as StandardRPCMatcher, a as StandardRPCCodec } from './server.BY9sDlwl.mjs';
4
-
5
- class RPCHandler {
6
- standardHandler;
7
- constructor(router, options = {}) {
8
- const jsonSerializer = new StandardRPCJsonSerializer(options);
9
- const serializer = new StandardRPCSerializer(jsonSerializer);
10
- const matcher = new StandardRPCMatcher();
11
- const codec = new StandardRPCCodec(serializer);
12
- this.standardHandler = new StandardHandler(router, matcher, codec, options);
13
- }
14
- async handle(request, ...[
15
- options = {}
16
- ]) {
17
- const standardRequest = toStandardLazyRequest(request);
18
- const result = await this.standardHandler.handle(standardRequest, options);
19
- if (!result.matched) {
20
- return result;
21
- }
22
- return {
23
- matched: true,
24
- response: toFetchResponse(result.response, options)
25
- };
26
- }
27
- }
28
-
29
- export { RPCHandler as R };