@orpc/server 0.0.0-next.c6c659d → 0.0.0-next.c7f8b1d

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 (73) hide show
  1. package/README.md +131 -0
  2. package/dist/adapters/fetch/index.d.mts +59 -0
  3. package/dist/adapters/fetch/index.d.ts +59 -0
  4. package/dist/adapters/fetch/index.mjs +105 -0
  5. package/dist/adapters/node/index.d.mts +58 -0
  6. package/dist/adapters/node/index.d.ts +58 -0
  7. package/dist/adapters/node/index.mjs +90 -0
  8. package/dist/adapters/standard/index.d.mts +26 -0
  9. package/dist/adapters/standard/index.d.ts +26 -0
  10. package/dist/adapters/standard/index.mjs +8 -0
  11. package/dist/index.d.mts +292 -0
  12. package/dist/index.d.ts +292 -0
  13. package/dist/index.mjs +363 -0
  14. package/dist/plugins/index.d.mts +125 -0
  15. package/dist/plugins/index.d.ts +125 -0
  16. package/dist/plugins/index.mjs +252 -0
  17. package/dist/shared/server.B1oIHH_j.d.mts +74 -0
  18. package/dist/shared/server.BVHsfJ99.d.mts +144 -0
  19. package/dist/shared/server.BVHsfJ99.d.ts +144 -0
  20. package/dist/shared/server.BVwwTHyO.mjs +9 -0
  21. package/dist/shared/server.BW-nUGgA.mjs +36 -0
  22. package/dist/shared/server.BuLPHTX1.d.mts +18 -0
  23. package/dist/shared/server.C37gDhSZ.mjs +364 -0
  24. package/dist/shared/server.CaWivVk3.d.ts +74 -0
  25. package/dist/shared/server.DFuJLDuo.mjs +190 -0
  26. package/dist/shared/server.DMhSfHk1.d.ts +10 -0
  27. package/dist/shared/server.D_vpYits.d.ts +18 -0
  28. package/dist/shared/server.Dwnm6cSk.d.mts +10 -0
  29. package/package.json +29 -31
  30. package/dist/chunk-MB74GD7C.js +0 -301
  31. package/dist/chunk-WUOGVGWG.js +0 -1
  32. package/dist/chunk-ZBJYYEII.js +0 -227
  33. package/dist/fetch.js +0 -15
  34. package/dist/hono.js +0 -30
  35. package/dist/index.js +0 -550
  36. package/dist/next.js +0 -36
  37. package/dist/node.js +0 -87
  38. package/dist/src/adapters/fetch/index.d.ts +0 -6
  39. package/dist/src/adapters/fetch/orpc-handler.d.ts +0 -20
  40. package/dist/src/adapters/fetch/orpc-payload-codec.d.ts +0 -16
  41. package/dist/src/adapters/fetch/orpc-procedure-matcher.d.ts +0 -12
  42. package/dist/src/adapters/fetch/super-json.d.ts +0 -12
  43. package/dist/src/adapters/fetch/types.d.ts +0 -21
  44. package/dist/src/adapters/hono/index.d.ts +0 -3
  45. package/dist/src/adapters/hono/middleware.d.ts +0 -12
  46. package/dist/src/adapters/next/index.d.ts +0 -3
  47. package/dist/src/adapters/next/serve.d.ts +0 -19
  48. package/dist/src/adapters/node/index.d.ts +0 -5
  49. package/dist/src/adapters/node/orpc-handler.d.ts +0 -12
  50. package/dist/src/adapters/node/request-listener.d.ts +0 -28
  51. package/dist/src/adapters/node/types.d.ts +0 -22
  52. package/dist/src/builder.d.ts +0 -36
  53. package/dist/src/error.d.ts +0 -10
  54. package/dist/src/hidden.d.ts +0 -6
  55. package/dist/src/implementer-chainable.d.ts +0 -10
  56. package/dist/src/index.d.ts +0 -25
  57. package/dist/src/lazy-decorated.d.ts +0 -7
  58. package/dist/src/lazy-utils.d.ts +0 -4
  59. package/dist/src/lazy.d.ts +0 -18
  60. package/dist/src/middleware-decorated.d.ts +0 -9
  61. package/dist/src/middleware.d.ts +0 -35
  62. package/dist/src/procedure-builder.d.ts +0 -24
  63. package/dist/src/procedure-client.d.ts +0 -22
  64. package/dist/src/procedure-decorated.d.ts +0 -26
  65. package/dist/src/procedure-implementer.d.ts +0 -19
  66. package/dist/src/procedure-utils.d.ts +0 -17
  67. package/dist/src/procedure.d.ts +0 -43
  68. package/dist/src/router-builder.d.ts +0 -29
  69. package/dist/src/router-client.d.ts +0 -27
  70. package/dist/src/router-implementer.d.ts +0 -21
  71. package/dist/src/router.d.ts +0 -16
  72. package/dist/src/types.d.ts +0 -14
  73. package/dist/src/utils.d.ts +0 -3
@@ -0,0 +1,252 @@
1
+ import { value, isAsyncIteratorObject } from '@orpc/shared';
2
+ import { parseBatchRequest, toBatchResponse } from '@orpc/standard-server/batch';
3
+ import { ORPCError } from '@orpc/client';
4
+ export { S as StrictGetMethodPlugin } from '../shared/server.BW-nUGgA.mjs';
5
+ import '@orpc/contract';
6
+
7
+ class BatchHandlerPlugin {
8
+ maxSize;
9
+ mapRequestItem;
10
+ successStatus;
11
+ headers;
12
+ order = 5e6;
13
+ constructor(options = {}) {
14
+ this.maxSize = options.maxSize ?? 10;
15
+ this.mapRequestItem = options.mapRequestItem ?? ((request, { request: batchRequest }) => ({
16
+ ...request,
17
+ headers: {
18
+ ...batchRequest.headers,
19
+ ...request.headers
20
+ }
21
+ }));
22
+ this.successStatus = options.successStatus ?? 207;
23
+ this.headers = options.headers ?? {};
24
+ }
25
+ init(options) {
26
+ options.rootInterceptors ??= [];
27
+ options.rootInterceptors.unshift(async (options2) => {
28
+ if (options2.request.headers["x-orpc-batch"] !== "1") {
29
+ return options2.next();
30
+ }
31
+ let isParsing = false;
32
+ try {
33
+ isParsing = true;
34
+ const parsed = parseBatchRequest({ ...options2.request, body: await options2.request.body() });
35
+ isParsing = false;
36
+ const maxSize = await value(this.maxSize, options2);
37
+ if (parsed.length > maxSize) {
38
+ return {
39
+ matched: true,
40
+ response: {
41
+ status: 413,
42
+ headers: {},
43
+ body: "Batch request size exceeds the maximum allowed size"
44
+ }
45
+ };
46
+ }
47
+ const responses = parsed.map(
48
+ (request, index) => {
49
+ const mapped = this.mapRequestItem(request, options2);
50
+ return options2.next({ ...options2, request: { ...mapped, body: () => Promise.resolve(mapped.body) } }).then(({ response: response2, matched }) => {
51
+ if (matched) {
52
+ if (response2.body instanceof Blob || response2.body instanceof FormData || isAsyncIteratorObject(response2.body)) {
53
+ return {
54
+ index,
55
+ status: 500,
56
+ headers: {},
57
+ body: "Batch responses do not support file/blob, or event-iterator. Please call this procedure separately outside of the batch request."
58
+ };
59
+ }
60
+ return { ...response2, index };
61
+ }
62
+ return { index, status: 404, headers: {}, body: "No procedure matched" };
63
+ }).catch(() => {
64
+ return { index, status: 500, headers: {}, body: "Internal server error" };
65
+ });
66
+ }
67
+ );
68
+ await Promise.race(responses);
69
+ const status = await value(this.successStatus, responses, options2);
70
+ const headers = await value(this.headers, responses, options2);
71
+ const response = toBatchResponse({
72
+ status,
73
+ headers,
74
+ body: async function* () {
75
+ const promises = [...responses];
76
+ while (true) {
77
+ const handling = promises.filter((p) => p !== void 0);
78
+ if (handling.length === 0) {
79
+ return;
80
+ }
81
+ const result = await Promise.race(handling);
82
+ promises[result.index] = void 0;
83
+ yield result;
84
+ }
85
+ }()
86
+ });
87
+ return {
88
+ matched: true,
89
+ response
90
+ };
91
+ } catch (cause) {
92
+ if (isParsing) {
93
+ return {
94
+ matched: true,
95
+ response: { status: 400, headers: {}, body: "Invalid batch request, this could be caused by a malformed request body or a missing header" }
96
+ };
97
+ }
98
+ throw cause;
99
+ }
100
+ });
101
+ }
102
+ }
103
+
104
+ class CORSPlugin {
105
+ options;
106
+ order = 9e6;
107
+ constructor(options = {}) {
108
+ const defaults = {
109
+ origin: (origin) => origin,
110
+ allowMethods: ["GET", "HEAD", "PUT", "POST", "DELETE", "PATCH"]
111
+ };
112
+ this.options = {
113
+ ...defaults,
114
+ ...options
115
+ };
116
+ }
117
+ init(options) {
118
+ options.rootInterceptors ??= [];
119
+ options.rootInterceptors.unshift(async (interceptorOptions) => {
120
+ if (interceptorOptions.request.method === "OPTIONS") {
121
+ const resHeaders = {};
122
+ if (this.options.maxAge !== void 0) {
123
+ resHeaders["access-control-max-age"] = this.options.maxAge.toString();
124
+ }
125
+ if (this.options.allowMethods?.length) {
126
+ resHeaders["access-control-allow-methods"] = this.options.allowMethods.join(",");
127
+ }
128
+ const allowHeaders = this.options.allowHeaders ?? interceptorOptions.request.headers["access-control-request-headers"];
129
+ if (Array.isArray(allowHeaders) && allowHeaders.length) {
130
+ resHeaders["access-control-allow-headers"] = allowHeaders.join(",");
131
+ } else if (typeof allowHeaders === "string") {
132
+ resHeaders["access-control-allow-headers"] = allowHeaders;
133
+ }
134
+ return {
135
+ matched: true,
136
+ response: {
137
+ status: 204,
138
+ headers: resHeaders,
139
+ body: void 0
140
+ }
141
+ };
142
+ }
143
+ return interceptorOptions.next();
144
+ });
145
+ options.rootInterceptors.unshift(async (interceptorOptions) => {
146
+ const result = await interceptorOptions.next();
147
+ if (!result.matched) {
148
+ return result;
149
+ }
150
+ const origin = Array.isArray(interceptorOptions.request.headers.origin) ? interceptorOptions.request.headers.origin.join(",") : interceptorOptions.request.headers.origin || "";
151
+ const allowedOrigin = await value(this.options.origin, origin, interceptorOptions);
152
+ const allowedOriginArr = Array.isArray(allowedOrigin) ? allowedOrigin : [allowedOrigin];
153
+ if (allowedOriginArr.includes("*")) {
154
+ result.response.headers["access-control-allow-origin"] = "*";
155
+ } else {
156
+ if (allowedOriginArr.includes(origin)) {
157
+ result.response.headers["access-control-allow-origin"] = origin;
158
+ }
159
+ result.response.headers.vary = interceptorOptions.request.headers.vary ?? "origin";
160
+ }
161
+ const allowedTimingOrigin = await value(this.options.timingOrigin, origin, interceptorOptions);
162
+ const allowedTimingOriginArr = Array.isArray(allowedTimingOrigin) ? allowedTimingOrigin : [allowedTimingOrigin];
163
+ if (allowedTimingOriginArr.includes("*")) {
164
+ result.response.headers["timing-allow-origin"] = "*";
165
+ } else if (allowedTimingOriginArr.includes(origin)) {
166
+ result.response.headers["timing-allow-origin"] = origin;
167
+ }
168
+ if (this.options.credentials) {
169
+ result.response.headers["access-control-allow-credentials"] = "true";
170
+ }
171
+ if (this.options.exposeHeaders?.length) {
172
+ result.response.headers["access-control-expose-headers"] = this.options.exposeHeaders.join(",");
173
+ }
174
+ return result;
175
+ });
176
+ }
177
+ }
178
+
179
+ class ResponseHeadersPlugin {
180
+ init(options) {
181
+ options.rootInterceptors ??= [];
182
+ options.rootInterceptors.push(async (interceptorOptions) => {
183
+ const resHeaders = interceptorOptions.context.resHeaders ?? new Headers();
184
+ const result = await interceptorOptions.next({
185
+ ...interceptorOptions,
186
+ context: {
187
+ ...interceptorOptions.context,
188
+ resHeaders
189
+ }
190
+ });
191
+ if (!result.matched) {
192
+ return result;
193
+ }
194
+ const responseHeaders = result.response.headers;
195
+ for (const [key, value] of resHeaders) {
196
+ if (Array.isArray(responseHeaders[key])) {
197
+ responseHeaders[key].push(value);
198
+ } else if (responseHeaders[key] !== void 0) {
199
+ responseHeaders[key] = [responseHeaders[key], value];
200
+ } else {
201
+ responseHeaders[key] = value;
202
+ }
203
+ }
204
+ return result;
205
+ });
206
+ }
207
+ }
208
+
209
+ const SIMPLE_CSRF_PROTECTION_CONTEXT_SYMBOL = Symbol("SIMPLE_CSRF_PROTECTION_CONTEXT");
210
+ class SimpleCsrfProtectionHandlerPlugin {
211
+ headerName;
212
+ headerValue;
213
+ exclude;
214
+ error;
215
+ constructor(options = {}) {
216
+ this.headerName = options.headerName ?? "x-csrf-token";
217
+ this.headerValue = options.headerValue ?? "orpc";
218
+ this.exclude = options.exclude ?? false;
219
+ this.error = options.error ?? new ORPCError("CSRF_TOKEN_MISMATCH", {
220
+ status: 403,
221
+ message: "Invalid CSRF token"
222
+ });
223
+ }
224
+ order = 8e6;
225
+ init(options) {
226
+ options.rootInterceptors ??= [];
227
+ options.clientInterceptors ??= [];
228
+ options.rootInterceptors.unshift(async (options2) => {
229
+ const headerName = await value(this.headerName, options2);
230
+ const headerValue = await value(this.headerValue, options2);
231
+ return options2.next({
232
+ ...options2,
233
+ context: {
234
+ ...options2.context,
235
+ [SIMPLE_CSRF_PROTECTION_CONTEXT_SYMBOL]: options2.request.headers[headerName] === headerValue
236
+ }
237
+ });
238
+ });
239
+ options.clientInterceptors.unshift(async (options2) => {
240
+ if (typeof options2.context[SIMPLE_CSRF_PROTECTION_CONTEXT_SYMBOL] !== "boolean") {
241
+ throw new TypeError("[SimpleCsrfProtectionHandlerPlugin] CSRF protection context has been corrupted or modified by another plugin or interceptor");
242
+ }
243
+ const excluded = await value(this.exclude, options2);
244
+ if (!excluded && !options2.context[SIMPLE_CSRF_PROTECTION_CONTEXT_SYMBOL]) {
245
+ throw this.error;
246
+ }
247
+ return options2.next();
248
+ });
249
+ }
250
+ }
251
+
252
+ export { BatchHandlerPlugin, CORSPlugin, ResponseHeadersPlugin, SimpleCsrfProtectionHandlerPlugin };
@@ -0,0 +1,74 @@
1
+ import { HTTPPath, ORPCError } from '@orpc/client';
2
+ import { Meta, InferSchemaOutput, AnySchema, ErrorFromErrorMap } from '@orpc/contract';
3
+ import { Interceptor, ThrowableError } from '@orpc/shared';
4
+ import { StandardResponse, StandardLazyRequest } from '@orpc/standard-server';
5
+ import { C as Context, f as AnyRouter, h as AnyProcedure, F as ProcedureClientInterceptorOptions, R as Router } from './server.BVHsfJ99.mjs';
6
+
7
+ interface StandardHandlerPlugin<TContext extends Context> {
8
+ order?: number;
9
+ init?(options: StandardHandlerOptions<TContext>): void;
10
+ }
11
+ declare class CompositeStandardHandlerPlugin<T extends Context, TPlugin extends StandardHandlerPlugin<T>> implements StandardHandlerPlugin<T> {
12
+ protected readonly plugins: TPlugin[];
13
+ constructor(plugins?: readonly TPlugin[]);
14
+ init(options: StandardHandlerOptions<T>): void;
15
+ }
16
+
17
+ type StandardParams = Record<string, string>;
18
+ type StandardMatchResult = {
19
+ path: readonly string[];
20
+ procedure: AnyProcedure;
21
+ params?: StandardParams;
22
+ } | undefined;
23
+ interface StandardMatcher {
24
+ init(router: AnyRouter): void;
25
+ match(method: string, pathname: HTTPPath): Promise<StandardMatchResult>;
26
+ }
27
+ interface StandardCodec {
28
+ encode(output: unknown, procedure: AnyProcedure): StandardResponse;
29
+ encodeError(error: ORPCError<any, any>): StandardResponse;
30
+ decode(request: StandardLazyRequest, params: StandardParams | undefined, procedure: AnyProcedure): Promise<unknown>;
31
+ }
32
+
33
+ interface StandardHandleOptions<T extends Context> {
34
+ prefix?: HTTPPath;
35
+ context: T;
36
+ }
37
+ type StandardHandleResult = {
38
+ matched: true;
39
+ response: StandardResponse;
40
+ } | {
41
+ matched: false;
42
+ response: undefined;
43
+ };
44
+ interface StandardHandlerInterceptorOptions<T extends Context> extends StandardHandleOptions<T> {
45
+ request: StandardLazyRequest;
46
+ }
47
+ interface StandardHandlerOptions<TContext extends Context> {
48
+ plugins?: StandardHandlerPlugin<TContext>[];
49
+ /**
50
+ * Interceptors at the request level, helpful when you want catch errors
51
+ */
52
+ interceptors?: Interceptor<StandardHandlerInterceptorOptions<TContext>, StandardHandleResult, ThrowableError>[];
53
+ /**
54
+ * Interceptors at the root level, helpful when you want override the request/response
55
+ */
56
+ rootInterceptors?: Interceptor<StandardHandlerInterceptorOptions<TContext>, StandardHandleResult, ThrowableError>[];
57
+ /**
58
+ *
59
+ * Interceptors for procedure client.
60
+ */
61
+ clientInterceptors?: Interceptor<ProcedureClientInterceptorOptions<TContext, Record<never, never>, Meta>, InferSchemaOutput<AnySchema>, ErrorFromErrorMap<Record<never, never>>>[];
62
+ }
63
+ declare class StandardHandler<T extends Context> {
64
+ private readonly matcher;
65
+ private readonly codec;
66
+ private readonly interceptors;
67
+ private readonly clientInterceptors;
68
+ private readonly rootInterceptors;
69
+ constructor(router: Router<any, T>, matcher: StandardMatcher, codec: StandardCodec, options: NoInfer<StandardHandlerOptions<T>>);
70
+ handle(request: StandardLazyRequest, options: StandardHandleOptions<T>): Promise<StandardHandleResult>;
71
+ }
72
+
73
+ export { CompositeStandardHandlerPlugin as C, StandardHandler as i };
74
+ export type { StandardHandlerInterceptorOptions as S, StandardHandlerPlugin as a, StandardHandlerOptions as b, StandardCodec as c, StandardParams as d, StandardMatcher as e, StandardMatchResult as f, StandardHandleOptions as g, StandardHandleResult as h };
@@ -0,0 +1,144 @@
1
+ import { ORPCErrorCode, ORPCErrorOptions, ORPCError, HTTPPath, ClientContext, Client } from '@orpc/client';
2
+ import { ErrorMap, ErrorMapItem, InferSchemaInput, AnySchema, Meta, ContractProcedureDef, InferSchemaOutput, ErrorFromErrorMap, AnyContractRouter, ContractProcedure } from '@orpc/contract';
3
+ import { MaybeOptionalOptions, Promisable, Interceptor, Value } from '@orpc/shared';
4
+
5
+ type Context = Record<PropertyKey, any>;
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>;
9
+
10
+ type ORPCErrorConstructorMapItemOptions<TData> = Omit<ORPCErrorOptions<TData>, 'defined' | 'status'>;
11
+ type ORPCErrorConstructorMapItem<TCode extends ORPCErrorCode, TInData> = (...rest: MaybeOptionalOptions<ORPCErrorConstructorMapItemOptions<TInData>>) => ORPCError<TCode, TInData>;
12
+ type ORPCErrorConstructorMap<T extends ErrorMap> = {
13
+ [K in keyof T]: K extends ORPCErrorCode ? T[K] extends ErrorMapItem<infer UInputSchema> ? ORPCErrorConstructorMapItem<K, InferSchemaInput<UInputSchema>> : never : never;
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
+
18
+ declare const LAZY_SYMBOL: unique symbol;
19
+ interface LazyMeta {
20
+ prefix?: HTTPPath;
21
+ }
22
+ interface Lazy<T> {
23
+ [LAZY_SYMBOL]: {
24
+ loader: () => Promise<{
25
+ default: T;
26
+ }>;
27
+ meta: LazyMeta;
28
+ };
29
+ }
30
+ type Lazyable<T> = T | Lazy<T>;
31
+ declare function lazy<T>(loader: () => Promise<{
32
+ default: T;
33
+ }>, meta?: LazyMeta): Lazy<T>;
34
+ declare function isLazy(item: unknown): item is Lazy<any>;
35
+ declare function getLazyMeta(lazied: Lazy<any>): LazyMeta;
36
+ declare function unlazy<T extends Lazyable<any>>(lazied: T): Promise<{
37
+ default: T extends Lazy<infer U> ? U : T;
38
+ }>;
39
+
40
+ interface ProcedureHandlerOptions<TCurrentContext extends Context, TInput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
41
+ context: TCurrentContext;
42
+ input: TInput;
43
+ path: readonly string[];
44
+ procedure: Procedure<Context, Context, AnySchema, AnySchema, ErrorMap, TMeta>;
45
+ signal?: AbortSignal;
46
+ lastEventId: string | undefined;
47
+ errors: TErrorConstructorMap;
48
+ }
49
+ interface ProcedureHandler<TCurrentContext extends Context, TInput, THandlerOutput, TErrorMap extends ErrorMap, TMeta extends Meta> {
50
+ (opt: ProcedureHandlerOptions<TCurrentContext, TInput, ORPCErrorConstructorMap<TErrorMap>, TMeta>): Promisable<THandlerOutput>;
51
+ }
52
+ interface ProcedureDef<TInitialContext extends Context, TCurrentContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap, TMeta> {
53
+ __initialContext?: (type: TInitialContext) => unknown;
54
+ middlewares: readonly AnyMiddleware[];
55
+ inputValidationIndex: number;
56
+ outputValidationIndex: number;
57
+ handler: ProcedureHandler<TCurrentContext, any, any, any, any>;
58
+ }
59
+ declare class Procedure<TInitialContext extends Context, TCurrentContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> {
60
+ '~orpc': ProcedureDef<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, TErrorMap, TMeta>;
61
+ constructor(def: ProcedureDef<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, TErrorMap, TMeta>);
62
+ }
63
+ type AnyProcedure = Procedure<any, any, any, any, any, any>;
64
+ declare function isProcedure(item: unknown): item is AnyProcedure;
65
+
66
+ type MiddlewareResult<TOutContext extends Context, TOutput> = Promisable<{
67
+ output: TOutput;
68
+ context: TOutContext;
69
+ }>;
70
+ type MiddlewareNextFnOptions<TOutContext extends Context> = Record<never, never> extends TOutContext ? {
71
+ context?: TOutContext;
72
+ } : {
73
+ context: TOutContext;
74
+ };
75
+ interface MiddlewareNextFn<TOutput> {
76
+ <U extends Context = Record<never, never>>(...rest: MaybeOptionalOptions<MiddlewareNextFnOptions<U>>): MiddlewareResult<U, TOutput>;
77
+ }
78
+ interface MiddlewareOutputFn<TOutput> {
79
+ (output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
80
+ }
81
+ interface MiddlewareOptions<TInContext extends Context, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
82
+ context: TInContext;
83
+ path: readonly string[];
84
+ procedure: Procedure<Context, Context, AnySchema, AnySchema, ErrorMap, TMeta>;
85
+ signal?: AbortSignal;
86
+ lastEventId: string | undefined;
87
+ next: MiddlewareNextFn<TOutput>;
88
+ errors: TErrorConstructorMap;
89
+ }
90
+ interface Middleware<TInContext extends Context, TOutContext extends Context, TInput, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
91
+ (options: MiddlewareOptions<TInContext, TOutput, TErrorConstructorMap, TMeta>, input: TInput, output: MiddlewareOutputFn<TOutput>): Promisable<MiddlewareResult<TOutContext, TOutput>>;
92
+ }
93
+ type AnyMiddleware = Middleware<any, any, any, any, any, any>;
94
+ interface MapInputMiddleware<TInput, TMappedInput> {
95
+ (input: TInput): TMappedInput;
96
+ }
97
+ declare function middlewareOutputFn<TOutput>(output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
98
+
99
+ type ProcedureClient<TClientContext extends ClientContext, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> = Client<TClientContext, InferSchemaInput<TInputSchema>, InferSchemaOutput<TOutputSchema>, ErrorFromErrorMap<TErrorMap>>;
100
+ interface ProcedureClientInterceptorOptions<TInitialContext extends Context, TErrorMap extends ErrorMap, TMeta extends Meta> {
101
+ context: TInitialContext;
102
+ input: unknown;
103
+ errors: ORPCErrorConstructorMap<TErrorMap>;
104
+ path: readonly string[];
105
+ procedure: Procedure<Context, Context, AnySchema, AnySchema, ErrorMap, TMeta>;
106
+ signal?: AbortSignal;
107
+ lastEventId: string | undefined;
108
+ }
109
+ /**
110
+ * Options for creating a procedure caller with comprehensive type safety
111
+ */
112
+ type CreateProcedureClientOptions<TInitialContext extends Context, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext> = {
113
+ /**
114
+ * This is helpful for logging and analytics.
115
+ */
116
+ path?: readonly string[];
117
+ interceptors?: Interceptor<ProcedureClientInterceptorOptions<TInitialContext, TErrorMap, TMeta>, InferSchemaOutput<TOutputSchema>, ErrorFromErrorMap<TErrorMap>>[];
118
+ } & (Record<never, never> extends TInitialContext ? {
119
+ context?: Value<TInitialContext, [clientContext: TClientContext]>;
120
+ } : {
121
+ context: Value<TInitialContext, [clientContext: TClientContext]>;
122
+ });
123
+ declare function createProcedureClient<TInitialContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext>(lazyableProcedure: Lazyable<Procedure<TInitialContext, any, TInputSchema, TOutputSchema, TErrorMap, TMeta>>, ...[options]: MaybeOptionalOptions<CreateProcedureClientOptions<TInitialContext, TOutputSchema, TErrorMap, TMeta, TClientContext>>): ProcedureClient<TClientContext, TInputSchema, TOutputSchema, TErrorMap>;
124
+
125
+ type Router<T extends AnyContractRouter, TInitialContext extends Context> = T extends ContractProcedure<infer UInputSchema, infer UOutputSchema, infer UErrorMap, infer UMeta> ? Procedure<TInitialContext, any, UInputSchema, UOutputSchema, UErrorMap, UMeta> : {
126
+ [K in keyof T]: T[K] extends AnyContractRouter ? Lazyable<Router<T[K], TInitialContext>> : never;
127
+ };
128
+ type AnyRouter = Router<any, any>;
129
+ type InferRouterInitialContext<T extends AnyRouter> = T extends Router<any, infer UInitialContext> ? UInitialContext : never;
130
+ type InferRouterInitialContexts<T extends AnyRouter> = T extends Procedure<infer UInitialContext, any, any, any, any, any> ? UInitialContext : {
131
+ [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInitialContexts<U> : never;
132
+ };
133
+ type InferRouterCurrentContexts<T extends AnyRouter> = T extends Procedure<any, infer UCurrentContext, any, any, any, any> ? UCurrentContext : {
134
+ [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterCurrentContexts<U> : never;
135
+ };
136
+ type InferRouterInputs<T extends AnyRouter> = T extends Procedure<any, any, infer UInputSchema, any, any, any> ? InferSchemaInput<UInputSchema> : {
137
+ [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInputs<U> : never;
138
+ };
139
+ type InferRouterOutputs<T extends AnyRouter> = T extends Procedure<any, any, any, infer UOutputSchema, any, any> ? InferSchemaOutput<UOutputSchema> : {
140
+ [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterOutputs<U> : never;
141
+ };
142
+
143
+ export { isProcedure as E, createProcedureClient as G, Procedure as P, createORPCErrorConstructorMap as l, mergeCurrentContext as m, LAZY_SYMBOL as n, lazy as p, isLazy as q, getLazyMeta as r, unlazy as u, validateORPCError as v, middlewareOutputFn as z };
144
+ export type { AnyMiddleware as A, ProcedureHandlerOptions as B, Context as C, ProcedureDef as D, ProcedureClientInterceptorOptions as F, InferRouterInitialContexts as H, InferRouterInitialContext as I, InferRouterCurrentContexts as J, InferRouterInputs as K, Lazyable as L, Middleware as M, InferRouterOutputs as N, ORPCErrorConstructorMap as O, Router as R, MergedInitialContext as a, MergedCurrentContext as b, MapInputMiddleware as c, CreateProcedureClientOptions as d, ProcedureClient as e, AnyRouter as f, Lazy as g, AnyProcedure as h, ProcedureHandler as i, ORPCErrorConstructorMapItemOptions as j, ORPCErrorConstructorMapItem as k, LazyMeta as o, MiddlewareResult as s, MiddlewareNextFnOptions as t, MiddlewareNextFn as w, MiddlewareOutputFn as x, MiddlewareOptions as y };
@@ -0,0 +1,144 @@
1
+ import { ORPCErrorCode, ORPCErrorOptions, ORPCError, HTTPPath, ClientContext, Client } from '@orpc/client';
2
+ import { ErrorMap, ErrorMapItem, InferSchemaInput, AnySchema, Meta, ContractProcedureDef, InferSchemaOutput, ErrorFromErrorMap, AnyContractRouter, ContractProcedure } from '@orpc/contract';
3
+ import { MaybeOptionalOptions, Promisable, Interceptor, Value } from '@orpc/shared';
4
+
5
+ type Context = Record<PropertyKey, any>;
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>;
9
+
10
+ type ORPCErrorConstructorMapItemOptions<TData> = Omit<ORPCErrorOptions<TData>, 'defined' | 'status'>;
11
+ type ORPCErrorConstructorMapItem<TCode extends ORPCErrorCode, TInData> = (...rest: MaybeOptionalOptions<ORPCErrorConstructorMapItemOptions<TInData>>) => ORPCError<TCode, TInData>;
12
+ type ORPCErrorConstructorMap<T extends ErrorMap> = {
13
+ [K in keyof T]: K extends ORPCErrorCode ? T[K] extends ErrorMapItem<infer UInputSchema> ? ORPCErrorConstructorMapItem<K, InferSchemaInput<UInputSchema>> : never : never;
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
+
18
+ declare const LAZY_SYMBOL: unique symbol;
19
+ interface LazyMeta {
20
+ prefix?: HTTPPath;
21
+ }
22
+ interface Lazy<T> {
23
+ [LAZY_SYMBOL]: {
24
+ loader: () => Promise<{
25
+ default: T;
26
+ }>;
27
+ meta: LazyMeta;
28
+ };
29
+ }
30
+ type Lazyable<T> = T | Lazy<T>;
31
+ declare function lazy<T>(loader: () => Promise<{
32
+ default: T;
33
+ }>, meta?: LazyMeta): Lazy<T>;
34
+ declare function isLazy(item: unknown): item is Lazy<any>;
35
+ declare function getLazyMeta(lazied: Lazy<any>): LazyMeta;
36
+ declare function unlazy<T extends Lazyable<any>>(lazied: T): Promise<{
37
+ default: T extends Lazy<infer U> ? U : T;
38
+ }>;
39
+
40
+ interface ProcedureHandlerOptions<TCurrentContext extends Context, TInput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
41
+ context: TCurrentContext;
42
+ input: TInput;
43
+ path: readonly string[];
44
+ procedure: Procedure<Context, Context, AnySchema, AnySchema, ErrorMap, TMeta>;
45
+ signal?: AbortSignal;
46
+ lastEventId: string | undefined;
47
+ errors: TErrorConstructorMap;
48
+ }
49
+ interface ProcedureHandler<TCurrentContext extends Context, TInput, THandlerOutput, TErrorMap extends ErrorMap, TMeta extends Meta> {
50
+ (opt: ProcedureHandlerOptions<TCurrentContext, TInput, ORPCErrorConstructorMap<TErrorMap>, TMeta>): Promisable<THandlerOutput>;
51
+ }
52
+ interface ProcedureDef<TInitialContext extends Context, TCurrentContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap, TMeta> {
53
+ __initialContext?: (type: TInitialContext) => unknown;
54
+ middlewares: readonly AnyMiddleware[];
55
+ inputValidationIndex: number;
56
+ outputValidationIndex: number;
57
+ handler: ProcedureHandler<TCurrentContext, any, any, any, any>;
58
+ }
59
+ declare class Procedure<TInitialContext extends Context, TCurrentContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> {
60
+ '~orpc': ProcedureDef<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, TErrorMap, TMeta>;
61
+ constructor(def: ProcedureDef<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, TErrorMap, TMeta>);
62
+ }
63
+ type AnyProcedure = Procedure<any, any, any, any, any, any>;
64
+ declare function isProcedure(item: unknown): item is AnyProcedure;
65
+
66
+ type MiddlewareResult<TOutContext extends Context, TOutput> = Promisable<{
67
+ output: TOutput;
68
+ context: TOutContext;
69
+ }>;
70
+ type MiddlewareNextFnOptions<TOutContext extends Context> = Record<never, never> extends TOutContext ? {
71
+ context?: TOutContext;
72
+ } : {
73
+ context: TOutContext;
74
+ };
75
+ interface MiddlewareNextFn<TOutput> {
76
+ <U extends Context = Record<never, never>>(...rest: MaybeOptionalOptions<MiddlewareNextFnOptions<U>>): MiddlewareResult<U, TOutput>;
77
+ }
78
+ interface MiddlewareOutputFn<TOutput> {
79
+ (output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
80
+ }
81
+ interface MiddlewareOptions<TInContext extends Context, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
82
+ context: TInContext;
83
+ path: readonly string[];
84
+ procedure: Procedure<Context, Context, AnySchema, AnySchema, ErrorMap, TMeta>;
85
+ signal?: AbortSignal;
86
+ lastEventId: string | undefined;
87
+ next: MiddlewareNextFn<TOutput>;
88
+ errors: TErrorConstructorMap;
89
+ }
90
+ interface Middleware<TInContext extends Context, TOutContext extends Context, TInput, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
91
+ (options: MiddlewareOptions<TInContext, TOutput, TErrorConstructorMap, TMeta>, input: TInput, output: MiddlewareOutputFn<TOutput>): Promisable<MiddlewareResult<TOutContext, TOutput>>;
92
+ }
93
+ type AnyMiddleware = Middleware<any, any, any, any, any, any>;
94
+ interface MapInputMiddleware<TInput, TMappedInput> {
95
+ (input: TInput): TMappedInput;
96
+ }
97
+ declare function middlewareOutputFn<TOutput>(output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
98
+
99
+ type ProcedureClient<TClientContext extends ClientContext, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> = Client<TClientContext, InferSchemaInput<TInputSchema>, InferSchemaOutput<TOutputSchema>, ErrorFromErrorMap<TErrorMap>>;
100
+ interface ProcedureClientInterceptorOptions<TInitialContext extends Context, TErrorMap extends ErrorMap, TMeta extends Meta> {
101
+ context: TInitialContext;
102
+ input: unknown;
103
+ errors: ORPCErrorConstructorMap<TErrorMap>;
104
+ path: readonly string[];
105
+ procedure: Procedure<Context, Context, AnySchema, AnySchema, ErrorMap, TMeta>;
106
+ signal?: AbortSignal;
107
+ lastEventId: string | undefined;
108
+ }
109
+ /**
110
+ * Options for creating a procedure caller with comprehensive type safety
111
+ */
112
+ type CreateProcedureClientOptions<TInitialContext extends Context, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext> = {
113
+ /**
114
+ * This is helpful for logging and analytics.
115
+ */
116
+ path?: readonly string[];
117
+ interceptors?: Interceptor<ProcedureClientInterceptorOptions<TInitialContext, TErrorMap, TMeta>, InferSchemaOutput<TOutputSchema>, ErrorFromErrorMap<TErrorMap>>[];
118
+ } & (Record<never, never> extends TInitialContext ? {
119
+ context?: Value<TInitialContext, [clientContext: TClientContext]>;
120
+ } : {
121
+ context: Value<TInitialContext, [clientContext: TClientContext]>;
122
+ });
123
+ declare function createProcedureClient<TInitialContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext>(lazyableProcedure: Lazyable<Procedure<TInitialContext, any, TInputSchema, TOutputSchema, TErrorMap, TMeta>>, ...[options]: MaybeOptionalOptions<CreateProcedureClientOptions<TInitialContext, TOutputSchema, TErrorMap, TMeta, TClientContext>>): ProcedureClient<TClientContext, TInputSchema, TOutputSchema, TErrorMap>;
124
+
125
+ type Router<T extends AnyContractRouter, TInitialContext extends Context> = T extends ContractProcedure<infer UInputSchema, infer UOutputSchema, infer UErrorMap, infer UMeta> ? Procedure<TInitialContext, any, UInputSchema, UOutputSchema, UErrorMap, UMeta> : {
126
+ [K in keyof T]: T[K] extends AnyContractRouter ? Lazyable<Router<T[K], TInitialContext>> : never;
127
+ };
128
+ type AnyRouter = Router<any, any>;
129
+ type InferRouterInitialContext<T extends AnyRouter> = T extends Router<any, infer UInitialContext> ? UInitialContext : never;
130
+ type InferRouterInitialContexts<T extends AnyRouter> = T extends Procedure<infer UInitialContext, any, any, any, any, any> ? UInitialContext : {
131
+ [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInitialContexts<U> : never;
132
+ };
133
+ type InferRouterCurrentContexts<T extends AnyRouter> = T extends Procedure<any, infer UCurrentContext, any, any, any, any> ? UCurrentContext : {
134
+ [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterCurrentContexts<U> : never;
135
+ };
136
+ type InferRouterInputs<T extends AnyRouter> = T extends Procedure<any, any, infer UInputSchema, any, any, any> ? InferSchemaInput<UInputSchema> : {
137
+ [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInputs<U> : never;
138
+ };
139
+ type InferRouterOutputs<T extends AnyRouter> = T extends Procedure<any, any, any, infer UOutputSchema, any, any> ? InferSchemaOutput<UOutputSchema> : {
140
+ [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterOutputs<U> : never;
141
+ };
142
+
143
+ export { isProcedure as E, createProcedureClient as G, Procedure as P, createORPCErrorConstructorMap as l, mergeCurrentContext as m, LAZY_SYMBOL as n, lazy as p, isLazy as q, getLazyMeta as r, unlazy as u, validateORPCError as v, middlewareOutputFn as z };
144
+ export type { AnyMiddleware as A, ProcedureHandlerOptions as B, Context as C, ProcedureDef as D, ProcedureClientInterceptorOptions as F, InferRouterInitialContexts as H, InferRouterInitialContext as I, InferRouterCurrentContexts as J, InferRouterInputs as K, Lazyable as L, Middleware as M, InferRouterOutputs as N, ORPCErrorConstructorMap as O, Router as R, MergedInitialContext as a, MergedCurrentContext as b, MapInputMiddleware as c, CreateProcedureClientOptions as d, ProcedureClient as e, AnyRouter as f, Lazy as g, AnyProcedure as h, ProcedureHandler as i, ORPCErrorConstructorMapItemOptions as j, ORPCErrorConstructorMapItem as k, LazyMeta as o, MiddlewareResult as s, MiddlewareNextFnOptions as t, MiddlewareNextFn as w, MiddlewareOutputFn as x, MiddlewareOptions as y };
@@ -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 };
@@ -0,0 +1,36 @@
1
+ import { ORPCError, fallbackContractConfig } from '@orpc/contract';
2
+
3
+ const STRICT_GET_METHOD_PLUGIN_IS_GET_METHOD_CONTEXT_SYMBOL = Symbol("STRICT_GET_METHOD_PLUGIN_IS_GET_METHOD_CONTEXT");
4
+ class StrictGetMethodPlugin {
5
+ error;
6
+ order = 7e6;
7
+ constructor(options = {}) {
8
+ this.error = options.error ?? new ORPCError("METHOD_NOT_SUPPORTED");
9
+ }
10
+ init(options) {
11
+ options.rootInterceptors ??= [];
12
+ options.clientInterceptors ??= [];
13
+ options.rootInterceptors.unshift((options2) => {
14
+ const isGetMethod = options2.request.method === "GET";
15
+ return options2.next({
16
+ ...options2,
17
+ context: {
18
+ ...options2.context,
19
+ [STRICT_GET_METHOD_PLUGIN_IS_GET_METHOD_CONTEXT_SYMBOL]: isGetMethod
20
+ }
21
+ });
22
+ });
23
+ options.clientInterceptors.unshift((options2) => {
24
+ if (typeof options2.context[STRICT_GET_METHOD_PLUGIN_IS_GET_METHOD_CONTEXT_SYMBOL] !== "boolean") {
25
+ throw new TypeError("[StrictGetMethodPlugin] strict GET method context has been corrupted or modified by another plugin or interceptor");
26
+ }
27
+ const procedureMethod = fallbackContractConfig("defaultMethod", options2.procedure["~orpc"].route.method);
28
+ if (options2.context[STRICT_GET_METHOD_PLUGIN_IS_GET_METHOD_CONTEXT_SYMBOL] && procedureMethod !== "GET") {
29
+ throw this.error;
30
+ }
31
+ return options2.next();
32
+ });
33
+ }
34
+ }
35
+
36
+ export { StrictGetMethodPlugin as S };