@orpc/server 1.14.6 → 2.0.0-beta.10

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 (78) hide show
  1. package/README.md +78 -137
  2. package/dist/adapters/crossws/index.d.mts +42 -21
  3. package/dist/adapters/crossws/index.d.ts +42 -21
  4. package/dist/adapters/crossws/index.mjs +37 -18
  5. package/dist/adapters/fetch/index.d.mts +83 -67
  6. package/dist/adapters/fetch/index.d.ts +83 -67
  7. package/dist/adapters/fetch/index.mjs +131 -106
  8. package/dist/adapters/message-port/index.d.mts +51 -34
  9. package/dist/adapters/message-port/index.d.ts +51 -34
  10. package/dist/adapters/message-port/index.mjs +73 -38
  11. package/dist/adapters/node/index.d.mts +82 -60
  12. package/dist/adapters/node/index.d.ts +82 -60
  13. package/dist/adapters/node/index.mjs +127 -98
  14. package/dist/adapters/standard/index.d.mts +16 -18
  15. package/dist/adapters/standard/index.d.ts +16 -18
  16. package/dist/adapters/standard/index.mjs +5 -5
  17. package/dist/adapters/standard-peer/index.d.mts +12 -14
  18. package/dist/adapters/standard-peer/index.d.ts +12 -14
  19. package/dist/adapters/standard-peer/index.mjs +2 -21
  20. package/dist/adapters/websocket/index.d.mts +50 -36
  21. package/dist/adapters/websocket/index.d.ts +50 -36
  22. package/dist/adapters/websocket/index.mjs +55 -42
  23. package/dist/extensions/callable.d.mts +10 -0
  24. package/dist/extensions/callable.d.ts +10 -0
  25. package/dist/extensions/callable.mjs +11 -0
  26. package/dist/helpers/index.d.mts +2 -2
  27. package/dist/helpers/index.d.ts +2 -2
  28. package/dist/helpers/index.mjs +1 -1
  29. package/dist/index.d.mts +163 -770
  30. package/dist/index.d.ts +163 -770
  31. package/dist/index.mjs +296 -403
  32. package/dist/plugins/index.d.mts +120 -127
  33. package/dist/plugins/index.d.ts +120 -127
  34. package/dist/plugins/index.mjs +252 -223
  35. package/dist/shared/server.BL22TloH.d.mts +184 -0
  36. package/dist/shared/server.BL22TloH.d.ts +184 -0
  37. package/dist/shared/server.BsNNjG5J.d.mts +61 -0
  38. package/dist/shared/server.BwHnWUuN.mjs +222 -0
  39. package/dist/shared/server.CX4vUnDk.mjs +11 -0
  40. package/dist/shared/server.Cj5lgPuG.d.mts +66 -0
  41. package/dist/shared/server.CjOb6ItT.mjs +41 -0
  42. package/dist/shared/server.CrlKQucM.mjs +233 -0
  43. package/dist/shared/server.DBANIheG.d.ts +104 -0
  44. package/dist/shared/server.DJIkub_t.d.mts +104 -0
  45. package/dist/shared/server.D_QauotT.mjs +30 -0
  46. package/dist/shared/server.GDpX6Df8.mjs +271 -0
  47. package/dist/shared/server.Pa0F03f_.d.ts +61 -0
  48. package/dist/shared/{server.DZ5BIITo.mjs → server.W91HSRkE.mjs} +2 -2
  49. package/dist/shared/server.d_2NS3g5.d.ts +66 -0
  50. package/package.json +26 -55
  51. package/dist/adapters/aws-lambda/index.d.mts +0 -46
  52. package/dist/adapters/aws-lambda/index.d.ts +0 -46
  53. package/dist/adapters/aws-lambda/index.mjs +0 -40
  54. package/dist/adapters/bun-ws/index.d.mts +0 -36
  55. package/dist/adapters/bun-ws/index.d.ts +0 -36
  56. package/dist/adapters/bun-ws/index.mjs +0 -47
  57. package/dist/adapters/fastify/index.d.mts +0 -53
  58. package/dist/adapters/fastify/index.d.ts +0 -53
  59. package/dist/adapters/fastify/index.mjs +0 -52
  60. package/dist/adapters/ws/index.d.mts +0 -31
  61. package/dist/adapters/ws/index.d.ts +0 -31
  62. package/dist/adapters/ws/index.mjs +0 -37
  63. package/dist/hibernation/index.d.mts +0 -44
  64. package/dist/hibernation/index.d.ts +0 -44
  65. package/dist/hibernation/index.mjs +0 -65
  66. package/dist/shared/server.7cEtMB30.d.ts +0 -74
  67. package/dist/shared/server.B8gYOD5g.d.mts +0 -12
  68. package/dist/shared/server.BqadksTP.d.mts +0 -74
  69. package/dist/shared/server.C8_sRzQB.d.mts +0 -42
  70. package/dist/shared/server.ChUyt5-i.d.mts +0 -32
  71. package/dist/shared/server.ChyoA9XY.d.ts +0 -42
  72. package/dist/shared/server.DEBcqOjg.mjs +0 -418
  73. package/dist/shared/server.EfTOZ2Q7.d.ts +0 -12
  74. package/dist/shared/server.TEVCLCFC.mjs +0 -39
  75. package/dist/shared/server.ZxHCEN1h.mjs +0 -226
  76. package/dist/shared/server.qKsRrdxW.d.mts +0 -193
  77. package/dist/shared/server.qKsRrdxW.d.ts +0 -193
  78. package/dist/shared/server.yoEB3Fx4.d.ts +0 -32
@@ -0,0 +1,184 @@
1
+ import { ORPCErrorCode, ORPCErrorOptions, ORPCError, AnyORPCError, ClientContext, Client } from '@orpc/client';
2
+ import { ErrorMap, ErrorMapItem, InferSchemaInput, AnyMetaPlugin, AnySchema, ProcedureContractDefinition, Meta, InferSchemaOutput, ORPCErrorFromErrorMap } from '@orpc/contract';
3
+ import { MaybeOptionalOptions, Promisable, Interceptor, PromiseWithError, ThrowableError, Value } from '@orpc/shared';
4
+
5
+ interface Context {
6
+ [key: PropertyKey]: any;
7
+ }
8
+ type MergedInitialContext<TInitial extends Context, TOutContext extends Context, TInContext extends Context> = Exclude<keyof TInContext, keyof TInitial | keyof TOutContext> extends never ? TInitial : TInitial & Omit<TInContext, keyof TInitial | keyof TOutContext>;
9
+ type MergedContext<TCurrent extends Context, TOutContext extends Context> = keyof TOutContext extends never ? TCurrent : Omit<TCurrent, keyof TOutContext> & TOutContext;
10
+
11
+ type ORPCErrorConstructorMapItemOptions<TData> = Omit<ORPCErrorOptions<TData>, 'status'>;
12
+ interface ORPCErrorConstructorMapItem<TCode extends ORPCErrorCode, TInData> {
13
+ (...rest: MaybeOptionalOptions<ORPCErrorConstructorMapItemOptions<TInData>>): ORPCError<TCode, TInData>;
14
+ }
15
+ type ORPCErrorConstructorMap<T extends ErrorMap> = {
16
+ [K in keyof T]: K extends ORPCErrorCode ? T[K] extends ErrorMapItem<infer UInputSchema> ? ORPCErrorConstructorMapItem<K, InferSchemaInput<UInputSchema>> : never : never;
17
+ };
18
+ /**
19
+ * Creates a map of ORPC error constructors.
20
+ *
21
+ * The returned object is a `Proxy` that allows access to arbitrary error codes:
22
+ * - If the code exists in the provided `errorMap`, the corresponding constructor
23
+ * will create a **defined** `ORPCError`.
24
+ * - If the code does not exist, a fallback `ORPCError` constructor is returned.
25
+ *
26
+ * The `in` operator can be used to check whether an error code is explicitly
27
+ * defined in the map.
28
+ *
29
+ * @example
30
+ * ```ts
31
+ * const errorMap = createORPCErrorConstructorMap({
32
+ * NOT_FOUND: {
33
+ * status: 404,
34
+ * message: 'Not Found',
35
+ * },
36
+ * })
37
+ *
38
+ * if ('NOT_FOUND' in errorMap) {
39
+ * const error = errorMap.NOT_FOUND()
40
+ * }
41
+ * ```
42
+ */
43
+ declare function createORPCErrorConstructorMap<T extends ErrorMap>(errorMap: T): ORPCErrorConstructorMap<T>;
44
+
45
+ type MiddlewareResult<TOutContext extends Context, TOutput> = Promisable<{
46
+ output: TOutput;
47
+ context: TOutContext;
48
+ }>;
49
+ type MiddlewareNextOptions<TOutContext extends Context> = object extends TOutContext ? {
50
+ context?: TOutContext;
51
+ } : {
52
+ context: TOutContext;
53
+ };
54
+ interface MiddlewareNext<TOutput> {
55
+ <T extends Context = object>(...rest: MaybeOptionalOptions<MiddlewareNextOptions<T>>): MiddlewareResult<T, TOutput>;
56
+ }
57
+ type MiddlewareDoneOptions<TOutContext, TOutput> = (object extends TOutContext ? {
58
+ context?: TOutContext;
59
+ } : {
60
+ context: TOutContext;
61
+ }) & {
62
+ output: TOutput;
63
+ };
64
+ interface MiddlewareDone<TOutput> {
65
+ /**
66
+ * Create a successful result and terminate the middleware chain early.
67
+ */
68
+ <TOutContext extends Context = object>(...rest: MaybeOptionalOptions<MiddlewareDoneOptions<TOutContext, TOutput>>): MiddlewareResult<TOutContext, TOutput>;
69
+ }
70
+ interface MiddlewareOptions<TInContext extends Context, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>> {
71
+ context: TInContext;
72
+ path: string[];
73
+ procedure: AnyProcedure;
74
+ signal?: AbortSignal | undefined;
75
+ lastEventId: string | undefined;
76
+ /**
77
+ * Invoke to continue the middleware chain.
78
+ */
79
+ next: MiddlewareNext<TOutput>;
80
+ errors: TErrorConstructorMap;
81
+ }
82
+ interface MiddlewareDefinition<TErrorMap extends ErrorMap> {
83
+ errorMap?: TErrorMap | undefined;
84
+ metaPlugins?: AnyMetaPlugin[] | undefined;
85
+ }
86
+ interface Middleware<TInContext extends Context, TOutContext extends Context, TInput, TOutput, TErrorMap extends ErrorMap> {
87
+ /** this property should be optional to support inline middleware */
88
+ '~orpc'?: MiddlewareDefinition<TErrorMap> | undefined;
89
+ (opts: MiddlewareOptions<TInContext, TOutput, ORPCErrorConstructorMap<TErrorMap>>, input: TInput, done: MiddlewareDone<TOutput>): Promisable<MiddlewareResult<TOutContext, TOutput>>;
90
+ }
91
+ type AnyMiddleware = Middleware<any, any, any, any, any>;
92
+
93
+ interface ProcedureHandlerOptions<TCurrentContext extends Context, TInput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>> {
94
+ context: TCurrentContext;
95
+ input: TInput;
96
+ path: string[];
97
+ procedure: Procedure<Context, Context, AnySchema, AnySchema, ErrorMap, any>;
98
+ signal?: AbortSignal | undefined;
99
+ lastEventId?: string | undefined;
100
+ errors: TErrorConstructorMap;
101
+ }
102
+ interface ProcedureHandler<TCurrentContext extends Context, TInput, THandlerOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>> {
103
+ (opts: ProcedureHandlerOptions<TCurrentContext, TInput, TErrorConstructorMap>, input: TInput): Promisable<THandlerOutput>;
104
+ }
105
+ interface OrderedMiddleware {
106
+ /**
107
+ * Snapshot of `inputSchemas.length`
108
+ * at the time this middleware was used.
109
+ *
110
+ * @default 0
111
+ */
112
+ inputSchemasLengthAtUse?: number | undefined;
113
+ /**
114
+ * Snapshot of `outputSchemas.length`
115
+ * at the time this middleware was used.
116
+ *
117
+ * @default 0
118
+ */
119
+ outputSchemasLengthAtUse?: number | undefined;
120
+ middleware: AnyMiddleware;
121
+ }
122
+ interface ProcedureDefinition<TInitialContext extends Context, TInjectedContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TReturnedError extends AnyORPCError> extends ProcedureContractDefinition<TInputSchema, TOutputSchema, TErrorMap> {
123
+ __TInitialContext?: (type: TInitialContext) => unknown;
124
+ __TInjectedContext?: (type: TInjectedContext) => unknown;
125
+ __TReturnedError?: () => TReturnedError;
126
+ /**
127
+ * When enabled, errors returned (not thrown) by the handler are passed through as-is,
128
+ * rather than being transformed into inferrable errors.
129
+ *
130
+ * This is intended for the contract-first approach, where the procedure adheres to an
131
+ * external contract and returned errors should not affect the inferred contract types.
132
+ *
133
+ * @default false
134
+ */
135
+ opaqueReturnedErrors?: boolean | undefined;
136
+ orderedMiddlewares: OrderedMiddleware[];
137
+ handler: ProcedureHandler<any, any, any, any>;
138
+ }
139
+ declare class Procedure<TInitialContext extends Context, TInjectedContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TReturnedError extends AnyORPCError> {
140
+ '~orpc': ProcedureDefinition<TInitialContext, TInjectedContext, TInputSchema, TOutputSchema, TErrorMap, TReturnedError>;
141
+ constructor(def: ProcedureDefinition<TInitialContext, TInjectedContext, TInputSchema, TOutputSchema, TErrorMap, TReturnedError>);
142
+ /**
143
+ * Checks if the given instance satisfies the {@see Procedure} class/interface.
144
+ */
145
+ static [Symbol.hasInstance](instance: unknown): boolean;
146
+ }
147
+ type AnyProcedure = Procedure<any, any, any, any, any, any>;
148
+
149
+ interface LazyDefinition<T> {
150
+ meta: Meta;
151
+ metaPlugins?: AnyMetaPlugin[] | undefined;
152
+ loader: () => Promise<{
153
+ default: T;
154
+ }>;
155
+ }
156
+ declare class Lazy<T> {
157
+ '~orpc': LazyDefinition<T>;
158
+ constructor(def: LazyDefinition<T>);
159
+ /**
160
+ * Checks if the given instance satisfies the {@see Lazy} class/interface.
161
+ */
162
+ static [Symbol.hasInstance](instance: unknown): boolean;
163
+ }
164
+ type Lazyable<T> = T | Lazy<T>;
165
+ declare function unlazy<T extends Lazyable<any>>(maybeLazy: T): Promise<{
166
+ default: T extends Lazy<infer U> ? U : T;
167
+ }>;
168
+
169
+ type ProcedureClient<TClientContext extends ClientContext, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TReturnedORPCError extends AnyORPCError> = Client<TClientContext, InferSchemaInput<TInputSchema>, InferSchemaOutput<TOutputSchema>, ORPCErrorFromErrorMap<TErrorMap> | TReturnedORPCError | ThrowableError>;
170
+ interface ProcedureClientInterceptorOptions<TInitialContext extends Context, TErrorMap extends ErrorMap> extends ProcedureHandlerOptions<TInitialContext, unknown, ORPCErrorConstructorMap<TErrorMap>> {
171
+ }
172
+ type ProcedureClientInterceptor<TInitialContext extends Context, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TReturnedError extends AnyORPCError> = Interceptor<ProcedureClientInterceptorOptions<TInitialContext, TErrorMap>, PromiseWithError<InferSchemaOutput<TOutputSchema>, ORPCErrorFromErrorMap<TErrorMap> | TReturnedError | ThrowableError>>;
173
+ type ProcedureClientOptions<TInitialContext extends Context, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TReturnedError extends AnyORPCError, TClientContext extends ClientContext> = {
174
+ path?: string[];
175
+ interceptors?: ProcedureClientInterceptor<TInitialContext, TOutputSchema, TErrorMap, TReturnedError>[];
176
+ } & (object extends TInitialContext ? {
177
+ context?: Value<Promisable<TInitialContext>, [clientContext: TClientContext]>;
178
+ } : {
179
+ context: Value<Promisable<TInitialContext>, [clientContext: TClientContext]>;
180
+ });
181
+ declare function createProcedureClient<TInitialContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TReturnedError extends AnyORPCError, TClientContext extends ClientContext = object>(lazyableProcedure: Lazyable<Procedure<TInitialContext, any, TInputSchema, TOutputSchema, TErrorMap, TReturnedError>>, ...rest: MaybeOptionalOptions<ProcedureClientOptions<TInitialContext, TOutputSchema, TErrorMap, TReturnedError, TClientContext>>): ProcedureClient<TClientContext, TInputSchema, TOutputSchema, TErrorMap, TReturnedError>;
182
+
183
+ export { Procedure as a, Lazy as g, createORPCErrorConstructorMap as w, createProcedureClient as x, unlazy as y };
184
+ export type { AnyProcedure as A, Context as C, Lazyable as L, MergedContext as M, ORPCErrorConstructorMap as O, ProcedureClientOptions as P, ProcedureClient as b, ProcedureClientInterceptor as c, Middleware as d, MergedInitialContext as e, AnyMiddleware as f, ProcedureHandler as h, OrderedMiddleware as i, LazyDefinition as j, MiddlewareDefinition as k, MiddlewareDone as l, MiddlewareDoneOptions as m, MiddlewareNext as n, MiddlewareNextOptions as o, MiddlewareOptions as p, MiddlewareResult as q, ORPCErrorConstructorMapItem as r, ORPCErrorConstructorMapItemOptions as s, ProcedureClientInterceptorOptions as t, ProcedureDefinition as u, ProcedureHandlerOptions as v };
@@ -0,0 +1,184 @@
1
+ import { ORPCErrorCode, ORPCErrorOptions, ORPCError, AnyORPCError, ClientContext, Client } from '@orpc/client';
2
+ import { ErrorMap, ErrorMapItem, InferSchemaInput, AnyMetaPlugin, AnySchema, ProcedureContractDefinition, Meta, InferSchemaOutput, ORPCErrorFromErrorMap } from '@orpc/contract';
3
+ import { MaybeOptionalOptions, Promisable, Interceptor, PromiseWithError, ThrowableError, Value } from '@orpc/shared';
4
+
5
+ interface Context {
6
+ [key: PropertyKey]: any;
7
+ }
8
+ type MergedInitialContext<TInitial extends Context, TOutContext extends Context, TInContext extends Context> = Exclude<keyof TInContext, keyof TInitial | keyof TOutContext> extends never ? TInitial : TInitial & Omit<TInContext, keyof TInitial | keyof TOutContext>;
9
+ type MergedContext<TCurrent extends Context, TOutContext extends Context> = keyof TOutContext extends never ? TCurrent : Omit<TCurrent, keyof TOutContext> & TOutContext;
10
+
11
+ type ORPCErrorConstructorMapItemOptions<TData> = Omit<ORPCErrorOptions<TData>, 'status'>;
12
+ interface ORPCErrorConstructorMapItem<TCode extends ORPCErrorCode, TInData> {
13
+ (...rest: MaybeOptionalOptions<ORPCErrorConstructorMapItemOptions<TInData>>): ORPCError<TCode, TInData>;
14
+ }
15
+ type ORPCErrorConstructorMap<T extends ErrorMap> = {
16
+ [K in keyof T]: K extends ORPCErrorCode ? T[K] extends ErrorMapItem<infer UInputSchema> ? ORPCErrorConstructorMapItem<K, InferSchemaInput<UInputSchema>> : never : never;
17
+ };
18
+ /**
19
+ * Creates a map of ORPC error constructors.
20
+ *
21
+ * The returned object is a `Proxy` that allows access to arbitrary error codes:
22
+ * - If the code exists in the provided `errorMap`, the corresponding constructor
23
+ * will create a **defined** `ORPCError`.
24
+ * - If the code does not exist, a fallback `ORPCError` constructor is returned.
25
+ *
26
+ * The `in` operator can be used to check whether an error code is explicitly
27
+ * defined in the map.
28
+ *
29
+ * @example
30
+ * ```ts
31
+ * const errorMap = createORPCErrorConstructorMap({
32
+ * NOT_FOUND: {
33
+ * status: 404,
34
+ * message: 'Not Found',
35
+ * },
36
+ * })
37
+ *
38
+ * if ('NOT_FOUND' in errorMap) {
39
+ * const error = errorMap.NOT_FOUND()
40
+ * }
41
+ * ```
42
+ */
43
+ declare function createORPCErrorConstructorMap<T extends ErrorMap>(errorMap: T): ORPCErrorConstructorMap<T>;
44
+
45
+ type MiddlewareResult<TOutContext extends Context, TOutput> = Promisable<{
46
+ output: TOutput;
47
+ context: TOutContext;
48
+ }>;
49
+ type MiddlewareNextOptions<TOutContext extends Context> = object extends TOutContext ? {
50
+ context?: TOutContext;
51
+ } : {
52
+ context: TOutContext;
53
+ };
54
+ interface MiddlewareNext<TOutput> {
55
+ <T extends Context = object>(...rest: MaybeOptionalOptions<MiddlewareNextOptions<T>>): MiddlewareResult<T, TOutput>;
56
+ }
57
+ type MiddlewareDoneOptions<TOutContext, TOutput> = (object extends TOutContext ? {
58
+ context?: TOutContext;
59
+ } : {
60
+ context: TOutContext;
61
+ }) & {
62
+ output: TOutput;
63
+ };
64
+ interface MiddlewareDone<TOutput> {
65
+ /**
66
+ * Create a successful result and terminate the middleware chain early.
67
+ */
68
+ <TOutContext extends Context = object>(...rest: MaybeOptionalOptions<MiddlewareDoneOptions<TOutContext, TOutput>>): MiddlewareResult<TOutContext, TOutput>;
69
+ }
70
+ interface MiddlewareOptions<TInContext extends Context, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>> {
71
+ context: TInContext;
72
+ path: string[];
73
+ procedure: AnyProcedure;
74
+ signal?: AbortSignal | undefined;
75
+ lastEventId: string | undefined;
76
+ /**
77
+ * Invoke to continue the middleware chain.
78
+ */
79
+ next: MiddlewareNext<TOutput>;
80
+ errors: TErrorConstructorMap;
81
+ }
82
+ interface MiddlewareDefinition<TErrorMap extends ErrorMap> {
83
+ errorMap?: TErrorMap | undefined;
84
+ metaPlugins?: AnyMetaPlugin[] | undefined;
85
+ }
86
+ interface Middleware<TInContext extends Context, TOutContext extends Context, TInput, TOutput, TErrorMap extends ErrorMap> {
87
+ /** this property should be optional to support inline middleware */
88
+ '~orpc'?: MiddlewareDefinition<TErrorMap> | undefined;
89
+ (opts: MiddlewareOptions<TInContext, TOutput, ORPCErrorConstructorMap<TErrorMap>>, input: TInput, done: MiddlewareDone<TOutput>): Promisable<MiddlewareResult<TOutContext, TOutput>>;
90
+ }
91
+ type AnyMiddleware = Middleware<any, any, any, any, any>;
92
+
93
+ interface ProcedureHandlerOptions<TCurrentContext extends Context, TInput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>> {
94
+ context: TCurrentContext;
95
+ input: TInput;
96
+ path: string[];
97
+ procedure: Procedure<Context, Context, AnySchema, AnySchema, ErrorMap, any>;
98
+ signal?: AbortSignal | undefined;
99
+ lastEventId?: string | undefined;
100
+ errors: TErrorConstructorMap;
101
+ }
102
+ interface ProcedureHandler<TCurrentContext extends Context, TInput, THandlerOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>> {
103
+ (opts: ProcedureHandlerOptions<TCurrentContext, TInput, TErrorConstructorMap>, input: TInput): Promisable<THandlerOutput>;
104
+ }
105
+ interface OrderedMiddleware {
106
+ /**
107
+ * Snapshot of `inputSchemas.length`
108
+ * at the time this middleware was used.
109
+ *
110
+ * @default 0
111
+ */
112
+ inputSchemasLengthAtUse?: number | undefined;
113
+ /**
114
+ * Snapshot of `outputSchemas.length`
115
+ * at the time this middleware was used.
116
+ *
117
+ * @default 0
118
+ */
119
+ outputSchemasLengthAtUse?: number | undefined;
120
+ middleware: AnyMiddleware;
121
+ }
122
+ interface ProcedureDefinition<TInitialContext extends Context, TInjectedContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TReturnedError extends AnyORPCError> extends ProcedureContractDefinition<TInputSchema, TOutputSchema, TErrorMap> {
123
+ __TInitialContext?: (type: TInitialContext) => unknown;
124
+ __TInjectedContext?: (type: TInjectedContext) => unknown;
125
+ __TReturnedError?: () => TReturnedError;
126
+ /**
127
+ * When enabled, errors returned (not thrown) by the handler are passed through as-is,
128
+ * rather than being transformed into inferrable errors.
129
+ *
130
+ * This is intended for the contract-first approach, where the procedure adheres to an
131
+ * external contract and returned errors should not affect the inferred contract types.
132
+ *
133
+ * @default false
134
+ */
135
+ opaqueReturnedErrors?: boolean | undefined;
136
+ orderedMiddlewares: OrderedMiddleware[];
137
+ handler: ProcedureHandler<any, any, any, any>;
138
+ }
139
+ declare class Procedure<TInitialContext extends Context, TInjectedContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TReturnedError extends AnyORPCError> {
140
+ '~orpc': ProcedureDefinition<TInitialContext, TInjectedContext, TInputSchema, TOutputSchema, TErrorMap, TReturnedError>;
141
+ constructor(def: ProcedureDefinition<TInitialContext, TInjectedContext, TInputSchema, TOutputSchema, TErrorMap, TReturnedError>);
142
+ /**
143
+ * Checks if the given instance satisfies the {@see Procedure} class/interface.
144
+ */
145
+ static [Symbol.hasInstance](instance: unknown): boolean;
146
+ }
147
+ type AnyProcedure = Procedure<any, any, any, any, any, any>;
148
+
149
+ interface LazyDefinition<T> {
150
+ meta: Meta;
151
+ metaPlugins?: AnyMetaPlugin[] | undefined;
152
+ loader: () => Promise<{
153
+ default: T;
154
+ }>;
155
+ }
156
+ declare class Lazy<T> {
157
+ '~orpc': LazyDefinition<T>;
158
+ constructor(def: LazyDefinition<T>);
159
+ /**
160
+ * Checks if the given instance satisfies the {@see Lazy} class/interface.
161
+ */
162
+ static [Symbol.hasInstance](instance: unknown): boolean;
163
+ }
164
+ type Lazyable<T> = T | Lazy<T>;
165
+ declare function unlazy<T extends Lazyable<any>>(maybeLazy: T): Promise<{
166
+ default: T extends Lazy<infer U> ? U : T;
167
+ }>;
168
+
169
+ type ProcedureClient<TClientContext extends ClientContext, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TReturnedORPCError extends AnyORPCError> = Client<TClientContext, InferSchemaInput<TInputSchema>, InferSchemaOutput<TOutputSchema>, ORPCErrorFromErrorMap<TErrorMap> | TReturnedORPCError | ThrowableError>;
170
+ interface ProcedureClientInterceptorOptions<TInitialContext extends Context, TErrorMap extends ErrorMap> extends ProcedureHandlerOptions<TInitialContext, unknown, ORPCErrorConstructorMap<TErrorMap>> {
171
+ }
172
+ type ProcedureClientInterceptor<TInitialContext extends Context, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TReturnedError extends AnyORPCError> = Interceptor<ProcedureClientInterceptorOptions<TInitialContext, TErrorMap>, PromiseWithError<InferSchemaOutput<TOutputSchema>, ORPCErrorFromErrorMap<TErrorMap> | TReturnedError | ThrowableError>>;
173
+ type ProcedureClientOptions<TInitialContext extends Context, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TReturnedError extends AnyORPCError, TClientContext extends ClientContext> = {
174
+ path?: string[];
175
+ interceptors?: ProcedureClientInterceptor<TInitialContext, TOutputSchema, TErrorMap, TReturnedError>[];
176
+ } & (object extends TInitialContext ? {
177
+ context?: Value<Promisable<TInitialContext>, [clientContext: TClientContext]>;
178
+ } : {
179
+ context: Value<Promisable<TInitialContext>, [clientContext: TClientContext]>;
180
+ });
181
+ declare function createProcedureClient<TInitialContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TReturnedError extends AnyORPCError, TClientContext extends ClientContext = object>(lazyableProcedure: Lazyable<Procedure<TInitialContext, any, TInputSchema, TOutputSchema, TErrorMap, TReturnedError>>, ...rest: MaybeOptionalOptions<ProcedureClientOptions<TInitialContext, TOutputSchema, TErrorMap, TReturnedError, TClientContext>>): ProcedureClient<TClientContext, TInputSchema, TOutputSchema, TErrorMap, TReturnedError>;
182
+
183
+ export { Procedure as a, Lazy as g, createORPCErrorConstructorMap as w, createProcedureClient as x, unlazy as y };
184
+ export type { AnyProcedure as A, Context as C, Lazyable as L, MergedContext as M, ORPCErrorConstructorMap as O, ProcedureClientOptions as P, ProcedureClient as b, ProcedureClientInterceptor as c, Middleware as d, MergedInitialContext as e, AnyMiddleware as f, ProcedureHandler as h, OrderedMiddleware as i, LazyDefinition as j, MiddlewareDefinition as k, MiddlewareDone as l, MiddlewareDoneOptions as m, MiddlewareNext as n, MiddlewareNextOptions as o, MiddlewareOptions as p, MiddlewareResult as q, ORPCErrorConstructorMapItem as r, ORPCErrorConstructorMapItemOptions as s, ProcedureClientInterceptorOptions as t, ProcedureDefinition as u, ProcedureHandlerOptions as v };
@@ -0,0 +1,61 @@
1
+ import { RouterContract, ProcedureContract, ORPCErrorFromErrorMap, ThrowableError, InferSchemaInput, InferSchemaOutput } from '@orpc/contract';
2
+ import { C as Context, a as Procedure, L as Lazyable, M as MergedContext } from './server.BL22TloH.mjs';
3
+
4
+ type Router<TInitialContext extends Context> = Procedure<TInitialContext, any, any, any, any, any> | {
5
+ [k: string]: Lazyable<Router<TInitialContext>>;
6
+ };
7
+ type ContractedRouter<T extends RouterContract, TInitialContext extends Context> = T extends ProcedureContract<infer $InputSchema, infer $OutputSchema, infer $ErrorMap> ? Procedure<TInitialContext, any, $InputSchema, $OutputSchema, $ErrorMap, never> : {
8
+ [K in keyof T]: T[K] extends RouterContract ? Lazyable<ContractedRouter<T[K], TInitialContext>> : never;
9
+ };
10
+ type AnyRouter = Router<any>;
11
+ type InferRouterInitialContext<T extends AnyRouter> = T extends Router<infer $> ? $ : never;
12
+ /**
13
+ * Infer all initial context of the router.
14
+ *
15
+ * @info A procedure is a router too.
16
+ * @see {@link https://orpc.dev/docs/router#utilities Router Utilities Docs}
17
+ */
18
+ type InferRouterInitialContexts<T extends AnyRouter> = T extends Procedure<infer UInitialContext, any, any, any, any, any> ? UInitialContext : {
19
+ [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInitialContexts<U> : never;
20
+ };
21
+ /**
22
+ * Infer all current context of the router.
23
+ *
24
+ * @info A procedure is a router too.
25
+ * @see {@link https://orpc.dev/docs/router#utilities Router Utilities Docs}
26
+ */
27
+ type InferRouterFinalContexts<T extends AnyRouter> = T extends Procedure<infer UInitialContext, infer UInjectedContext, any, any, any, any> ? MergedContext<UInitialContext, UInjectedContext> : {
28
+ [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterFinalContexts<U> : never;
29
+ };
30
+ /**
31
+ * Infer all router inputs
32
+ *
33
+ * @info A procedure is a router too.
34
+ * @see {@link https://orpc.dev/docs/router#utilities Router Utilities Docs}
35
+ */
36
+ type InferRouterInputs<T extends AnyRouter> = T extends Procedure<any, any, infer UInputSchema, any, any, any> ? InferSchemaInput<UInputSchema> : {
37
+ [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInputs<U> : never;
38
+ };
39
+ /**
40
+ * Infer all router outputs
41
+ *
42
+ * @info A procedure is a router too.
43
+ * @see {@link https://orpc.dev/docs/router#utilities Router Utilities Docs}
44
+ */
45
+ type InferRouterOutputs<T extends AnyRouter> = T extends Procedure<any, any, any, infer UOutputSchema, any, any> ? InferSchemaOutput<UOutputSchema> : {
46
+ [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterOutputs<U> : never;
47
+ };
48
+ /**
49
+ * Infer the union of throwable errors for entire router.
50
+ */
51
+ type InferRouterError<T extends AnyRouter> = T extends Procedure<any, any, any, any, infer UErrorMap, infer UReturnedError> ? ORPCErrorFromErrorMap<UErrorMap> | UReturnedError | ThrowableError : {
52
+ [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterError<U> : never;
53
+ }[keyof T];
54
+ /**
55
+ * Infer throwable errors for each procedure, preserving the router shape.
56
+ */
57
+ type InferRouterErrors<T extends AnyRouter> = T extends Procedure<any, any, any, any, infer UErrorMap, infer UReturnedError> ? ORPCErrorFromErrorMap<UErrorMap> | UReturnedError | ThrowableError : {
58
+ [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterErrors<U> : never;
59
+ };
60
+
61
+ export type { AnyRouter as A, ContractedRouter as C, InferRouterInitialContext as I, Router as R, InferRouterError as a, InferRouterErrors as b, InferRouterFinalContexts as c, InferRouterInitialContexts as d, InferRouterInputs as e, InferRouterOutputs as f };
@@ -0,0 +1,222 @@
1
+ import { c as createProcedureClient, P as Procedure, L as Lazy, u as unlazy } from './server.CrlKQucM.mjs';
2
+ import { resolveMetaPlugins, mergeErrorMap, ProcedureContract } from '@orpc/contract';
3
+ import { getOrBind, isTypescriptObject } from '@orpc/shared';
4
+
5
+ const DEFAULT_SUCCESS_STATUS = 200;
6
+ const DEFAULT_ERROR_STATUS = 500;
7
+
8
+ function createGuardedProcedureLazy(lazy) {
9
+ const guarded = new Lazy({
10
+ ...lazy["~orpc"],
11
+ async loader() {
12
+ const { default: maybeProcedure } = await unlazy(lazy);
13
+ if (!(maybeProcedure instanceof Procedure)) {
14
+ throw new TypeError(`
15
+ Expected a lazy<procedure> but got lazy<unknown>.
16
+ This should be caught by TypeScript compilation.
17
+ Please report this issue if you think this is a bug.
18
+ `);
19
+ }
20
+ return { default: maybeProcedure };
21
+ }
22
+ });
23
+ return guarded;
24
+ }
25
+ function createContractProcedure(procedure, contract) {
26
+ return new Procedure({
27
+ ...procedure["~orpc"],
28
+ errorMap: contract["~orpc"].errorMap,
29
+ meta: contract["~orpc"].meta,
30
+ metaPlugins: contract["~orpc"].metaPlugins
31
+ });
32
+ }
33
+ function call(lazyableProcedure, ...[
34
+ input = void 0,
35
+ options = {}
36
+ ]) {
37
+ return createProcedureClient(lazyableProcedure, options)(input, options);
38
+ }
39
+
40
+ const HIDDEN_ROUTER_CONTRACT_SYMBOL = Symbol.for("ORPC_HIDDEN_ROUTER_CONTRACT");
41
+ function withHiddenRouterContract(router, contract) {
42
+ return new Proxy(router, {
43
+ get(target, key) {
44
+ if (key === HIDDEN_ROUTER_CONTRACT_SYMBOL) {
45
+ return contract;
46
+ }
47
+ return getOrBind(target, key);
48
+ }
49
+ });
50
+ }
51
+ function getHiddenRouterContract(router) {
52
+ return router[HIDDEN_ROUTER_CONTRACT_SYMBOL];
53
+ }
54
+
55
+ function augmentRouter(router, options) {
56
+ if (router instanceof Lazy) {
57
+ const [meta, metaPlugins] = resolveMetaPlugins(
58
+ options.meta,
59
+ options.metaPlugins,
60
+ router["~orpc"].metaPlugins
61
+ );
62
+ const enhanced2 = new Lazy({
63
+ meta,
64
+ metaPlugins,
65
+ async loader() {
66
+ const { default: unlaziedRouter } = await unlazy(router);
67
+ const enhanced3 = augmentRouter(unlaziedRouter, options);
68
+ return unlazy(enhanced3);
69
+ }
70
+ });
71
+ return enhanced2;
72
+ }
73
+ if (router instanceof Procedure) {
74
+ const [meta, metaPlugins] = resolveMetaPlugins(
75
+ options.meta,
76
+ options.metaPlugins,
77
+ router["~orpc"].metaPlugins
78
+ );
79
+ const enhanced2 = new Procedure({
80
+ ...router["~orpc"],
81
+ meta,
82
+ metaPlugins,
83
+ errorMap: mergeErrorMap(options.errorMap, router["~orpc"].errorMap),
84
+ orderedMiddlewares: [
85
+ ...options.middlewares.map((middleware) => ({ middleware, inputSchemasLengthAtUse: 0, outputSchemasLengthAtUse: 0 })),
86
+ ...router["~orpc"].orderedMiddlewares
87
+ ]
88
+ });
89
+ return enhanced2;
90
+ }
91
+ if (!isTypescriptObject(router)) {
92
+ return router;
93
+ }
94
+ const enhanced = {};
95
+ for (const key in router) {
96
+ enhanced[key] = augmentRouter(router[key], options);
97
+ }
98
+ return enhanced;
99
+ }
100
+ function augmentImplementedRouter(router, options) {
101
+ if (router instanceof Lazy) {
102
+ const enhanced2 = new Lazy({
103
+ ...router["~orpc"],
104
+ async loader() {
105
+ const { default: unlaziedRouter } = await unlazy(router);
106
+ const enhanced3 = augmentImplementedRouter(unlaziedRouter, options);
107
+ return unlazy(enhanced3);
108
+ }
109
+ });
110
+ return enhanced2;
111
+ }
112
+ if (router instanceof Procedure) {
113
+ const enhanced2 = new Procedure({
114
+ ...router["~orpc"],
115
+ orderedMiddlewares: [
116
+ ...options.middlewares.map((middleware) => ({ middleware, inputSchemasLengthAtUse: 0, outputSchemasLengthAtUse: 0 })),
117
+ ...router["~orpc"].orderedMiddlewares
118
+ ]
119
+ });
120
+ return enhanced2;
121
+ }
122
+ if (!isTypescriptObject(router)) {
123
+ return router;
124
+ }
125
+ const enhanced = {};
126
+ for (const key in router) {
127
+ enhanced[key] = augmentImplementedRouter(router[key], options);
128
+ }
129
+ return enhanced;
130
+ }
131
+ function getRouter(router, path) {
132
+ let current = router;
133
+ for (let i = 0; i < path.length; i++) {
134
+ const segment = path[i];
135
+ if (!isTypescriptObject(current)) {
136
+ return void 0;
137
+ }
138
+ if (current instanceof Procedure) {
139
+ return void 0;
140
+ }
141
+ if (!(current instanceof Lazy)) {
142
+ current = current[segment];
143
+ continue;
144
+ }
145
+ const lazied = current;
146
+ const rest = path.slice(i);
147
+ return new Lazy({
148
+ ...lazied["~orpc"],
149
+ async loader() {
150
+ const unwrapped = await unlazy(lazied);
151
+ const next = getRouter(unwrapped.default, rest);
152
+ return await unlazy(next);
153
+ }
154
+ });
155
+ }
156
+ if (!isTypescriptObject(current)) {
157
+ return void 0;
158
+ }
159
+ return current;
160
+ }
161
+ function walkProcedureContractsSync(router, callback, path = []) {
162
+ const hiddenContract = getHiddenRouterContract(router);
163
+ if (hiddenContract !== void 0) {
164
+ router = hiddenContract;
165
+ }
166
+ if (router instanceof ProcedureContract) {
167
+ callback(router, path);
168
+ return [];
169
+ }
170
+ if (!isTypescriptObject(router)) {
171
+ return [];
172
+ }
173
+ const lazyResults = [];
174
+ for (const key in router) {
175
+ const value = router[key];
176
+ if (value instanceof Lazy) {
177
+ lazyResults.push({ router: value, path: [...path, key] });
178
+ } else {
179
+ lazyResults.push(...walkProcedureContractsSync(value, callback, [...path, key]));
180
+ }
181
+ }
182
+ return lazyResults;
183
+ }
184
+ async function walkProcedureContractsAsync(router, callback, path = []) {
185
+ const hiddenContract = getHiddenRouterContract(router);
186
+ if (hiddenContract !== void 0) {
187
+ router = hiddenContract;
188
+ }
189
+ if (router instanceof ProcedureContract) {
190
+ await callback(router, path);
191
+ return;
192
+ }
193
+ if (!isTypescriptObject(router)) {
194
+ return;
195
+ }
196
+ for (const key in router) {
197
+ const value = router[key];
198
+ if (value instanceof Lazy) {
199
+ const { default: router2 } = await unlazy(value);
200
+ await walkProcedureContractsAsync(router2, callback, [...path, key]);
201
+ } else {
202
+ await walkProcedureContractsAsync(value, callback, [...path, key]);
203
+ }
204
+ }
205
+ }
206
+ async function unlazyRouter(router) {
207
+ if (router instanceof Procedure) {
208
+ return router;
209
+ }
210
+ if (!isTypescriptObject(router)) {
211
+ return router;
212
+ }
213
+ const unlazied = {};
214
+ for (const key in router) {
215
+ const item = router[key];
216
+ const { default: unlaziedRouter } = await unlazy(item);
217
+ unlazied[key] = await unlazyRouter(unlaziedRouter);
218
+ }
219
+ return unlazied;
220
+ }
221
+
222
+ export { DEFAULT_SUCCESS_STATUS as D, DEFAULT_ERROR_STATUS as a, augmentRouter as b, createContractProcedure as c, augmentImplementedRouter as d, withHiddenRouterContract as e, createGuardedProcedureLazy as f, getRouter as g, call as h, getHiddenRouterContract as i, walkProcedureContractsAsync as j, unlazyRouter as u, walkProcedureContractsSync as w };
@@ -0,0 +1,11 @@
1
+ import { value } from '@orpc/shared';
2
+
3
+ function createStandardPeerRequestHandler(handler, options) {
4
+ return async (request) => {
5
+ const context = await value(options.context ?? {}, request);
6
+ const { response } = await handler.handle(request, { ...options, context });
7
+ return response ?? { status: 404, headers: {}, body: "No procedure matched" };
8
+ };
9
+ }
10
+
11
+ export { createStandardPeerRequestHandler as c };