@temporary-name/server 1.9.3-alpha.592ae1e02e18c5274906b132313b083116248636 → 1.9.3-alpha.5c041722542bb790586629fc98d4c72d9f6ee65e

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.
Files changed (29) hide show
  1. package/dist/adapters/aws-lambda/index.d.mts +3 -3
  2. package/dist/adapters/aws-lambda/index.d.ts +3 -3
  3. package/dist/adapters/aws-lambda/index.mjs +4 -6
  4. package/dist/adapters/fetch/index.d.mts +3 -3
  5. package/dist/adapters/fetch/index.d.ts +3 -3
  6. package/dist/adapters/fetch/index.mjs +4 -6
  7. package/dist/adapters/node/index.d.mts +3 -3
  8. package/dist/adapters/node/index.d.ts +3 -3
  9. package/dist/adapters/node/index.mjs +4 -6
  10. package/dist/adapters/standard/index.d.mts +14 -30
  11. package/dist/adapters/standard/index.d.ts +14 -30
  12. package/dist/adapters/standard/index.mjs +4 -6
  13. package/dist/index.d.mts +60 -334
  14. package/dist/index.d.ts +60 -334
  15. package/dist/index.mjs +111 -354
  16. package/dist/openapi/index.mjs +30 -64
  17. package/dist/plugins/index.d.mts +2 -2
  18. package/dist/plugins/index.d.ts +2 -2
  19. package/dist/shared/{server.BKSOrA6h.d.mts → server.B5czSQQh.d.mts} +114 -71
  20. package/dist/shared/{server.BKSOrA6h.d.ts → server.B5czSQQh.d.ts} +114 -71
  21. package/dist/shared/server.BdGbIyq8.mjs +396 -0
  22. package/dist/shared/{server.BeuTpcmO.d.mts → server.Bk8Hmx7Z.d.mts} +2 -2
  23. package/dist/shared/{server.CQyYNJ1H.d.ts → server.C4l2ZA3m.d.ts} +1 -2
  24. package/dist/shared/{server.SLLuK6_v.d.ts → server.D7UjcuIv.d.ts} +2 -2
  25. package/dist/shared/{server.BEHw7Eyx.mjs → server.Kxw442A9.mjs} +1 -1
  26. package/dist/shared/{server.DLsti1Pv.mjs → server.SmOM3Fae.mjs} +58 -89
  27. package/dist/shared/{server.C1fnTLq0.d.mts → server.TgmlNh8r.d.mts} +1 -2
  28. package/package.json +10 -9
  29. package/dist/shared/server.BKh8I1Ny.mjs +0 -239
@@ -1,5 +1,5 @@
1
- import { ErrorMap, ErrorMapItem, InferSchemaInput, AnySchema, Meta, ContractProcedureDef, InferSchemaOutput, ErrorFromErrorMap, AnyContractRouter, ContractProcedure } from '@temporary-name/contract';
2
- import { ORPCErrorCode, MaybeOptionalOptions, ORPCErrorOptions, ORPCError, HTTPPath, Promisable, ClientContext, Interceptor, PromiseWithError, Value, Client } from '@temporary-name/shared';
1
+ import { ErrorMap, ErrorMapItem, InferSchemaInput, Schemas, Meta, ContractProcedureDef, AnyContractRouter, ContractProcedure, 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
3
 
4
4
  type Context = Record<PropertyKey, any>;
5
5
  type MergedInitialContext<TInitial extends Context, TAdditional extends Context, TCurrent extends Context> = TInitial & Omit<TAdditional, keyof TCurrent>;
@@ -13,38 +13,11 @@ type ORPCErrorConstructorMap<T extends ErrorMap> = {
13
13
  };
14
14
  declare function createORPCErrorConstructorMap<T extends ErrorMap>(errors: T): ORPCErrorConstructorMap<T>;
15
15
 
16
- declare const LAZY_SYMBOL: unique symbol;
17
- interface LazyMeta {
18
- prefix?: HTTPPath;
19
- }
20
- interface Lazy<T> {
21
- [LAZY_SYMBOL]: {
22
- loader: () => Promise<{
23
- default: T;
24
- }>;
25
- meta: LazyMeta;
26
- };
27
- }
28
- type Lazyable<T> = T | Lazy<T>;
29
- /**
30
- * Creates a lazy-loaded item.
31
- *
32
- * @warning The `prefix` in `meta` only holds metadata and does not apply the prefix to the lazy router, use `os.prefix(...).lazyRoute(...)` instead.
33
- */
34
- declare function lazy<T>(loader: () => Promise<{
35
- default: T;
36
- }>, meta?: LazyMeta): Lazy<T>;
37
- declare function isLazy(item: unknown): item is Lazy<any>;
38
- declare function getLazyMeta(lazied: Lazy<any>): LazyMeta;
39
- declare function unlazy<T extends Lazyable<any>>(lazied: T): Promise<{
40
- default: T extends Lazy<infer U> ? U : T;
41
- }>;
42
-
43
16
  interface ProcedureHandlerOptions<TCurrentContext extends Context, TInput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
44
17
  context: TCurrentContext;
45
18
  input: TInput;
46
19
  path: readonly string[];
47
- procedure: Procedure<Context, Context, AnySchema, AnySchema, ErrorMap, TMeta>;
20
+ procedure: Procedure<Context, Context, Schemas, ErrorMap, TMeta>;
48
21
  signal?: AbortSignal;
49
22
  lastEventId: string | undefined;
50
23
  errors: TErrorConstructorMap;
@@ -52,7 +25,7 @@ interface ProcedureHandlerOptions<TCurrentContext extends Context, TInput, TErro
52
25
  interface ProcedureHandler<TCurrentContext extends Context, TInput, THandlerOutput, TErrorMap extends ErrorMap, TMeta extends Meta> {
53
26
  (opt: ProcedureHandlerOptions<TCurrentContext, TInput, ORPCErrorConstructorMap<TErrorMap>, TMeta>): Promisable<THandlerOutput>;
54
27
  }
55
- interface ProcedureDef<TInitialContext extends Context, TCurrentContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap, TMeta> {
28
+ interface ProcedureDef<TInitialContext extends Context, TCurrentContext extends Context, TSchemas extends Schemas, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedureDef<TSchemas, TErrorMap, TMeta> {
56
29
  __initialContext?: (type: TInitialContext) => unknown;
57
30
  middlewares: readonly AnyMiddleware[];
58
31
  inputValidationIndex: number;
@@ -64,14 +37,14 @@ interface ProcedureDef<TInitialContext extends Context, TCurrentContext extends
64
37
  *
65
38
  * @see {@link https://orpc.unnoq.com/docs/procedure Procedure Docs}
66
39
  */
67
- declare class Procedure<TInitialContext extends Context, TCurrentContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> {
40
+ declare class Procedure<TInitialContext extends Context, TCurrentContext extends Context, TSchemas extends Schemas, TErrorMap extends ErrorMap, TMeta extends Meta> {
68
41
  /**
69
42
  * This property holds the defined options.
70
43
  */
71
- '~orpc': ProcedureDef<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, TErrorMap, TMeta>;
72
- constructor(def: ProcedureDef<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, TErrorMap, TMeta>);
44
+ '~orpc': ProcedureDef<TInitialContext, TCurrentContext, TSchemas, TErrorMap, TMeta>;
45
+ constructor(def: ProcedureDef<TInitialContext, TCurrentContext, TSchemas, TErrorMap, TMeta>);
73
46
  }
74
- type AnyProcedure = Procedure<any, any, any, any, any, any>;
47
+ type AnyProcedure = Procedure<any, any, Schemas, any, any>;
75
48
  declare function isProcedure(item: unknown): item is AnyProcedure;
76
49
 
77
50
  type MiddlewareResult<TOutContext extends Context, TOutput> = Promisable<{
@@ -92,7 +65,7 @@ interface MiddlewareOutputFn<TOutput> {
92
65
  interface MiddlewareOptions<TInContext extends Context, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
93
66
  context: TInContext;
94
67
  path: readonly string[];
95
- procedure: Procedure<Context, Context, AnySchema, AnySchema, ErrorMap, TMeta>;
68
+ procedure: Procedure<Context, Context, Schemas, ErrorMap, TMeta>;
96
69
  signal?: AbortSignal;
97
70
  lastEventId: string | undefined;
98
71
  next: MiddlewareNextFn<TOutput>;
@@ -112,41 +85,13 @@ interface MapInputMiddleware<TInput, TMappedInput> {
112
85
  }
113
86
  declare function middlewareOutputFn<TOutput>(output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
114
87
 
115
- type ProcedureClient<TClientContext extends ClientContext, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> = Client<TClientContext, InferSchemaInput<TInputSchema>, InferSchemaOutput<TOutputSchema>, ErrorFromErrorMap<TErrorMap>>;
116
- interface ProcedureClientInterceptorOptions<TInitialContext extends Context, TErrorMap extends ErrorMap, TMeta extends Meta> {
117
- context: TInitialContext;
118
- input: unknown;
119
- errors: ORPCErrorConstructorMap<TErrorMap>;
120
- path: readonly string[];
121
- procedure: Procedure<Context, Context, AnySchema, AnySchema, ErrorMap, TMeta>;
122
- signal?: AbortSignal;
123
- lastEventId: string | undefined;
124
- }
125
- type CreateProcedureClientOptions<TInitialContext extends Context, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext> = {
126
- /**
127
- * This is helpful for logging and analytics.
128
- */
129
- path?: readonly string[];
130
- interceptors?: Interceptor<ProcedureClientInterceptorOptions<TInitialContext, TErrorMap, TMeta>, PromiseWithError<InferSchemaOutput<TOutputSchema>, ErrorFromErrorMap<TErrorMap>>>[];
131
- } & (Record<never, never> extends TInitialContext ? {
132
- context?: Value<Promisable<TInitialContext>, [clientContext: TClientContext]>;
133
- } : {
134
- context: Value<Promisable<TInitialContext>, [clientContext: TClientContext]>;
135
- });
136
- /**
137
- * Create Server-side client from a procedure.
138
- *
139
- * @see {@link https://orpc.unnoq.com/docs/client/server-side Server-side Client Docs}
140
- */
141
- declare function createProcedureClient<TInitialContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext>(lazyableProcedure: Lazyable<Procedure<TInitialContext, any, TInputSchema, TOutputSchema, TErrorMap, TMeta>>, ...rest: MaybeOptionalOptions<CreateProcedureClientOptions<TInitialContext, TOutputSchema, TErrorMap, TMeta, TClientContext>>): ProcedureClient<TClientContext, TInputSchema, TOutputSchema, TErrorMap>;
142
-
143
88
  /**
144
89
  * Represents a router, which defines a hierarchical structure of procedures.
145
90
  *
146
91
  * @info A procedure is a router too.
147
92
  * @see {@link https://orpc.unnoq.com/docs/contract-first/define-contract#contract-router Contract Router Docs}
148
93
  */
149
- type Router<T extends AnyContractRouter, TInitialContext extends Context> = T extends ContractProcedure<infer UInputSchema, infer UOutputSchema, infer UErrorMap, infer UMeta> ? Procedure<TInitialContext, any, UInputSchema, UOutputSchema, UErrorMap, UMeta> : {
94
+ type Router<T extends AnyContractRouter, TInitialContext extends Context> = T extends ContractProcedure<infer USchemas, infer UErrorMap, infer UMeta> ? Procedure<TInitialContext, any, USchemas, UErrorMap, UMeta> : {
150
95
  [K in keyof T]: T[K] extends AnyContractRouter ? Lazyable<Router<T[K], TInitialContext>> : never;
151
96
  };
152
97
  type AnyRouter = Router<any, any>;
@@ -157,7 +102,7 @@ type InferRouterInitialContext<T extends AnyRouter> = T extends Router<any, infe
157
102
  * @info A procedure is a router too.
158
103
  * @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
159
104
  */
160
- type InferRouterInitialContexts<T extends AnyRouter> = T extends Procedure<infer UInitialContext, any, any, any, any, any> ? UInitialContext : {
105
+ type InferRouterInitialContexts<T extends AnyRouter> = T extends Procedure<infer UInitialContext, any, any, any, any> ? UInitialContext : {
161
106
  [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInitialContexts<U> : never;
162
107
  };
163
108
  /**
@@ -166,7 +111,7 @@ type InferRouterInitialContexts<T extends AnyRouter> = T extends Procedure<infer
166
111
  * @info A procedure is a router too.
167
112
  * @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
168
113
  */
169
- type InferRouterCurrentContexts<T extends AnyRouter> = T extends Procedure<any, infer UCurrentContext, any, any, any, any> ? UCurrentContext : {
114
+ type InferRouterCurrentContexts<T extends AnyRouter> = T extends Procedure<any, infer UCurrentContext, any, any, any> ? UCurrentContext : {
170
115
  [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterCurrentContexts<U> : never;
171
116
  };
172
117
  /**
@@ -175,7 +120,7 @@ type InferRouterCurrentContexts<T extends AnyRouter> = T extends Procedure<any,
175
120
  * @info A procedure is a router too.
176
121
  * @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
177
122
  */
178
- type InferRouterInputs<T extends AnyRouter> = T extends Procedure<any, any, infer UInputSchema, any, any, any> ? InferSchemaInput<UInputSchema> : {
123
+ type InferRouterInputs<T extends AnyRouter> = T extends Procedure<any, any, infer USchemas, any, any> ? InferSchemaInput<USchemas['inputSchema']> : {
179
124
  [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInputs<U> : never;
180
125
  };
181
126
  /**
@@ -184,9 +129,107 @@ type InferRouterInputs<T extends AnyRouter> = T extends Procedure<any, any, infe
184
129
  * @info A procedure is a router too.
185
130
  * @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
186
131
  */
187
- type InferRouterOutputs<T extends AnyRouter> = T extends Procedure<any, any, any, infer UOutputSchema, any, any> ? InferSchemaOutput<UOutputSchema> : {
132
+ type InferRouterOutputs<T extends AnyRouter> = T extends Procedure<any, any, infer USchemas, any, any> ? InferSchemaOutput<USchemas['outputSchema']> : {
188
133
  [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterOutputs<U> : never;
189
134
  };
190
135
 
191
- export { isProcedure as D, createProcedureClient as F, Procedure as P, createORPCErrorConstructorMap as l, mergeCurrentContext as m, LAZY_SYMBOL as n, lazy as p, isLazy as q, getLazyMeta as r, unlazy as u, middlewareOutputFn as y };
192
- export type { AnyMiddleware as A, ProcedureDef as B, Context as C, ProcedureClientInterceptorOptions as E, InferRouterInitialContexts as G, InferRouterCurrentContexts as H, InferRouterInitialContext as I, InferRouterInputs as J, InferRouterOutputs as K, Lazyable as L, Middleware as M, ORPCErrorConstructorMap as O, Router as R, MergedInitialContext as a, MergedCurrentContext as b, MapInputMiddleware as c, CreateProcedureClientOptions as d, ProcedureClient as e, AnyRouter as f, Lazy as g, AnyProcedure as h, ProcedureHandler as i, ORPCErrorConstructorMapItemOptions as j, ORPCErrorConstructorMapItem as k, LazyMeta as o, MiddlewareResult as s, MiddlewareNextFnOptions as t, MiddlewareNextFn as v, MiddlewareOutputFn as w, MiddlewareOptions as x, ProcedureHandlerOptions as z };
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, InferSchemaInput<TSchemas['inputSchema']>, InferSchemaOutput<TSchemas['outputSchema']>, ErrorFromErrorMap<TErrorMap>>;
207
+ interface ProcedureClientInterceptorOptions<TInitialContext extends Context, TErrorMap extends ErrorMap, TMeta extends Meta> {
208
+ context: TInitialContext;
209
+ input: unknown;
210
+ errors: ORPCErrorConstructorMap<TErrorMap>;
211
+ path: readonly string[];
212
+ procedure: Procedure<Context, Context, Schemas, ErrorMap, TMeta>;
213
+ signal?: AbortSignal;
214
+ lastEventId: string | undefined;
215
+ }
216
+ type CreateProcedureClientOptions<TInitialContext extends Context, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext> = {
217
+ /**
218
+ * This is helpful for logging and analytics.
219
+ */
220
+ path?: readonly string[];
221
+ interceptors?: Interceptor<ProcedureClientInterceptorOptions<TInitialContext, TErrorMap, TMeta>, PromiseWithError<InferSchemaOutput<TOutputSchema>, ErrorFromErrorMap<TErrorMap>>>[];
222
+ } & (Record<never, never> extends TInitialContext ? {
223
+ context?: Value<Promisable<TInitialContext>, [clientContext: TClientContext]>;
224
+ } : {
225
+ context: Value<Promisable<TInitialContext>, [clientContext: TClientContext]>;
226
+ });
227
+ /**
228
+ * Create Server-side client from a procedure.
229
+ *
230
+ * @see {@link https://orpc.unnoq.com/docs/client/server-side Server-side Client Docs}
231
+ */
232
+ 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>;
233
+
234
+ export { traverseContractProcedures as $, middlewareOutputFn as B, isProcedure as G, createProcedureClient as J, Procedure as P, getRouter as T, createAccessibleLazyRouter as V, enhanceRouter as W, resolveContractProcedures as a0, unlazyRouter as a2, 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 };
235
+ export type { AnyMiddleware as A, Context as C, ProcedureHandlerOptions as D, EnhanceRouterOptions as E, ProcedureDef as F, ProcedureClientInterceptorOptions as H, InferRouterInitialContext as I, InferRouterInitialContexts as K, Lazy as L, Middleware as M, InferRouterCurrentContexts as N, ORPCErrorConstructorMap as O, InferRouterInputs as Q, Router as R, InferRouterOutputs as S, AccessibleLazyRouter as U, TraverseContractProceduresOptions as X, TraverseContractProcedureCallbackOptions as Y, ContractProcedureCallbackOptions as Z, LazyTraverseContractProceduresOptions as _, CreateProcedureClientOptions as a, UnlaziedRouter as a1, 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, MiddlewareNextFnOptions as w, MiddlewareNextFn as x, MiddlewareOutputFn as y, MiddlewareOptions as z };
@@ -0,0 +1,396 @@
1
+ import { isContractProcedure, mergePrefix, mergeErrorMap, enhanceRoute, validateORPCError, ValidationError } from '@temporary-name/contract';
2
+ import { resolveMaybeOptionalOptions, ORPCError, toArray, value, runWithSpan, intercept, isAsyncIteratorObject, overlayProxy, asyncIteratorWithSpan } from '@temporary-name/shared';
3
+ import { HibernationEventIterator, mapEventIterator } from '@temporary-name/standard-server';
4
+ import { safeParseAsync } from '@temporary-name/zod';
5
+
6
+ function isStartWithMiddlewares(middlewares, compare) {
7
+ if (compare.length > middlewares.length) {
8
+ return false;
9
+ }
10
+ for (let i = 0; i < middlewares.length; i++) {
11
+ if (compare[i] === void 0) {
12
+ return true;
13
+ }
14
+ if (middlewares[i] !== compare[i]) {
15
+ return false;
16
+ }
17
+ }
18
+ return true;
19
+ }
20
+ function mergeMiddlewares(first, second, options) {
21
+ if (options.dedupeLeading && isStartWithMiddlewares(second, first)) {
22
+ return second;
23
+ }
24
+ return [...first, ...second];
25
+ }
26
+ function addMiddleware(middlewares, addition) {
27
+ return [...middlewares, addition];
28
+ }
29
+
30
+ class Procedure {
31
+ /**
32
+ * This property holds the defined options.
33
+ */
34
+ "~orpc";
35
+ constructor(def) {
36
+ this["~orpc"] = def;
37
+ }
38
+ }
39
+ function isProcedure(item) {
40
+ if (item instanceof Procedure) {
41
+ return true;
42
+ }
43
+ return isContractProcedure(item) && "middlewares" in item["~orpc"] && "handler" in item["~orpc"];
44
+ }
45
+
46
+ function mergeCurrentContext(context, other) {
47
+ return { ...context, ...other };
48
+ }
49
+
50
+ function createORPCErrorConstructorMap(errors) {
51
+ const proxy = new Proxy(errors, {
52
+ get(target, code) {
53
+ if (typeof code !== "string") {
54
+ return Reflect.get(target, code);
55
+ }
56
+ const item = (...rest) => {
57
+ const options = resolveMaybeOptionalOptions(rest);
58
+ const config = errors[code];
59
+ return new ORPCError(code, {
60
+ defined: Boolean(config),
61
+ status: config?.status,
62
+ message: options.message ?? config?.message,
63
+ data: options.data,
64
+ cause: options.cause
65
+ });
66
+ };
67
+ return item;
68
+ }
69
+ });
70
+ return proxy;
71
+ }
72
+
73
+ const HIDDEN_ROUTER_CONTRACT_SYMBOL = Symbol("ORPC_HIDDEN_ROUTER_CONTRACT");
74
+ function setHiddenRouterContract(router, contract) {
75
+ return new Proxy(router, {
76
+ get(target, key) {
77
+ if (key === HIDDEN_ROUTER_CONTRACT_SYMBOL) {
78
+ return contract;
79
+ }
80
+ return Reflect.get(target, key);
81
+ }
82
+ });
83
+ }
84
+ function getHiddenRouterContract(router) {
85
+ return router[HIDDEN_ROUTER_CONTRACT_SYMBOL];
86
+ }
87
+
88
+ function getRouter(router, path) {
89
+ let current = router;
90
+ for (let i = 0; i < path.length; i++) {
91
+ const segment = path[i];
92
+ if (!current) {
93
+ return void 0;
94
+ }
95
+ if (isProcedure(current)) {
96
+ return void 0;
97
+ }
98
+ if (!isLazy(current)) {
99
+ current = current[segment];
100
+ continue;
101
+ }
102
+ const lazied = current;
103
+ const rest = path.slice(i);
104
+ return lazyInternal(async () => {
105
+ const unwrapped = await unlazy(lazied);
106
+ const next = getRouter(unwrapped.default, rest);
107
+ return unlazy(next);
108
+ }, getLazyMeta(lazied));
109
+ }
110
+ return current;
111
+ }
112
+ function createAccessibleLazyRouter(lazied) {
113
+ const recursive = new Proxy(lazied, {
114
+ get(target, key) {
115
+ if (typeof key !== "string") {
116
+ return Reflect.get(target, key);
117
+ }
118
+ const next = getRouter(lazied, [key]);
119
+ return createAccessibleLazyRouter(next);
120
+ }
121
+ });
122
+ return recursive;
123
+ }
124
+ function enhanceRouter(router, options) {
125
+ if (isLazy(router)) {
126
+ const laziedMeta = getLazyMeta(router);
127
+ const enhancedPrefix = laziedMeta?.prefix ? mergePrefix(options.prefix, laziedMeta?.prefix) : options.prefix;
128
+ const enhanced2 = lazyInternal(
129
+ async () => {
130
+ const { default: unlaziedRouter } = await unlazy(router);
131
+ const enhanced3 = enhanceRouter(unlaziedRouter, options);
132
+ return unlazy(enhanced3);
133
+ },
134
+ {
135
+ ...laziedMeta,
136
+ prefix: enhancedPrefix
137
+ }
138
+ );
139
+ const accessible = createAccessibleLazyRouter(enhanced2);
140
+ return accessible;
141
+ }
142
+ if (isProcedure(router)) {
143
+ const newMiddlewares = mergeMiddlewares(options.middlewares, router["~orpc"].middlewares, {
144
+ dedupeLeading: options.dedupeLeadingMiddlewares
145
+ });
146
+ const newMiddlewareAdded = newMiddlewares.length - router["~orpc"].middlewares.length;
147
+ const enhanced2 = new Procedure({
148
+ ...router["~orpc"],
149
+ route: enhanceRoute(router["~orpc"].route, options),
150
+ errorMap: mergeErrorMap(options.errorMap, router["~orpc"].errorMap),
151
+ middlewares: newMiddlewares,
152
+ inputValidationIndex: router["~orpc"].inputValidationIndex + newMiddlewareAdded,
153
+ outputValidationIndex: router["~orpc"].outputValidationIndex + newMiddlewareAdded
154
+ });
155
+ return enhanced2;
156
+ }
157
+ const enhanced = {};
158
+ for (const key in router) {
159
+ enhanced[key] = enhanceRouter(router[key], options);
160
+ }
161
+ return enhanced;
162
+ }
163
+ function traverseContractProcedures(options, callback, lazyOptions = []) {
164
+ let currentRouter = options.router;
165
+ const hiddenContract = getHiddenRouterContract(options.router);
166
+ if (hiddenContract !== void 0) {
167
+ currentRouter = hiddenContract;
168
+ }
169
+ if (isLazy(currentRouter)) {
170
+ lazyOptions.push({
171
+ router: currentRouter,
172
+ path: options.path
173
+ });
174
+ } else if (isContractProcedure(currentRouter)) {
175
+ callback({
176
+ contract: currentRouter,
177
+ path: options.path
178
+ });
179
+ } else {
180
+ for (const key in currentRouter) {
181
+ traverseContractProcedures(
182
+ {
183
+ router: currentRouter[key],
184
+ path: [...options.path, key]
185
+ },
186
+ callback,
187
+ lazyOptions
188
+ );
189
+ }
190
+ }
191
+ return lazyOptions;
192
+ }
193
+ async function resolveContractProcedures(options, callback) {
194
+ const pending = [options];
195
+ for (const options2 of pending) {
196
+ const lazyOptions = traverseContractProcedures(options2, callback);
197
+ for (const options3 of lazyOptions) {
198
+ const { default: router } = await unlazy(options3.router);
199
+ pending.push({
200
+ router,
201
+ path: options3.path
202
+ });
203
+ }
204
+ }
205
+ }
206
+ async function unlazyRouter(router) {
207
+ if (isProcedure(router)) {
208
+ return router;
209
+ }
210
+ const unlazied = {};
211
+ for (const key in router) {
212
+ const item = router[key];
213
+ const { default: unlaziedRouter } = await unlazy(item);
214
+ unlazied[key] = await unlazyRouter(unlaziedRouter);
215
+ }
216
+ return unlazied;
217
+ }
218
+
219
+ const LAZY_SYMBOL = Symbol("ORPC_LAZY_SYMBOL");
220
+ function lazyInternal(loader, meta = {}) {
221
+ return {
222
+ [LAZY_SYMBOL]: {
223
+ loader,
224
+ meta
225
+ }
226
+ };
227
+ }
228
+ function lazy(prefix, loader) {
229
+ return enhanceRouter(lazyInternal(loader), {
230
+ middlewares: [],
231
+ errorMap: {},
232
+ dedupeLeadingMiddlewares: true,
233
+ prefix
234
+ });
235
+ }
236
+ function isLazy(item) {
237
+ return (typeof item === "object" || typeof item === "function") && item !== null && LAZY_SYMBOL in item;
238
+ }
239
+ function getLazyMeta(lazied) {
240
+ return lazied[LAZY_SYMBOL].meta;
241
+ }
242
+ function unlazy(lazied) {
243
+ return isLazy(lazied) ? lazied[LAZY_SYMBOL].loader() : Promise.resolve({ default: lazied });
244
+ }
245
+
246
+ function middlewareOutputFn(output) {
247
+ return { output, context: {} };
248
+ }
249
+
250
+ function createProcedureClient(lazyableProcedure, ...rest) {
251
+ const options = resolveMaybeOptionalOptions(rest);
252
+ return async (...[input, callerOptions]) => {
253
+ const path = toArray(options.path);
254
+ const { default: procedure } = await unlazy(lazyableProcedure);
255
+ const clientContext = callerOptions?.context ?? {};
256
+ const context = await value(options.context ?? {}, clientContext);
257
+ const errors = createORPCErrorConstructorMap(procedure["~orpc"].errorMap);
258
+ const validateError = async (e) => {
259
+ if (e instanceof ORPCError) {
260
+ return await validateORPCError(procedure["~orpc"].errorMap, e);
261
+ }
262
+ return e;
263
+ };
264
+ try {
265
+ const output = await runWithSpan({ name: "call_procedure", signal: callerOptions?.signal }, (span) => {
266
+ span?.setAttribute("procedure.path", [...path]);
267
+ return intercept(
268
+ toArray(options.interceptors),
269
+ {
270
+ context,
271
+ input,
272
+ // input only optional when it undefinable so we can safely cast it
273
+ errors,
274
+ path,
275
+ procedure,
276
+ signal: callerOptions?.signal,
277
+ lastEventId: callerOptions?.lastEventId
278
+ },
279
+ (interceptorOptions) => executeProcedureInternal(interceptorOptions.procedure, interceptorOptions)
280
+ );
281
+ });
282
+ if (isAsyncIteratorObject(output)) {
283
+ if (output instanceof HibernationEventIterator) {
284
+ return output;
285
+ }
286
+ return overlayProxy(
287
+ output,
288
+ mapEventIterator(
289
+ asyncIteratorWithSpan(
290
+ { name: "consume_event_iterator_output", signal: callerOptions?.signal },
291
+ output
292
+ ),
293
+ {
294
+ value: (v) => v,
295
+ error: (e) => validateError(e)
296
+ }
297
+ )
298
+ );
299
+ }
300
+ return output;
301
+ } catch (e) {
302
+ throw await validateError(e);
303
+ }
304
+ };
305
+ }
306
+ async function validateInput(procedure, input) {
307
+ const schema = procedure["~orpc"].schemas.inputSchema;
308
+ if (!schema) {
309
+ return input;
310
+ }
311
+ return runWithSpan({ name: "validate_input" }, async () => {
312
+ const result = await safeParseAsync(schema, input);
313
+ if (!result.success) {
314
+ throw new ORPCError("BAD_REQUEST", {
315
+ message: "Input validation failed",
316
+ data: {
317
+ issues: result.error.issues
318
+ },
319
+ cause: new ValidationError({
320
+ message: "Input validation failed",
321
+ issues: result.error.issues,
322
+ data: input
323
+ })
324
+ });
325
+ }
326
+ return result.data;
327
+ });
328
+ }
329
+ async function validateOutput(procedure, output) {
330
+ const schema = procedure["~orpc"].schemas.outputSchema;
331
+ if (!schema) {
332
+ return output;
333
+ }
334
+ return runWithSpan({ name: "validate_output" }, async () => {
335
+ const result = await safeParseAsync(schema, output);
336
+ if (!result.success) {
337
+ throw new ORPCError("INTERNAL_SERVER_ERROR", {
338
+ message: "Output validation failed",
339
+ cause: new ValidationError({
340
+ message: "Output validation failed",
341
+ issues: result.error.issues,
342
+ data: output
343
+ })
344
+ });
345
+ }
346
+ return result.data;
347
+ });
348
+ }
349
+ async function executeProcedureInternal(procedure, options) {
350
+ const middlewares = procedure["~orpc"].middlewares;
351
+ const inputValidationIndex = Math.min(
352
+ Math.max(0, procedure["~orpc"].inputValidationIndex),
353
+ middlewares.length
354
+ );
355
+ const outputValidationIndex = Math.min(
356
+ Math.max(0, procedure["~orpc"].outputValidationIndex),
357
+ middlewares.length
358
+ );
359
+ const next = async (index, context, input) => {
360
+ let currentInput = input;
361
+ if (index === inputValidationIndex) {
362
+ currentInput = await validateInput(procedure, currentInput);
363
+ }
364
+ const mid = middlewares[index];
365
+ const output = mid ? await runWithSpan({ name: `middleware.${mid.name}`, signal: options.signal }, async (span) => {
366
+ span?.setAttribute("middleware.index", index);
367
+ span?.setAttribute("middleware.name", mid.name);
368
+ const result = await mid(
369
+ {
370
+ ...options,
371
+ context,
372
+ next: async (...[nextOptions]) => {
373
+ const nextContext = nextOptions?.context ?? {};
374
+ return {
375
+ output: await next(index + 1, mergeCurrentContext(context, nextContext), currentInput),
376
+ context: nextContext
377
+ };
378
+ }
379
+ },
380
+ currentInput,
381
+ middlewareOutputFn
382
+ );
383
+ return result.output;
384
+ }) : await runWithSpan(
385
+ { name: "handler", signal: options.signal },
386
+ () => procedure["~orpc"].handler({ ...options, context, input: currentInput })
387
+ );
388
+ if (index === outputValidationIndex) {
389
+ return await validateOutput(procedure, output);
390
+ }
391
+ return output;
392
+ };
393
+ return next(0, options.context, options.input);
394
+ }
395
+
396
+ export { LAZY_SYMBOL as L, Procedure as P, addMiddleware as a, isLazy as b, createProcedureClient as c, getRouter as d, enhanceRouter as e, createORPCErrorConstructorMap as f, getLazyMeta as g, lazy as h, isProcedure as i, middlewareOutputFn as j, isStartWithMiddlewares as k, lazyInternal as l, mergeCurrentContext as m, mergeMiddlewares as n, getHiddenRouterContract as o, createAccessibleLazyRouter as p, unlazyRouter as q, resolveContractProcedures as r, setHiddenRouterContract as s, traverseContractProcedures as t, unlazy as u };
@@ -1,6 +1,6 @@
1
1
  import { HTTPPath } from '@temporary-name/shared';
2
- import { C as Context } from './server.BKSOrA6h.mjs';
3
- import { c as StandardHandleOptions } from './server.C1fnTLq0.mjs';
2
+ import { C as Context } from './server.B5czSQQh.mjs';
3
+ import { c as StandardHandleOptions } from './server.TgmlNh8r.mjs';
4
4
 
5
5
  type FriendlyStandardHandleOptions<T extends Context> = Omit<StandardHandleOptions<T>, 'context'> & (Record<never, never> extends T ? {
6
6
  context?: T;