@temporary-name/server 1.9.3-alpha.65222302f1b71807a849530b3fe0fa0326d3c1a2 → 1.9.3-alpha.6dce39d156e86570d7f905d2e09463c8e1c80db2
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/dist/adapters/aws-lambda/index.d.mts +3 -3
- package/dist/adapters/aws-lambda/index.d.ts +3 -3
- package/dist/adapters/aws-lambda/index.mjs +4 -6
- package/dist/adapters/fetch/index.d.mts +3 -3
- package/dist/adapters/fetch/index.d.ts +3 -3
- package/dist/adapters/fetch/index.mjs +4 -6
- package/dist/adapters/node/index.d.mts +3 -3
- package/dist/adapters/node/index.d.ts +3 -3
- package/dist/adapters/node/index.mjs +4 -6
- package/dist/adapters/standard/index.d.mts +20 -31
- package/dist/adapters/standard/index.d.ts +20 -31
- package/dist/adapters/standard/index.mjs +4 -6
- package/dist/index.d.mts +72 -139
- package/dist/index.d.ts +72 -139
- package/dist/index.mjs +106 -294
- package/dist/openapi/index.d.mts +1 -1
- package/dist/openapi/index.d.ts +1 -1
- package/dist/openapi/index.mjs +60 -77
- package/dist/plugins/index.d.mts +2 -2
- package/dist/plugins/index.d.ts +2 -2
- package/dist/shared/{server.CQyYNJ1H.d.ts → server.BSJugGGA.d.mts} +1 -2
- package/dist/shared/{server.SLLuK6_v.d.ts → server.Bs8EZATl.d.ts} +2 -2
- package/dist/shared/server.CHV9AQHl.mjs +412 -0
- package/dist/shared/server.CWWP8ypC.d.mts +239 -0
- package/dist/shared/server.CWWP8ypC.d.ts +239 -0
- package/dist/shared/{server.DLsti1Pv.mjs → server.Cn7WsRHl.mjs} +56 -95
- package/dist/shared/{server.BeuTpcmO.d.mts → server.DrMgIiBr.d.mts} +2 -2
- package/dist/shared/{server.C1fnTLq0.d.mts → server.EKwDe0d2.d.ts} +1 -2
- package/dist/shared/{server.BEHw7Eyx.mjs → server.JtIZ8YG7.mjs} +1 -11
- package/package.json +10 -9
- package/dist/shared/server.BKSOrA6h.d.mts +0 -192
- package/dist/shared/server.BKSOrA6h.d.ts +0 -192
- package/dist/shared/server.BKh8I1Ny.mjs +0 -239
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import { ErrorMap, ErrorMapItem, InferSchemaInput, Schemas, Meta, ContractProcedureDef, AnyContractRouter, ContractProcedure, InferProcedureClientInputs, InferSchemaOutput, EnhanceRouteOptions, MergedErrorMap, AnyContractProcedure, ContractRouter, AnySchema, ErrorFromErrorMap } from '@temporary-name/contract';
|
|
2
|
+
import { ORPCErrorCode, MaybeOptionalOptions, ORPCErrorOptions, ORPCError, Promisable, HTTPPath, ClientContext, Interceptor, PromiseWithError, Value, Client } from '@temporary-name/shared';
|
|
3
|
+
|
|
4
|
+
type Context = Record<PropertyKey, any>;
|
|
5
|
+
type MergedInitialContext<TInitial extends Context, TAdditional extends Context, TCurrent extends Context> = TInitial & Omit<TAdditional, keyof TCurrent>;
|
|
6
|
+
type MergedCurrentContext<T extends Context, U extends Context> = Omit<T, keyof U> & U;
|
|
7
|
+
declare function mergeCurrentContext<T extends Context, U extends Context>(context: T, other: U): MergedCurrentContext<T, U>;
|
|
8
|
+
|
|
9
|
+
type ORPCErrorConstructorMapItemOptions<TData> = Omit<ORPCErrorOptions<TData>, 'defined' | 'status'>;
|
|
10
|
+
type ORPCErrorConstructorMapItem<TCode extends ORPCErrorCode, TInData> = (...rest: MaybeOptionalOptions<ORPCErrorConstructorMapItemOptions<TInData>>) => ORPCError<TCode, TInData>;
|
|
11
|
+
type ORPCErrorConstructorMap<T extends ErrorMap> = {
|
|
12
|
+
[K in keyof T]: K extends ORPCErrorCode ? T[K] extends ErrorMapItem<infer UInputSchema> ? ORPCErrorConstructorMapItem<K, InferSchemaInput<UInputSchema>> : never : never;
|
|
13
|
+
};
|
|
14
|
+
declare function createORPCErrorConstructorMap<T extends ErrorMap>(errors: T): ORPCErrorConstructorMap<T>;
|
|
15
|
+
|
|
16
|
+
interface ProcedureHandlerOptions<TCurrentContext extends Context, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
|
|
17
|
+
context: TCurrentContext;
|
|
18
|
+
path: readonly string[];
|
|
19
|
+
procedure: Procedure<Context, Context, Schemas, ErrorMap, TMeta>;
|
|
20
|
+
signal?: AbortSignal;
|
|
21
|
+
lastEventId: string | undefined;
|
|
22
|
+
errors: TErrorConstructorMap;
|
|
23
|
+
}
|
|
24
|
+
interface ProcedureHandler<TCurrentContext extends Context, TInput extends {
|
|
25
|
+
path: any;
|
|
26
|
+
query: any;
|
|
27
|
+
body: any;
|
|
28
|
+
}, THandlerOutput, TErrorMap extends ErrorMap, TMeta extends Meta> {
|
|
29
|
+
(input: TInput, opt: ProcedureHandlerOptions<TCurrentContext, ORPCErrorConstructorMap<TErrorMap>, TMeta>): Promisable<THandlerOutput>;
|
|
30
|
+
}
|
|
31
|
+
interface ProcedureDef<TInitialContext extends Context, TCurrentContext extends Context, TSchemas extends Schemas, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedureDef<TSchemas, TErrorMap, TMeta> {
|
|
32
|
+
__initialContext?: (type: TInitialContext) => unknown;
|
|
33
|
+
middlewares: readonly AnyMiddleware[];
|
|
34
|
+
inputValidationIndex: number;
|
|
35
|
+
outputValidationIndex: number;
|
|
36
|
+
handler: ProcedureHandler<TCurrentContext, any, any, any, any>;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* This class represents a procedure.
|
|
40
|
+
*
|
|
41
|
+
* @see {@link https://orpc.unnoq.com/docs/procedure Procedure Docs}
|
|
42
|
+
*/
|
|
43
|
+
declare class Procedure<TInitialContext extends Context, TCurrentContext extends Context, TSchemas extends Schemas, TErrorMap extends ErrorMap, TMeta extends Meta> {
|
|
44
|
+
/**
|
|
45
|
+
* This property holds the defined options.
|
|
46
|
+
*/
|
|
47
|
+
'~orpc': ProcedureDef<TInitialContext, TCurrentContext, TSchemas, TErrorMap, TMeta>;
|
|
48
|
+
constructor(def: ProcedureDef<TInitialContext, TCurrentContext, TSchemas, TErrorMap, TMeta>);
|
|
49
|
+
}
|
|
50
|
+
type AnyProcedure = Procedure<any, any, Schemas, any, any>;
|
|
51
|
+
declare function isProcedure(item: unknown): item is AnyProcedure;
|
|
52
|
+
|
|
53
|
+
type MiddlewareResult<TOutContext extends Context, TOutput> = Promisable<{
|
|
54
|
+
output: TOutput;
|
|
55
|
+
context: TOutContext;
|
|
56
|
+
}>;
|
|
57
|
+
interface MiddlewareNextFn<TOutput> {
|
|
58
|
+
<U extends Context = {}>(options?: {
|
|
59
|
+
context?: U;
|
|
60
|
+
}): MiddlewareResult<U, TOutput>;
|
|
61
|
+
}
|
|
62
|
+
interface MiddlewareOutputFn<TOutput> {
|
|
63
|
+
(output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
|
|
64
|
+
}
|
|
65
|
+
interface MiddlewareOptions<TInContext extends Context, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
|
|
66
|
+
context: TInContext;
|
|
67
|
+
path: readonly string[];
|
|
68
|
+
procedure: Procedure<Context, Context, Schemas, ErrorMap, TMeta>;
|
|
69
|
+
signal?: AbortSignal;
|
|
70
|
+
lastEventId: string | undefined;
|
|
71
|
+
next: MiddlewareNextFn<TOutput>;
|
|
72
|
+
errors: TErrorConstructorMap;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* A function that represents a middleware.
|
|
76
|
+
*
|
|
77
|
+
* @see {@link https://orpc.unnoq.com/docs/middleware Middleware Docs}
|
|
78
|
+
*/
|
|
79
|
+
interface Middleware<TInContext extends Context, TOutContext extends Context, TInput, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
|
|
80
|
+
(options: MiddlewareOptions<TInContext, TOutput, TErrorConstructorMap, TMeta>, input: TInput, output: MiddlewareOutputFn<TOutput>): Promisable<MiddlewareResult<TOutContext, TOutput>>;
|
|
81
|
+
}
|
|
82
|
+
type AnyMiddleware = Middleware<any, any, any, any, any, any>;
|
|
83
|
+
interface MapInputMiddleware<TInput, TMappedInput> {
|
|
84
|
+
(input: TInput): TMappedInput;
|
|
85
|
+
}
|
|
86
|
+
declare function middlewareOutputFn<TOutput>(output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Represents a router, which defines a hierarchical structure of procedures.
|
|
90
|
+
*
|
|
91
|
+
* @info A procedure is a router too.
|
|
92
|
+
* @see {@link https://orpc.unnoq.com/docs/contract-first/define-contract#contract-router Contract Router Docs}
|
|
93
|
+
*/
|
|
94
|
+
type Router<T extends AnyContractRouter, TInitialContext extends Context> = T extends ContractProcedure<infer USchemas, infer UErrorMap, infer UMeta> ? Procedure<TInitialContext, any, USchemas, UErrorMap, UMeta> : {
|
|
95
|
+
[K in keyof T]: T[K] extends AnyContractRouter ? Lazyable<Router<T[K], TInitialContext>> : never;
|
|
96
|
+
};
|
|
97
|
+
type AnyRouter = Router<any, any>;
|
|
98
|
+
type InferRouterInitialContext<T extends AnyRouter> = T extends Router<any, infer UInitialContext> ? UInitialContext : never;
|
|
99
|
+
/**
|
|
100
|
+
* Infer all initial context of the router.
|
|
101
|
+
*
|
|
102
|
+
* @info A procedure is a router too.
|
|
103
|
+
* @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
|
|
104
|
+
*/
|
|
105
|
+
type InferRouterInitialContexts<T extends AnyRouter> = T extends Procedure<infer UInitialContext, any, any, any, any> ? UInitialContext : {
|
|
106
|
+
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInitialContexts<U> : never;
|
|
107
|
+
};
|
|
108
|
+
/**
|
|
109
|
+
* Infer all current context of the router.
|
|
110
|
+
*
|
|
111
|
+
* @info A procedure is a router too.
|
|
112
|
+
* @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
|
|
113
|
+
*/
|
|
114
|
+
type InferRouterCurrentContexts<T extends AnyRouter> = T extends Procedure<any, infer UCurrentContext, any, any, any> ? UCurrentContext : {
|
|
115
|
+
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterCurrentContexts<U> : never;
|
|
116
|
+
};
|
|
117
|
+
/**
|
|
118
|
+
* Infer all router inputs
|
|
119
|
+
*
|
|
120
|
+
* @info A procedure is a router too.
|
|
121
|
+
* @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
|
|
122
|
+
*/
|
|
123
|
+
type InferRouterInputs<T extends AnyRouter> = T extends Procedure<any, any, infer USchemas, any, any> ? InferProcedureClientInputs<USchemas> : {
|
|
124
|
+
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInputs<U> : never;
|
|
125
|
+
};
|
|
126
|
+
/**
|
|
127
|
+
* Infer all router outputs
|
|
128
|
+
*
|
|
129
|
+
* @info A procedure is a router too.
|
|
130
|
+
* @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
|
|
131
|
+
*/
|
|
132
|
+
type InferRouterOutputs<T extends AnyRouter> = T extends Procedure<any, any, infer USchemas, any, any> ? InferSchemaOutput<USchemas['outputSchema']> : {
|
|
133
|
+
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterOutputs<U> : never;
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
declare function getRouter<T extends Lazyable<AnyRouter | undefined>>(router: T, path: readonly string[]): T extends Lazy<any> ? Lazy<AnyRouter | undefined> : Lazyable<AnyRouter | undefined>;
|
|
137
|
+
type AccessibleLazyRouter<T extends Lazyable<AnyRouter | undefined>> = T extends Lazy<infer U extends AnyRouter | undefined | Lazy<AnyRouter | undefined>> ? AccessibleLazyRouter<U> : T extends AnyProcedure | undefined ? Lazy<T> : Lazy<T> & {
|
|
138
|
+
[K in keyof T]: T[K] extends Lazyable<AnyRouter> ? AccessibleLazyRouter<T[K]> : never;
|
|
139
|
+
};
|
|
140
|
+
declare function createAccessibleLazyRouter<T extends Lazy<AnyRouter | undefined>>(lazied: T): AccessibleLazyRouter<T>;
|
|
141
|
+
type EnhancedRouter<T extends Lazyable<AnyRouter>, TInitialContext extends Context, TCurrentContext extends Context, TErrorMap extends ErrorMap> = T extends Lazy<infer U extends AnyRouter> ? AccessibleLazyRouter<EnhancedRouter<U, TInitialContext, TCurrentContext, TErrorMap>> : T extends (Procedure<infer UInitialContext, infer UCurrentContext, infer USchemas, infer UErrorMap, infer UMeta>) ? Procedure<MergedInitialContext<TInitialContext, UInitialContext, TCurrentContext>, UCurrentContext, USchemas, MergedErrorMap<TErrorMap, UErrorMap>, UMeta> : {
|
|
142
|
+
[K in keyof T]: T[K] extends Lazyable<AnyRouter> ? EnhancedRouter<T[K], TInitialContext, TCurrentContext, TErrorMap> : never;
|
|
143
|
+
};
|
|
144
|
+
interface EnhanceRouterOptions<TErrorMap extends ErrorMap> extends EnhanceRouteOptions {
|
|
145
|
+
middlewares: readonly AnyMiddleware[];
|
|
146
|
+
errorMap: TErrorMap;
|
|
147
|
+
dedupeLeadingMiddlewares: boolean;
|
|
148
|
+
}
|
|
149
|
+
declare function enhanceRouter<T extends Lazyable<AnyRouter>, TInitialContext extends Context, TCurrentContext extends Context, TErrorMap extends ErrorMap>(router: T, options: EnhanceRouterOptions<TErrorMap>): EnhancedRouter<T, TInitialContext, TCurrentContext, TErrorMap>;
|
|
150
|
+
interface TraverseContractProceduresOptions {
|
|
151
|
+
router: AnyContractRouter | AnyRouter;
|
|
152
|
+
path: readonly string[];
|
|
153
|
+
}
|
|
154
|
+
interface TraverseContractProcedureCallbackOptions {
|
|
155
|
+
contract: AnyContractProcedure | AnyProcedure;
|
|
156
|
+
path: readonly string[];
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* @deprecated Use `TraverseContractProcedureCallbackOptions` instead.
|
|
160
|
+
*/
|
|
161
|
+
type ContractProcedureCallbackOptions = TraverseContractProcedureCallbackOptions;
|
|
162
|
+
interface LazyTraverseContractProceduresOptions {
|
|
163
|
+
router: Lazy<AnyRouter>;
|
|
164
|
+
path: readonly string[];
|
|
165
|
+
}
|
|
166
|
+
declare function traverseContractProcedures(options: TraverseContractProceduresOptions, callback: (options: TraverseContractProcedureCallbackOptions) => void, lazyOptions?: LazyTraverseContractProceduresOptions[]): LazyTraverseContractProceduresOptions[];
|
|
167
|
+
declare function resolveContractProcedures(options: TraverseContractProceduresOptions, callback: (options: TraverseContractProcedureCallbackOptions) => void): Promise<void>;
|
|
168
|
+
type UnlaziedRouter<T extends AnyRouter> = T extends AnyProcedure ? T : {
|
|
169
|
+
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? UnlaziedRouter<U> : never;
|
|
170
|
+
};
|
|
171
|
+
declare function unlazyRouter<T extends AnyRouter>(router: T): Promise<UnlaziedRouter<T>>;
|
|
172
|
+
|
|
173
|
+
declare const LAZY_SYMBOL: unique symbol;
|
|
174
|
+
interface LazyMeta {
|
|
175
|
+
prefix?: HTTPPath;
|
|
176
|
+
}
|
|
177
|
+
interface Lazy<T> {
|
|
178
|
+
[LAZY_SYMBOL]: {
|
|
179
|
+
loader: () => Promise<{
|
|
180
|
+
default: T;
|
|
181
|
+
}>;
|
|
182
|
+
meta: LazyMeta;
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
type Lazyable<T> = T | Lazy<T>;
|
|
186
|
+
/**
|
|
187
|
+
* @internal
|
|
188
|
+
*/
|
|
189
|
+
declare function lazyInternal<T>(loader: () => Promise<{
|
|
190
|
+
default: T;
|
|
191
|
+
}>, meta?: LazyMeta): Lazy<T>;
|
|
192
|
+
/**
|
|
193
|
+
* Creates a lazy-loaded item.
|
|
194
|
+
*
|
|
195
|
+
* @warning The `prefix` in `meta` only holds metadata and does not apply the prefix to the lazy router, use `os.prefix(...).lazyRoute(...)` instead.
|
|
196
|
+
*/
|
|
197
|
+
declare function lazy<T extends Router<ContractRouter<{}>, any>>(prefix: HTTPPath, loader: () => Promise<{
|
|
198
|
+
default: T;
|
|
199
|
+
}>): EnhancedRouter<Lazy<T>, {}, {}, {}>;
|
|
200
|
+
declare function isLazy(item: unknown): item is Lazy<any>;
|
|
201
|
+
declare function getLazyMeta(lazied: Lazy<any>): LazyMeta;
|
|
202
|
+
declare function unlazy<T extends Lazyable<any>>(lazied: T): Promise<{
|
|
203
|
+
default: T extends Lazy<infer U> ? U : T;
|
|
204
|
+
}>;
|
|
205
|
+
|
|
206
|
+
type ProcedureClient<TClientContext extends ClientContext, TSchemas extends Schemas, TErrorMap extends ErrorMap> = Client<TClientContext, InferProcedureClientInputs<TSchemas>, InferSchemaOutput<TSchemas['outputSchema']>, ErrorFromErrorMap<TErrorMap>>;
|
|
207
|
+
interface ProcedureClientInterceptorOptions<TInitialContext extends Context, TErrorMap extends ErrorMap, TMeta extends Meta> {
|
|
208
|
+
context: TInitialContext;
|
|
209
|
+
input: {
|
|
210
|
+
path: unknown;
|
|
211
|
+
query: unknown;
|
|
212
|
+
body: unknown;
|
|
213
|
+
};
|
|
214
|
+
errors: ORPCErrorConstructorMap<TErrorMap>;
|
|
215
|
+
path: readonly string[];
|
|
216
|
+
procedure: Procedure<Context, Context, Schemas, ErrorMap, TMeta>;
|
|
217
|
+
signal?: AbortSignal;
|
|
218
|
+
lastEventId: string | undefined;
|
|
219
|
+
}
|
|
220
|
+
type CreateProcedureClientOptions<TInitialContext extends Context, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext> = {
|
|
221
|
+
/**
|
|
222
|
+
* This is helpful for logging and analytics.
|
|
223
|
+
*/
|
|
224
|
+
path?: readonly string[];
|
|
225
|
+
interceptors?: Interceptor<ProcedureClientInterceptorOptions<TInitialContext, TErrorMap, TMeta>, PromiseWithError<InferSchemaOutput<TOutputSchema>, ErrorFromErrorMap<TErrorMap>>>[];
|
|
226
|
+
} & (Record<never, never> extends TInitialContext ? {
|
|
227
|
+
context?: Value<Promisable<TInitialContext>, [clientContext: TClientContext]>;
|
|
228
|
+
} : {
|
|
229
|
+
context: Value<Promisable<TInitialContext>, [clientContext: TClientContext]>;
|
|
230
|
+
});
|
|
231
|
+
/**
|
|
232
|
+
* Create Server-side client from a procedure.
|
|
233
|
+
*
|
|
234
|
+
* @see {@link https://orpc.unnoq.com/docs/client/server-side Server-side Client Docs}
|
|
235
|
+
*/
|
|
236
|
+
declare function createProcedureClient<TInitialContext extends Context, TSchemas extends Schemas, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext>(lazyableProcedure: Lazyable<Procedure<TInitialContext, any, TSchemas, TErrorMap, TMeta>>, ...rest: MaybeOptionalOptions<CreateProcedureClientOptions<TInitialContext, TSchemas['outputSchema'], TErrorMap, TMeta, TClientContext>>): ProcedureClient<TClientContext, TSchemas, TErrorMap>;
|
|
237
|
+
|
|
238
|
+
export { resolveContractProcedures as $, isProcedure as F, createProcedureClient as H, Procedure as P, getRouter as S, createAccessibleLazyRouter as U, enhanceRouter as V, traverseContractProcedures as _, unlazyRouter as a1, mergeCurrentContext as m, createORPCErrorConstructorMap as n, LAZY_SYMBOL as o, lazyInternal as q, lazy as r, isLazy as s, getLazyMeta as t, unlazy as u, middlewareOutputFn as z };
|
|
239
|
+
export type { AnyMiddleware as A, ProcedureHandlerOptions as B, Context as C, ProcedureDef as D, EnhanceRouterOptions as E, ProcedureClientInterceptorOptions as G, InferRouterInitialContext as I, InferRouterInitialContexts as J, InferRouterCurrentContexts as K, Lazy as L, Middleware as M, InferRouterInputs as N, ORPCErrorConstructorMap as O, InferRouterOutputs as Q, Router as R, AccessibleLazyRouter as T, TraverseContractProceduresOptions as W, TraverseContractProcedureCallbackOptions as X, ContractProcedureCallbackOptions as Y, LazyTraverseContractProceduresOptions as Z, CreateProcedureClientOptions as a, UnlaziedRouter as a0, ProcedureClient as b, MergedInitialContext as c, MergedCurrentContext as d, ProcedureHandler as e, EnhancedRouter as f, MapInputMiddleware as g, AnyProcedure as h, Lazyable as i, AnyRouter as j, ORPCErrorConstructorMapItemOptions as k, ORPCErrorConstructorMapItem as l, LazyMeta as p, MiddlewareResult as v, MiddlewareNextFn as w, MiddlewareOutputFn as x, MiddlewareOptions as y };
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import { ErrorMap, ErrorMapItem, InferSchemaInput, Schemas, Meta, ContractProcedureDef, AnyContractRouter, ContractProcedure, InferProcedureClientInputs, InferSchemaOutput, EnhanceRouteOptions, MergedErrorMap, AnyContractProcedure, ContractRouter, AnySchema, ErrorFromErrorMap } from '@temporary-name/contract';
|
|
2
|
+
import { ORPCErrorCode, MaybeOptionalOptions, ORPCErrorOptions, ORPCError, Promisable, HTTPPath, ClientContext, Interceptor, PromiseWithError, Value, Client } from '@temporary-name/shared';
|
|
3
|
+
|
|
4
|
+
type Context = Record<PropertyKey, any>;
|
|
5
|
+
type MergedInitialContext<TInitial extends Context, TAdditional extends Context, TCurrent extends Context> = TInitial & Omit<TAdditional, keyof TCurrent>;
|
|
6
|
+
type MergedCurrentContext<T extends Context, U extends Context> = Omit<T, keyof U> & U;
|
|
7
|
+
declare function mergeCurrentContext<T extends Context, U extends Context>(context: T, other: U): MergedCurrentContext<T, U>;
|
|
8
|
+
|
|
9
|
+
type ORPCErrorConstructorMapItemOptions<TData> = Omit<ORPCErrorOptions<TData>, 'defined' | 'status'>;
|
|
10
|
+
type ORPCErrorConstructorMapItem<TCode extends ORPCErrorCode, TInData> = (...rest: MaybeOptionalOptions<ORPCErrorConstructorMapItemOptions<TInData>>) => ORPCError<TCode, TInData>;
|
|
11
|
+
type ORPCErrorConstructorMap<T extends ErrorMap> = {
|
|
12
|
+
[K in keyof T]: K extends ORPCErrorCode ? T[K] extends ErrorMapItem<infer UInputSchema> ? ORPCErrorConstructorMapItem<K, InferSchemaInput<UInputSchema>> : never : never;
|
|
13
|
+
};
|
|
14
|
+
declare function createORPCErrorConstructorMap<T extends ErrorMap>(errors: T): ORPCErrorConstructorMap<T>;
|
|
15
|
+
|
|
16
|
+
interface ProcedureHandlerOptions<TCurrentContext extends Context, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
|
|
17
|
+
context: TCurrentContext;
|
|
18
|
+
path: readonly string[];
|
|
19
|
+
procedure: Procedure<Context, Context, Schemas, ErrorMap, TMeta>;
|
|
20
|
+
signal?: AbortSignal;
|
|
21
|
+
lastEventId: string | undefined;
|
|
22
|
+
errors: TErrorConstructorMap;
|
|
23
|
+
}
|
|
24
|
+
interface ProcedureHandler<TCurrentContext extends Context, TInput extends {
|
|
25
|
+
path: any;
|
|
26
|
+
query: any;
|
|
27
|
+
body: any;
|
|
28
|
+
}, THandlerOutput, TErrorMap extends ErrorMap, TMeta extends Meta> {
|
|
29
|
+
(input: TInput, opt: ProcedureHandlerOptions<TCurrentContext, ORPCErrorConstructorMap<TErrorMap>, TMeta>): Promisable<THandlerOutput>;
|
|
30
|
+
}
|
|
31
|
+
interface ProcedureDef<TInitialContext extends Context, TCurrentContext extends Context, TSchemas extends Schemas, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedureDef<TSchemas, TErrorMap, TMeta> {
|
|
32
|
+
__initialContext?: (type: TInitialContext) => unknown;
|
|
33
|
+
middlewares: readonly AnyMiddleware[];
|
|
34
|
+
inputValidationIndex: number;
|
|
35
|
+
outputValidationIndex: number;
|
|
36
|
+
handler: ProcedureHandler<TCurrentContext, any, any, any, any>;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* This class represents a procedure.
|
|
40
|
+
*
|
|
41
|
+
* @see {@link https://orpc.unnoq.com/docs/procedure Procedure Docs}
|
|
42
|
+
*/
|
|
43
|
+
declare class Procedure<TInitialContext extends Context, TCurrentContext extends Context, TSchemas extends Schemas, TErrorMap extends ErrorMap, TMeta extends Meta> {
|
|
44
|
+
/**
|
|
45
|
+
* This property holds the defined options.
|
|
46
|
+
*/
|
|
47
|
+
'~orpc': ProcedureDef<TInitialContext, TCurrentContext, TSchemas, TErrorMap, TMeta>;
|
|
48
|
+
constructor(def: ProcedureDef<TInitialContext, TCurrentContext, TSchemas, TErrorMap, TMeta>);
|
|
49
|
+
}
|
|
50
|
+
type AnyProcedure = Procedure<any, any, Schemas, any, any>;
|
|
51
|
+
declare function isProcedure(item: unknown): item is AnyProcedure;
|
|
52
|
+
|
|
53
|
+
type MiddlewareResult<TOutContext extends Context, TOutput> = Promisable<{
|
|
54
|
+
output: TOutput;
|
|
55
|
+
context: TOutContext;
|
|
56
|
+
}>;
|
|
57
|
+
interface MiddlewareNextFn<TOutput> {
|
|
58
|
+
<U extends Context = {}>(options?: {
|
|
59
|
+
context?: U;
|
|
60
|
+
}): MiddlewareResult<U, TOutput>;
|
|
61
|
+
}
|
|
62
|
+
interface MiddlewareOutputFn<TOutput> {
|
|
63
|
+
(output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
|
|
64
|
+
}
|
|
65
|
+
interface MiddlewareOptions<TInContext extends Context, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
|
|
66
|
+
context: TInContext;
|
|
67
|
+
path: readonly string[];
|
|
68
|
+
procedure: Procedure<Context, Context, Schemas, ErrorMap, TMeta>;
|
|
69
|
+
signal?: AbortSignal;
|
|
70
|
+
lastEventId: string | undefined;
|
|
71
|
+
next: MiddlewareNextFn<TOutput>;
|
|
72
|
+
errors: TErrorConstructorMap;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* A function that represents a middleware.
|
|
76
|
+
*
|
|
77
|
+
* @see {@link https://orpc.unnoq.com/docs/middleware Middleware Docs}
|
|
78
|
+
*/
|
|
79
|
+
interface Middleware<TInContext extends Context, TOutContext extends Context, TInput, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
|
|
80
|
+
(options: MiddlewareOptions<TInContext, TOutput, TErrorConstructorMap, TMeta>, input: TInput, output: MiddlewareOutputFn<TOutput>): Promisable<MiddlewareResult<TOutContext, TOutput>>;
|
|
81
|
+
}
|
|
82
|
+
type AnyMiddleware = Middleware<any, any, any, any, any, any>;
|
|
83
|
+
interface MapInputMiddleware<TInput, TMappedInput> {
|
|
84
|
+
(input: TInput): TMappedInput;
|
|
85
|
+
}
|
|
86
|
+
declare function middlewareOutputFn<TOutput>(output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Represents a router, which defines a hierarchical structure of procedures.
|
|
90
|
+
*
|
|
91
|
+
* @info A procedure is a router too.
|
|
92
|
+
* @see {@link https://orpc.unnoq.com/docs/contract-first/define-contract#contract-router Contract Router Docs}
|
|
93
|
+
*/
|
|
94
|
+
type Router<T extends AnyContractRouter, TInitialContext extends Context> = T extends ContractProcedure<infer USchemas, infer UErrorMap, infer UMeta> ? Procedure<TInitialContext, any, USchemas, UErrorMap, UMeta> : {
|
|
95
|
+
[K in keyof T]: T[K] extends AnyContractRouter ? Lazyable<Router<T[K], TInitialContext>> : never;
|
|
96
|
+
};
|
|
97
|
+
type AnyRouter = Router<any, any>;
|
|
98
|
+
type InferRouterInitialContext<T extends AnyRouter> = T extends Router<any, infer UInitialContext> ? UInitialContext : never;
|
|
99
|
+
/**
|
|
100
|
+
* Infer all initial context of the router.
|
|
101
|
+
*
|
|
102
|
+
* @info A procedure is a router too.
|
|
103
|
+
* @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
|
|
104
|
+
*/
|
|
105
|
+
type InferRouterInitialContexts<T extends AnyRouter> = T extends Procedure<infer UInitialContext, any, any, any, any> ? UInitialContext : {
|
|
106
|
+
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInitialContexts<U> : never;
|
|
107
|
+
};
|
|
108
|
+
/**
|
|
109
|
+
* Infer all current context of the router.
|
|
110
|
+
*
|
|
111
|
+
* @info A procedure is a router too.
|
|
112
|
+
* @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
|
|
113
|
+
*/
|
|
114
|
+
type InferRouterCurrentContexts<T extends AnyRouter> = T extends Procedure<any, infer UCurrentContext, any, any, any> ? UCurrentContext : {
|
|
115
|
+
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterCurrentContexts<U> : never;
|
|
116
|
+
};
|
|
117
|
+
/**
|
|
118
|
+
* Infer all router inputs
|
|
119
|
+
*
|
|
120
|
+
* @info A procedure is a router too.
|
|
121
|
+
* @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
|
|
122
|
+
*/
|
|
123
|
+
type InferRouterInputs<T extends AnyRouter> = T extends Procedure<any, any, infer USchemas, any, any> ? InferProcedureClientInputs<USchemas> : {
|
|
124
|
+
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInputs<U> : never;
|
|
125
|
+
};
|
|
126
|
+
/**
|
|
127
|
+
* Infer all router outputs
|
|
128
|
+
*
|
|
129
|
+
* @info A procedure is a router too.
|
|
130
|
+
* @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
|
|
131
|
+
*/
|
|
132
|
+
type InferRouterOutputs<T extends AnyRouter> = T extends Procedure<any, any, infer USchemas, any, any> ? InferSchemaOutput<USchemas['outputSchema']> : {
|
|
133
|
+
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterOutputs<U> : never;
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
declare function getRouter<T extends Lazyable<AnyRouter | undefined>>(router: T, path: readonly string[]): T extends Lazy<any> ? Lazy<AnyRouter | undefined> : Lazyable<AnyRouter | undefined>;
|
|
137
|
+
type AccessibleLazyRouter<T extends Lazyable<AnyRouter | undefined>> = T extends Lazy<infer U extends AnyRouter | undefined | Lazy<AnyRouter | undefined>> ? AccessibleLazyRouter<U> : T extends AnyProcedure | undefined ? Lazy<T> : Lazy<T> & {
|
|
138
|
+
[K in keyof T]: T[K] extends Lazyable<AnyRouter> ? AccessibleLazyRouter<T[K]> : never;
|
|
139
|
+
};
|
|
140
|
+
declare function createAccessibleLazyRouter<T extends Lazy<AnyRouter | undefined>>(lazied: T): AccessibleLazyRouter<T>;
|
|
141
|
+
type EnhancedRouter<T extends Lazyable<AnyRouter>, TInitialContext extends Context, TCurrentContext extends Context, TErrorMap extends ErrorMap> = T extends Lazy<infer U extends AnyRouter> ? AccessibleLazyRouter<EnhancedRouter<U, TInitialContext, TCurrentContext, TErrorMap>> : T extends (Procedure<infer UInitialContext, infer UCurrentContext, infer USchemas, infer UErrorMap, infer UMeta>) ? Procedure<MergedInitialContext<TInitialContext, UInitialContext, TCurrentContext>, UCurrentContext, USchemas, MergedErrorMap<TErrorMap, UErrorMap>, UMeta> : {
|
|
142
|
+
[K in keyof T]: T[K] extends Lazyable<AnyRouter> ? EnhancedRouter<T[K], TInitialContext, TCurrentContext, TErrorMap> : never;
|
|
143
|
+
};
|
|
144
|
+
interface EnhanceRouterOptions<TErrorMap extends ErrorMap> extends EnhanceRouteOptions {
|
|
145
|
+
middlewares: readonly AnyMiddleware[];
|
|
146
|
+
errorMap: TErrorMap;
|
|
147
|
+
dedupeLeadingMiddlewares: boolean;
|
|
148
|
+
}
|
|
149
|
+
declare function enhanceRouter<T extends Lazyable<AnyRouter>, TInitialContext extends Context, TCurrentContext extends Context, TErrorMap extends ErrorMap>(router: T, options: EnhanceRouterOptions<TErrorMap>): EnhancedRouter<T, TInitialContext, TCurrentContext, TErrorMap>;
|
|
150
|
+
interface TraverseContractProceduresOptions {
|
|
151
|
+
router: AnyContractRouter | AnyRouter;
|
|
152
|
+
path: readonly string[];
|
|
153
|
+
}
|
|
154
|
+
interface TraverseContractProcedureCallbackOptions {
|
|
155
|
+
contract: AnyContractProcedure | AnyProcedure;
|
|
156
|
+
path: readonly string[];
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* @deprecated Use `TraverseContractProcedureCallbackOptions` instead.
|
|
160
|
+
*/
|
|
161
|
+
type ContractProcedureCallbackOptions = TraverseContractProcedureCallbackOptions;
|
|
162
|
+
interface LazyTraverseContractProceduresOptions {
|
|
163
|
+
router: Lazy<AnyRouter>;
|
|
164
|
+
path: readonly string[];
|
|
165
|
+
}
|
|
166
|
+
declare function traverseContractProcedures(options: TraverseContractProceduresOptions, callback: (options: TraverseContractProcedureCallbackOptions) => void, lazyOptions?: LazyTraverseContractProceduresOptions[]): LazyTraverseContractProceduresOptions[];
|
|
167
|
+
declare function resolveContractProcedures(options: TraverseContractProceduresOptions, callback: (options: TraverseContractProcedureCallbackOptions) => void): Promise<void>;
|
|
168
|
+
type UnlaziedRouter<T extends AnyRouter> = T extends AnyProcedure ? T : {
|
|
169
|
+
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? UnlaziedRouter<U> : never;
|
|
170
|
+
};
|
|
171
|
+
declare function unlazyRouter<T extends AnyRouter>(router: T): Promise<UnlaziedRouter<T>>;
|
|
172
|
+
|
|
173
|
+
declare const LAZY_SYMBOL: unique symbol;
|
|
174
|
+
interface LazyMeta {
|
|
175
|
+
prefix?: HTTPPath;
|
|
176
|
+
}
|
|
177
|
+
interface Lazy<T> {
|
|
178
|
+
[LAZY_SYMBOL]: {
|
|
179
|
+
loader: () => Promise<{
|
|
180
|
+
default: T;
|
|
181
|
+
}>;
|
|
182
|
+
meta: LazyMeta;
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
type Lazyable<T> = T | Lazy<T>;
|
|
186
|
+
/**
|
|
187
|
+
* @internal
|
|
188
|
+
*/
|
|
189
|
+
declare function lazyInternal<T>(loader: () => Promise<{
|
|
190
|
+
default: T;
|
|
191
|
+
}>, meta?: LazyMeta): Lazy<T>;
|
|
192
|
+
/**
|
|
193
|
+
* Creates a lazy-loaded item.
|
|
194
|
+
*
|
|
195
|
+
* @warning The `prefix` in `meta` only holds metadata and does not apply the prefix to the lazy router, use `os.prefix(...).lazyRoute(...)` instead.
|
|
196
|
+
*/
|
|
197
|
+
declare function lazy<T extends Router<ContractRouter<{}>, any>>(prefix: HTTPPath, loader: () => Promise<{
|
|
198
|
+
default: T;
|
|
199
|
+
}>): EnhancedRouter<Lazy<T>, {}, {}, {}>;
|
|
200
|
+
declare function isLazy(item: unknown): item is Lazy<any>;
|
|
201
|
+
declare function getLazyMeta(lazied: Lazy<any>): LazyMeta;
|
|
202
|
+
declare function unlazy<T extends Lazyable<any>>(lazied: T): Promise<{
|
|
203
|
+
default: T extends Lazy<infer U> ? U : T;
|
|
204
|
+
}>;
|
|
205
|
+
|
|
206
|
+
type ProcedureClient<TClientContext extends ClientContext, TSchemas extends Schemas, TErrorMap extends ErrorMap> = Client<TClientContext, InferProcedureClientInputs<TSchemas>, InferSchemaOutput<TSchemas['outputSchema']>, ErrorFromErrorMap<TErrorMap>>;
|
|
207
|
+
interface ProcedureClientInterceptorOptions<TInitialContext extends Context, TErrorMap extends ErrorMap, TMeta extends Meta> {
|
|
208
|
+
context: TInitialContext;
|
|
209
|
+
input: {
|
|
210
|
+
path: unknown;
|
|
211
|
+
query: unknown;
|
|
212
|
+
body: unknown;
|
|
213
|
+
};
|
|
214
|
+
errors: ORPCErrorConstructorMap<TErrorMap>;
|
|
215
|
+
path: readonly string[];
|
|
216
|
+
procedure: Procedure<Context, Context, Schemas, ErrorMap, TMeta>;
|
|
217
|
+
signal?: AbortSignal;
|
|
218
|
+
lastEventId: string | undefined;
|
|
219
|
+
}
|
|
220
|
+
type CreateProcedureClientOptions<TInitialContext extends Context, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext> = {
|
|
221
|
+
/**
|
|
222
|
+
* This is helpful for logging and analytics.
|
|
223
|
+
*/
|
|
224
|
+
path?: readonly string[];
|
|
225
|
+
interceptors?: Interceptor<ProcedureClientInterceptorOptions<TInitialContext, TErrorMap, TMeta>, PromiseWithError<InferSchemaOutput<TOutputSchema>, ErrorFromErrorMap<TErrorMap>>>[];
|
|
226
|
+
} & (Record<never, never> extends TInitialContext ? {
|
|
227
|
+
context?: Value<Promisable<TInitialContext>, [clientContext: TClientContext]>;
|
|
228
|
+
} : {
|
|
229
|
+
context: Value<Promisable<TInitialContext>, [clientContext: TClientContext]>;
|
|
230
|
+
});
|
|
231
|
+
/**
|
|
232
|
+
* Create Server-side client from a procedure.
|
|
233
|
+
*
|
|
234
|
+
* @see {@link https://orpc.unnoq.com/docs/client/server-side Server-side Client Docs}
|
|
235
|
+
*/
|
|
236
|
+
declare function createProcedureClient<TInitialContext extends Context, TSchemas extends Schemas, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext>(lazyableProcedure: Lazyable<Procedure<TInitialContext, any, TSchemas, TErrorMap, TMeta>>, ...rest: MaybeOptionalOptions<CreateProcedureClientOptions<TInitialContext, TSchemas['outputSchema'], TErrorMap, TMeta, TClientContext>>): ProcedureClient<TClientContext, TSchemas, TErrorMap>;
|
|
237
|
+
|
|
238
|
+
export { resolveContractProcedures as $, isProcedure as F, createProcedureClient as H, Procedure as P, getRouter as S, createAccessibleLazyRouter as U, enhanceRouter as V, traverseContractProcedures as _, unlazyRouter as a1, mergeCurrentContext as m, createORPCErrorConstructorMap as n, LAZY_SYMBOL as o, lazyInternal as q, lazy as r, isLazy as s, getLazyMeta as t, unlazy as u, middlewareOutputFn as z };
|
|
239
|
+
export type { AnyMiddleware as A, ProcedureHandlerOptions as B, Context as C, ProcedureDef as D, EnhanceRouterOptions as E, ProcedureClientInterceptorOptions as G, InferRouterInitialContext as I, InferRouterInitialContexts as J, InferRouterCurrentContexts as K, Lazy as L, Middleware as M, InferRouterInputs as N, ORPCErrorConstructorMap as O, InferRouterOutputs as Q, Router as R, AccessibleLazyRouter as T, TraverseContractProceduresOptions as W, TraverseContractProcedureCallbackOptions as X, ContractProcedureCallbackOptions as Y, LazyTraverseContractProceduresOptions as Z, CreateProcedureClientOptions as a, UnlaziedRouter as a0, ProcedureClient as b, MergedInitialContext as c, MergedCurrentContext as d, ProcedureHandler as e, EnhancedRouter as f, MapInputMiddleware as g, AnyProcedure as h, Lazyable as i, AnyRouter as j, ORPCErrorConstructorMapItemOptions as k, ORPCErrorConstructorMapItem as l, LazyMeta as p, MiddlewareResult as v, MiddlewareNextFn as w, MiddlewareOutputFn as x, MiddlewareOptions as y };
|