@temporary-name/server 1.9.3-alpha.e2d8d164da72fb570c2b14a4fa956c80f9e33cdc → 1.9.3-alpha.ec3bfb9dce56198911349c322c970208b21b50db
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 +4 -6
- package/dist/adapters/aws-lambda/index.d.ts +4 -6
- package/dist/adapters/aws-lambda/index.mjs +4 -4
- package/dist/adapters/fetch/index.d.mts +8 -86
- package/dist/adapters/fetch/index.d.ts +8 -86
- package/dist/adapters/fetch/index.mjs +16 -155
- package/dist/adapters/node/index.d.mts +8 -63
- package/dist/adapters/node/index.d.ts +8 -63
- package/dist/adapters/node/index.mjs +14 -120
- package/dist/adapters/standard/index.d.mts +10 -7
- package/dist/adapters/standard/index.d.ts +10 -7
- package/dist/adapters/standard/index.mjs +4 -4
- package/dist/helpers/index.mjs +3 -29
- package/dist/index.d.mts +121 -239
- package/dist/index.d.ts +121 -239
- package/dist/index.mjs +167 -352
- package/dist/openapi/index.d.mts +12 -28
- package/dist/openapi/index.d.ts +12 -28
- package/dist/openapi/index.mjs +59 -112
- package/dist/shared/server.C1RJffw4.mjs +30 -0
- package/dist/shared/server.CQIFwyhc.mjs +40 -0
- package/dist/shared/server.CVhIyQ4x.d.mts +41 -0
- package/dist/shared/server.CYa9puL2.mjs +403 -0
- package/dist/shared/server.ChOv1yG3.mjs +319 -0
- package/dist/shared/server.Cj3_Lp61.d.mts +373 -0
- package/dist/shared/server.Cj3_Lp61.d.ts +373 -0
- package/dist/shared/server.Cza0RB3u.mjs +160 -0
- package/dist/shared/server.D8RAzJ_p.d.ts +41 -0
- package/dist/shared/server.YUvuxHty.mjs +48 -0
- package/package.json +10 -28
- package/dist/plugins/index.d.mts +0 -160
- package/dist/plugins/index.d.ts +0 -160
- package/dist/plugins/index.mjs +0 -288
- package/dist/shared/server.B93y_8tj.d.mts +0 -23
- package/dist/shared/server.BYYf0Wn6.mjs +0 -202
- package/dist/shared/server.C3RuMHWl.d.mts +0 -192
- package/dist/shared/server.C3RuMHWl.d.ts +0 -192
- package/dist/shared/server.CT1xhSmE.d.mts +0 -56
- package/dist/shared/server.CqTex_jI.mjs +0 -265
- package/dist/shared/server.D_fags8X.d.ts +0 -23
- package/dist/shared/server.Kxw442A9.mjs +0 -247
- package/dist/shared/server.cjcgLdr1.d.ts +0 -56
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
import { Promisable, HTTPMethod, HTTPPath, OutputStructure, OpenAPI, IsEqual, StandardLazyRequest } from '@temporary-name/shared';
|
|
2
|
+
import * as z from '@temporary-name/zod';
|
|
3
|
+
|
|
4
|
+
type Context = object;
|
|
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 Meta = Record<string, any>;
|
|
13
|
+
declare function mergeMeta<T extends Meta>(meta1: T, meta2: T): T;
|
|
14
|
+
|
|
15
|
+
type MiddlewareResult<TOutContext extends Context, TOutput> = Promisable<{
|
|
16
|
+
output: TOutput;
|
|
17
|
+
context: TOutContext;
|
|
18
|
+
}>;
|
|
19
|
+
interface MiddlewareNextFn<TOutput> {
|
|
20
|
+
<U extends Context = {}>(options?: {
|
|
21
|
+
context?: U;
|
|
22
|
+
}): MiddlewareResult<U, TOutput>;
|
|
23
|
+
}
|
|
24
|
+
interface MiddlewareOutputFn<TOutput> {
|
|
25
|
+
(output: TOutput): MiddlewareResult<{}, TOutput>;
|
|
26
|
+
}
|
|
27
|
+
interface MiddlewareOptions<TInContext extends Context, TOutput, TMeta extends Meta> extends ProcedureHandlerOptions<TInContext, TMeta> {
|
|
28
|
+
next: MiddlewareNextFn<TOutput>;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* A function that represents a middleware.
|
|
32
|
+
*
|
|
33
|
+
* @see {@link https://orpc.unnoq.com/docs/middleware Middleware Docs}
|
|
34
|
+
*/
|
|
35
|
+
interface Middleware<TInContext extends Context, TOutContext extends Context, TInput, TOutput, TMeta extends Meta> {
|
|
36
|
+
(options: MiddlewareOptions<TInContext, TOutput, TMeta>, input: TInput, output: MiddlewareOutputFn<TOutput>): Promisable<MiddlewareResult<TOutContext, TOutput>>;
|
|
37
|
+
}
|
|
38
|
+
type AnyMiddleware = Middleware<any, any, any, any, any>;
|
|
39
|
+
interface MapInputMiddleware<TInput, TMappedInput> {
|
|
40
|
+
(input: TInput): TMappedInput;
|
|
41
|
+
}
|
|
42
|
+
declare function middlewareOutputFn<TOutput>(output: TOutput): MiddlewareResult<{}, TOutput>;
|
|
43
|
+
|
|
44
|
+
interface Route {
|
|
45
|
+
/**
|
|
46
|
+
* The HTTP method of the procedure.
|
|
47
|
+
* This option is typically relevant when integrating with OpenAPI.
|
|
48
|
+
*
|
|
49
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/routing OpenAPI Routing Docs}
|
|
50
|
+
*/
|
|
51
|
+
method?: HTTPMethod;
|
|
52
|
+
/**
|
|
53
|
+
* The HTTP path of the procedure.
|
|
54
|
+
* This option is typically relevant when integrating with OpenAPI.
|
|
55
|
+
*
|
|
56
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/routing OpenAPI Routing Docs}
|
|
57
|
+
*/
|
|
58
|
+
path?: HTTPPath;
|
|
59
|
+
/**
|
|
60
|
+
* The operation ID of the endpoint.
|
|
61
|
+
* This option is typically relevant when integrating with OpenAPI.
|
|
62
|
+
*
|
|
63
|
+
* @default Concatenation of router segments
|
|
64
|
+
*/
|
|
65
|
+
operationId?: string;
|
|
66
|
+
/**
|
|
67
|
+
* The summary of the procedure.
|
|
68
|
+
* This option is typically relevant when integrating with OpenAPI.
|
|
69
|
+
*
|
|
70
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
|
|
71
|
+
*/
|
|
72
|
+
summary?: string;
|
|
73
|
+
/**
|
|
74
|
+
* The description of the procedure.
|
|
75
|
+
* This option is typically relevant when integrating with OpenAPI.
|
|
76
|
+
*
|
|
77
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
|
|
78
|
+
*/
|
|
79
|
+
description?: string;
|
|
80
|
+
/**
|
|
81
|
+
* Marks the procedure as deprecated.
|
|
82
|
+
* This option is typically relevant when integrating with OpenAPI.
|
|
83
|
+
*
|
|
84
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
|
|
85
|
+
*/
|
|
86
|
+
deprecated?: boolean;
|
|
87
|
+
/**
|
|
88
|
+
* The tags of the procedure.
|
|
89
|
+
* This option is typically relevant when integrating with OpenAPI.
|
|
90
|
+
*
|
|
91
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
|
|
92
|
+
*/
|
|
93
|
+
tags?: readonly string[];
|
|
94
|
+
/**
|
|
95
|
+
* The status code of the response when the procedure is successful.
|
|
96
|
+
* The status code must be in the 200-399 range.
|
|
97
|
+
* This option is typically relevant when integrating with OpenAPI.
|
|
98
|
+
*
|
|
99
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/routing OpenAPI Routing Docs}
|
|
100
|
+
* @default 200
|
|
101
|
+
*/
|
|
102
|
+
successStatus?: number;
|
|
103
|
+
/**
|
|
104
|
+
* The description of the response when the procedure is successful.
|
|
105
|
+
* This option is typically relevant when integrating with OpenAPI.
|
|
106
|
+
*
|
|
107
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
|
|
108
|
+
* @default 'OK'
|
|
109
|
+
*/
|
|
110
|
+
successDescription?: string;
|
|
111
|
+
/**
|
|
112
|
+
* Determines how the response should be structured based on the output.
|
|
113
|
+
*
|
|
114
|
+
* @option 'compact'
|
|
115
|
+
* The output data is directly returned as the response body.
|
|
116
|
+
*
|
|
117
|
+
* @option 'detailed'
|
|
118
|
+
* Return an object with optional properties:
|
|
119
|
+
* - `status`: The response status (must be in 200-399 range) if not set fallback to `successStatus`.
|
|
120
|
+
* - `headers`: Custom headers to merge with the response headers (`Record<string, string | string[] | undefined>`)
|
|
121
|
+
* - `body`: The response body.
|
|
122
|
+
*
|
|
123
|
+
* Example:
|
|
124
|
+
* ```ts
|
|
125
|
+
* const output = {
|
|
126
|
+
* status: 201,
|
|
127
|
+
* headers: { 'x-custom-header': 'value' },
|
|
128
|
+
* body: { message: 'Hello, world!' },
|
|
129
|
+
* };
|
|
130
|
+
* ```
|
|
131
|
+
*
|
|
132
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/input-output-structure OpenAPI Input/Output Structure Docs}
|
|
133
|
+
* @default 'compact'
|
|
134
|
+
*/
|
|
135
|
+
outputStructure?: OutputStructure;
|
|
136
|
+
/**
|
|
137
|
+
* Override entire auto-generated OpenAPI Operation Object Specification.
|
|
138
|
+
*
|
|
139
|
+
* @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata Operation Metadata Docs}
|
|
140
|
+
*/
|
|
141
|
+
spec?: OpenAPI.OperationObject | ((current: OpenAPI.OperationObject) => OpenAPI.OperationObject);
|
|
142
|
+
}
|
|
143
|
+
interface EnhanceRouteOptions {
|
|
144
|
+
prefix?: HTTPPath;
|
|
145
|
+
tags?: readonly string[];
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
type Schema<TInput, TOutput> = z.core.$ZodType<TOutput, TInput>;
|
|
149
|
+
type AnySchema = z.core.$ZodType<any, any>;
|
|
150
|
+
type AnyShape = Readonly<Record<string, AnySchema>>;
|
|
151
|
+
type UnionToIntersection<T extends readonly object[]> = T extends readonly [infer First, ...infer Rest] ? First & (Rest extends readonly object[] ? UnionToIntersection<Rest> : {}) : {};
|
|
152
|
+
type WrapShape<U extends AnySchema | AnyShape> = U extends AnySchema ? U : U extends AnyShape ? z.KrustyObject<U, z.core.$strip> : never;
|
|
153
|
+
type SchemaIssue = z.core.$ZodIssue;
|
|
154
|
+
type InferSchemaInput<T extends AnySchema> = z.input<T>;
|
|
155
|
+
type InferSchemaOutput<T extends AnySchema> = z.output<T>;
|
|
156
|
+
type InferHandlerInputs<TSchemas extends Schemas> = {
|
|
157
|
+
path: InferSchemaOutput<TSchemas['pathSchema']>;
|
|
158
|
+
query: InferSchemaOutput<TSchemas['querySchema']>;
|
|
159
|
+
body: InferSchemaOutput<TSchemas['bodySchema']>;
|
|
160
|
+
};
|
|
161
|
+
type InferProcedureClientInputs<TSchemas extends Schemas> = {
|
|
162
|
+
path: InferSchemaInput<TSchemas['pathSchema']>;
|
|
163
|
+
query: InferSchemaInput<TSchemas['querySchema']>;
|
|
164
|
+
body: InferSchemaInput<TSchemas['bodySchema']>;
|
|
165
|
+
};
|
|
166
|
+
type TypeRest<TInput, TOutput> = [map: (input: TInput) => Promisable<TOutput>] | (IsEqual<TInput, TOutput> extends true ? [] : never);
|
|
167
|
+
interface Schemas {
|
|
168
|
+
pathSchema: AnySchema;
|
|
169
|
+
querySchema: AnySchema;
|
|
170
|
+
bodySchema: AnySchema;
|
|
171
|
+
outputSchema: AnySchema;
|
|
172
|
+
}
|
|
173
|
+
declare const initialSchemas: {
|
|
174
|
+
pathSchema: z.KrustyObject<{}, z.core.$strict, z.KrustyInternals<string>>;
|
|
175
|
+
querySchema: z.KrustyObject<{}, z.core.$strict, z.KrustyInternals<string>>;
|
|
176
|
+
bodySchema: z.KrustyObject<{}, z.core.$strict, z.KrustyInternals<string>>;
|
|
177
|
+
outputSchema: z.KrustyUnknown<z.KrustyInternals<string>>;
|
|
178
|
+
};
|
|
179
|
+
type InitialSchemas = typeof initialSchemas;
|
|
180
|
+
type MergedSchemas<T1 extends Schemas, T2 extends Partial<Schemas>> = {
|
|
181
|
+
pathSchema: T2['pathSchema'] extends AnySchema ? T2['pathSchema'] : T1['pathSchema'];
|
|
182
|
+
querySchema: T2['querySchema'] extends AnySchema ? T2['querySchema'] : T1['querySchema'];
|
|
183
|
+
bodySchema: T2['bodySchema'] extends AnySchema ? T2['bodySchema'] : T1['bodySchema'];
|
|
184
|
+
outputSchema: T2['outputSchema'] extends AnySchema ? T2['outputSchema'] : T1['outputSchema'];
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
type DefaultProcedureHandlerOptions = ProcedureHandlerOptions<Context, Meta>;
|
|
188
|
+
interface ProcedureHandlerOptions<TCurrentContext extends Context, TMeta extends Meta> {
|
|
189
|
+
context: TCurrentContext;
|
|
190
|
+
path: readonly string[];
|
|
191
|
+
procedure: Procedure<Context, Context, Schemas, TMeta>;
|
|
192
|
+
signal?: AbortSignal;
|
|
193
|
+
request?: StandardLazyRequest;
|
|
194
|
+
lastEventId: string | undefined;
|
|
195
|
+
}
|
|
196
|
+
interface ProcedureHandler<TCurrentContext extends Context, TInput extends {
|
|
197
|
+
path: any;
|
|
198
|
+
query: any;
|
|
199
|
+
body: any;
|
|
200
|
+
}, THandlerOutput, TMeta extends Meta> {
|
|
201
|
+
(input: TInput, opt: ProcedureHandlerOptions<TCurrentContext, TMeta>): Promisable<THandlerOutput>;
|
|
202
|
+
}
|
|
203
|
+
interface ContractDef<TSchemas extends Schemas, TMeta extends Meta> {
|
|
204
|
+
meta: TMeta;
|
|
205
|
+
route: Route;
|
|
206
|
+
schemas: TSchemas;
|
|
207
|
+
middlewares: readonly AnyMiddleware[];
|
|
208
|
+
authConfigs: TypedAuthConfig[];
|
|
209
|
+
inputValidationIndex: number;
|
|
210
|
+
outputValidationIndex: number;
|
|
211
|
+
}
|
|
212
|
+
type AnyContractDef = ContractDef<Schemas, Meta>;
|
|
213
|
+
declare class Contract<TDef extends AnyContractDef = AnyContractDef> {
|
|
214
|
+
/**
|
|
215
|
+
* This property holds the defined options.
|
|
216
|
+
*/
|
|
217
|
+
'~orpc': TDef;
|
|
218
|
+
constructor(def: TDef);
|
|
219
|
+
}
|
|
220
|
+
interface ProcedureDef<TInitialContext extends Context, TCurrentContext extends Context, TSchemas extends Schemas, TMeta extends Meta> extends ContractDef<TSchemas, TMeta> {
|
|
221
|
+
handler: ProcedureHandler<TCurrentContext, any, any, any>;
|
|
222
|
+
'~~DO_NOT_USE_initialContextHack'?: (type: TInitialContext) => unknown;
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* This class represents a procedure.
|
|
226
|
+
*
|
|
227
|
+
* @see {@link https://orpc.unnoq.com/docs/procedure Procedure Docs}
|
|
228
|
+
*/
|
|
229
|
+
declare class Procedure<TInitialContext extends Context, TCurrentContext extends Context, TSchemas extends Schemas, TMeta extends Meta> extends Contract<ProcedureDef<TInitialContext, TCurrentContext, TSchemas, TMeta>> {
|
|
230
|
+
}
|
|
231
|
+
type AnyProcedure = Procedure<any, any, Schemas, any>;
|
|
232
|
+
declare function isProcedure(item: unknown): item is AnyProcedure;
|
|
233
|
+
|
|
234
|
+
type ValidatedAuthContext = {};
|
|
235
|
+
interface BasicAuthConfig<TAuthContext extends ValidatedAuthContext, TCurrentContext extends Context, TMeta extends Meta> {
|
|
236
|
+
tokenPrefix?: string;
|
|
237
|
+
validate: (username: string, password: string, options: ProcedureHandlerOptions<TCurrentContext, TMeta>) => Promisable<TAuthContext>;
|
|
238
|
+
}
|
|
239
|
+
interface TokenAuthConfig<TAuthContext extends ValidatedAuthContext, TCurrentContext extends Context, TMeta extends Meta> {
|
|
240
|
+
tokenPrefix?: string;
|
|
241
|
+
validate: (token: string, options: ProcedureHandlerOptions<TCurrentContext, TMeta>) => Promisable<TAuthContext>;
|
|
242
|
+
}
|
|
243
|
+
interface NamedTokenAuthConfig<TAuthContext extends ValidatedAuthContext, TCurrentContext extends Context, TMeta extends Meta> extends TokenAuthConfig<TAuthContext, TCurrentContext, TMeta> {
|
|
244
|
+
name: string;
|
|
245
|
+
}
|
|
246
|
+
type AuthType = 'header' | 'query' | 'cookie' | 'bearer' | 'basic' | 'none';
|
|
247
|
+
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;
|
|
248
|
+
type TypedAuthConfig<TAuthType extends AuthType = AuthType> = {
|
|
249
|
+
type: TAuthType;
|
|
250
|
+
} & AuthConfig<TAuthType, object>;
|
|
251
|
+
|
|
252
|
+
type ContractRouter = Contract | {
|
|
253
|
+
[k: string]: ContractRouter;
|
|
254
|
+
};
|
|
255
|
+
/**
|
|
256
|
+
* Represents a router, which defines a hierarchical structure of procedures.
|
|
257
|
+
*
|
|
258
|
+
* @info A procedure is a router too.
|
|
259
|
+
* @see {@link https://orpc.unnoq.com/docs/contract-first/define-contract#contract-router Contract Router Docs}
|
|
260
|
+
*/
|
|
261
|
+
type Router<TInitialContext extends Context> = Procedure<TInitialContext, any, any, any> | {
|
|
262
|
+
[k: string]: Lazyable<Router<TInitialContext>>;
|
|
263
|
+
};
|
|
264
|
+
type AnyRouter = Router<any>;
|
|
265
|
+
type InferRouterInitialContext<T extends AnyRouter> = T extends Router<infer UInitialContext> ? UInitialContext : never;
|
|
266
|
+
/**
|
|
267
|
+
* Infer all initial context of the router.
|
|
268
|
+
*
|
|
269
|
+
* @info A procedure is a router too.
|
|
270
|
+
* @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
|
|
271
|
+
*/
|
|
272
|
+
type InferRouterInitialContexts<T extends AnyRouter> = T extends Procedure<infer UInitialContext, any, any, any> ? UInitialContext : {
|
|
273
|
+
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInitialContexts<U> : never;
|
|
274
|
+
};
|
|
275
|
+
/**
|
|
276
|
+
* Infer all current context of the router.
|
|
277
|
+
*
|
|
278
|
+
* @info A procedure is a router too.
|
|
279
|
+
* @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
|
|
280
|
+
*/
|
|
281
|
+
type InferRouterCurrentContexts<T extends AnyRouter> = T extends Procedure<any, infer UCurrentContext, any, any> ? UCurrentContext : {
|
|
282
|
+
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterCurrentContexts<U> : never;
|
|
283
|
+
};
|
|
284
|
+
/**
|
|
285
|
+
* Infer all router inputs
|
|
286
|
+
*
|
|
287
|
+
* @info A procedure is a router too.
|
|
288
|
+
* @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
|
|
289
|
+
*/
|
|
290
|
+
type InferRouterInputs<T extends AnyRouter> = T extends Procedure<any, any, infer USchemas, any> ? InferProcedureClientInputs<USchemas> : {
|
|
291
|
+
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInputs<U> : never;
|
|
292
|
+
};
|
|
293
|
+
/**
|
|
294
|
+
* Infer all router outputs
|
|
295
|
+
*
|
|
296
|
+
* @info A procedure is a router too.
|
|
297
|
+
* @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
|
|
298
|
+
*/
|
|
299
|
+
type InferRouterOutputs<T extends AnyRouter> = T extends Procedure<any, any, infer USchemas, any> ? InferSchemaOutput<USchemas['outputSchema']> : {
|
|
300
|
+
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterOutputs<U> : never;
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
declare function getRouter<T extends Lazyable<AnyRouter | undefined>>(router: T, path: readonly string[]): T extends Lazy<any> ? Lazy<AnyRouter | undefined> : Lazyable<AnyRouter | undefined>;
|
|
304
|
+
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> & {
|
|
305
|
+
[K in keyof T]: T[K] extends Lazyable<AnyRouter> ? AccessibleLazyRouter<T[K]> : never;
|
|
306
|
+
};
|
|
307
|
+
declare function createAccessibleLazyRouter<T extends Lazy<AnyRouter | undefined>>(lazied: T): AccessibleLazyRouter<T>;
|
|
308
|
+
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> : {
|
|
309
|
+
[K in keyof T]: T[K] extends Lazyable<AnyRouter> ? EnhancedRouter<T[K], TInitialContext, TCurrentContext> : never;
|
|
310
|
+
};
|
|
311
|
+
interface EnhanceRouterOptions extends EnhanceRouteOptions {
|
|
312
|
+
middlewares: readonly AnyMiddleware[];
|
|
313
|
+
dedupeLeadingMiddlewares: boolean;
|
|
314
|
+
}
|
|
315
|
+
declare function enhanceRouter<T extends Lazyable<AnyRouter>, TInitialContext extends Context, TCurrentContext extends Context>(router: T, options: EnhanceRouterOptions): EnhancedRouter<T, TInitialContext, TCurrentContext>;
|
|
316
|
+
interface TraverseContractProceduresOptions {
|
|
317
|
+
router: ContractRouter | AnyRouter;
|
|
318
|
+
path: readonly string[];
|
|
319
|
+
}
|
|
320
|
+
interface TraverseContractProcedureCallbackOptions {
|
|
321
|
+
contract: Contract;
|
|
322
|
+
path: readonly string[];
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* @deprecated Use `TraverseContractProcedureCallbackOptions` instead.
|
|
326
|
+
*/
|
|
327
|
+
type ContractProcedureCallbackOptions = TraverseContractProcedureCallbackOptions;
|
|
328
|
+
interface LazyTraverseContractProceduresOptions {
|
|
329
|
+
router: Lazy<AnyRouter>;
|
|
330
|
+
path: readonly string[];
|
|
331
|
+
}
|
|
332
|
+
declare function traverseContractProcedures(options: TraverseContractProceduresOptions, callback: (options: TraverseContractProcedureCallbackOptions) => void, lazyOptions?: LazyTraverseContractProceduresOptions[]): LazyTraverseContractProceduresOptions[];
|
|
333
|
+
declare function resolveContractProcedures(options: TraverseContractProceduresOptions, callback: (options: TraverseContractProcedureCallbackOptions) => void): Promise<void>;
|
|
334
|
+
type UnlaziedRouter<T extends AnyRouter> = T extends AnyProcedure ? T : {
|
|
335
|
+
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? UnlaziedRouter<U> : never;
|
|
336
|
+
};
|
|
337
|
+
declare function unlazyRouter<T extends AnyRouter>(router: T): Promise<UnlaziedRouter<T>>;
|
|
338
|
+
|
|
339
|
+
declare const LAZY_SYMBOL: unique symbol;
|
|
340
|
+
interface LazyMeta {
|
|
341
|
+
prefix?: HTTPPath;
|
|
342
|
+
}
|
|
343
|
+
interface Lazy<T> {
|
|
344
|
+
[LAZY_SYMBOL]: {
|
|
345
|
+
loader: () => Promise<{
|
|
346
|
+
default: T;
|
|
347
|
+
}>;
|
|
348
|
+
meta: LazyMeta;
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
type Lazyable<T> = T | Lazy<T>;
|
|
352
|
+
/**
|
|
353
|
+
* @internal
|
|
354
|
+
*/
|
|
355
|
+
declare function lazyInternal<T>(loader: () => Promise<{
|
|
356
|
+
default: T;
|
|
357
|
+
}>, meta?: LazyMeta): Lazy<T>;
|
|
358
|
+
/**
|
|
359
|
+
* Creates a lazy-loaded item.
|
|
360
|
+
*
|
|
361
|
+
* @warning The `prefix` in `meta` only holds metadata and does not apply the prefix to the lazy router, use `os.prefix(...).lazyRoute(...)` instead.
|
|
362
|
+
*/
|
|
363
|
+
declare function lazy<T extends Router<any>>(prefix: HTTPPath, loader: () => Promise<{
|
|
364
|
+
default: T;
|
|
365
|
+
}>): EnhancedRouter<Lazy<T>, {}, {}>;
|
|
366
|
+
declare function isLazy(item: unknown): item is Lazy<any>;
|
|
367
|
+
declare function getLazyMeta(lazied: Lazy<any>): LazyMeta;
|
|
368
|
+
declare function unlazy<T extends Lazyable<any>>(lazied: T): Promise<{
|
|
369
|
+
default: T extends Lazy<infer U> ? U : T;
|
|
370
|
+
}>;
|
|
371
|
+
|
|
372
|
+
export { lazyInternal as D, lazy as F, isLazy as G, getLazyMeta as H, unlazy as J, mergeMeta as K, Procedure as P, middlewareOutputFn as X, isProcedure as a0, getRouter as a6, createAccessibleLazyRouter as a8, enhanceRouter as a9, traverseContractProcedures as ae, resolveContractProcedures as af, unlazyRouter as ah, initialSchemas as aj, Contract as b, mergeCurrentContext as x, LAZY_SYMBOL as y };
|
|
373
|
+
export type { ProcedureDef as $, AnyShape as A, BuildContextWithAuth as B, Context as C, EnhanceRouterOptions as E, InferProcedureClientInputs as I, Lazyable as L, Meta as M, MiddlewareResult as N, MiddlewareNextFn as O, MiddlewareOutputFn as Q, Route as R, Schemas as S, MiddlewareOptions as T, UnionToIntersection as U, ValidatedAuthContext as V, WrapShape as W, DefaultProcedureHandlerOptions as Y, ProcedureHandlerOptions as Z, AnyContractDef as _, InferSchemaOutput as a, ContractRouter as a1, InferRouterInitialContexts as a2, InferRouterCurrentContexts as a3, InferRouterInputs as a4, InferRouterOutputs as a5, AccessibleLazyRouter as a7, TraverseContractProceduresOptions as aa, TraverseContractProcedureCallbackOptions as ab, ContractProcedureCallbackOptions as ac, LazyTraverseContractProceduresOptions as ad, UnlaziedRouter as ag, TypeRest as ai, InitialSchemas as ak, MergedSchemas as c, AnySchema as d, Middleware as e, MergedCurrentContext as f, MergedInitialContext as g, AuthType as h, AuthConfig as i, ProcedureHandler as j, InferHandlerInputs as k, InferSchemaInput as l, Router as m, EnhancedRouter as n, MapInputMiddleware as o, ContractDef as p, SchemaIssue as q, Schema as r, AnyMiddleware as s, Lazy as t, AnyProcedure as u, AnyRouter as v, InferRouterInitialContext as w, LazyMeta as z };
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { resolveMaybeOptionalOptions, toArray, value, runWithSpan, isAsyncIteratorObject, overlayProxy, asyncIteratorWithSpan, ORPCError } from '@temporary-name/shared';
|
|
2
|
+
import { HibernationEventIterator, mapEventIterator } from '@temporary-name/standard-server';
|
|
3
|
+
import { safeDecodeAsync, safeEncodeAsync } from '@temporary-name/zod';
|
|
4
|
+
import { u as unlazy, V as ValidationError } from './server.ChOv1yG3.mjs';
|
|
5
|
+
|
|
6
|
+
function mergeCurrentContext(context, other) {
|
|
7
|
+
return { ...context, ...other };
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function middlewareOutputFn(output) {
|
|
11
|
+
return { output, context: {} };
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function createProcedureClient(lazyableProcedure, ...rest) {
|
|
15
|
+
const options = resolveMaybeOptionalOptions(rest);
|
|
16
|
+
return async (...[input, callerOptions]) => {
|
|
17
|
+
const path = toArray(options.path);
|
|
18
|
+
const { default: procedure } = await unlazy(lazyableProcedure);
|
|
19
|
+
const clientContext = callerOptions?.context ?? {};
|
|
20
|
+
const context = await value(options.context ?? {}, clientContext);
|
|
21
|
+
const output = await runWithSpan({ name: "call_procedure", signal: callerOptions?.signal }, (span) => {
|
|
22
|
+
span?.setAttribute("procedure.path", [...path]);
|
|
23
|
+
return executeProcedureInternal(procedure, input, {
|
|
24
|
+
context,
|
|
25
|
+
path,
|
|
26
|
+
procedure,
|
|
27
|
+
request: callerOptions?.request,
|
|
28
|
+
signal: callerOptions?.signal,
|
|
29
|
+
lastEventId: callerOptions?.lastEventId
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
if (isAsyncIteratorObject(output)) {
|
|
33
|
+
if (output instanceof HibernationEventIterator) {
|
|
34
|
+
return output;
|
|
35
|
+
}
|
|
36
|
+
return overlayProxy(
|
|
37
|
+
output,
|
|
38
|
+
mapEventIterator(
|
|
39
|
+
asyncIteratorWithSpan(
|
|
40
|
+
{ name: "consume_event_iterator_output", signal: callerOptions?.signal },
|
|
41
|
+
output
|
|
42
|
+
),
|
|
43
|
+
{
|
|
44
|
+
value: (v) => v,
|
|
45
|
+
error: async (e) => e
|
|
46
|
+
}
|
|
47
|
+
)
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
return output;
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
async function validateInput(procedure, input) {
|
|
54
|
+
const schemas = procedure["~orpc"].schemas;
|
|
55
|
+
return runWithSpan({ name: "validate_input" }, async () => {
|
|
56
|
+
const resultBody = await safeDecodeAsync(schemas.bodySchema, input.body, { parseType: "body" });
|
|
57
|
+
const resultPath = await safeDecodeAsync(schemas.pathSchema, input.path, { parseType: "path" });
|
|
58
|
+
const resultQuery = await safeDecodeAsync(schemas.querySchema, input.query, { parseType: "query" });
|
|
59
|
+
const issues = [];
|
|
60
|
+
if (!resultBody.success) {
|
|
61
|
+
issues.push(...resultBody.error.issues.map((i) => ({ ...i, path: ["body", ...i.path] })));
|
|
62
|
+
}
|
|
63
|
+
if (!resultPath.success) {
|
|
64
|
+
issues.push(...resultPath.error.issues.map((i) => ({ ...i, path: ["path", ...i.path] })));
|
|
65
|
+
}
|
|
66
|
+
if (!resultQuery.success) {
|
|
67
|
+
issues.push(...resultQuery.error.issues.map((i) => ({ ...i, path: ["query", ...i.path] })));
|
|
68
|
+
}
|
|
69
|
+
if (issues.length > 0) {
|
|
70
|
+
throw new ORPCError("BAD_REQUEST", {
|
|
71
|
+
message: "Input validation failed",
|
|
72
|
+
data: {
|
|
73
|
+
issues
|
|
74
|
+
},
|
|
75
|
+
cause: new ValidationError({
|
|
76
|
+
message: "Input validation failed",
|
|
77
|
+
issues,
|
|
78
|
+
data: input
|
|
79
|
+
})
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
const results = {
|
|
83
|
+
body: resultBody.data,
|
|
84
|
+
path: resultPath.data,
|
|
85
|
+
query: resultQuery.data
|
|
86
|
+
};
|
|
87
|
+
return results;
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
async function validateOutput(procedure, output) {
|
|
91
|
+
const schema = procedure["~orpc"].schemas.outputSchema;
|
|
92
|
+
if (!schema) {
|
|
93
|
+
return output;
|
|
94
|
+
}
|
|
95
|
+
return runWithSpan({ name: "validate_output" }, async () => {
|
|
96
|
+
const result = await safeEncodeAsync(schema, output, { parseType: "output" });
|
|
97
|
+
if (!result.success) {
|
|
98
|
+
throw new ORPCError("INTERNAL_SERVER_ERROR", {
|
|
99
|
+
message: "Output validation failed",
|
|
100
|
+
cause: new ValidationError({
|
|
101
|
+
message: "Output validation failed",
|
|
102
|
+
issues: result.error.issues,
|
|
103
|
+
data: output
|
|
104
|
+
})
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
return result.data;
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
async function executeProcedureInternal(procedure, input, options) {
|
|
111
|
+
const middlewares = procedure["~orpc"].middlewares;
|
|
112
|
+
const inputValidationIndex = Math.min(
|
|
113
|
+
Math.max(0, procedure["~orpc"].inputValidationIndex),
|
|
114
|
+
middlewares.length
|
|
115
|
+
);
|
|
116
|
+
const outputValidationIndex = Math.min(
|
|
117
|
+
Math.max(0, procedure["~orpc"].outputValidationIndex),
|
|
118
|
+
middlewares.length
|
|
119
|
+
);
|
|
120
|
+
const next = async (index, context, input2) => {
|
|
121
|
+
let currentInput = input2;
|
|
122
|
+
if (index === inputValidationIndex) {
|
|
123
|
+
currentInput = await validateInput(procedure, currentInput);
|
|
124
|
+
}
|
|
125
|
+
const mid = middlewares[index];
|
|
126
|
+
const output = mid ? await runWithSpan({ name: `middleware.${mid.name}`, signal: options.signal }, async (span) => {
|
|
127
|
+
span?.setAttribute("middleware.index", index);
|
|
128
|
+
span?.setAttribute("middleware.name", mid.name);
|
|
129
|
+
const result = await mid(
|
|
130
|
+
{
|
|
131
|
+
...options,
|
|
132
|
+
context,
|
|
133
|
+
next: async (nextOptions) => {
|
|
134
|
+
const nextContext = nextOptions?.context ?? {};
|
|
135
|
+
return {
|
|
136
|
+
output: await next(index + 1, mergeCurrentContext(context, nextContext), currentInput),
|
|
137
|
+
// NB: Pretty sure this isn't used (or meant to be used) at runtime, it's just there
|
|
138
|
+
// to get type inference in the builder (via the caller returning the output of next() in
|
|
139
|
+
// the middleware function)
|
|
140
|
+
context: nextContext
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
},
|
|
144
|
+
currentInput,
|
|
145
|
+
middlewareOutputFn
|
|
146
|
+
);
|
|
147
|
+
return result.output;
|
|
148
|
+
}) : await runWithSpan(
|
|
149
|
+
{ name: "handler", signal: options.signal },
|
|
150
|
+
() => procedure["~orpc"].handler(currentInput, { ...options, context })
|
|
151
|
+
);
|
|
152
|
+
if (index === outputValidationIndex) {
|
|
153
|
+
return await validateOutput(procedure, output);
|
|
154
|
+
}
|
|
155
|
+
return output;
|
|
156
|
+
};
|
|
157
|
+
return next(0, options.context, input);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export { middlewareOutputFn as a, createProcedureClient as c, mergeCurrentContext as m };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { HTTPPath, StandardResponse, StandardLazyRequest } from '@temporary-name/shared';
|
|
2
|
+
import { C as Context, m as Router } from './server.Cj3_Lp61.js';
|
|
3
|
+
|
|
4
|
+
interface StandardHandleOptions<T extends Context> {
|
|
5
|
+
prefix?: HTTPPath;
|
|
6
|
+
context: T;
|
|
7
|
+
}
|
|
8
|
+
type StandardHandleResult = {
|
|
9
|
+
matched: true;
|
|
10
|
+
response: StandardResponse;
|
|
11
|
+
} | {
|
|
12
|
+
matched: false;
|
|
13
|
+
response: undefined;
|
|
14
|
+
};
|
|
15
|
+
interface StandardHandlerOptions<_TContext extends Context> {
|
|
16
|
+
}
|
|
17
|
+
declare class StandardHandler<T extends Context> {
|
|
18
|
+
private readonly matcher;
|
|
19
|
+
constructor(router: Router<T>, _options: NoInfer<StandardHandlerOptions<T>>);
|
|
20
|
+
handle(request: StandardLazyRequest, options: StandardHandleOptions<T>): Promise<StandardHandleResult>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
type FriendlyStandardHandleOptions<T extends Context> = Omit<StandardHandleOptions<T>, 'context'> & (object extends T ? {
|
|
24
|
+
context?: T;
|
|
25
|
+
} : {
|
|
26
|
+
context: T;
|
|
27
|
+
});
|
|
28
|
+
declare function resolveFriendlyStandardHandleOptions<T extends Context>(options: FriendlyStandardHandleOptions<T>): StandardHandleOptions<T>;
|
|
29
|
+
/**
|
|
30
|
+
* {@link https://github.com/unjs/rou3}
|
|
31
|
+
*
|
|
32
|
+
* @internal
|
|
33
|
+
*/
|
|
34
|
+
declare function toRou3Pattern(path: HTTPPath): string;
|
|
35
|
+
/**
|
|
36
|
+
* @internal
|
|
37
|
+
*/
|
|
38
|
+
declare function decodeParams(params: Record<string, string>): Record<string, string>;
|
|
39
|
+
|
|
40
|
+
export { StandardHandler as c, decodeParams as d, resolveFriendlyStandardHandleOptions as r, toRou3Pattern as t };
|
|
41
|
+
export type { FriendlyStandardHandleOptions as F, StandardHandleOptions as S, StandardHandleResult as a, StandardHandlerOptions as b };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { isAsyncIteratorObject, ORPCError } from '@temporary-name/shared';
|
|
2
|
+
import { mapEventIterator } from '@temporary-name/standard-server';
|
|
3
|
+
import { custom, safeParseAsync } from '@temporary-name/zod';
|
|
4
|
+
import { V as ValidationError } from './server.ChOv1yG3.mjs';
|
|
5
|
+
|
|
6
|
+
const EVENT_ITERATOR_DETAILS_SYMBOL = Symbol("ORPC_EVENT_ITERATOR_DETAILS");
|
|
7
|
+
function eventIterator(yields, returns) {
|
|
8
|
+
const schema = custom(
|
|
9
|
+
(iterator) => isAsyncIteratorObject(iterator)
|
|
10
|
+
).transform((iterator) => {
|
|
11
|
+
const mapped = mapEventIterator(iterator, {
|
|
12
|
+
async value(value, done) {
|
|
13
|
+
const schema2 = done ? returns : yields;
|
|
14
|
+
if (!schema2) {
|
|
15
|
+
return value;
|
|
16
|
+
}
|
|
17
|
+
const result = await safeParseAsync(schema2, value);
|
|
18
|
+
if (result.success) {
|
|
19
|
+
return result.data;
|
|
20
|
+
} else {
|
|
21
|
+
throw new ORPCError("EVENT_ITERATOR_VALIDATION_FAILED", {
|
|
22
|
+
message: "Event iterator validation failed",
|
|
23
|
+
cause: new ValidationError({
|
|
24
|
+
issues: result.error.issues,
|
|
25
|
+
message: "Event iterator validation failed",
|
|
26
|
+
data: value
|
|
27
|
+
})
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
error: async (error) => error
|
|
32
|
+
});
|
|
33
|
+
return mapped;
|
|
34
|
+
});
|
|
35
|
+
schema[EVENT_ITERATOR_DETAILS_SYMBOL] = {
|
|
36
|
+
yields,
|
|
37
|
+
returns
|
|
38
|
+
};
|
|
39
|
+
return schema;
|
|
40
|
+
}
|
|
41
|
+
function getEventIteratorSchemaDetails(schema) {
|
|
42
|
+
if (schema === void 0) {
|
|
43
|
+
return void 0;
|
|
44
|
+
}
|
|
45
|
+
return schema[EVENT_ITERATOR_DETAILS_SYMBOL];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export { eventIterator as e, getEventIteratorSchemaDetails as g };
|