@orpc/server 0.0.0-next.9b9ade5 → 0.0.0-next.9cd4eb7
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.
- package/README.md +29 -22
- package/dist/adapters/aws-lambda/index.d.mts +45 -0
- package/dist/adapters/aws-lambda/index.d.ts +45 -0
- package/dist/adapters/aws-lambda/index.mjs +41 -0
- package/dist/adapters/bun-ws/index.d.mts +35 -0
- package/dist/adapters/bun-ws/index.d.ts +35 -0
- package/dist/adapters/bun-ws/index.mjs +47 -0
- package/dist/adapters/crossws/index.d.mts +32 -0
- package/dist/adapters/crossws/index.d.ts +32 -0
- package/dist/adapters/crossws/index.mjs +47 -0
- package/dist/adapters/fetch/index.d.mts +62 -10
- package/dist/adapters/fetch/index.d.ts +62 -10
- package/dist/adapters/fetch/index.mjs +109 -8
- package/dist/adapters/message-port/index.d.mts +30 -0
- package/dist/adapters/message-port/index.d.ts +30 -0
- package/dist/adapters/message-port/index.mjs +41 -0
- package/dist/adapters/node/index.d.mts +64 -21
- package/dist/adapters/node/index.d.ts +64 -21
- package/dist/adapters/node/index.mjs +87 -23
- package/dist/adapters/standard/index.d.mts +12 -10
- package/dist/adapters/standard/index.d.ts +12 -10
- package/dist/adapters/standard/index.mjs +5 -3
- package/dist/adapters/standard-peer/index.d.mts +14 -0
- package/dist/adapters/standard-peer/index.d.ts +14 -0
- package/dist/adapters/standard-peer/index.mjs +7 -0
- package/dist/adapters/websocket/index.d.mts +53 -0
- package/dist/adapters/websocket/index.d.ts +53 -0
- package/dist/adapters/websocket/index.mjs +69 -0
- package/dist/adapters/ws/index.d.mts +30 -0
- package/dist/adapters/ws/index.d.ts +30 -0
- package/dist/adapters/ws/index.mjs +39 -0
- package/dist/hibernation/index.d.mts +44 -0
- package/dist/hibernation/index.d.ts +44 -0
- package/dist/hibernation/index.mjs +65 -0
- package/dist/index.d.mts +634 -69
- package/dist/index.d.ts +634 -69
- package/dist/index.mjs +181 -36
- package/dist/plugins/index.d.mts +141 -16
- package/dist/plugins/index.d.ts +141 -16
- package/dist/plugins/index.mjs +172 -16
- package/dist/shared/{server.CPteJIPP.d.mts → server.6ohwBdwx.d.mts} +73 -24
- package/dist/shared/{server.CPteJIPP.d.ts → server.6ohwBdwx.d.ts} +73 -24
- package/dist/shared/server.B3dVpAsJ.d.mts +12 -0
- package/dist/shared/server.B6GspgNq.d.ts +12 -0
- package/dist/shared/server.BE3B4vij.d.ts +74 -0
- package/dist/shared/server.BW-nUGgA.mjs +36 -0
- package/dist/shared/server.BtQsqpPB.d.mts +74 -0
- package/dist/shared/{server.CSZRzcSW.mjs → server.CB8Snncu.mjs} +59 -31
- package/dist/shared/server.CKafa5G2.d.mts +12 -0
- package/dist/shared/server.CeW2jMCj.d.ts +12 -0
- package/dist/shared/{server.CMrS28Go.mjs → server.DLJzqnSX.mjs} +47 -25
- package/dist/shared/server.DZ5BIITo.mjs +9 -0
- package/dist/shared/server.DxNFsvpM.mjs +20 -0
- package/package.json +64 -20
- package/dist/adapters/hono/index.d.mts +0 -19
- package/dist/adapters/hono/index.d.ts +0 -19
- package/dist/adapters/hono/index.mjs +0 -32
- package/dist/adapters/next/index.d.mts +0 -26
- package/dist/adapters/next/index.d.ts +0 -26
- package/dist/adapters/next/index.mjs +0 -29
- package/dist/shared/server.CM3tWr3C.d.mts +0 -75
- package/dist/shared/server.Cq3B6PoL.mjs +0 -28
- package/dist/shared/server.DmW25ynm.d.ts +0 -75
- package/dist/shared/server.Q6ZmnTgO.mjs +0 -12
@@ -1,19 +1,19 @@
|
|
1
|
-
import { ORPCErrorCode, ORPCErrorOptions, ORPCError, ClientContext, Client } from '@orpc/client';
|
2
|
-
import {
|
3
|
-
import {
|
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, PromiseWithError, Value } from '@orpc/shared';
|
4
4
|
|
5
|
-
type Context = Record<
|
6
|
-
type
|
7
|
-
|
8
|
-
|
9
|
-
[K in keyof T]: IsNever<T[K]>;
|
10
|
-
}[keyof T] ? never : unknown;
|
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>;
|
11
9
|
|
12
10
|
type ORPCErrorConstructorMapItemOptions<TData> = Omit<ORPCErrorOptions<TData>, 'defined' | 'status'>;
|
13
11
|
type ORPCErrorConstructorMapItem<TCode extends ORPCErrorCode, TInData> = (...rest: MaybeOptionalOptions<ORPCErrorConstructorMapItemOptions<TInData>>) => ORPCError<TCode, TInData>;
|
14
12
|
type ORPCErrorConstructorMap<T extends ErrorMap> = {
|
15
13
|
[K in keyof T]: K extends ORPCErrorCode ? T[K] extends ErrorMapItem<infer UInputSchema> ? ORPCErrorConstructorMapItem<K, InferSchemaInput<UInputSchema>> : never : never;
|
16
14
|
};
|
15
|
+
declare function createORPCErrorConstructorMap<T extends ErrorMap>(errors: T): ORPCErrorConstructorMap<T>;
|
16
|
+
declare function validateORPCError(map: ErrorMap, error: ORPCError<any, any>): Promise<ORPCError<string, unknown>>;
|
17
17
|
|
18
18
|
declare const LAZY_SYMBOL: unique symbol;
|
19
19
|
interface LazyMeta {
|
@@ -28,6 +28,9 @@ interface Lazy<T> {
|
|
28
28
|
};
|
29
29
|
}
|
30
30
|
type Lazyable<T> = T | Lazy<T>;
|
31
|
+
/**
|
32
|
+
* Create a lazy thing.
|
33
|
+
*/
|
31
34
|
declare function lazy<T>(loader: () => Promise<{
|
32
35
|
default: T;
|
33
36
|
}>, meta?: LazyMeta): Lazy<T>;
|
@@ -51,12 +54,20 @@ interface ProcedureHandler<TCurrentContext extends Context, TInput, THandlerOutp
|
|
51
54
|
}
|
52
55
|
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
56
|
__initialContext?: (type: TInitialContext) => unknown;
|
54
|
-
middlewares: AnyMiddleware[];
|
57
|
+
middlewares: readonly AnyMiddleware[];
|
55
58
|
inputValidationIndex: number;
|
56
59
|
outputValidationIndex: number;
|
57
60
|
handler: ProcedureHandler<TCurrentContext, any, any, any, any>;
|
58
61
|
}
|
62
|
+
/**
|
63
|
+
* This class represents a procedure.
|
64
|
+
*
|
65
|
+
* @see {@link https://orpc.unnoq.com/docs/procedure Procedure Docs}
|
66
|
+
*/
|
59
67
|
declare class Procedure<TInitialContext extends Context, TCurrentContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> {
|
68
|
+
/**
|
69
|
+
* This property holds the defined options.
|
70
|
+
*/
|
60
71
|
'~orpc': ProcedureDef<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
61
72
|
constructor(def: ProcedureDef<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, TErrorMap, TMeta>);
|
62
73
|
}
|
@@ -72,8 +83,8 @@ type MiddlewareNextFnOptions<TOutContext extends Context> = Record<never, never>
|
|
72
83
|
} : {
|
73
84
|
context: TOutContext;
|
74
85
|
};
|
75
|
-
interface MiddlewareNextFn<
|
76
|
-
<U extends Context
|
86
|
+
interface MiddlewareNextFn<TOutput> {
|
87
|
+
<U extends Context = Record<never, never>>(...rest: MaybeOptionalOptions<MiddlewareNextFnOptions<U>>): MiddlewareResult<U, TOutput>;
|
77
88
|
}
|
78
89
|
interface MiddlewareOutputFn<TOutput> {
|
79
90
|
(output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
|
@@ -84,9 +95,14 @@ interface MiddlewareOptions<TInContext extends Context, TOutput, TErrorConstruct
|
|
84
95
|
procedure: Procedure<Context, Context, AnySchema, AnySchema, ErrorMap, TMeta>;
|
85
96
|
signal?: AbortSignal;
|
86
97
|
lastEventId: string | undefined;
|
87
|
-
next: MiddlewareNextFn<
|
98
|
+
next: MiddlewareNextFn<TOutput>;
|
88
99
|
errors: TErrorConstructorMap;
|
89
100
|
}
|
101
|
+
/**
|
102
|
+
* A function that represents a middleware.
|
103
|
+
*
|
104
|
+
* @see {@link https://orpc.unnoq.com/docs/middleware Middleware Docs}
|
105
|
+
*/
|
90
106
|
interface Middleware<TInContext extends Context, TOutContext extends Context, TInput, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
|
91
107
|
(options: MiddlewareOptions<TInContext, TOutput, TErrorConstructorMap, TMeta>, input: TInput, output: MiddlewareOutputFn<TOutput>): Promisable<MiddlewareResult<TOutContext, TOutput>>;
|
92
108
|
}
|
@@ -97,47 +113,80 @@ interface MapInputMiddleware<TInput, TMappedInput> {
|
|
97
113
|
declare function middlewareOutputFn<TOutput>(output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
|
98
114
|
|
99
115
|
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,
|
116
|
+
interface ProcedureClientInterceptorOptions<TInitialContext extends Context, TErrorMap extends ErrorMap, TMeta extends Meta> {
|
101
117
|
context: TInitialContext;
|
102
|
-
input:
|
118
|
+
input: unknown;
|
103
119
|
errors: ORPCErrorConstructorMap<TErrorMap>;
|
104
120
|
path: readonly string[];
|
105
121
|
procedure: Procedure<Context, Context, AnySchema, AnySchema, ErrorMap, TMeta>;
|
106
122
|
signal?: AbortSignal;
|
107
123
|
lastEventId: string | undefined;
|
108
124
|
}
|
109
|
-
|
110
|
-
* Options for creating a procedure caller with comprehensive type safety
|
111
|
-
*/
|
112
|
-
type CreateProcedureClientOptions<TInitialContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext> = {
|
125
|
+
type CreateProcedureClientOptions<TInitialContext extends Context, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext> = {
|
113
126
|
/**
|
114
127
|
* This is helpful for logging and analytics.
|
115
128
|
*/
|
116
129
|
path?: readonly string[];
|
117
|
-
interceptors?: Interceptor<ProcedureClientInterceptorOptions<TInitialContext,
|
130
|
+
interceptors?: Interceptor<ProcedureClientInterceptorOptions<TInitialContext, TErrorMap, TMeta>, PromiseWithError<InferSchemaOutput<TOutputSchema>, ErrorFromErrorMap<TErrorMap>>>[];
|
118
131
|
} & (Record<never, never> extends TInitialContext ? {
|
119
|
-
context?: Value<TInitialContext
|
132
|
+
context?: Value<Promisable<TInitialContext>, [clientContext: TClientContext]>;
|
120
133
|
} : {
|
121
|
-
context: Value<TInitialContext
|
134
|
+
context: Value<Promisable<TInitialContext>, [clientContext: TClientContext]>;
|
122
135
|
});
|
123
|
-
|
136
|
+
/**
|
137
|
+
* Create Server-side client from a procedure.
|
138
|
+
*
|
139
|
+
* @see {@link https://orpc.unnoq.com/docs/client/server-side Server-side Client Docs}
|
140
|
+
*/
|
141
|
+
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
142
|
|
143
|
+
/**
|
144
|
+
* Represents a router, which defines a hierarchical structure of procedures.
|
145
|
+
*
|
146
|
+
* @info A procedure is a router too.
|
147
|
+
* @see {@link https://orpc.unnoq.com/docs/contract-first/define-contract#contract-router Contract Router Docs}
|
148
|
+
*/
|
125
149
|
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
150
|
[K in keyof T]: T[K] extends AnyContractRouter ? Lazyable<Router<T[K], TInitialContext>> : never;
|
127
151
|
};
|
128
152
|
type AnyRouter = Router<any, any>;
|
129
153
|
type InferRouterInitialContext<T extends AnyRouter> = T extends Router<any, infer UInitialContext> ? UInitialContext : never;
|
154
|
+
/**
|
155
|
+
* Infer all initial context of the router.
|
156
|
+
*
|
157
|
+
* @info A procedure is a router too.
|
158
|
+
* @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
|
159
|
+
*/
|
130
160
|
type InferRouterInitialContexts<T extends AnyRouter> = T extends Procedure<infer UInitialContext, any, any, any, any, any> ? UInitialContext : {
|
131
161
|
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInitialContexts<U> : never;
|
132
162
|
};
|
163
|
+
/**
|
164
|
+
* Infer all current context of the router.
|
165
|
+
*
|
166
|
+
* @info A procedure is a router too.
|
167
|
+
* @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
|
168
|
+
*/
|
133
169
|
type InferRouterCurrentContexts<T extends AnyRouter> = T extends Procedure<any, infer UCurrentContext, any, any, any, any> ? UCurrentContext : {
|
134
170
|
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterCurrentContexts<U> : never;
|
135
171
|
};
|
172
|
+
/**
|
173
|
+
* Infer all router inputs
|
174
|
+
*
|
175
|
+
* @info A procedure is a router too.
|
176
|
+
* @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
|
177
|
+
*/
|
136
178
|
type InferRouterInputs<T extends AnyRouter> = T extends Procedure<any, any, infer UInputSchema, any, any, any> ? InferSchemaInput<UInputSchema> : {
|
137
179
|
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInputs<U> : never;
|
138
180
|
};
|
181
|
+
/**
|
182
|
+
* Infer all router outputs
|
183
|
+
*
|
184
|
+
* @info A procedure is a router too.
|
185
|
+
* @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
|
186
|
+
*/
|
139
187
|
type InferRouterOutputs<T extends AnyRouter> = T extends Procedure<any, any, any, infer UOutputSchema, any, any> ? InferSchemaOutput<UOutputSchema> : {
|
140
188
|
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterOutputs<U> : never;
|
141
189
|
};
|
142
190
|
|
143
|
-
export {
|
191
|
+
export { middlewareOutputFn as B, isProcedure as F, createProcedureClient as G, Procedure as b, mergeCurrentContext as m, createORPCErrorConstructorMap as n, LAZY_SYMBOL as o, lazy as q, isLazy as r, getLazyMeta as s, unlazy as u, validateORPCError as v };
|
192
|
+
export type { AnyProcedure as A, Context as C, ProcedureHandlerOptions as D, ProcedureDef as E, InferRouterInitialContexts as H, InferRouterInitialContext as I, InferRouterCurrentContexts as J, InferRouterInputs as K, Lazyable as L, Middleware as M, InferRouterOutputs as N, ORPCErrorConstructorMap as O, ProcedureClientInterceptorOptions as P, Router as R, AnyRouter as a, MergedInitialContext as c, MergedCurrentContext as d, MapInputMiddleware as e, CreateProcedureClientOptions as f, ProcedureClient as g, AnyMiddleware as h, Lazy as i, ProcedureHandler as j, ORPCErrorConstructorMapItemOptions as k, ORPCErrorConstructorMapItem as l, LazyMeta as p, MiddlewareResult as t, MiddlewareNextFnOptions as w, MiddlewareNextFn as x, MiddlewareOutputFn as y, MiddlewareOptions as z };
|
@@ -0,0 +1,12 @@
|
|
1
|
+
import { C as Context } from './server.6ohwBdwx.mjs';
|
2
|
+
import { b as StandardHandleOptions } from './server.BtQsqpPB.mjs';
|
3
|
+
|
4
|
+
type FriendlyStandardHandleOptions<T extends Context> = Omit<StandardHandleOptions<T>, 'context'> & (Record<never, never> extends T ? {
|
5
|
+
context?: T;
|
6
|
+
} : {
|
7
|
+
context: T;
|
8
|
+
});
|
9
|
+
declare function resolveFriendlyStandardHandleOptions<T extends Context>(options: FriendlyStandardHandleOptions<T>): StandardHandleOptions<T>;
|
10
|
+
|
11
|
+
export { resolveFriendlyStandardHandleOptions as r };
|
12
|
+
export type { FriendlyStandardHandleOptions as F };
|
@@ -0,0 +1,12 @@
|
|
1
|
+
import { StandardRPCJsonSerializerOptions } from '@orpc/client/standard';
|
2
|
+
import { C as Context, R as Router } from './server.6ohwBdwx.js';
|
3
|
+
import { S as StandardHandlerOptions, a as StandardHandler } from './server.BE3B4vij.js';
|
4
|
+
|
5
|
+
interface StandardRPCHandlerOptions<T extends Context> extends StandardHandlerOptions<T>, StandardRPCJsonSerializerOptions {
|
6
|
+
}
|
7
|
+
declare class StandardRPCHandler<T extends Context> extends StandardHandler<T> {
|
8
|
+
constructor(router: Router<any, T>, options?: StandardRPCHandlerOptions<T>);
|
9
|
+
}
|
10
|
+
|
11
|
+
export { StandardRPCHandler as a };
|
12
|
+
export type { StandardRPCHandlerOptions as S };
|
@@ -0,0 +1,74 @@
|
|
1
|
+
import { HTTPPath, ORPCError } from '@orpc/client';
|
2
|
+
import { Meta } from '@orpc/contract';
|
3
|
+
import { Interceptor } from '@orpc/shared';
|
4
|
+
import { StandardResponse, StandardLazyRequest } from '@orpc/standard-server';
|
5
|
+
import { C as Context, R as Router, a as AnyRouter, A as AnyProcedure, P as ProcedureClientInterceptorOptions } from './server.6ohwBdwx.js';
|
6
|
+
|
7
|
+
interface StandardHandlerPlugin<T extends Context> {
|
8
|
+
order?: number;
|
9
|
+
init?(options: StandardHandlerOptions<T>, router: Router<any, T>): 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>, router: Router<any, 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>, Promise<StandardHandleResult>>[];
|
53
|
+
/**
|
54
|
+
* Interceptors at the root level, helpful when you want override the request/response
|
55
|
+
*/
|
56
|
+
rootInterceptors?: Interceptor<StandardHandlerInterceptorOptions<TContext>, Promise<StandardHandleResult>>[];
|
57
|
+
/**
|
58
|
+
*
|
59
|
+
* Interceptors for procedure client.
|
60
|
+
*/
|
61
|
+
clientInterceptors?: Interceptor<ProcedureClientInterceptorOptions<TContext, Record<never, never>, Meta>, Promise<unknown>>[];
|
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 a };
|
74
|
+
export type { StandardHandlerOptions as S, StandardHandleOptions as b, StandardHandlerPlugin as c, StandardCodec as d, StandardParams as e, StandardMatcher as f, StandardMatchResult as g, StandardHandleResult as h, StandardHandlerInterceptorOptions as i };
|
@@ -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 };
|
@@ -0,0 +1,74 @@
|
|
1
|
+
import { HTTPPath, ORPCError } from '@orpc/client';
|
2
|
+
import { Meta } from '@orpc/contract';
|
3
|
+
import { Interceptor } from '@orpc/shared';
|
4
|
+
import { StandardResponse, StandardLazyRequest } from '@orpc/standard-server';
|
5
|
+
import { C as Context, R as Router, a as AnyRouter, A as AnyProcedure, P as ProcedureClientInterceptorOptions } from './server.6ohwBdwx.mjs';
|
6
|
+
|
7
|
+
interface StandardHandlerPlugin<T extends Context> {
|
8
|
+
order?: number;
|
9
|
+
init?(options: StandardHandlerOptions<T>, router: Router<any, T>): 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>, router: Router<any, 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>, Promise<StandardHandleResult>>[];
|
53
|
+
/**
|
54
|
+
* Interceptors at the root level, helpful when you want override the request/response
|
55
|
+
*/
|
56
|
+
rootInterceptors?: Interceptor<StandardHandlerInterceptorOptions<TContext>, Promise<StandardHandleResult>>[];
|
57
|
+
/**
|
58
|
+
*
|
59
|
+
* Interceptors for procedure client.
|
60
|
+
*/
|
61
|
+
clientInterceptors?: Interceptor<ProcedureClientInterceptorOptions<TContext, Record<never, never>, Meta>, Promise<unknown>>[];
|
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 a };
|
74
|
+
export type { StandardHandlerOptions as S, StandardHandleOptions as b, StandardHandlerPlugin as c, StandardCodec as d, StandardParams as e, StandardMatcher as f, StandardMatchResult as g, StandardHandleResult as h, StandardHandlerInterceptorOptions as i };
|
@@ -1,51 +1,69 @@
|
|
1
|
+
import { toHttpPath, StandardRPCJsonSerializer, StandardRPCSerializer } from '@orpc/client/standard';
|
1
2
|
import { ORPCError, toORPCError } from '@orpc/client';
|
2
|
-
import {
|
3
|
-
import {
|
4
|
-
import { c as createProcedureClient, t as traverseContractProcedures,
|
3
|
+
import { toArray, intercept, parseEmptyableJSON, NullProtoObj } from '@orpc/shared';
|
4
|
+
import { flattenHeader } from '@orpc/standard-server';
|
5
|
+
import { c as createProcedureClient, t as traverseContractProcedures, i as isProcedure, u as unlazy, g as getRouter, a as createContractedProcedure } from './server.DLJzqnSX.mjs';
|
6
|
+
|
7
|
+
class CompositeStandardHandlerPlugin {
|
8
|
+
plugins;
|
9
|
+
constructor(plugins = []) {
|
10
|
+
this.plugins = [...plugins].sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
|
11
|
+
}
|
12
|
+
init(options, router) {
|
13
|
+
for (const plugin of this.plugins) {
|
14
|
+
plugin.init?.(options, router);
|
15
|
+
}
|
16
|
+
}
|
17
|
+
}
|
5
18
|
|
6
19
|
class StandardHandler {
|
7
20
|
constructor(router, matcher, codec, options) {
|
8
21
|
this.matcher = matcher;
|
9
22
|
this.codec = codec;
|
10
|
-
|
11
|
-
|
12
|
-
this.
|
23
|
+
const plugins = new CompositeStandardHandlerPlugin(options.plugins);
|
24
|
+
plugins.init(options, router);
|
25
|
+
this.interceptors = toArray(options.interceptors);
|
26
|
+
this.clientInterceptors = toArray(options.clientInterceptors);
|
27
|
+
this.rootInterceptors = toArray(options.rootInterceptors);
|
13
28
|
this.matcher.init(router);
|
14
29
|
}
|
15
|
-
|
16
|
-
|
30
|
+
interceptors;
|
31
|
+
clientInterceptors;
|
32
|
+
rootInterceptors;
|
33
|
+
async handle(request, options) {
|
34
|
+
const prefix = options.prefix?.replace(/\/$/, "") || void 0;
|
35
|
+
if (prefix && !request.url.pathname.startsWith(`${prefix}/`) && request.url.pathname !== prefix) {
|
36
|
+
return { matched: false, response: void 0 };
|
37
|
+
}
|
17
38
|
return intercept(
|
18
|
-
this.
|
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
|
-
},
|
39
|
+
this.rootInterceptors,
|
40
|
+
{ ...options, request, prefix },
|
25
41
|
async (interceptorOptions) => {
|
26
42
|
let isDecoding = false;
|
27
43
|
try {
|
28
44
|
return await intercept(
|
29
|
-
this.
|
45
|
+
this.interceptors,
|
30
46
|
interceptorOptions,
|
31
|
-
async (
|
32
|
-
const method =
|
33
|
-
const url =
|
34
|
-
const pathname =
|
35
|
-
const match = await this.matcher.match(method, pathname);
|
47
|
+
async ({ request: request2, context, prefix: prefix2 }) => {
|
48
|
+
const method = request2.method;
|
49
|
+
const url = request2.url;
|
50
|
+
const pathname = prefix2 ? url.pathname.replace(prefix2, "") : url.pathname;
|
51
|
+
const match = await this.matcher.match(method, `/${pathname.replace(/^\/|\/$/g, "")}`);
|
36
52
|
if (!match) {
|
37
53
|
return { matched: false, response: void 0 };
|
38
54
|
}
|
39
55
|
const client = createProcedureClient(match.procedure, {
|
40
|
-
context
|
56
|
+
context,
|
41
57
|
path: match.path,
|
42
|
-
interceptors: this.
|
58
|
+
interceptors: this.clientInterceptors
|
43
59
|
});
|
44
60
|
isDecoding = true;
|
45
|
-
const input = await this.codec.decode(
|
61
|
+
const input = await this.codec.decode(request2, match.params, match.procedure);
|
46
62
|
isDecoding = false;
|
47
|
-
const
|
48
|
-
|
63
|
+
const output = await client(input, {
|
64
|
+
signal: request2.signal,
|
65
|
+
lastEventId: flattenHeader(request2.headers["last-event-id"])
|
66
|
+
});
|
49
67
|
const response = this.codec.encode(output, match.procedure);
|
50
68
|
return {
|
51
69
|
matched: true,
|
@@ -54,7 +72,7 @@ class StandardHandler {
|
|
54
72
|
}
|
55
73
|
);
|
56
74
|
} catch (e) {
|
57
|
-
const error = isDecoding ? new ORPCError("BAD_REQUEST", {
|
75
|
+
const error = isDecoding && !(e instanceof ORPCError) ? new ORPCError("BAD_REQUEST", {
|
58
76
|
message: `Malformed request. Ensure the request body is properly formatted and the 'Content-Type' header is set correctly.`,
|
59
77
|
cause: e
|
60
78
|
}) : toORPCError(e);
|
@@ -69,7 +87,7 @@ class StandardHandler {
|
|
69
87
|
}
|
70
88
|
}
|
71
89
|
|
72
|
-
class
|
90
|
+
class StandardRPCCodec {
|
73
91
|
constructor(serializer) {
|
74
92
|
this.serializer = serializer;
|
75
93
|
}
|
@@ -93,8 +111,8 @@ class RPCCodec {
|
|
93
111
|
}
|
94
112
|
}
|
95
113
|
|
96
|
-
class
|
97
|
-
tree =
|
114
|
+
class StandardRPCMatcher {
|
115
|
+
tree = new NullProtoObj();
|
98
116
|
pendingRouters = [];
|
99
117
|
init(router, path = []) {
|
100
118
|
const laziedOptions = traverseContractProcedures({ router, path }, ({ path: path2, contract }) => {
|
@@ -155,4 +173,14 @@ class RPCMatcher {
|
|
155
173
|
}
|
156
174
|
}
|
157
175
|
|
158
|
-
|
176
|
+
class StandardRPCHandler extends StandardHandler {
|
177
|
+
constructor(router, options = {}) {
|
178
|
+
const jsonSerializer = new StandardRPCJsonSerializer(options);
|
179
|
+
const serializer = new StandardRPCSerializer(jsonSerializer);
|
180
|
+
const matcher = new StandardRPCMatcher();
|
181
|
+
const codec = new StandardRPCCodec(serializer);
|
182
|
+
super(router, matcher, codec, options);
|
183
|
+
}
|
184
|
+
}
|
185
|
+
|
186
|
+
export { CompositeStandardHandlerPlugin as C, StandardHandler as S, StandardRPCCodec as a, StandardRPCHandler as b, StandardRPCMatcher as c };
|
@@ -0,0 +1,12 @@
|
|
1
|
+
import { StandardRPCJsonSerializerOptions } from '@orpc/client/standard';
|
2
|
+
import { C as Context, R as Router } from './server.6ohwBdwx.mjs';
|
3
|
+
import { S as StandardHandlerOptions, a as StandardHandler } from './server.BtQsqpPB.mjs';
|
4
|
+
|
5
|
+
interface StandardRPCHandlerOptions<T extends Context> extends StandardHandlerOptions<T>, StandardRPCJsonSerializerOptions {
|
6
|
+
}
|
7
|
+
declare class StandardRPCHandler<T extends Context> extends StandardHandler<T> {
|
8
|
+
constructor(router: Router<any, T>, options?: StandardRPCHandlerOptions<T>);
|
9
|
+
}
|
10
|
+
|
11
|
+
export { StandardRPCHandler as a };
|
12
|
+
export type { StandardRPCHandlerOptions as S };
|
@@ -0,0 +1,12 @@
|
|
1
|
+
import { C as Context } from './server.6ohwBdwx.js';
|
2
|
+
import { b as StandardHandleOptions } from './server.BE3B4vij.js';
|
3
|
+
|
4
|
+
type FriendlyStandardHandleOptions<T extends Context> = Omit<StandardHandleOptions<T>, 'context'> & (Record<never, never> extends T ? {
|
5
|
+
context?: T;
|
6
|
+
} : {
|
7
|
+
context: T;
|
8
|
+
});
|
9
|
+
declare function resolveFriendlyStandardHandleOptions<T extends Context>(options: FriendlyStandardHandleOptions<T>): StandardHandleOptions<T>;
|
10
|
+
|
11
|
+
export { resolveFriendlyStandardHandleOptions as r };
|
12
|
+
export type { FriendlyStandardHandleOptions as F };
|