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