@orpc/server 0.0.0-next.a895960 → 0.0.0-next.a8e421c

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 (54) hide show
  1. package/README.md +122 -0
  2. package/dist/adapters/fetch/index.d.mts +28 -0
  3. package/dist/adapters/fetch/index.d.ts +28 -0
  4. package/dist/adapters/fetch/index.mjs +9 -0
  5. package/dist/adapters/hono/index.d.mts +21 -0
  6. package/dist/adapters/hono/index.d.ts +21 -0
  7. package/dist/adapters/hono/index.mjs +32 -0
  8. package/dist/adapters/next/index.d.mts +28 -0
  9. package/dist/adapters/next/index.d.ts +28 -0
  10. package/dist/adapters/next/index.mjs +29 -0
  11. package/dist/adapters/node/index.d.mts +36 -0
  12. package/dist/adapters/node/index.d.ts +36 -0
  13. package/dist/adapters/node/index.mjs +32 -0
  14. package/dist/adapters/standard/index.d.mts +26 -0
  15. package/dist/adapters/standard/index.d.ts +26 -0
  16. package/dist/adapters/standard/index.mjs +6 -0
  17. package/dist/index.d.mts +278 -0
  18. package/dist/index.d.ts +278 -0
  19. package/dist/index.mjs +342 -0
  20. package/dist/plugins/index.d.mts +31 -0
  21. package/dist/plugins/index.d.ts +31 -0
  22. package/dist/plugins/index.mjs +103 -0
  23. package/dist/shared/server.B52eKaNe.d.mts +8 -0
  24. package/dist/shared/server.BA-onDGB.d.ts +8 -0
  25. package/dist/shared/server.BY9sDlwl.mjs +158 -0
  26. package/dist/shared/server.BYTulgUc.d.mts +144 -0
  27. package/dist/shared/server.BYTulgUc.d.ts +144 -0
  28. package/dist/shared/server.BtxZnWJ9.mjs +374 -0
  29. package/dist/shared/server.CDzXh8DM.d.mts +75 -0
  30. package/dist/shared/server.CkGvC2T0.d.ts +75 -0
  31. package/dist/shared/server.Dba3Iiyp.mjs +12 -0
  32. package/dist/shared/server.Del5OmaY.mjs +29 -0
  33. package/package.json +41 -18
  34. package/dist/chunk-3JMSDC5L.js +0 -274
  35. package/dist/fetch.js +0 -112
  36. package/dist/index.js +0 -467
  37. package/dist/src/builder.d.ts +0 -53
  38. package/dist/src/fetch/handle.d.ts +0 -7
  39. package/dist/src/fetch/handler.d.ts +0 -3
  40. package/dist/src/fetch/index.d.ts +0 -4
  41. package/dist/src/fetch/types.d.ts +0 -28
  42. package/dist/src/index.d.ts +0 -17
  43. package/dist/src/lazy.d.ts +0 -23
  44. package/dist/src/middleware.d.ts +0 -26
  45. package/dist/src/procedure-builder.d.ts +0 -31
  46. package/dist/src/procedure-caller.d.ts +0 -26
  47. package/dist/src/procedure-implementer.d.ts +0 -22
  48. package/dist/src/procedure.d.ts +0 -32
  49. package/dist/src/router-builder.d.ts +0 -27
  50. package/dist/src/router-caller.d.ts +0 -25
  51. package/dist/src/router-implementer.d.ts +0 -24
  52. package/dist/src/router.d.ts +0 -21
  53. package/dist/src/types.d.ts +0 -14
  54. package/dist/src/utils.d.ts +0 -3
@@ -0,0 +1,31 @@
1
+ import { b as StandardHandlerInterceptorOptions, H as HandlerPlugin, a as StandardHandlerOptions } from '../shared/server.CDzXh8DM.mjs';
2
+ export { C as CompositeHandlerPlugin } from '../shared/server.CDzXh8DM.mjs';
3
+ import { Value } from '@orpc/shared';
4
+ import { C as Context } from '../shared/server.BYTulgUc.mjs';
5
+ import '@orpc/contract';
6
+ import '@orpc/standard-server';
7
+ import '@orpc/client';
8
+
9
+ interface CORSOptions<TContext extends Context> {
10
+ origin?: Value<string | readonly string[] | null | undefined, [origin: string, options: StandardHandlerInterceptorOptions<TContext>]>;
11
+ timingOrigin?: Value<string | readonly string[] | null | undefined, [origin: string, options: StandardHandlerInterceptorOptions<TContext>]>;
12
+ allowMethods?: readonly string[];
13
+ allowHeaders?: readonly string[];
14
+ maxAge?: number;
15
+ credentials?: boolean;
16
+ exposeHeaders?: readonly string[];
17
+ }
18
+ declare class CORSPlugin<TContext extends Context> implements HandlerPlugin<TContext> {
19
+ private readonly options;
20
+ constructor(options?: CORSOptions<TContext>);
21
+ init(options: StandardHandlerOptions<TContext>): void;
22
+ }
23
+
24
+ interface ResponseHeadersPluginContext {
25
+ resHeaders?: Headers;
26
+ }
27
+ declare class ResponseHeadersPlugin<TContext extends ResponseHeadersPluginContext & Context> implements HandlerPlugin<TContext> {
28
+ init(options: StandardHandlerOptions<TContext>): void;
29
+ }
30
+
31
+ export { type CORSOptions, CORSPlugin, HandlerPlugin, ResponseHeadersPlugin, type ResponseHeadersPluginContext };
@@ -0,0 +1,31 @@
1
+ import { b as StandardHandlerInterceptorOptions, H as HandlerPlugin, a as StandardHandlerOptions } from '../shared/server.CkGvC2T0.js';
2
+ export { C as CompositeHandlerPlugin } from '../shared/server.CkGvC2T0.js';
3
+ import { Value } from '@orpc/shared';
4
+ import { C as Context } from '../shared/server.BYTulgUc.js';
5
+ import '@orpc/contract';
6
+ import '@orpc/standard-server';
7
+ import '@orpc/client';
8
+
9
+ interface CORSOptions<TContext extends Context> {
10
+ origin?: Value<string | readonly string[] | null | undefined, [origin: string, options: StandardHandlerInterceptorOptions<TContext>]>;
11
+ timingOrigin?: Value<string | readonly string[] | null | undefined, [origin: string, options: StandardHandlerInterceptorOptions<TContext>]>;
12
+ allowMethods?: readonly string[];
13
+ allowHeaders?: readonly string[];
14
+ maxAge?: number;
15
+ credentials?: boolean;
16
+ exposeHeaders?: readonly string[];
17
+ }
18
+ declare class CORSPlugin<TContext extends Context> implements HandlerPlugin<TContext> {
19
+ private readonly options;
20
+ constructor(options?: CORSOptions<TContext>);
21
+ init(options: StandardHandlerOptions<TContext>): void;
22
+ }
23
+
24
+ interface ResponseHeadersPluginContext {
25
+ resHeaders?: Headers;
26
+ }
27
+ declare class ResponseHeadersPlugin<TContext extends ResponseHeadersPluginContext & Context> implements HandlerPlugin<TContext> {
28
+ init(options: StandardHandlerOptions<TContext>): void;
29
+ }
30
+
31
+ export { type CORSOptions, CORSPlugin, HandlerPlugin, ResponseHeadersPlugin, type ResponseHeadersPluginContext };
@@ -0,0 +1,103 @@
1
+ export { C as CompositeHandlerPlugin } from '../shared/server.Dba3Iiyp.mjs';
2
+ import { value } from '@orpc/shared';
3
+
4
+ class CORSPlugin {
5
+ options;
6
+ constructor(options = {}) {
7
+ const defaults = {
8
+ origin: (origin) => origin,
9
+ allowMethods: ["GET", "HEAD", "PUT", "POST", "DELETE", "PATCH"]
10
+ };
11
+ this.options = {
12
+ ...defaults,
13
+ ...options
14
+ };
15
+ }
16
+ init(options) {
17
+ options.rootInterceptors ??= [];
18
+ options.rootInterceptors.unshift(async (interceptorOptions) => {
19
+ if (interceptorOptions.request.method === "OPTIONS") {
20
+ const resHeaders = {};
21
+ if (this.options.maxAge !== void 0) {
22
+ resHeaders["access-control-max-age"] = this.options.maxAge.toString();
23
+ }
24
+ if (this.options.allowMethods?.length) {
25
+ resHeaders["access-control-allow-methods"] = this.options.allowMethods.join(",");
26
+ }
27
+ const allowHeaders = this.options.allowHeaders ?? interceptorOptions.request.headers["access-control-request-headers"];
28
+ if (Array.isArray(allowHeaders) && allowHeaders.length) {
29
+ resHeaders["access-control-allow-headers"] = allowHeaders.join(",");
30
+ } else if (typeof allowHeaders === "string") {
31
+ resHeaders["access-control-allow-headers"] = allowHeaders;
32
+ }
33
+ return {
34
+ matched: true,
35
+ response: {
36
+ status: 204,
37
+ headers: resHeaders,
38
+ body: void 0
39
+ }
40
+ };
41
+ }
42
+ return interceptorOptions.next();
43
+ });
44
+ options.rootInterceptors.unshift(async (interceptorOptions) => {
45
+ const result = await interceptorOptions.next();
46
+ if (!result.matched) {
47
+ return result;
48
+ }
49
+ const origin = Array.isArray(interceptorOptions.request.headers.origin) ? interceptorOptions.request.headers.origin.join(",") : interceptorOptions.request.headers.origin || "";
50
+ const allowedOrigin = await value(this.options.origin, origin, interceptorOptions);
51
+ const allowedOriginArr = Array.isArray(allowedOrigin) ? allowedOrigin : [allowedOrigin];
52
+ if (allowedOriginArr.includes("*")) {
53
+ result.response.headers["access-control-allow-origin"] = "*";
54
+ } else {
55
+ if (allowedOriginArr.includes(origin)) {
56
+ result.response.headers["access-control-allow-origin"] = origin;
57
+ }
58
+ result.response.headers.vary = interceptorOptions.request.headers.vary ?? "origin";
59
+ }
60
+ const allowedTimingOrigin = await value(this.options.timingOrigin, origin, interceptorOptions);
61
+ const allowedTimingOriginArr = Array.isArray(allowedTimingOrigin) ? allowedTimingOrigin : [allowedTimingOrigin];
62
+ if (allowedTimingOriginArr.includes("*")) {
63
+ result.response.headers["timing-allow-origin"] = "*";
64
+ } else if (allowedTimingOriginArr.includes(origin)) {
65
+ result.response.headers["timing-allow-origin"] = origin;
66
+ }
67
+ if (this.options.credentials) {
68
+ result.response.headers["access-control-allow-credentials"] = "true";
69
+ }
70
+ if (this.options.exposeHeaders?.length) {
71
+ result.response.headers["access-control-expose-headers"] = this.options.exposeHeaders.join(",");
72
+ }
73
+ return result;
74
+ });
75
+ }
76
+ }
77
+
78
+ class ResponseHeadersPlugin {
79
+ init(options) {
80
+ options.rootInterceptors ??= [];
81
+ options.rootInterceptors.push(async (interceptorOptions) => {
82
+ const headers = new Headers();
83
+ interceptorOptions.context.resHeaders = headers;
84
+ const result = await interceptorOptions.next();
85
+ if (!result.matched) {
86
+ return result;
87
+ }
88
+ const responseHeaders = result.response.headers;
89
+ for (const [key, value] of headers) {
90
+ if (Array.isArray(responseHeaders[key])) {
91
+ responseHeaders[key].push(value);
92
+ } else if (responseHeaders[key] !== void 0) {
93
+ responseHeaders[key] = [responseHeaders[key], value];
94
+ } else {
95
+ responseHeaders[key] = value;
96
+ }
97
+ }
98
+ return result;
99
+ });
100
+ }
101
+ }
102
+
103
+ export { CORSPlugin, ResponseHeadersPlugin };
@@ -0,0 +1,8 @@
1
+ import { StandardRPCJsonSerializerOptions } from '@orpc/client/standard';
2
+ import { C as Context } from './server.BYTulgUc.mjs';
3
+ import { a as StandardHandlerOptions } from './server.CDzXh8DM.mjs';
4
+
5
+ interface StandardRPCHandlerOptions<T extends Context> extends StandardHandlerOptions<T>, StandardRPCJsonSerializerOptions {
6
+ }
7
+
8
+ export type { StandardRPCHandlerOptions as S };
@@ -0,0 +1,8 @@
1
+ import { StandardRPCJsonSerializerOptions } from '@orpc/client/standard';
2
+ import { C as Context } from './server.BYTulgUc.js';
3
+ import { a as StandardHandlerOptions } from './server.CkGvC2T0.js';
4
+
5
+ interface StandardRPCHandlerOptions<T extends Context> extends StandardHandlerOptions<T>, StandardRPCJsonSerializerOptions {
6
+ }
7
+
8
+ export type { StandardRPCHandlerOptions as S };
@@ -0,0 +1,158 @@
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';
5
+
6
+ class StandardHandler {
7
+ constructor(router, matcher, codec, options) {
8
+ this.matcher = matcher;
9
+ this.codec = codec;
10
+ this.options = options;
11
+ this.plugin = new CompositeHandlerPlugin(options.plugins);
12
+ this.plugin.init(this.options);
13
+ this.matcher.init(router);
14
+ }
15
+ plugin;
16
+ handle(request, ...[options]) {
17
+ 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
+ },
25
+ async (interceptorOptions) => {
26
+ let isDecoding = false;
27
+ try {
28
+ return await intercept(
29
+ this.options.interceptors ?? [],
30
+ 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);
36
+ if (!match) {
37
+ return { matched: false, response: void 0 };
38
+ }
39
+ const client = createProcedureClient(match.procedure, {
40
+ context: interceptorOptions2.context,
41
+ path: match.path,
42
+ interceptors: this.options.clientInterceptors
43
+ });
44
+ isDecoding = true;
45
+ const input = await this.codec.decode(request, match.params, match.procedure);
46
+ 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 });
49
+ const response = this.codec.encode(output, match.procedure);
50
+ return {
51
+ matched: true,
52
+ response
53
+ };
54
+ }
55
+ );
56
+ } catch (e) {
57
+ const error = isDecoding ? new ORPCError("BAD_REQUEST", {
58
+ message: `Malformed request. Ensure the request body is properly formatted and the 'Content-Type' header is set correctly.`,
59
+ cause: e
60
+ }) : toORPCError(e);
61
+ const response = this.codec.encodeError(error);
62
+ return {
63
+ matched: true,
64
+ response
65
+ };
66
+ }
67
+ }
68
+ );
69
+ }
70
+ }
71
+
72
+ class StandardRPCCodec {
73
+ constructor(serializer) {
74
+ this.serializer = serializer;
75
+ }
76
+ async decode(request, _params, _procedure) {
77
+ const serialized = request.method === "GET" ? parseEmptyableJSON(request.url.searchParams.getAll("data").at(-1)) : await request.body();
78
+ return this.serializer.deserialize(serialized);
79
+ }
80
+ encode(output, _procedure) {
81
+ return {
82
+ status: 200,
83
+ headers: {},
84
+ body: this.serializer.serialize(output)
85
+ };
86
+ }
87
+ encodeError(error) {
88
+ return {
89
+ status: error.status,
90
+ headers: {},
91
+ body: this.serializer.serialize(error.toJSON())
92
+ };
93
+ }
94
+ }
95
+
96
+ class StandardRPCMatcher {
97
+ tree = {};
98
+ pendingRouters = [];
99
+ init(router, path = []) {
100
+ const laziedOptions = traverseContractProcedures({ router, path }, ({ path: path2, contract }) => {
101
+ const httpPath = toHttpPath(path2);
102
+ if (isProcedure(contract)) {
103
+ this.tree[httpPath] = {
104
+ path: path2,
105
+ contract,
106
+ procedure: contract,
107
+ // this mean dev not used contract-first so we can used contract as procedure directly
108
+ router
109
+ };
110
+ } else {
111
+ this.tree[httpPath] = {
112
+ path: path2,
113
+ contract,
114
+ procedure: void 0,
115
+ router
116
+ };
117
+ }
118
+ });
119
+ this.pendingRouters.push(...laziedOptions.map((option) => ({
120
+ ...option,
121
+ httpPathPrefix: toHttpPath(option.path)
122
+ })));
123
+ }
124
+ async match(_method, pathname) {
125
+ if (this.pendingRouters.length) {
126
+ const newPendingRouters = [];
127
+ for (const pendingRouter of this.pendingRouters) {
128
+ if (pathname.startsWith(pendingRouter.httpPathPrefix)) {
129
+ const { default: router } = await unlazy(pendingRouter.router);
130
+ this.init(router, pendingRouter.path);
131
+ } else {
132
+ newPendingRouters.push(pendingRouter);
133
+ }
134
+ }
135
+ this.pendingRouters = newPendingRouters;
136
+ }
137
+ const match = this.tree[pathname];
138
+ if (!match) {
139
+ return void 0;
140
+ }
141
+ if (!match.procedure) {
142
+ const { default: maybeProcedure } = await unlazy(getRouter(match.router, match.path));
143
+ if (!isProcedure(maybeProcedure)) {
144
+ throw new Error(`
145
+ [Contract-First] Missing or invalid implementation for procedure at path: ${toHttpPath(match.path)}.
146
+ Ensure that the procedure is correctly defined and matches the expected contract.
147
+ `);
148
+ }
149
+ match.procedure = createContractedProcedure(maybeProcedure, match.contract);
150
+ }
151
+ return {
152
+ path: match.path,
153
+ procedure: match.procedure
154
+ };
155
+ }
156
+ }
157
+
158
+ export { StandardHandler as S, StandardRPCCodec as a, StandardRPCMatcher as b };
@@ -0,0 +1,144 @@
1
+ import { ORPCErrorCode, ORPCErrorOptions, ORPCError, ClientContext, Client } from '@orpc/client';
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';
4
+
5
+ type Context = Record<string, 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
+ type ContextExtendsGuard<T extends Context, U extends Context> = T extends U ? unknown : never;
10
+
11
+ type ORPCErrorConstructorMapItemOptions<TData> = Omit<ORPCErrorOptions<TData>, 'defined' | 'status'>;
12
+ type ORPCErrorConstructorMapItem<TCode extends ORPCErrorCode, TInData> = (...rest: MaybeOptionalOptions<ORPCErrorConstructorMapItemOptions<TInData>>) => ORPCError<TCode, TInData>;
13
+ type ORPCErrorConstructorMap<T extends ErrorMap> = {
14
+ [K in keyof T]: K extends ORPCErrorCode ? T[K] extends ErrorMapItem<infer UInputSchema> ? ORPCErrorConstructorMapItem<K, InferSchemaInput<UInputSchema>> : never : never;
15
+ };
16
+ declare function createORPCErrorConstructorMap<T extends ErrorMap>(errors: T): ORPCErrorConstructorMap<T>;
17
+ declare function validateORPCError(map: ErrorMap, error: ORPCError<any, any>): Promise<ORPCError<string, unknown>>;
18
+
19
+ declare const LAZY_SYMBOL: unique symbol;
20
+ interface LazyMeta {
21
+ prefix?: HTTPPath;
22
+ }
23
+ interface Lazy<T> {
24
+ [LAZY_SYMBOL]: {
25
+ loader: () => Promise<{
26
+ default: T;
27
+ }>;
28
+ meta: LazyMeta;
29
+ };
30
+ }
31
+ type Lazyable<T> = T | Lazy<T>;
32
+ declare function lazy<T>(loader: () => Promise<{
33
+ default: T;
34
+ }>, meta?: LazyMeta): Lazy<T>;
35
+ declare function isLazy(item: unknown): item is Lazy<any>;
36
+ declare function getLazyMeta(lazied: Lazy<any>): LazyMeta;
37
+ declare function unlazy<T extends Lazyable<any>>(lazied: T): Promise<{
38
+ default: T extends Lazy<infer U> ? U : T;
39
+ }>;
40
+
41
+ interface ProcedureHandlerOptions<TCurrentContext extends Context, TInput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
42
+ context: TCurrentContext;
43
+ input: TInput;
44
+ path: readonly string[];
45
+ procedure: Procedure<Context, Context, AnySchema, AnySchema, ErrorMap, TMeta>;
46
+ signal?: AbortSignal;
47
+ lastEventId: string | undefined;
48
+ errors: TErrorConstructorMap;
49
+ }
50
+ interface ProcedureHandler<TCurrentContext extends Context, TInput, THandlerOutput, TErrorMap extends ErrorMap, TMeta extends Meta> {
51
+ (opt: ProcedureHandlerOptions<TCurrentContext, TInput, ORPCErrorConstructorMap<TErrorMap>, TMeta>): Promisable<THandlerOutput>;
52
+ }
53
+ 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> {
54
+ __initialContext?: (type: TInitialContext) => unknown;
55
+ middlewares: readonly AnyMiddleware[];
56
+ inputValidationIndex: number;
57
+ outputValidationIndex: number;
58
+ handler: ProcedureHandler<TCurrentContext, any, any, any, any>;
59
+ }
60
+ declare class Procedure<TInitialContext extends Context, TCurrentContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> {
61
+ '~orpc': ProcedureDef<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, TErrorMap, TMeta>;
62
+ constructor(def: ProcedureDef<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, TErrorMap, TMeta>);
63
+ }
64
+ type AnyProcedure = Procedure<any, any, any, any, any, any>;
65
+ declare function isProcedure(item: unknown): item is AnyProcedure;
66
+
67
+ type MiddlewareResult<TOutContext extends Context, TOutput> = Promisable<{
68
+ output: TOutput;
69
+ context: TOutContext;
70
+ }>;
71
+ type MiddlewareNextFnOptions<TOutContext extends Context> = Record<never, never> extends TOutContext ? {
72
+ context?: TOutContext;
73
+ } : {
74
+ context: TOutContext;
75
+ };
76
+ interface MiddlewareNextFn<TOutput> {
77
+ <U extends Context = Record<never, never>>(...rest: MaybeOptionalOptions<MiddlewareNextFnOptions<U>>): MiddlewareResult<U, TOutput>;
78
+ }
79
+ interface MiddlewareOutputFn<TOutput> {
80
+ (output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
81
+ }
82
+ interface MiddlewareOptions<TInContext extends Context, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
83
+ context: TInContext;
84
+ path: readonly string[];
85
+ procedure: Procedure<Context, Context, AnySchema, AnySchema, ErrorMap, TMeta>;
86
+ signal?: AbortSignal;
87
+ lastEventId: string | undefined;
88
+ next: MiddlewareNextFn<TOutput>;
89
+ errors: TErrorConstructorMap;
90
+ }
91
+ interface Middleware<TInContext extends Context, TOutContext extends Context, TInput, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
92
+ (options: MiddlewareOptions<TInContext, TOutput, TErrorConstructorMap, TMeta>, input: TInput, output: MiddlewareOutputFn<TOutput>): Promisable<MiddlewareResult<TOutContext, TOutput>>;
93
+ }
94
+ type AnyMiddleware = Middleware<any, any, any, any, any, any>;
95
+ interface MapInputMiddleware<TInput, TMappedInput> {
96
+ (input: TInput): TMappedInput;
97
+ }
98
+ declare function middlewareOutputFn<TOutput>(output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
99
+
100
+ type ProcedureClient<TClientContext extends ClientContext, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> = Client<TClientContext, InferSchemaInput<TInputSchema>, InferSchemaOutput<TOutputSchema>, ErrorFromErrorMap<TErrorMap>>;
101
+ interface ProcedureClientInterceptorOptions<TInitialContext extends Context, TInputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> {
102
+ context: TInitialContext;
103
+ input: InferSchemaInput<TInputSchema>;
104
+ errors: ORPCErrorConstructorMap<TErrorMap>;
105
+ path: readonly string[];
106
+ procedure: Procedure<Context, Context, AnySchema, AnySchema, ErrorMap, TMeta>;
107
+ signal?: AbortSignal;
108
+ lastEventId: string | undefined;
109
+ }
110
+ /**
111
+ * Options for creating a procedure caller with comprehensive type safety
112
+ */
113
+ type CreateProcedureClientOptions<TInitialContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext> = {
114
+ /**
115
+ * This is helpful for logging and analytics.
116
+ */
117
+ path?: readonly string[];
118
+ interceptors?: Interceptor<ProcedureClientInterceptorOptions<TInitialContext, TInputSchema, TErrorMap, TMeta>, InferSchemaOutput<TOutputSchema>, ErrorFromErrorMap<TErrorMap>>[];
119
+ } & (Record<never, never> extends TInitialContext ? {
120
+ context?: Value<TInitialContext, [clientContext: TClientContext]>;
121
+ } : {
122
+ context: Value<TInitialContext, [clientContext: TClientContext]>;
123
+ });
124
+ 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>;
125
+
126
+ 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> : {
127
+ [K in keyof T]: T[K] extends AnyContractRouter ? Lazyable<Router<T[K], TInitialContext>> : never;
128
+ };
129
+ type AnyRouter = Router<any, any>;
130
+ type InferRouterInitialContext<T extends AnyRouter> = T extends Router<any, infer UInitialContext> ? UInitialContext : never;
131
+ type InferRouterInitialContexts<T extends AnyRouter> = T extends Procedure<infer UInitialContext, any, any, any, any, any> ? UInitialContext : {
132
+ [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInitialContexts<U> : never;
133
+ };
134
+ type InferRouterCurrentContexts<T extends AnyRouter> = T extends Procedure<any, infer UCurrentContext, any, any, any, any> ? UCurrentContext : {
135
+ [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterCurrentContexts<U> : never;
136
+ };
137
+ type InferRouterInputs<T extends AnyRouter> = T extends Procedure<any, any, infer UInputSchema, any, any, any> ? InferSchemaInput<UInputSchema> : {
138
+ [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInputs<U> : never;
139
+ };
140
+ type InferRouterOutputs<T extends AnyRouter> = T extends Procedure<any, any, any, infer UOutputSchema, any, any> ? InferSchemaOutput<UOutputSchema> : {
141
+ [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterOutputs<U> : never;
142
+ };
143
+
144
+ export { type AnyProcedure as A, type MiddlewareOptions as B, type Context as C, middlewareOutputFn as D, type ProcedureHandlerOptions as E, type ProcedureDef as F, isProcedure as G, createProcedureClient as H, type InferRouterInitialContext as I, type InferRouterInitialContexts as J, type InferRouterCurrentContexts as K, type Lazyable as L, type Middleware as M, type InferRouterInputs as N, type ORPCErrorConstructorMap as O, type ProcedureClientInterceptorOptions as P, type InferRouterOutputs as Q, type Router as R, type AnyRouter as a, Procedure as b, type ContextExtendsGuard as c, type MergedCurrentContext as d, type MergedInitialContext as e, type MapInputMiddleware as f, type CreateProcedureClientOptions as g, type ProcedureClient as h, type AnyMiddleware as i, type Lazy as j, type ProcedureHandler as k, type ORPCErrorConstructorMapItemOptions as l, mergeCurrentContext as m, type ORPCErrorConstructorMapItem as n, createORPCErrorConstructorMap as o, LAZY_SYMBOL as p, type LazyMeta as q, lazy as r, isLazy as s, getLazyMeta as t, unlazy as u, validateORPCError as v, type MiddlewareResult as w, type MiddlewareNextFnOptions as x, type MiddlewareNextFn as y, type MiddlewareOutputFn as z };
@@ -0,0 +1,144 @@
1
+ import { ORPCErrorCode, ORPCErrorOptions, ORPCError, ClientContext, Client } from '@orpc/client';
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';
4
+
5
+ type Context = Record<string, 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
+ type ContextExtendsGuard<T extends Context, U extends Context> = T extends U ? unknown : never;
10
+
11
+ type ORPCErrorConstructorMapItemOptions<TData> = Omit<ORPCErrorOptions<TData>, 'defined' | 'status'>;
12
+ type ORPCErrorConstructorMapItem<TCode extends ORPCErrorCode, TInData> = (...rest: MaybeOptionalOptions<ORPCErrorConstructorMapItemOptions<TInData>>) => ORPCError<TCode, TInData>;
13
+ type ORPCErrorConstructorMap<T extends ErrorMap> = {
14
+ [K in keyof T]: K extends ORPCErrorCode ? T[K] extends ErrorMapItem<infer UInputSchema> ? ORPCErrorConstructorMapItem<K, InferSchemaInput<UInputSchema>> : never : never;
15
+ };
16
+ declare function createORPCErrorConstructorMap<T extends ErrorMap>(errors: T): ORPCErrorConstructorMap<T>;
17
+ declare function validateORPCError(map: ErrorMap, error: ORPCError<any, any>): Promise<ORPCError<string, unknown>>;
18
+
19
+ declare const LAZY_SYMBOL: unique symbol;
20
+ interface LazyMeta {
21
+ prefix?: HTTPPath;
22
+ }
23
+ interface Lazy<T> {
24
+ [LAZY_SYMBOL]: {
25
+ loader: () => Promise<{
26
+ default: T;
27
+ }>;
28
+ meta: LazyMeta;
29
+ };
30
+ }
31
+ type Lazyable<T> = T | Lazy<T>;
32
+ declare function lazy<T>(loader: () => Promise<{
33
+ default: T;
34
+ }>, meta?: LazyMeta): Lazy<T>;
35
+ declare function isLazy(item: unknown): item is Lazy<any>;
36
+ declare function getLazyMeta(lazied: Lazy<any>): LazyMeta;
37
+ declare function unlazy<T extends Lazyable<any>>(lazied: T): Promise<{
38
+ default: T extends Lazy<infer U> ? U : T;
39
+ }>;
40
+
41
+ interface ProcedureHandlerOptions<TCurrentContext extends Context, TInput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
42
+ context: TCurrentContext;
43
+ input: TInput;
44
+ path: readonly string[];
45
+ procedure: Procedure<Context, Context, AnySchema, AnySchema, ErrorMap, TMeta>;
46
+ signal?: AbortSignal;
47
+ lastEventId: string | undefined;
48
+ errors: TErrorConstructorMap;
49
+ }
50
+ interface ProcedureHandler<TCurrentContext extends Context, TInput, THandlerOutput, TErrorMap extends ErrorMap, TMeta extends Meta> {
51
+ (opt: ProcedureHandlerOptions<TCurrentContext, TInput, ORPCErrorConstructorMap<TErrorMap>, TMeta>): Promisable<THandlerOutput>;
52
+ }
53
+ 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> {
54
+ __initialContext?: (type: TInitialContext) => unknown;
55
+ middlewares: readonly AnyMiddleware[];
56
+ inputValidationIndex: number;
57
+ outputValidationIndex: number;
58
+ handler: ProcedureHandler<TCurrentContext, any, any, any, any>;
59
+ }
60
+ declare class Procedure<TInitialContext extends Context, TCurrentContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> {
61
+ '~orpc': ProcedureDef<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, TErrorMap, TMeta>;
62
+ constructor(def: ProcedureDef<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, TErrorMap, TMeta>);
63
+ }
64
+ type AnyProcedure = Procedure<any, any, any, any, any, any>;
65
+ declare function isProcedure(item: unknown): item is AnyProcedure;
66
+
67
+ type MiddlewareResult<TOutContext extends Context, TOutput> = Promisable<{
68
+ output: TOutput;
69
+ context: TOutContext;
70
+ }>;
71
+ type MiddlewareNextFnOptions<TOutContext extends Context> = Record<never, never> extends TOutContext ? {
72
+ context?: TOutContext;
73
+ } : {
74
+ context: TOutContext;
75
+ };
76
+ interface MiddlewareNextFn<TOutput> {
77
+ <U extends Context = Record<never, never>>(...rest: MaybeOptionalOptions<MiddlewareNextFnOptions<U>>): MiddlewareResult<U, TOutput>;
78
+ }
79
+ interface MiddlewareOutputFn<TOutput> {
80
+ (output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
81
+ }
82
+ interface MiddlewareOptions<TInContext extends Context, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
83
+ context: TInContext;
84
+ path: readonly string[];
85
+ procedure: Procedure<Context, Context, AnySchema, AnySchema, ErrorMap, TMeta>;
86
+ signal?: AbortSignal;
87
+ lastEventId: string | undefined;
88
+ next: MiddlewareNextFn<TOutput>;
89
+ errors: TErrorConstructorMap;
90
+ }
91
+ interface Middleware<TInContext extends Context, TOutContext extends Context, TInput, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
92
+ (options: MiddlewareOptions<TInContext, TOutput, TErrorConstructorMap, TMeta>, input: TInput, output: MiddlewareOutputFn<TOutput>): Promisable<MiddlewareResult<TOutContext, TOutput>>;
93
+ }
94
+ type AnyMiddleware = Middleware<any, any, any, any, any, any>;
95
+ interface MapInputMiddleware<TInput, TMappedInput> {
96
+ (input: TInput): TMappedInput;
97
+ }
98
+ declare function middlewareOutputFn<TOutput>(output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
99
+
100
+ type ProcedureClient<TClientContext extends ClientContext, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> = Client<TClientContext, InferSchemaInput<TInputSchema>, InferSchemaOutput<TOutputSchema>, ErrorFromErrorMap<TErrorMap>>;
101
+ interface ProcedureClientInterceptorOptions<TInitialContext extends Context, TInputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> {
102
+ context: TInitialContext;
103
+ input: InferSchemaInput<TInputSchema>;
104
+ errors: ORPCErrorConstructorMap<TErrorMap>;
105
+ path: readonly string[];
106
+ procedure: Procedure<Context, Context, AnySchema, AnySchema, ErrorMap, TMeta>;
107
+ signal?: AbortSignal;
108
+ lastEventId: string | undefined;
109
+ }
110
+ /**
111
+ * Options for creating a procedure caller with comprehensive type safety
112
+ */
113
+ type CreateProcedureClientOptions<TInitialContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext> = {
114
+ /**
115
+ * This is helpful for logging and analytics.
116
+ */
117
+ path?: readonly string[];
118
+ interceptors?: Interceptor<ProcedureClientInterceptorOptions<TInitialContext, TInputSchema, TErrorMap, TMeta>, InferSchemaOutput<TOutputSchema>, ErrorFromErrorMap<TErrorMap>>[];
119
+ } & (Record<never, never> extends TInitialContext ? {
120
+ context?: Value<TInitialContext, [clientContext: TClientContext]>;
121
+ } : {
122
+ context: Value<TInitialContext, [clientContext: TClientContext]>;
123
+ });
124
+ 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>;
125
+
126
+ 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> : {
127
+ [K in keyof T]: T[K] extends AnyContractRouter ? Lazyable<Router<T[K], TInitialContext>> : never;
128
+ };
129
+ type AnyRouter = Router<any, any>;
130
+ type InferRouterInitialContext<T extends AnyRouter> = T extends Router<any, infer UInitialContext> ? UInitialContext : never;
131
+ type InferRouterInitialContexts<T extends AnyRouter> = T extends Procedure<infer UInitialContext, any, any, any, any, any> ? UInitialContext : {
132
+ [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInitialContexts<U> : never;
133
+ };
134
+ type InferRouterCurrentContexts<T extends AnyRouter> = T extends Procedure<any, infer UCurrentContext, any, any, any, any> ? UCurrentContext : {
135
+ [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterCurrentContexts<U> : never;
136
+ };
137
+ type InferRouterInputs<T extends AnyRouter> = T extends Procedure<any, any, infer UInputSchema, any, any, any> ? InferSchemaInput<UInputSchema> : {
138
+ [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInputs<U> : never;
139
+ };
140
+ type InferRouterOutputs<T extends AnyRouter> = T extends Procedure<any, any, any, infer UOutputSchema, any, any> ? InferSchemaOutput<UOutputSchema> : {
141
+ [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterOutputs<U> : never;
142
+ };
143
+
144
+ export { type AnyProcedure as A, type MiddlewareOptions as B, type Context as C, middlewareOutputFn as D, type ProcedureHandlerOptions as E, type ProcedureDef as F, isProcedure as G, createProcedureClient as H, type InferRouterInitialContext as I, type InferRouterInitialContexts as J, type InferRouterCurrentContexts as K, type Lazyable as L, type Middleware as M, type InferRouterInputs as N, type ORPCErrorConstructorMap as O, type ProcedureClientInterceptorOptions as P, type InferRouterOutputs as Q, type Router as R, type AnyRouter as a, Procedure as b, type ContextExtendsGuard as c, type MergedCurrentContext as d, type MergedInitialContext as e, type MapInputMiddleware as f, type CreateProcedureClientOptions as g, type ProcedureClient as h, type AnyMiddleware as i, type Lazy as j, type ProcedureHandler as k, type ORPCErrorConstructorMapItemOptions as l, mergeCurrentContext as m, type ORPCErrorConstructorMapItem as n, createORPCErrorConstructorMap as o, LAZY_SYMBOL as p, type LazyMeta as q, lazy as r, isLazy as s, getLazyMeta as t, unlazy as u, validateORPCError as v, type MiddlewareResult as w, type MiddlewareNextFnOptions as x, type MiddlewareNextFn as y, type MiddlewareOutputFn as z };