@orpc/server 2.0.0-beta.47 → 2.0.0-beta.49

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 (178) hide show
  1. package/README.md +6 -7
  2. package/dist/adapters/aws-lambda/handler.d.ts +25 -0
  3. package/dist/adapters/aws-lambda/handler.js +24 -0
  4. package/dist/adapters/aws-lambda/index.d.ts +10 -45
  5. package/dist/adapters/aws-lambda/index.js +2 -0
  6. package/dist/adapters/aws-lambda/rpc-handler.d.ts +19 -0
  7. package/dist/adapters/aws-lambda/rpc-handler.js +18 -0
  8. package/dist/adapters/crossws/handler.d.ts +45 -0
  9. package/dist/adapters/crossws/handler.js +60 -0
  10. package/dist/adapters/crossws/index.d.ts +2 -63
  11. package/dist/adapters/crossws/index.js +2 -0
  12. package/dist/adapters/crossws/rpc-handler.d.ts +16 -0
  13. package/dist/adapters/crossws/rpc-handler.js +15 -0
  14. package/dist/adapters/fastify/handler.d.ts +21 -0
  15. package/dist/adapters/fastify/handler.js +20 -0
  16. package/dist/adapters/fastify/index.d.ts +2 -42
  17. package/dist/adapters/fastify/index.js +2 -0
  18. package/dist/adapters/fastify/rpc-handler.d.ts +15 -0
  19. package/dist/adapters/fastify/rpc-handler.js +14 -0
  20. package/dist/adapters/fetch/handler.d.ts +23 -0
  21. package/dist/adapters/fetch/handler.js +22 -0
  22. package/dist/adapters/fetch/index.d.ts +5 -45
  23. package/dist/adapters/fetch/index.js +7 -0
  24. package/dist/adapters/fetch/rpc-handler.d.ts +17 -0
  25. package/dist/adapters/fetch/rpc-handler.js +16 -0
  26. package/dist/adapters/message-port/{index.d.mts → handler.d.ts} +17 -30
  27. package/dist/adapters/message-port/handler.js +86 -0
  28. package/dist/adapters/message-port/index.d.ts +2 -77
  29. package/dist/adapters/message-port/index.js +2 -0
  30. package/dist/adapters/message-port/rpc-handler.d.ts +16 -0
  31. package/dist/adapters/message-port/rpc-handler.js +15 -0
  32. package/dist/adapters/node/handler.d.ts +21 -0
  33. package/dist/adapters/node/handler.js +20 -0
  34. package/dist/adapters/node/index.d.ts +5 -42
  35. package/dist/adapters/node/index.js +7 -0
  36. package/dist/adapters/node/rpc-handler.d.ts +16 -0
  37. package/dist/adapters/node/rpc-handler.js +15 -0
  38. package/dist/adapters/standard/codec.d.ts +16 -0
  39. package/dist/adapters/standard/codec.js +1 -0
  40. package/dist/adapters/standard/handler.d.ts +68 -0
  41. package/dist/adapters/standard/handler.js +205 -0
  42. package/dist/adapters/standard/index.d.ts +6 -95
  43. package/dist/adapters/standard/index.js +5 -0
  44. package/dist/adapters/standard/plugin.d.ts +32 -0
  45. package/dist/adapters/standard/plugin.js +16 -0
  46. package/dist/adapters/standard/rpc-handler-codec.d.ts +46 -0
  47. package/dist/adapters/standard/rpc-handler-codec.js +51 -0
  48. package/dist/adapters/standard/rpc-matcher.d.ts +53 -0
  49. package/dist/adapters/standard/rpc-matcher.js +152 -0
  50. package/dist/adapters/standard/utils.d.ts +10 -0
  51. package/dist/adapters/standard/utils.js +6 -0
  52. package/dist/adapters/standard-peer/index.d.ts +1 -11
  53. package/dist/adapters/standard-peer/index.js +1 -0
  54. package/dist/adapters/standard-peer/utils.d.ts +4 -0
  55. package/dist/adapters/standard-peer/utils.js +7 -0
  56. package/dist/adapters/websocket/{index.d.mts → handler.d.ts} +11 -29
  57. package/dist/adapters/websocket/handler.js +85 -0
  58. package/dist/adapters/websocket/index.d.ts +2 -77
  59. package/dist/adapters/websocket/index.js +2 -0
  60. package/dist/adapters/websocket/rpc-handler.d.ts +16 -0
  61. package/dist/adapters/websocket/rpc-handler.js +15 -0
  62. package/dist/builder-variants.d.ts +226 -0
  63. package/dist/builder-variants.js +1 -0
  64. package/dist/builder.d.ts +127 -0
  65. package/dist/builder.js +226 -0
  66. package/dist/constants.d.ts +2 -0
  67. package/dist/constants.js +2 -0
  68. package/dist/context.d.ts +6 -0
  69. package/dist/context.js +1 -0
  70. package/dist/extensions/callable.d.ts +6 -5
  71. package/dist/extensions/callable.js +7 -0
  72. package/dist/helpers/base64url.d.ts +31 -0
  73. package/dist/helpers/base64url.js +53 -0
  74. package/dist/helpers/cookie.d.ts +51 -0
  75. package/dist/helpers/cookie.js +69 -0
  76. package/dist/helpers/encryption.d.ts +28 -0
  77. package/dist/helpers/encryption.js +80 -0
  78. package/dist/helpers/index.d.ts +4 -167
  79. package/dist/helpers/index.js +4 -0
  80. package/dist/helpers/signing.d.ts +50 -0
  81. package/dist/helpers/signing.js +88 -0
  82. package/dist/implementer-procedure.d.ts +16 -0
  83. package/dist/implementer-procedure.js +44 -0
  84. package/dist/implementer-router.d.ts +40 -0
  85. package/dist/implementer-router.js +117 -0
  86. package/dist/implementer.d.ts +16 -0
  87. package/dist/implementer.js +37 -0
  88. package/dist/index.d.ts +45 -460
  89. package/dist/index.js +48 -0
  90. package/dist/lazy.d.ts +20 -0
  91. package/dist/lazy.js +31 -0
  92. package/dist/middleware-decorated.d.ts +10 -0
  93. package/dist/middleware-decorated.js +71 -0
  94. package/dist/middleware.d.ts +54 -0
  95. package/dist/middleware.js +1 -0
  96. package/dist/plugins/batch.d.ts +102 -0
  97. package/dist/plugins/batch.js +405 -0
  98. package/dist/plugins/cors.d.ts +73 -0
  99. package/dist/plugins/cors.js +112 -0
  100. package/dist/plugins/get-method-csrf-protection.d.ts +23 -0
  101. package/dist/plugins/get-method-csrf-protection.js +64 -0
  102. package/dist/plugins/head-method.d.ts +23 -0
  103. package/dist/plugins/head-method.js +104 -0
  104. package/dist/plugins/index.d.ts +29 -422
  105. package/dist/plugins/index.js +29 -0
  106. package/dist/plugins/method-override.d.ts +38 -0
  107. package/dist/plugins/method-override.js +50 -0
  108. package/dist/plugins/request-compression.d.ts +16 -0
  109. package/dist/plugins/request-compression.js +82 -0
  110. package/dist/plugins/request-headers.d.ts +22 -0
  111. package/dist/plugins/request-headers.js +30 -0
  112. package/dist/{shared/server.CgBF3Vj1.d.ts → plugins/request-limit.d.ts} +4 -8
  113. package/dist/plugins/request-limit.js +68 -0
  114. package/dist/plugins/response-compression.d.ts +59 -0
  115. package/dist/plugins/response-compression.js +209 -0
  116. package/dist/plugins/response-headers.d.ts +30 -0
  117. package/dist/plugins/response-headers.js +47 -0
  118. package/dist/plugins/rethrow.d.ts +33 -0
  119. package/dist/plugins/rethrow.js +59 -0
  120. package/dist/plugins/signal-sync.d.ts +13 -0
  121. package/dist/plugins/signal-sync.js +91 -0
  122. package/dist/plugins/timeout.d.ts +37 -0
  123. package/dist/plugins/timeout.js +59 -0
  124. package/dist/procedure-client.d.ts +19 -0
  125. package/dist/procedure-client.js +175 -0
  126. package/dist/procedure-decorated.d.ts +10 -0
  127. package/dist/procedure-decorated.js +44 -0
  128. package/dist/procedure-utils.d.ts +41 -0
  129. package/dist/procedure-utils.js +49 -0
  130. package/dist/procedure.d.ts +72 -0
  131. package/dist/procedure.js +24 -0
  132. package/dist/router-client.d.ts +23 -0
  133. package/dist/router-client.js +43 -0
  134. package/dist/router-hidden.d.ts +5 -0
  135. package/dist/router-hidden.js +14 -0
  136. package/dist/router-utils.d.ts +48 -0
  137. package/dist/router-utils.js +202 -0
  138. package/dist/{shared/server.Dz7j9BEh.d.ts → router.d.ts} +14 -15
  139. package/dist/router.js +1 -0
  140. package/package.json +84 -86
  141. package/dist/adapters/aws-lambda/index.d.mts +0 -51
  142. package/dist/adapters/aws-lambda/index.mjs +0 -39
  143. package/dist/adapters/crossws/index.d.mts +0 -63
  144. package/dist/adapters/crossws/index.mjs +0 -68
  145. package/dist/adapters/fastify/index.d.mts +0 -42
  146. package/dist/adapters/fastify/index.mjs +0 -39
  147. package/dist/adapters/fetch/index.d.mts +0 -47
  148. package/dist/adapters/fetch/index.mjs +0 -42
  149. package/dist/adapters/message-port/index.mjs +0 -87
  150. package/dist/adapters/node/index.d.mts +0 -44
  151. package/dist/adapters/node/index.mjs +0 -41
  152. package/dist/adapters/standard/index.d.mts +0 -95
  153. package/dist/adapters/standard/index.mjs +0 -8
  154. package/dist/adapters/standard-peer/index.d.mts +0 -11
  155. package/dist/adapters/standard-peer/index.mjs +0 -10
  156. package/dist/adapters/websocket/index.mjs +0 -87
  157. package/dist/extensions/callable.d.mts +0 -10
  158. package/dist/extensions/callable.mjs +0 -11
  159. package/dist/helpers/index.d.mts +0 -167
  160. package/dist/helpers/index.mjs +0 -200
  161. package/dist/index.d.mts +0 -476
  162. package/dist/index.mjs +0 -463
  163. package/dist/plugins/index.d.mts +0 -432
  164. package/dist/plugins/index.mjs +0 -965
  165. package/dist/shared/server.1ExWFMze.mjs +0 -69
  166. package/dist/shared/server.9B1jEm5a.d.mts +0 -105
  167. package/dist/shared/server.BB464y-6.mjs +0 -207
  168. package/dist/shared/server.BC5BIu02.d.mts +0 -73
  169. package/dist/shared/server.BSYGskId.d.ts +0 -14
  170. package/dist/shared/server.BvG3boBj.d.mts +0 -14
  171. package/dist/shared/server.CTV_mC2P.d.mts +0 -34
  172. package/dist/shared/server.CYbIpHbH.mjs +0 -239
  173. package/dist/shared/server.Chq5FpXf.mjs +0 -353
  174. package/dist/shared/server.DFPxP2v3.d.mts +0 -166
  175. package/dist/shared/server.DFPxP2v3.d.ts +0 -166
  176. package/dist/shared/server.DOg7ke6y.d.ts +0 -105
  177. package/dist/shared/server.W91HSRkE.mjs +0 -9
  178. package/dist/shared/server.sBQSgqK3.mjs +0 -41
@@ -0,0 +1,72 @@
1
+ import type { AnySchema, ErrorMap, ORPCErrorConstructorMap, ProcedureContractDefinition } from '@orpc/contract';
2
+ import type { Promisable } from '@orpc/shared';
3
+ import type { Context } from './context.ts';
4
+ import type { AnyMiddleware } from './middleware.ts';
5
+ export interface ProcedureHandlerOptions<TCurrentContext extends Context, TInput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>> {
6
+ context: TCurrentContext;
7
+ input: TInput;
8
+ path: string[];
9
+ procedure: Procedure<Context, Context, AnySchema, AnySchema, ErrorMap>;
10
+ signal?: AbortSignal | undefined;
11
+ lastEventId?: string | undefined;
12
+ errors: TErrorConstructorMap;
13
+ }
14
+ export interface ProcedureHandler<TCurrentContext extends Context, TInput, THandlerOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>> {
15
+ (opts: ProcedureHandlerOptions<TCurrentContext, TInput, TErrorConstructorMap>, input: TInput): Promisable<THandlerOutput>;
16
+ }
17
+ export interface OrderedMiddleware {
18
+ /**
19
+ * Snapshot of `inputSchemas.length`
20
+ * at the time this middleware was used.
21
+ *
22
+ * @default 0
23
+ */
24
+ inputSchemasLengthAtUse?: number | undefined;
25
+ /**
26
+ * Snapshot of `outputSchemas.length`
27
+ * at the time this middleware was used.
28
+ *
29
+ * @default 0
30
+ */
31
+ outputSchemasLengthAtUse?: number | undefined;
32
+ middleware: AnyMiddleware;
33
+ }
34
+ export interface ProcedureConfig {
35
+ /**
36
+ * When enabled, input schemas are not validated at runtime.
37
+ * Schemas are still used for type inference and OpenAPI generation.
38
+ *
39
+ * @remarks
40
+ * **Warning**: Do not disable validation for schemas that transform values.
41
+ *
42
+ * @default false
43
+ */
44
+ disableInputValidation?: boolean | undefined;
45
+ /**
46
+ * When enabled, output schemas are not validated at runtime.
47
+ * Schemas are still used for type inference and OpenAPI generation.
48
+ *
49
+ * Useful when output schemas exist only for specification generation.
50
+ *
51
+ * @remarks
52
+ * **Warning**: Do not disable validation for schemas that transform values.
53
+ *
54
+ * @default false
55
+ */
56
+ disableOutputValidation?: boolean | undefined;
57
+ }
58
+ export interface ProcedureDefinition<TInitialContext extends Context, TInjectedContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> extends ProcedureContractDefinition<TInputSchema, TOutputSchema, TErrorMap>, ProcedureConfig {
59
+ __TInitialContext?: (type: TInitialContext) => unknown;
60
+ __TInjectedContext?: (type: TInjectedContext) => unknown;
61
+ orderedMiddlewares: OrderedMiddleware[];
62
+ handler: ProcedureHandler<any, any, any, any>;
63
+ }
64
+ export declare class Procedure<TInitialContext extends Context, TInjectedContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> {
65
+ '~orpc': ProcedureDefinition<TInitialContext, TInjectedContext, TInputSchema, TOutputSchema, TErrorMap>;
66
+ constructor(def: ProcedureDefinition<TInitialContext, TInjectedContext, TInputSchema, TOutputSchema, TErrorMap>);
67
+ /**
68
+ * Checks if the given instance satisfies the {@link Procedure} class/interface.
69
+ */
70
+ static [Symbol.hasInstance](instance: unknown): boolean;
71
+ }
72
+ export type AnyProcedure = Procedure<any, any, any, any, any>;
@@ -0,0 +1,24 @@
1
+ import { ProcedureContract } from '@orpc/contract';
2
+ import { getConstructor } from '@orpc/shared';
3
+ export class Procedure {
4
+ '~orpc';
5
+ constructor(def) {
6
+ this['~orpc'] = def;
7
+ }
8
+ /**
9
+ * Checks if the given instance satisfies the {@link Procedure} class/interface.
10
+ */
11
+ static [Symbol.hasInstance](instance) {
12
+ if (this !== Procedure) {
13
+ // fallback to default instanceof check if this is extended class
14
+ return Function.prototype[Symbol.hasInstance].call(this, instance);
15
+ }
16
+ const constructor = getConstructor(instance);
17
+ if (constructor === Procedure) {
18
+ return true;
19
+ }
20
+ return (instance instanceof ProcedureContract &&
21
+ Array.isArray(instance['~orpc'].orderedMiddlewares) &&
22
+ typeof instance['~orpc'].handler === 'function');
23
+ }
24
+ }
@@ -0,0 +1,23 @@
1
+ import type { ClientContext } from '@orpc/client';
2
+ import type { ErrorMap, Schema } from '@orpc/contract';
3
+ import type { MaybeOptionalOptions } from '@orpc/shared';
4
+ import type { Lazyable } from './lazy.ts';
5
+ import type { ProcedureClient, ProcedureClientOptions } from './procedure-client.ts';
6
+ import { Procedure } from './procedure.ts';
7
+ import type { AnyRouter, InferRouterInitialContext } from './router.ts';
8
+ /**
9
+ * The client type derived from a router, exposing every procedure as a callable
10
+ * function while preserving the router's shape.
11
+ *
12
+ * @see {@link https://orpc.dev/docs/client/client-side#creating-a-client | Client-Side Clients - Creating a Client}
13
+ */
14
+ export type RouterClient<TRouter extends AnyRouter, TClientContext extends ClientContext = object> = TRouter extends Procedure<any, any, infer $InputSchema, infer $OutputSchema, infer $ErrorMap> ? ProcedureClient<TClientContext, $InputSchema, $OutputSchema, $ErrorMap> : {
15
+ [K in keyof TRouter]: TRouter[K] extends Lazyable<infer U extends AnyRouter> ? RouterClient<U, TClientContext> : never;
16
+ };
17
+ /**
18
+ * Creates a server-side client that calls the router's procedures directly,
19
+ * within the same process, without any network layer.
20
+ *
21
+ * @see {@link https://orpc.dev/docs/client/server-side#router-clients | Server-Side Clients - Router Clients}
22
+ */
23
+ export declare function createRouterClient<T extends AnyRouter, TClientContext extends ClientContext = object>(router: Lazyable<T | undefined>, ...rest: MaybeOptionalOptions<ProcedureClientOptions<InferRouterInitialContext<T>, Schema<unknown>, ErrorMap, TClientContext>>): RouterClient<T, TClientContext>;
@@ -0,0 +1,43 @@
1
+ import { RECURSIVE_CLIENT_UNWRAP_KEYS } from '@orpc/client';
2
+ import { resolveMaybeOptionalOptions, toArray } from '@orpc/shared';
3
+ import { Lazy } from './lazy.js';
4
+ import { createProcedureClient } from './procedure-client.js';
5
+ import { createGuardedProcedureLazy } from './procedure-utils.js';
6
+ import { Procedure } from './procedure.js';
7
+ import { getRouter } from './router-utils.js';
8
+ /**
9
+ * Creates a server-side client that calls the router's procedures directly,
10
+ * within the same process, without any network layer.
11
+ *
12
+ * @see {@link https://orpc.dev/docs/client/server-side#router-clients | Server-Side Clients - Router Clients}
13
+ */
14
+ export function createRouterClient(router, ...rest) {
15
+ const options = resolveMaybeOptionalOptions(rest);
16
+ if (router instanceof Procedure) {
17
+ return createProcedureClient(router, options);
18
+ }
19
+ const procedureCaller = router instanceof Lazy ? createProcedureClient(createGuardedProcedureLazy(router), options) : {};
20
+ const cache = new Map();
21
+ const recursive = new Proxy(procedureCaller, {
22
+ get(target, key) {
23
+ if (typeof key !== 'string' ||
24
+ (router instanceof Lazy && RECURSIVE_CLIENT_UNWRAP_KEYS.has(key))) {
25
+ return Reflect.get(target, key);
26
+ }
27
+ let nextClient = cache.get(key);
28
+ if (nextClient === undefined) {
29
+ const next = getRouter(router, [key]);
30
+ if (!next) {
31
+ return Reflect.get(target, key);
32
+ }
33
+ nextClient = createRouterClient(next, {
34
+ ...options,
35
+ path: [...toArray(options.path), key],
36
+ });
37
+ cache.set(key, nextClient);
38
+ }
39
+ return nextClient;
40
+ },
41
+ });
42
+ return recursive;
43
+ }
@@ -0,0 +1,5 @@
1
+ import type { RouterContract } from '@orpc/contract';
2
+ import type { Lazyable } from './lazy.ts';
3
+ import type { AnyRouter } from './router.ts';
4
+ export declare function withHiddenRouterContract<T extends Lazyable<AnyRouter>>(router: T, contract: RouterContract): T;
5
+ export declare function getHiddenRouterContract(router: Lazyable<AnyRouter | RouterContract>): RouterContract | undefined;
@@ -0,0 +1,14 @@
1
+ const HIDDEN_ROUTER_CONTRACT_SYMBOL = /* @__PURE__ */ Symbol.for('ORPC_HIDDEN_ROUTER_CONTRACT');
2
+ export function withHiddenRouterContract(router, contract) {
3
+ return new Proxy(router, {
4
+ get(target, key) {
5
+ if (key === HIDDEN_ROUTER_CONTRACT_SYMBOL) {
6
+ return contract;
7
+ }
8
+ return Reflect.get(target, key);
9
+ },
10
+ });
11
+ }
12
+ export function getHiddenRouterContract(router) {
13
+ return router[HIDDEN_ROUTER_CONTRACT_SYMBOL];
14
+ }
@@ -0,0 +1,48 @@
1
+ import type { AnyProcedureContract, AugmentContractRouterOptions, ErrorMap, MergedErrorMap, RouterContract } from '@orpc/contract';
2
+ import type { Promisable } from '@orpc/shared';
3
+ import type { Context, MergedInitialContext } from './context.ts';
4
+ import type { Lazyable } from './lazy.ts';
5
+ import { Lazy } from './lazy.ts';
6
+ import type { AnyMiddleware } from './middleware.ts';
7
+ import type { AnyProcedure, ProcedureConfig } from './procedure.ts';
8
+ import { Procedure } from './procedure.ts';
9
+ import type { AnyRouter } from './router.ts';
10
+ export type AugmentedRouter<T extends AnyRouter, TErrorMap extends ErrorMap> = T extends Procedure<infer $InitialContext, infer $CurrentContext, infer $InputSchema, infer $OutputSchema, infer $ErrorMap> ? Procedure<$InitialContext, $CurrentContext, $InputSchema, $OutputSchema, MergedErrorMap<TErrorMap, $ErrorMap>> : {
11
+ [K in keyof T]: T[K] extends Lazy<infer $ extends AnyRouter> ? Lazy<AugmentedRouter<$, TErrorMap>> : T[K] extends AnyRouter ? AugmentedRouter<T[K], TErrorMap> : never;
12
+ };
13
+ export type AugmentedRouterWithMiddlewares<T extends AnyRouter, TInitialContext extends Context, TInjectedContext extends Context, TErrorMap extends ErrorMap> = T extends Procedure<infer $InitialContext, infer $CurrentContext, infer $InputSchema, infer $OutputSchema, infer $ErrorMap> ? Procedure<MergedInitialContext<TInitialContext, TInjectedContext, $InitialContext>, $CurrentContext, $InputSchema, $OutputSchema, MergedErrorMap<TErrorMap, $ErrorMap>> : {
14
+ [K in keyof T]: T[K] extends Lazy<infer $ extends AnyRouter> ? Lazy<AugmentedRouterWithMiddlewares<$, TInitialContext, TInjectedContext, TErrorMap>> : T[K] extends AnyRouter ? AugmentedRouterWithMiddlewares<T[K], TInitialContext, TInjectedContext, TErrorMap> : never;
15
+ };
16
+ interface AugmentRouterOptions<TErrorMap extends ErrorMap> extends AugmentContractRouterOptions<TErrorMap>, ProcedureConfig {
17
+ middlewares: AnyMiddleware[];
18
+ }
19
+ export declare function augmentRouter<T extends AnyRouter, TInitialContext extends Context, TCurrentContext extends Context, TErrorMap extends ErrorMap>(router: T, options: AugmentRouterOptions<TErrorMap>): AugmentedRouter<T, TErrorMap> | AugmentedRouterWithMiddlewares<T, TInitialContext, TCurrentContext, TErrorMap>;
20
+ interface AugmentImplementedRouterOptions extends ProcedureConfig {
21
+ middlewares: AnyMiddleware[];
22
+ }
23
+ export declare function augmentImplementedRouter<T extends AnyRouter, TInitialContext extends Context, TCurrentContext extends Context>(router: T, options: AugmentImplementedRouterOptions): AugmentedRouter<T, object> | AugmentedRouterWithMiddlewares<T, TInitialContext, TCurrentContext, object>;
24
+ /**
25
+ * Resolves the procedure of a contract walked from a router: the procedure itself,
26
+ * or the implementation of a contract-first procedure, looked up by its path.
27
+ *
28
+ * @throws {TypeError} If a contract-first procedure has no valid implementation.
29
+ */
30
+ export declare function resolveContractProcedure(router: AnyRouter, contract: AnyProcedureContract, path: readonly string[]): Promise<AnyProcedure>;
31
+ export declare function getRouter<T extends Lazyable<AnyRouter | undefined>>(router: T, path: readonly string[]): T extends Lazy<any> ? Lazy<AnyRouter | undefined> : Lazyable<AnyRouter | undefined>;
32
+ export interface WalkProcedureContractsLazyResult {
33
+ router: Lazy<AnyRouter>;
34
+ path: string[];
35
+ }
36
+ export declare function walkProcedureContractsSync(router: RouterContract | AnyRouter, callback: (contract: AnyProcedureContract | AnyProcedure, path: string[]) => void, path?: string[]): WalkProcedureContractsLazyResult[];
37
+ export declare function walkProcedureContractsAsync(router: RouterContract | AnyRouter, callback: (contract: AnyProcedureContract | AnyProcedure, path: string[]) => Promisable<void>, path?: string[]): Promise<void>;
38
+ export type UnlaziedRouter<T extends AnyRouter> = T extends AnyProcedure ? T : {
39
+ [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? UnlaziedRouter<U> : never;
40
+ };
41
+ /**
42
+ * Fully resolves all lazy routers inside a router, returning a plain router.
43
+ * Useful for making a router with lazy parts contract-compatible.
44
+ *
45
+ * @see {@link https://orpc.dev/docs/contract/router#router-to-contract | Router Contract - Router to Contract}
46
+ */
47
+ export declare function unlazyRouter<T extends AnyRouter>(router: T): Promise<UnlaziedRouter<T>>;
48
+ export {};
@@ -0,0 +1,202 @@
1
+ import { mergeErrorMap, ProcedureContract, resolveMetaPlugins } from '@orpc/contract';
2
+ import { getOwn, isNonPrimitive, omit, setOwn } from '@orpc/shared';
3
+ import { Lazy, unlazy } from './lazy.js';
4
+ import { createContractProcedure } from './procedure-utils.js';
5
+ import { Procedure } from './procedure.js';
6
+ import { getHiddenRouterContract } from './router-hidden.js';
7
+ export function augmentRouter(router, options) {
8
+ if (router instanceof Lazy) {
9
+ const [meta, metaPlugins] = resolveMetaPlugins(options.meta, options.metaPlugins, router['~orpc'].metaPlugins);
10
+ const enhanced = new Lazy({
11
+ meta,
12
+ metaPlugins,
13
+ async loader() {
14
+ const { default: unlaziedRouter } = await unlazy(router);
15
+ const enhanced = augmentRouter(unlaziedRouter, options);
16
+ return unlazy(enhanced);
17
+ },
18
+ });
19
+ return enhanced;
20
+ }
21
+ if (router instanceof Procedure) {
22
+ const [meta, metaPlugins] = resolveMetaPlugins(options.meta, options.metaPlugins, router['~orpc'].metaPlugins);
23
+ const enhanced = new Procedure({
24
+ ...omit(options, ['middlewares', 'errorMap', 'meta', 'metaPlugins']),
25
+ ...router['~orpc'],
26
+ meta,
27
+ metaPlugins,
28
+ errorMap: mergeErrorMap(options.errorMap, router['~orpc'].errorMap),
29
+ orderedMiddlewares: [
30
+ ...options.middlewares.map((middleware) => ({
31
+ middleware,
32
+ inputSchemasLengthAtUse: 0,
33
+ outputSchemasLengthAtUse: 0,
34
+ })),
35
+ ...router['~orpc'].orderedMiddlewares,
36
+ ],
37
+ });
38
+ return enhanced;
39
+ }
40
+ if (!isNonPrimitive(router)) {
41
+ return router;
42
+ }
43
+ const enhanced = {};
44
+ for (const [key, child] of Object.entries(router)) {
45
+ setOwn(enhanced, key, augmentRouter(child, options));
46
+ }
47
+ return enhanced;
48
+ }
49
+ export function augmentImplementedRouter(router, options) {
50
+ if (router instanceof Lazy) {
51
+ const enhanced = new Lazy({
52
+ ...router['~orpc'],
53
+ async loader() {
54
+ const { default: unlaziedRouter } = await unlazy(router);
55
+ const enhanced = augmentImplementedRouter(unlaziedRouter, options);
56
+ return unlazy(enhanced);
57
+ },
58
+ });
59
+ return enhanced;
60
+ }
61
+ if (router instanceof Procedure) {
62
+ const enhanced = new Procedure({
63
+ ...omit(options, ['middlewares']),
64
+ ...router['~orpc'],
65
+ orderedMiddlewares: [
66
+ ...options.middlewares.map((middleware) => ({
67
+ middleware,
68
+ inputSchemasLengthAtUse: 0,
69
+ outputSchemasLengthAtUse: 0,
70
+ })),
71
+ ...router['~orpc'].orderedMiddlewares,
72
+ ],
73
+ });
74
+ return enhanced;
75
+ }
76
+ if (!isNonPrimitive(router)) {
77
+ return router;
78
+ }
79
+ const enhanced = {};
80
+ for (const [key, child] of Object.entries(router)) {
81
+ setOwn(enhanced, key, augmentImplementedRouter(child, options));
82
+ }
83
+ return enhanced;
84
+ }
85
+ /**
86
+ * Resolves the procedure of a contract walked from a router: the procedure itself,
87
+ * or the implementation of a contract-first procedure, looked up by its path.
88
+ *
89
+ * @throws {TypeError} If a contract-first procedure has no valid implementation.
90
+ */
91
+ export async function resolveContractProcedure(router, contract, path) {
92
+ if (contract instanceof Procedure) {
93
+ return contract;
94
+ }
95
+ const { default: maybeProcedure } = await unlazy(getRouter(router, path));
96
+ if (!(maybeProcedure instanceof Procedure)) {
97
+ throw new TypeError(`[Contract-First] Missing or invalid implementation for procedure at path: "${path.join('.')}". ` +
98
+ `Ensure the procedure is correctly implemented and matches its contract.`);
99
+ }
100
+ return createContractProcedure(maybeProcedure, contract);
101
+ }
102
+ export function getRouter(router, path) {
103
+ let current = router;
104
+ for (let i = 0; i < path.length; i++) {
105
+ const segment = path[i];
106
+ if (!isNonPrimitive(current)) {
107
+ return undefined;
108
+ }
109
+ if (current instanceof Procedure) {
110
+ return undefined;
111
+ }
112
+ if (!(current instanceof Lazy)) {
113
+ current = getOwn(current, segment);
114
+ continue;
115
+ }
116
+ const lazied = current;
117
+ const rest = path.slice(i);
118
+ return new Lazy({
119
+ ...lazied['~orpc'],
120
+ async loader() {
121
+ const unwrapped = await unlazy(lazied);
122
+ const next = getRouter(unwrapped.default, rest);
123
+ return await unlazy(next);
124
+ },
125
+ });
126
+ }
127
+ if (!isNonPrimitive(current)) {
128
+ return undefined;
129
+ }
130
+ return current;
131
+ }
132
+ export function walkProcedureContractsSync(router, callback, path = []) {
133
+ const lazyResults = [];
134
+ walkProcedureContractsSyncInternal(router, callback, path.slice(), lazyResults);
135
+ return lazyResults;
136
+ }
137
+ function walkProcedureContractsSyncInternal(router, callback, path, lazyResults) {
138
+ const hiddenContract = getHiddenRouterContract(router);
139
+ if (hiddenContract !== undefined) {
140
+ router = hiddenContract;
141
+ }
142
+ if (router instanceof ProcedureContract) {
143
+ callback(router, path.slice());
144
+ return;
145
+ }
146
+ if (!isNonPrimitive(router)) {
147
+ return;
148
+ }
149
+ for (const [key, value] of Object.entries(router)) {
150
+ path.push(key);
151
+ if (value instanceof Lazy) {
152
+ lazyResults.push({ router: value, path: path.slice() });
153
+ }
154
+ else {
155
+ walkProcedureContractsSyncInternal(value, callback, path, lazyResults);
156
+ }
157
+ path.pop();
158
+ }
159
+ }
160
+ export async function walkProcedureContractsAsync(router, callback, path = []) {
161
+ const hiddenContract = getHiddenRouterContract(router);
162
+ if (hiddenContract !== undefined) {
163
+ router = hiddenContract;
164
+ }
165
+ if (router instanceof ProcedureContract) {
166
+ await callback(router, path);
167
+ return;
168
+ }
169
+ if (!isNonPrimitive(router)) {
170
+ return;
171
+ }
172
+ for (const [key, value] of Object.entries(router)) {
173
+ if (value instanceof Lazy) {
174
+ const { default: router } = await unlazy(value);
175
+ await walkProcedureContractsAsync(router, callback, [...path, key]);
176
+ }
177
+ else {
178
+ await walkProcedureContractsAsync(value, callback, [...path, key]);
179
+ }
180
+ }
181
+ }
182
+ /**
183
+ * Fully resolves all lazy routers inside a router, returning a plain router.
184
+ * Useful for making a router with lazy parts contract-compatible.
185
+ *
186
+ * @see {@link https://orpc.dev/docs/contract/router#router-to-contract | Router Contract - Router to Contract}
187
+ */
188
+ export async function unlazyRouter(router) {
189
+ if (router instanceof Procedure) {
190
+ return router;
191
+ }
192
+ if (!isNonPrimitive(router)) {
193
+ return router;
194
+ }
195
+ const unlazied = {};
196
+ for (const key of Object.keys(router)) {
197
+ const item = router[key];
198
+ const { default: unlaziedRouter } = await unlazy(item);
199
+ setOwn(unlazied, key, await unlazyRouter(unlaziedRouter));
200
+ }
201
+ return unlazied;
202
+ }
@@ -1,14 +1,15 @@
1
- import { RouterContract, ProcedureContract, ORPCErrorFromErrorMap, ThrowableError, InferSchemaInput, InferSchemaOutput } from '@orpc/contract';
2
- import { C as Context, a as Procedure, L as Lazyable, M as MergedContext } from './server.DFPxP2v3.js';
3
-
4
- type Router<TInitialContext extends Context> = Procedure<TInitialContext, any, any, any, any> | {
1
+ import type { InferSchemaInput, InferSchemaOutput, ORPCErrorFromErrorMap, ProcedureContract, RouterContract, ThrowableError } from '@orpc/contract';
2
+ import type { Context, MergedContext } from './context.ts';
3
+ import type { Lazyable } from './lazy.ts';
4
+ import type { Procedure } from './procedure.ts';
5
+ export type Router<TInitialContext extends Context> = Procedure<TInitialContext, any, any, any, any> | {
5
6
  [k: string]: Lazyable<Router<TInitialContext>>;
6
7
  };
7
- type ContractedRouter<T extends RouterContract, TInitialContext extends Context> = T extends ProcedureContract<infer $InputSchema, infer $OutputSchema, infer $ErrorMap> ? Procedure<TInitialContext, any, $InputSchema, $OutputSchema, $ErrorMap> : {
8
+ export type ContractedRouter<T extends RouterContract, TInitialContext extends Context> = T extends ProcedureContract<infer $InputSchema, infer $OutputSchema, infer $ErrorMap> ? Procedure<TInitialContext, any, $InputSchema, $OutputSchema, $ErrorMap> : {
8
9
  [K in keyof T]: T[K] extends RouterContract ? Lazyable<ContractedRouter<T[K], TInitialContext>> : never;
9
10
  };
10
- type AnyRouter = Router<any>;
11
- type InferRouterInitialContext<T extends AnyRouter> = T extends Router<infer $> ? $ : never;
11
+ export type AnyRouter = Router<any>;
12
+ export type InferRouterInitialContext<T extends AnyRouter> = T extends Router<infer $> ? $ : never;
12
13
  /**
13
14
  * Infer all initial context of the router.
14
15
  *
@@ -17,7 +18,7 @@ type InferRouterInitialContext<T extends AnyRouter> = T extends Router<infer $>
17
18
  *
18
19
  * @see {@link https://orpc.dev/docs/router#infer-router-initial-contexts | Router - Infer Router Initial Contexts}
19
20
  */
20
- type InferRouterInitialContexts<T extends AnyRouter> = T extends Procedure<infer UInitialContext, any, any, any, any> ? UInitialContext : {
21
+ export type InferRouterInitialContexts<T extends AnyRouter> = T extends Procedure<infer UInitialContext, any, any, any, any> ? UInitialContext : {
21
22
  [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInitialContexts<U> : never;
22
23
  };
23
24
  /**
@@ -28,7 +29,7 @@ type InferRouterInitialContexts<T extends AnyRouter> = T extends Procedure<infer
28
29
  *
29
30
  * @see {@link https://orpc.dev/docs/router#infer-router-final-contexts | Router - Infer Router Final Contexts}
30
31
  */
31
- type InferRouterFinalContexts<T extends AnyRouter> = T extends Procedure<infer UInitialContext, infer UInjectedContext, any, any, any> ? MergedContext<UInitialContext, UInjectedContext> : {
32
+ export type InferRouterFinalContexts<T extends AnyRouter> = T extends Procedure<infer UInitialContext, infer UInjectedContext, any, any, any> ? MergedContext<UInitialContext, UInjectedContext> : {
32
33
  [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterFinalContexts<U> : never;
33
34
  };
34
35
  /**
@@ -39,7 +40,7 @@ type InferRouterFinalContexts<T extends AnyRouter> = T extends Procedure<infer U
39
40
  *
40
41
  * @see {@link https://orpc.dev/docs/router#infer-router-inputs | Router - Infer Router Inputs}
41
42
  */
42
- type InferRouterInputs<T extends AnyRouter> = T extends Procedure<any, any, infer UInputSchema, any, any> ? InferSchemaInput<UInputSchema> : {
43
+ export type InferRouterInputs<T extends AnyRouter> = T extends Procedure<any, any, infer UInputSchema, any, any> ? InferSchemaInput<UInputSchema> : {
43
44
  [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInputs<U> : never;
44
45
  };
45
46
  /**
@@ -50,7 +51,7 @@ type InferRouterInputs<T extends AnyRouter> = T extends Procedure<any, any, infe
50
51
  *
51
52
  * @see {@link https://orpc.dev/docs/router#infer-router-outputs | Router - Infer Router Outputs}
52
53
  */
53
- type InferRouterOutputs<T extends AnyRouter> = T extends Procedure<any, any, any, infer UOutputSchema, any> ? InferSchemaOutput<UOutputSchema> : {
54
+ export type InferRouterOutputs<T extends AnyRouter> = T extends Procedure<any, any, any, infer UOutputSchema, any> ? InferSchemaOutput<UOutputSchema> : {
54
55
  [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterOutputs<U> : never;
55
56
  };
56
57
  /**
@@ -58,7 +59,7 @@ type InferRouterOutputs<T extends AnyRouter> = T extends Procedure<any, any, any
58
59
  *
59
60
  * @see {@link https://orpc.dev/docs/router#infer-router-error | Router - Infer Router Error}
60
61
  */
61
- type InferRouterError<T extends AnyRouter> = T extends Procedure<any, any, any, any, infer UErrorMap> ? ORPCErrorFromErrorMap<UErrorMap> | ThrowableError : {
62
+ export type InferRouterError<T extends AnyRouter> = T extends Procedure<any, any, any, any, infer UErrorMap> ? ORPCErrorFromErrorMap<UErrorMap> | ThrowableError : {
62
63
  [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterError<U> : never;
63
64
  }[keyof T];
64
65
  /**
@@ -66,8 +67,6 @@ type InferRouterError<T extends AnyRouter> = T extends Procedure<any, any, any,
66
67
  *
67
68
  * @see {@link https://orpc.dev/docs/router#infer-router-errors | Router - Infer Router Errors}
68
69
  */
69
- type InferRouterErrors<T extends AnyRouter> = T extends Procedure<any, any, any, any, infer UErrorMap> ? ORPCErrorFromErrorMap<UErrorMap> | ThrowableError : {
70
+ export type InferRouterErrors<T extends AnyRouter> = T extends Procedure<any, any, any, any, infer UErrorMap> ? ORPCErrorFromErrorMap<UErrorMap> | ThrowableError : {
70
71
  [K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterErrors<U> : never;
71
72
  };
72
-
73
- export type { AnyRouter as A, ContractedRouter as C, InferRouterInitialContext as I, Router as R, InferRouterFinalContexts as a, InferRouterError as b, InferRouterErrors as c, InferRouterInitialContexts as d, InferRouterInputs as e, InferRouterOutputs as f };
package/dist/router.js ADDED
@@ -0,0 +1 @@
1
+ export {};