@orpc/server 1.14.5 → 2.0.0-beta.1
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 +75 -136
- package/dist/adapters/crossws/index.d.mts +42 -21
- package/dist/adapters/crossws/index.d.ts +42 -21
- package/dist/adapters/crossws/index.mjs +37 -18
- package/dist/adapters/fetch/index.d.mts +83 -67
- package/dist/adapters/fetch/index.d.ts +83 -67
- package/dist/adapters/fetch/index.mjs +131 -106
- package/dist/adapters/message-port/index.d.mts +51 -34
- package/dist/adapters/message-port/index.d.ts +51 -34
- package/dist/adapters/message-port/index.mjs +73 -38
- package/dist/adapters/node/index.d.mts +82 -60
- package/dist/adapters/node/index.d.ts +82 -60
- package/dist/adapters/node/index.mjs +127 -98
- package/dist/adapters/standard/index.d.mts +16 -18
- package/dist/adapters/standard/index.d.ts +16 -18
- package/dist/adapters/standard/index.mjs +5 -5
- package/dist/adapters/standard-peer/index.d.mts +12 -14
- package/dist/adapters/standard-peer/index.d.ts +12 -14
- package/dist/adapters/standard-peer/index.mjs +2 -21
- package/dist/adapters/websocket/index.d.mts +39 -34
- package/dist/adapters/websocket/index.d.ts +39 -34
- package/dist/adapters/websocket/index.mjs +42 -33
- package/dist/extensions/callable.d.mts +10 -0
- package/dist/extensions/callable.d.ts +10 -0
- package/dist/extensions/callable.mjs +11 -0
- package/dist/helpers/index.d.mts +2 -2
- package/dist/helpers/index.d.ts +2 -2
- package/dist/helpers/index.mjs +1 -1
- package/dist/index.d.mts +163 -770
- package/dist/index.d.ts +163 -770
- package/dist/index.mjs +296 -403
- package/dist/plugins/index.d.mts +105 -143
- package/dist/plugins/index.d.ts +105 -143
- package/dist/plugins/index.mjs +232 -255
- package/dist/shared/server.BB_Ik9Ph.d.mts +104 -0
- package/dist/shared/server.BL22TloH.d.mts +184 -0
- package/dist/shared/server.BL22TloH.d.ts +184 -0
- package/dist/shared/server.B_U9y00a.d.mts +66 -0
- package/dist/shared/server.BsNNjG5J.d.mts +61 -0
- package/dist/shared/server.BwHnWUuN.mjs +222 -0
- package/dist/shared/server.CX4vUnDk.mjs +11 -0
- package/dist/shared/server.CjOb6ItT.mjs +41 -0
- package/dist/shared/server.CrlKQucM.mjs +233 -0
- package/dist/shared/server.D_QauotT.mjs +30 -0
- package/dist/shared/server.EOHJ3NJr.d.ts +104 -0
- package/dist/shared/server.GDpX6Df8.mjs +271 -0
- package/dist/shared/server.Pa0F03f_.d.ts +61 -0
- package/dist/shared/server.T9F3bzZx.d.ts +66 -0
- package/dist/shared/{server.DZ5BIITo.mjs → server.W91HSRkE.mjs} +2 -2
- package/package.json +26 -55
- package/dist/adapters/aws-lambda/index.d.mts +0 -46
- package/dist/adapters/aws-lambda/index.d.ts +0 -46
- package/dist/adapters/aws-lambda/index.mjs +0 -40
- package/dist/adapters/bun-ws/index.d.mts +0 -36
- package/dist/adapters/bun-ws/index.d.ts +0 -36
- package/dist/adapters/bun-ws/index.mjs +0 -47
- package/dist/adapters/fastify/index.d.mts +0 -53
- package/dist/adapters/fastify/index.d.ts +0 -53
- package/dist/adapters/fastify/index.mjs +0 -52
- package/dist/adapters/ws/index.d.mts +0 -31
- package/dist/adapters/ws/index.d.ts +0 -31
- package/dist/adapters/ws/index.mjs +0 -37
- package/dist/hibernation/index.d.mts +0 -44
- package/dist/hibernation/index.d.ts +0 -44
- package/dist/hibernation/index.mjs +0 -65
- package/dist/shared/server.7cEtMB30.d.ts +0 -74
- package/dist/shared/server.B8gYOD5g.d.mts +0 -12
- package/dist/shared/server.BqadksTP.d.mts +0 -74
- package/dist/shared/server.C8_sRzQB.d.mts +0 -42
- package/dist/shared/server.ChUyt5-i.d.mts +0 -32
- package/dist/shared/server.ChyoA9XY.d.ts +0 -42
- package/dist/shared/server.DEBcqOjg.mjs +0 -418
- package/dist/shared/server.EfTOZ2Q7.d.ts +0 -12
- package/dist/shared/server.TEVCLCFC.mjs +0 -39
- package/dist/shared/server.ZxHCEN1h.mjs +0 -226
- package/dist/shared/server.qKsRrdxW.d.mts +0 -193
- package/dist/shared/server.qKsRrdxW.d.ts +0 -193
- package/dist/shared/server.yoEB3Fx4.d.ts +0 -32
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { ORPCErrorCode, ORPCErrorOptions, ORPCError, AnyORPCError, ClientContext, Client } from '@orpc/client';
|
|
2
|
+
import { ErrorMap, ErrorMapItem, InferSchemaInput, AnyMetaPlugin, AnySchema, ProcedureContractDefinition, Meta, InferSchemaOutput, ORPCErrorFromErrorMap } from '@orpc/contract';
|
|
3
|
+
import { MaybeOptionalOptions, Promisable, Interceptor, PromiseWithError, ThrowableError, Value } from '@orpc/shared';
|
|
4
|
+
|
|
5
|
+
interface Context {
|
|
6
|
+
[key: PropertyKey]: any;
|
|
7
|
+
}
|
|
8
|
+
type MergedInitialContext<TInitial extends Context, TOutContext extends Context, TInContext extends Context> = Exclude<keyof TInContext, keyof TInitial | keyof TOutContext> extends never ? TInitial : TInitial & Omit<TInContext, keyof TInitial | keyof TOutContext>;
|
|
9
|
+
type MergedContext<TCurrent extends Context, TOutContext extends Context> = keyof TOutContext extends never ? TCurrent : Omit<TCurrent, keyof TOutContext> & TOutContext;
|
|
10
|
+
|
|
11
|
+
type ORPCErrorConstructorMapItemOptions<TData> = Omit<ORPCErrorOptions<TData>, 'status'>;
|
|
12
|
+
interface ORPCErrorConstructorMapItem<TCode extends ORPCErrorCode, TInData> {
|
|
13
|
+
(...rest: MaybeOptionalOptions<ORPCErrorConstructorMapItemOptions<TInData>>): ORPCError<TCode, TInData>;
|
|
14
|
+
}
|
|
15
|
+
type ORPCErrorConstructorMap<T extends ErrorMap> = {
|
|
16
|
+
[K in keyof T]: K extends ORPCErrorCode ? T[K] extends ErrorMapItem<infer UInputSchema> ? ORPCErrorConstructorMapItem<K, InferSchemaInput<UInputSchema>> : never : never;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Creates a map of ORPC error constructors.
|
|
20
|
+
*
|
|
21
|
+
* The returned object is a `Proxy` that allows access to arbitrary error codes:
|
|
22
|
+
* - If the code exists in the provided `errorMap`, the corresponding constructor
|
|
23
|
+
* will create a **defined** `ORPCError`.
|
|
24
|
+
* - If the code does not exist, a fallback `ORPCError` constructor is returned.
|
|
25
|
+
*
|
|
26
|
+
* The `in` operator can be used to check whether an error code is explicitly
|
|
27
|
+
* defined in the map.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* const errorMap = createORPCErrorConstructorMap({
|
|
32
|
+
* NOT_FOUND: {
|
|
33
|
+
* status: 404,
|
|
34
|
+
* message: 'Not Found',
|
|
35
|
+
* },
|
|
36
|
+
* })
|
|
37
|
+
*
|
|
38
|
+
* if ('NOT_FOUND' in errorMap) {
|
|
39
|
+
* const error = errorMap.NOT_FOUND()
|
|
40
|
+
* }
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
declare function createORPCErrorConstructorMap<T extends ErrorMap>(errorMap: T): ORPCErrorConstructorMap<T>;
|
|
44
|
+
|
|
45
|
+
type MiddlewareResult<TOutContext extends Context, TOutput> = Promisable<{
|
|
46
|
+
output: TOutput;
|
|
47
|
+
context: TOutContext;
|
|
48
|
+
}>;
|
|
49
|
+
type MiddlewareNextOptions<TOutContext extends Context> = object extends TOutContext ? {
|
|
50
|
+
context?: TOutContext;
|
|
51
|
+
} : {
|
|
52
|
+
context: TOutContext;
|
|
53
|
+
};
|
|
54
|
+
interface MiddlewareNext<TOutput> {
|
|
55
|
+
<T extends Context = object>(...rest: MaybeOptionalOptions<MiddlewareNextOptions<T>>): MiddlewareResult<T, TOutput>;
|
|
56
|
+
}
|
|
57
|
+
type MiddlewareDoneOptions<TOutContext, TOutput> = (object extends TOutContext ? {
|
|
58
|
+
context?: TOutContext;
|
|
59
|
+
} : {
|
|
60
|
+
context: TOutContext;
|
|
61
|
+
}) & {
|
|
62
|
+
output: TOutput;
|
|
63
|
+
};
|
|
64
|
+
interface MiddlewareDone<TOutput> {
|
|
65
|
+
/**
|
|
66
|
+
* Create a successful result and terminate the middleware chain early.
|
|
67
|
+
*/
|
|
68
|
+
<TOutContext extends Context = object>(...rest: MaybeOptionalOptions<MiddlewareDoneOptions<TOutContext, TOutput>>): MiddlewareResult<TOutContext, TOutput>;
|
|
69
|
+
}
|
|
70
|
+
interface MiddlewareOptions<TInContext extends Context, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>> {
|
|
71
|
+
context: TInContext;
|
|
72
|
+
path: string[];
|
|
73
|
+
procedure: AnyProcedure;
|
|
74
|
+
signal?: AbortSignal | undefined;
|
|
75
|
+
lastEventId: string | undefined;
|
|
76
|
+
/**
|
|
77
|
+
* Invoke to continue the middleware chain.
|
|
78
|
+
*/
|
|
79
|
+
next: MiddlewareNext<TOutput>;
|
|
80
|
+
errors: TErrorConstructorMap;
|
|
81
|
+
}
|
|
82
|
+
interface MiddlewareDefinition<TErrorMap extends ErrorMap> {
|
|
83
|
+
errorMap?: TErrorMap | undefined;
|
|
84
|
+
metaPlugins?: AnyMetaPlugin[] | undefined;
|
|
85
|
+
}
|
|
86
|
+
interface Middleware<TInContext extends Context, TOutContext extends Context, TInput, TOutput, TErrorMap extends ErrorMap> {
|
|
87
|
+
/** this property should be optional to support inline middleware */
|
|
88
|
+
'~orpc'?: MiddlewareDefinition<TErrorMap> | undefined;
|
|
89
|
+
(opts: MiddlewareOptions<TInContext, TOutput, ORPCErrorConstructorMap<TErrorMap>>, input: TInput, done: MiddlewareDone<TOutput>): Promisable<MiddlewareResult<TOutContext, TOutput>>;
|
|
90
|
+
}
|
|
91
|
+
type AnyMiddleware = Middleware<any, any, any, any, any>;
|
|
92
|
+
|
|
93
|
+
interface ProcedureHandlerOptions<TCurrentContext extends Context, TInput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>> {
|
|
94
|
+
context: TCurrentContext;
|
|
95
|
+
input: TInput;
|
|
96
|
+
path: string[];
|
|
97
|
+
procedure: Procedure<Context, Context, AnySchema, AnySchema, ErrorMap, any>;
|
|
98
|
+
signal?: AbortSignal | undefined;
|
|
99
|
+
lastEventId?: string | undefined;
|
|
100
|
+
errors: TErrorConstructorMap;
|
|
101
|
+
}
|
|
102
|
+
interface ProcedureHandler<TCurrentContext extends Context, TInput, THandlerOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>> {
|
|
103
|
+
(opts: ProcedureHandlerOptions<TCurrentContext, TInput, TErrorConstructorMap>, input: TInput): Promisable<THandlerOutput>;
|
|
104
|
+
}
|
|
105
|
+
interface OrderedMiddleware {
|
|
106
|
+
/**
|
|
107
|
+
* Snapshot of `inputSchemas.length`
|
|
108
|
+
* at the time this middleware was used.
|
|
109
|
+
*
|
|
110
|
+
* @default 0
|
|
111
|
+
*/
|
|
112
|
+
inputSchemasLengthAtUse?: number | undefined;
|
|
113
|
+
/**
|
|
114
|
+
* Snapshot of `outputSchemas.length`
|
|
115
|
+
* at the time this middleware was used.
|
|
116
|
+
*
|
|
117
|
+
* @default 0
|
|
118
|
+
*/
|
|
119
|
+
outputSchemasLengthAtUse?: number | undefined;
|
|
120
|
+
middleware: AnyMiddleware;
|
|
121
|
+
}
|
|
122
|
+
interface ProcedureDefinition<TInitialContext extends Context, TInjectedContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TReturnedError extends AnyORPCError> extends ProcedureContractDefinition<TInputSchema, TOutputSchema, TErrorMap> {
|
|
123
|
+
__TInitialContext?: (type: TInitialContext) => unknown;
|
|
124
|
+
__TInjectedContext?: (type: TInjectedContext) => unknown;
|
|
125
|
+
__TReturnedError?: () => TReturnedError;
|
|
126
|
+
/**
|
|
127
|
+
* When enabled, errors returned (not thrown) by the handler are passed through as-is,
|
|
128
|
+
* rather than being transformed into inferrable errors.
|
|
129
|
+
*
|
|
130
|
+
* This is intended for the contract-first approach, where the procedure adheres to an
|
|
131
|
+
* external contract and returned errors should not affect the inferred contract types.
|
|
132
|
+
*
|
|
133
|
+
* @default false
|
|
134
|
+
*/
|
|
135
|
+
opaqueReturnedErrors?: boolean | undefined;
|
|
136
|
+
orderedMiddlewares: OrderedMiddleware[];
|
|
137
|
+
handler: ProcedureHandler<any, any, any, any>;
|
|
138
|
+
}
|
|
139
|
+
declare class Procedure<TInitialContext extends Context, TInjectedContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TReturnedError extends AnyORPCError> {
|
|
140
|
+
'~orpc': ProcedureDefinition<TInitialContext, TInjectedContext, TInputSchema, TOutputSchema, TErrorMap, TReturnedError>;
|
|
141
|
+
constructor(def: ProcedureDefinition<TInitialContext, TInjectedContext, TInputSchema, TOutputSchema, TErrorMap, TReturnedError>);
|
|
142
|
+
/**
|
|
143
|
+
* Checks if the given instance satisfies the {@see Procedure} class/interface.
|
|
144
|
+
*/
|
|
145
|
+
static [Symbol.hasInstance](instance: unknown): boolean;
|
|
146
|
+
}
|
|
147
|
+
type AnyProcedure = Procedure<any, any, any, any, any, any>;
|
|
148
|
+
|
|
149
|
+
interface LazyDefinition<T> {
|
|
150
|
+
meta: Meta;
|
|
151
|
+
metaPlugins?: AnyMetaPlugin[] | undefined;
|
|
152
|
+
loader: () => Promise<{
|
|
153
|
+
default: T;
|
|
154
|
+
}>;
|
|
155
|
+
}
|
|
156
|
+
declare class Lazy<T> {
|
|
157
|
+
'~orpc': LazyDefinition<T>;
|
|
158
|
+
constructor(def: LazyDefinition<T>);
|
|
159
|
+
/**
|
|
160
|
+
* Checks if the given instance satisfies the {@see Lazy} class/interface.
|
|
161
|
+
*/
|
|
162
|
+
static [Symbol.hasInstance](instance: unknown): boolean;
|
|
163
|
+
}
|
|
164
|
+
type Lazyable<T> = T | Lazy<T>;
|
|
165
|
+
declare function unlazy<T extends Lazyable<any>>(maybeLazy: T): Promise<{
|
|
166
|
+
default: T extends Lazy<infer U> ? U : T;
|
|
167
|
+
}>;
|
|
168
|
+
|
|
169
|
+
type ProcedureClient<TClientContext extends ClientContext, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TReturnedORPCError extends AnyORPCError> = Client<TClientContext, InferSchemaInput<TInputSchema>, InferSchemaOutput<TOutputSchema>, ORPCErrorFromErrorMap<TErrorMap> | TReturnedORPCError | ThrowableError>;
|
|
170
|
+
interface ProcedureClientInterceptorOptions<TInitialContext extends Context, TErrorMap extends ErrorMap> extends ProcedureHandlerOptions<TInitialContext, unknown, ORPCErrorConstructorMap<TErrorMap>> {
|
|
171
|
+
}
|
|
172
|
+
type ProcedureClientInterceptor<TInitialContext extends Context, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TReturnedError extends AnyORPCError> = Interceptor<ProcedureClientInterceptorOptions<TInitialContext, TErrorMap>, PromiseWithError<InferSchemaOutput<TOutputSchema>, ORPCErrorFromErrorMap<TErrorMap> | TReturnedError | ThrowableError>>;
|
|
173
|
+
type ProcedureClientOptions<TInitialContext extends Context, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TReturnedError extends AnyORPCError, TClientContext extends ClientContext> = {
|
|
174
|
+
path?: string[];
|
|
175
|
+
interceptors?: ProcedureClientInterceptor<TInitialContext, TOutputSchema, TErrorMap, TReturnedError>[];
|
|
176
|
+
} & (object extends TInitialContext ? {
|
|
177
|
+
context?: Value<Promisable<TInitialContext>, [clientContext: TClientContext]>;
|
|
178
|
+
} : {
|
|
179
|
+
context: Value<Promisable<TInitialContext>, [clientContext: TClientContext]>;
|
|
180
|
+
});
|
|
181
|
+
declare function createProcedureClient<TInitialContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TReturnedError extends AnyORPCError, TClientContext extends ClientContext = object>(lazyableProcedure: Lazyable<Procedure<TInitialContext, any, TInputSchema, TOutputSchema, TErrorMap, TReturnedError>>, ...rest: MaybeOptionalOptions<ProcedureClientOptions<TInitialContext, TOutputSchema, TErrorMap, TReturnedError, TClientContext>>): ProcedureClient<TClientContext, TInputSchema, TOutputSchema, TErrorMap, TReturnedError>;
|
|
182
|
+
|
|
183
|
+
export { Procedure as a, Lazy as g, createORPCErrorConstructorMap as w, createProcedureClient as x, unlazy as y };
|
|
184
|
+
export type { AnyProcedure as A, Context as C, Lazyable as L, MergedContext as M, ORPCErrorConstructorMap as O, ProcedureClientOptions as P, ProcedureClient as b, ProcedureClientInterceptor as c, Middleware as d, MergedInitialContext as e, AnyMiddleware as f, ProcedureHandler as h, OrderedMiddleware as i, LazyDefinition as j, MiddlewareDefinition as k, MiddlewareDone as l, MiddlewareDoneOptions as m, MiddlewareNext as n, MiddlewareNextOptions as o, MiddlewareOptions as p, MiddlewareResult as q, ORPCErrorConstructorMapItem as r, ORPCErrorConstructorMapItemOptions as s, ProcedureClientInterceptorOptions as t, ProcedureDefinition as u, ProcedureHandlerOptions as v };
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { ORPCErrorCode, ORPCErrorOptions, ORPCError, AnyORPCError, ClientContext, Client } from '@orpc/client';
|
|
2
|
+
import { ErrorMap, ErrorMapItem, InferSchemaInput, AnyMetaPlugin, AnySchema, ProcedureContractDefinition, Meta, InferSchemaOutput, ORPCErrorFromErrorMap } from '@orpc/contract';
|
|
3
|
+
import { MaybeOptionalOptions, Promisable, Interceptor, PromiseWithError, ThrowableError, Value } from '@orpc/shared';
|
|
4
|
+
|
|
5
|
+
interface Context {
|
|
6
|
+
[key: PropertyKey]: any;
|
|
7
|
+
}
|
|
8
|
+
type MergedInitialContext<TInitial extends Context, TOutContext extends Context, TInContext extends Context> = Exclude<keyof TInContext, keyof TInitial | keyof TOutContext> extends never ? TInitial : TInitial & Omit<TInContext, keyof TInitial | keyof TOutContext>;
|
|
9
|
+
type MergedContext<TCurrent extends Context, TOutContext extends Context> = keyof TOutContext extends never ? TCurrent : Omit<TCurrent, keyof TOutContext> & TOutContext;
|
|
10
|
+
|
|
11
|
+
type ORPCErrorConstructorMapItemOptions<TData> = Omit<ORPCErrorOptions<TData>, 'status'>;
|
|
12
|
+
interface ORPCErrorConstructorMapItem<TCode extends ORPCErrorCode, TInData> {
|
|
13
|
+
(...rest: MaybeOptionalOptions<ORPCErrorConstructorMapItemOptions<TInData>>): ORPCError<TCode, TInData>;
|
|
14
|
+
}
|
|
15
|
+
type ORPCErrorConstructorMap<T extends ErrorMap> = {
|
|
16
|
+
[K in keyof T]: K extends ORPCErrorCode ? T[K] extends ErrorMapItem<infer UInputSchema> ? ORPCErrorConstructorMapItem<K, InferSchemaInput<UInputSchema>> : never : never;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Creates a map of ORPC error constructors.
|
|
20
|
+
*
|
|
21
|
+
* The returned object is a `Proxy` that allows access to arbitrary error codes:
|
|
22
|
+
* - If the code exists in the provided `errorMap`, the corresponding constructor
|
|
23
|
+
* will create a **defined** `ORPCError`.
|
|
24
|
+
* - If the code does not exist, a fallback `ORPCError` constructor is returned.
|
|
25
|
+
*
|
|
26
|
+
* The `in` operator can be used to check whether an error code is explicitly
|
|
27
|
+
* defined in the map.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* const errorMap = createORPCErrorConstructorMap({
|
|
32
|
+
* NOT_FOUND: {
|
|
33
|
+
* status: 404,
|
|
34
|
+
* message: 'Not Found',
|
|
35
|
+
* },
|
|
36
|
+
* })
|
|
37
|
+
*
|
|
38
|
+
* if ('NOT_FOUND' in errorMap) {
|
|
39
|
+
* const error = errorMap.NOT_FOUND()
|
|
40
|
+
* }
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
declare function createORPCErrorConstructorMap<T extends ErrorMap>(errorMap: T): ORPCErrorConstructorMap<T>;
|
|
44
|
+
|
|
45
|
+
type MiddlewareResult<TOutContext extends Context, TOutput> = Promisable<{
|
|
46
|
+
output: TOutput;
|
|
47
|
+
context: TOutContext;
|
|
48
|
+
}>;
|
|
49
|
+
type MiddlewareNextOptions<TOutContext extends Context> = object extends TOutContext ? {
|
|
50
|
+
context?: TOutContext;
|
|
51
|
+
} : {
|
|
52
|
+
context: TOutContext;
|
|
53
|
+
};
|
|
54
|
+
interface MiddlewareNext<TOutput> {
|
|
55
|
+
<T extends Context = object>(...rest: MaybeOptionalOptions<MiddlewareNextOptions<T>>): MiddlewareResult<T, TOutput>;
|
|
56
|
+
}
|
|
57
|
+
type MiddlewareDoneOptions<TOutContext, TOutput> = (object extends TOutContext ? {
|
|
58
|
+
context?: TOutContext;
|
|
59
|
+
} : {
|
|
60
|
+
context: TOutContext;
|
|
61
|
+
}) & {
|
|
62
|
+
output: TOutput;
|
|
63
|
+
};
|
|
64
|
+
interface MiddlewareDone<TOutput> {
|
|
65
|
+
/**
|
|
66
|
+
* Create a successful result and terminate the middleware chain early.
|
|
67
|
+
*/
|
|
68
|
+
<TOutContext extends Context = object>(...rest: MaybeOptionalOptions<MiddlewareDoneOptions<TOutContext, TOutput>>): MiddlewareResult<TOutContext, TOutput>;
|
|
69
|
+
}
|
|
70
|
+
interface MiddlewareOptions<TInContext extends Context, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>> {
|
|
71
|
+
context: TInContext;
|
|
72
|
+
path: string[];
|
|
73
|
+
procedure: AnyProcedure;
|
|
74
|
+
signal?: AbortSignal | undefined;
|
|
75
|
+
lastEventId: string | undefined;
|
|
76
|
+
/**
|
|
77
|
+
* Invoke to continue the middleware chain.
|
|
78
|
+
*/
|
|
79
|
+
next: MiddlewareNext<TOutput>;
|
|
80
|
+
errors: TErrorConstructorMap;
|
|
81
|
+
}
|
|
82
|
+
interface MiddlewareDefinition<TErrorMap extends ErrorMap> {
|
|
83
|
+
errorMap?: TErrorMap | undefined;
|
|
84
|
+
metaPlugins?: AnyMetaPlugin[] | undefined;
|
|
85
|
+
}
|
|
86
|
+
interface Middleware<TInContext extends Context, TOutContext extends Context, TInput, TOutput, TErrorMap extends ErrorMap> {
|
|
87
|
+
/** this property should be optional to support inline middleware */
|
|
88
|
+
'~orpc'?: MiddlewareDefinition<TErrorMap> | undefined;
|
|
89
|
+
(opts: MiddlewareOptions<TInContext, TOutput, ORPCErrorConstructorMap<TErrorMap>>, input: TInput, done: MiddlewareDone<TOutput>): Promisable<MiddlewareResult<TOutContext, TOutput>>;
|
|
90
|
+
}
|
|
91
|
+
type AnyMiddleware = Middleware<any, any, any, any, any>;
|
|
92
|
+
|
|
93
|
+
interface ProcedureHandlerOptions<TCurrentContext extends Context, TInput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>> {
|
|
94
|
+
context: TCurrentContext;
|
|
95
|
+
input: TInput;
|
|
96
|
+
path: string[];
|
|
97
|
+
procedure: Procedure<Context, Context, AnySchema, AnySchema, ErrorMap, any>;
|
|
98
|
+
signal?: AbortSignal | undefined;
|
|
99
|
+
lastEventId?: string | undefined;
|
|
100
|
+
errors: TErrorConstructorMap;
|
|
101
|
+
}
|
|
102
|
+
interface ProcedureHandler<TCurrentContext extends Context, TInput, THandlerOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>> {
|
|
103
|
+
(opts: ProcedureHandlerOptions<TCurrentContext, TInput, TErrorConstructorMap>, input: TInput): Promisable<THandlerOutput>;
|
|
104
|
+
}
|
|
105
|
+
interface OrderedMiddleware {
|
|
106
|
+
/**
|
|
107
|
+
* Snapshot of `inputSchemas.length`
|
|
108
|
+
* at the time this middleware was used.
|
|
109
|
+
*
|
|
110
|
+
* @default 0
|
|
111
|
+
*/
|
|
112
|
+
inputSchemasLengthAtUse?: number | undefined;
|
|
113
|
+
/**
|
|
114
|
+
* Snapshot of `outputSchemas.length`
|
|
115
|
+
* at the time this middleware was used.
|
|
116
|
+
*
|
|
117
|
+
* @default 0
|
|
118
|
+
*/
|
|
119
|
+
outputSchemasLengthAtUse?: number | undefined;
|
|
120
|
+
middleware: AnyMiddleware;
|
|
121
|
+
}
|
|
122
|
+
interface ProcedureDefinition<TInitialContext extends Context, TInjectedContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TReturnedError extends AnyORPCError> extends ProcedureContractDefinition<TInputSchema, TOutputSchema, TErrorMap> {
|
|
123
|
+
__TInitialContext?: (type: TInitialContext) => unknown;
|
|
124
|
+
__TInjectedContext?: (type: TInjectedContext) => unknown;
|
|
125
|
+
__TReturnedError?: () => TReturnedError;
|
|
126
|
+
/**
|
|
127
|
+
* When enabled, errors returned (not thrown) by the handler are passed through as-is,
|
|
128
|
+
* rather than being transformed into inferrable errors.
|
|
129
|
+
*
|
|
130
|
+
* This is intended for the contract-first approach, where the procedure adheres to an
|
|
131
|
+
* external contract and returned errors should not affect the inferred contract types.
|
|
132
|
+
*
|
|
133
|
+
* @default false
|
|
134
|
+
*/
|
|
135
|
+
opaqueReturnedErrors?: boolean | undefined;
|
|
136
|
+
orderedMiddlewares: OrderedMiddleware[];
|
|
137
|
+
handler: ProcedureHandler<any, any, any, any>;
|
|
138
|
+
}
|
|
139
|
+
declare class Procedure<TInitialContext extends Context, TInjectedContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TReturnedError extends AnyORPCError> {
|
|
140
|
+
'~orpc': ProcedureDefinition<TInitialContext, TInjectedContext, TInputSchema, TOutputSchema, TErrorMap, TReturnedError>;
|
|
141
|
+
constructor(def: ProcedureDefinition<TInitialContext, TInjectedContext, TInputSchema, TOutputSchema, TErrorMap, TReturnedError>);
|
|
142
|
+
/**
|
|
143
|
+
* Checks if the given instance satisfies the {@see Procedure} class/interface.
|
|
144
|
+
*/
|
|
145
|
+
static [Symbol.hasInstance](instance: unknown): boolean;
|
|
146
|
+
}
|
|
147
|
+
type AnyProcedure = Procedure<any, any, any, any, any, any>;
|
|
148
|
+
|
|
149
|
+
interface LazyDefinition<T> {
|
|
150
|
+
meta: Meta;
|
|
151
|
+
metaPlugins?: AnyMetaPlugin[] | undefined;
|
|
152
|
+
loader: () => Promise<{
|
|
153
|
+
default: T;
|
|
154
|
+
}>;
|
|
155
|
+
}
|
|
156
|
+
declare class Lazy<T> {
|
|
157
|
+
'~orpc': LazyDefinition<T>;
|
|
158
|
+
constructor(def: LazyDefinition<T>);
|
|
159
|
+
/**
|
|
160
|
+
* Checks if the given instance satisfies the {@see Lazy} class/interface.
|
|
161
|
+
*/
|
|
162
|
+
static [Symbol.hasInstance](instance: unknown): boolean;
|
|
163
|
+
}
|
|
164
|
+
type Lazyable<T> = T | Lazy<T>;
|
|
165
|
+
declare function unlazy<T extends Lazyable<any>>(maybeLazy: T): Promise<{
|
|
166
|
+
default: T extends Lazy<infer U> ? U : T;
|
|
167
|
+
}>;
|
|
168
|
+
|
|
169
|
+
type ProcedureClient<TClientContext extends ClientContext, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TReturnedORPCError extends AnyORPCError> = Client<TClientContext, InferSchemaInput<TInputSchema>, InferSchemaOutput<TOutputSchema>, ORPCErrorFromErrorMap<TErrorMap> | TReturnedORPCError | ThrowableError>;
|
|
170
|
+
interface ProcedureClientInterceptorOptions<TInitialContext extends Context, TErrorMap extends ErrorMap> extends ProcedureHandlerOptions<TInitialContext, unknown, ORPCErrorConstructorMap<TErrorMap>> {
|
|
171
|
+
}
|
|
172
|
+
type ProcedureClientInterceptor<TInitialContext extends Context, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TReturnedError extends AnyORPCError> = Interceptor<ProcedureClientInterceptorOptions<TInitialContext, TErrorMap>, PromiseWithError<InferSchemaOutput<TOutputSchema>, ORPCErrorFromErrorMap<TErrorMap> | TReturnedError | ThrowableError>>;
|
|
173
|
+
type ProcedureClientOptions<TInitialContext extends Context, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TReturnedError extends AnyORPCError, TClientContext extends ClientContext> = {
|
|
174
|
+
path?: string[];
|
|
175
|
+
interceptors?: ProcedureClientInterceptor<TInitialContext, TOutputSchema, TErrorMap, TReturnedError>[];
|
|
176
|
+
} & (object extends TInitialContext ? {
|
|
177
|
+
context?: Value<Promisable<TInitialContext>, [clientContext: TClientContext]>;
|
|
178
|
+
} : {
|
|
179
|
+
context: Value<Promisable<TInitialContext>, [clientContext: TClientContext]>;
|
|
180
|
+
});
|
|
181
|
+
declare function createProcedureClient<TInitialContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TReturnedError extends AnyORPCError, TClientContext extends ClientContext = object>(lazyableProcedure: Lazyable<Procedure<TInitialContext, any, TInputSchema, TOutputSchema, TErrorMap, TReturnedError>>, ...rest: MaybeOptionalOptions<ProcedureClientOptions<TInitialContext, TOutputSchema, TErrorMap, TReturnedError, TClientContext>>): ProcedureClient<TClientContext, TInputSchema, TOutputSchema, TErrorMap, TReturnedError>;
|
|
182
|
+
|
|
183
|
+
export { Procedure as a, Lazy as g, createORPCErrorConstructorMap as w, createProcedureClient as x, unlazy as y };
|
|
184
|
+
export type { AnyProcedure as A, Context as C, Lazyable as L, MergedContext as M, ORPCErrorConstructorMap as O, ProcedureClientOptions as P, ProcedureClient as b, ProcedureClientInterceptor as c, Middleware as d, MergedInitialContext as e, AnyMiddleware as f, ProcedureHandler as h, OrderedMiddleware as i, LazyDefinition as j, MiddlewareDefinition as k, MiddlewareDone as l, MiddlewareDoneOptions as m, MiddlewareNext as n, MiddlewareNextOptions as o, MiddlewareOptions as p, MiddlewareResult as q, ORPCErrorConstructorMapItem as r, ORPCErrorConstructorMapItemOptions as s, ProcedureClientInterceptorOptions as t, ProcedureDefinition as u, ProcedureHandlerOptions as v };
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { RPCSerializer, AnyORPCError } from '@orpc/client';
|
|
2
|
+
import { Value, Promisable } from '@orpc/shared';
|
|
3
|
+
import { StandardMethod, StandardLazyRequest, StandardResponse } from '@standardserver/core';
|
|
4
|
+
import { A as AnyProcedure, C as Context } from './server.BL22TloH.mjs';
|
|
5
|
+
import { A as AnyRouter } from './server.BsNNjG5J.mjs';
|
|
6
|
+
import { AnyProcedureContract } from '@orpc/contract';
|
|
7
|
+
import { b as StandardHandlerHandleOptions, c as StandardHandlerCodec, d as StandardHandlerCodecResolvedProcedure } from './server.BB_Ik9Ph.mjs';
|
|
8
|
+
|
|
9
|
+
interface RPCMatcherOptions {
|
|
10
|
+
/**
|
|
11
|
+
* Filter which procedures are exposed for matching. Return `false` to exclude.
|
|
12
|
+
*
|
|
13
|
+
* @default true
|
|
14
|
+
*/
|
|
15
|
+
filter?: Value<boolean, [procedure: AnyProcedureContract | AnyProcedure, path: string[]]>;
|
|
16
|
+
}
|
|
17
|
+
declare class RPCMatcher {
|
|
18
|
+
private readonly filter;
|
|
19
|
+
private readonly rootRouter;
|
|
20
|
+
private readonly tree;
|
|
21
|
+
private pendingLazyRouters;
|
|
22
|
+
constructor(router: AnyRouter, options?: RPCMatcherOptions);
|
|
23
|
+
private index;
|
|
24
|
+
match(_method: StandardMethod, pathname: `/${string}`, prefix: `/${string}` | undefined): Promise<{
|
|
25
|
+
path: string[];
|
|
26
|
+
procedure: AnyProcedure;
|
|
27
|
+
} | undefined>;
|
|
28
|
+
private matchPathname;
|
|
29
|
+
private resolvePendingLazyRouters;
|
|
30
|
+
private resolveProcedure;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
interface RPCHandlerCodecOptions<T extends Context> extends RPCMatcherOptions {
|
|
34
|
+
/**
|
|
35
|
+
* Override the default RPC serializer.
|
|
36
|
+
*/
|
|
37
|
+
serializer?: Pick<RPCSerializer, keyof RPCSerializer>;
|
|
38
|
+
/**
|
|
39
|
+
* Resolve HTTP status for encoded successful outputs.
|
|
40
|
+
*
|
|
41
|
+
* Value should be in range `200-299`
|
|
42
|
+
* Return `undefined` or `null` to fallback to default
|
|
43
|
+
*
|
|
44
|
+
* @default DEFAULT_ERROR_STATUS, DEFAULT_ERROR_STATUS
|
|
45
|
+
*/
|
|
46
|
+
outputStatus?: Value<number | undefined | null, [output: unknown, procedure: AnyProcedure, path: string[], options: StandardHandlerHandleOptions<T>]>;
|
|
47
|
+
/**
|
|
48
|
+
* Mapping ORPCError Code -> HTTP Status Code
|
|
49
|
+
*
|
|
50
|
+
* @default COMMON_ERROR_STATUS_MAP
|
|
51
|
+
*/
|
|
52
|
+
errorStatusMap?: Record<string, number> | undefined;
|
|
53
|
+
}
|
|
54
|
+
declare class RPCHandlerCodec<T extends Context> implements StandardHandlerCodec<T> {
|
|
55
|
+
private readonly matcher;
|
|
56
|
+
private readonly serializer;
|
|
57
|
+
private readonly errorStatusMap;
|
|
58
|
+
private readonly outputStatus;
|
|
59
|
+
constructor(router: AnyRouter, options?: RPCHandlerCodecOptions<T>);
|
|
60
|
+
resolveProcedure(request: StandardLazyRequest, options: StandardHandlerHandleOptions<T>): Promise<StandardHandlerCodecResolvedProcedure | undefined>;
|
|
61
|
+
encodeOutput(output: unknown, procedure: AnyProcedure, path: string[], options: StandardHandlerHandleOptions<T>): Promisable<StandardResponse>;
|
|
62
|
+
encodeError(error: AnyORPCError, _procedure: AnyProcedure, _path: string[], _options: StandardHandlerHandleOptions<T>): Promisable<StandardResponse>;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export { RPCHandlerCodec as a, RPCMatcher as b };
|
|
66
|
+
export type { RPCHandlerCodecOptions as R, RPCMatcherOptions as c };
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { RouterContract, ProcedureContract, ORPCErrorFromErrorMap, ThrowableError, InferSchemaInput, InferSchemaOutput } from '@orpc/contract';
|
|
2
|
+
import { C as Context, a as Procedure, L as Lazyable, M as MergedContext } from './server.BL22TloH.mjs';
|
|
3
|
+
|
|
4
|
+
type Router<TInitialContext extends Context> = Procedure<TInitialContext, any, any, any, any, any> | {
|
|
5
|
+
[k: string]: Lazyable<Router<TInitialContext>>;
|
|
6
|
+
};
|
|
7
|
+
type ContractedRouter<T extends RouterContract, TInitialContext extends Context> = T extends ProcedureContract<infer $InputSchema, infer $OutputSchema, infer $ErrorMap> ? Procedure<TInitialContext, any, $InputSchema, $OutputSchema, $ErrorMap, never> : {
|
|
8
|
+
[K in keyof T]: T[K] extends RouterContract ? Lazyable<ContractedRouter<T[K], TInitialContext>> : never;
|
|
9
|
+
};
|
|
10
|
+
type AnyRouter = Router<any>;
|
|
11
|
+
type InferRouterInitialContext<T extends AnyRouter> = T extends Router<infer $> ? $ : never;
|
|
12
|
+
/**
|
|
13
|
+
* Infer all initial context of the router.
|
|
14
|
+
*
|
|
15
|
+
* @info A procedure is a router too.
|
|
16
|
+
* @see {@link https://orpc.dev/docs/router#utilities Router Utilities Docs}
|
|
17
|
+
*/
|
|
18
|
+
type InferRouterInitialContexts<T extends AnyRouter> = T extends Procedure<infer UInitialContext, any, any, any, any, any> ? UInitialContext : {
|
|
19
|
+
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInitialContexts<U> : never;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Infer all current context of the router.
|
|
23
|
+
*
|
|
24
|
+
* @info A procedure is a router too.
|
|
25
|
+
* @see {@link https://orpc.dev/docs/router#utilities Router Utilities Docs}
|
|
26
|
+
*/
|
|
27
|
+
type InferRouterFinalContexts<T extends AnyRouter> = T extends Procedure<infer UInitialContext, infer UInjectedContext, any, any, any, any> ? MergedContext<UInitialContext, UInjectedContext> : {
|
|
28
|
+
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterFinalContexts<U> : never;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Infer all router inputs
|
|
32
|
+
*
|
|
33
|
+
* @info A procedure is a router too.
|
|
34
|
+
* @see {@link https://orpc.dev/docs/router#utilities Router Utilities Docs}
|
|
35
|
+
*/
|
|
36
|
+
type InferRouterInputs<T extends AnyRouter> = T extends Procedure<any, any, infer UInputSchema, any, any, any> ? InferSchemaInput<UInputSchema> : {
|
|
37
|
+
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInputs<U> : never;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* Infer all router outputs
|
|
41
|
+
*
|
|
42
|
+
* @info A procedure is a router too.
|
|
43
|
+
* @see {@link https://orpc.dev/docs/router#utilities Router Utilities Docs}
|
|
44
|
+
*/
|
|
45
|
+
type InferRouterOutputs<T extends AnyRouter> = T extends Procedure<any, any, any, infer UOutputSchema, any, any> ? InferSchemaOutput<UOutputSchema> : {
|
|
46
|
+
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterOutputs<U> : never;
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Infer the union of throwable errors for entire router.
|
|
50
|
+
*/
|
|
51
|
+
type InferRouterError<T extends AnyRouter> = T extends Procedure<any, any, any, any, infer UErrorMap, infer UReturnedError> ? ORPCErrorFromErrorMap<UErrorMap> | UReturnedError | ThrowableError : {
|
|
52
|
+
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterError<U> : never;
|
|
53
|
+
}[keyof T];
|
|
54
|
+
/**
|
|
55
|
+
* Infer throwable errors for each procedure, preserving the router shape.
|
|
56
|
+
*/
|
|
57
|
+
type InferRouterErrors<T extends AnyRouter> = T extends Procedure<any, any, any, any, infer UErrorMap, infer UReturnedError> ? ORPCErrorFromErrorMap<UErrorMap> | UReturnedError | ThrowableError : {
|
|
58
|
+
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterErrors<U> : never;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export type { AnyRouter as A, ContractedRouter as C, InferRouterInitialContext as I, Router as R, InferRouterError as a, InferRouterErrors as b, InferRouterFinalContexts as c, InferRouterInitialContexts as d, InferRouterInputs as e, InferRouterOutputs as f };
|