@orpc/server 0.47.0 → 1.0.0-beta.1

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 (34) hide show
  1. package/README.md +3 -1
  2. package/dist/adapters/fetch/index.d.mts +5 -3
  3. package/dist/adapters/fetch/index.d.ts +5 -3
  4. package/dist/adapters/fetch/index.mjs +3 -3
  5. package/dist/adapters/hono/index.d.mts +4 -2
  6. package/dist/adapters/hono/index.d.ts +4 -2
  7. package/dist/adapters/hono/index.mjs +3 -3
  8. package/dist/adapters/next/index.d.mts +4 -2
  9. package/dist/adapters/next/index.d.ts +4 -2
  10. package/dist/adapters/next/index.mjs +3 -3
  11. package/dist/adapters/node/index.d.mts +5 -3
  12. package/dist/adapters/node/index.d.ts +5 -3
  13. package/dist/adapters/node/index.mjs +7 -6
  14. package/dist/adapters/standard/index.d.mts +10 -9
  15. package/dist/adapters/standard/index.d.ts +10 -9
  16. package/dist/adapters/standard/index.mjs +2 -2
  17. package/dist/index.d.mts +140 -117
  18. package/dist/index.d.ts +140 -117
  19. package/dist/index.mjs +55 -46
  20. package/dist/plugins/index.d.mts +12 -12
  21. package/dist/plugins/index.d.ts +12 -12
  22. package/dist/plugins/index.mjs +1 -1
  23. package/dist/shared/{server.Drm1Lma3.d.ts → server.B3Tm0IXY.d.ts} +12 -14
  24. package/dist/shared/server.BYTulgUc.d.mts +144 -0
  25. package/dist/shared/server.BYTulgUc.d.ts +144 -0
  26. package/dist/shared/{server.CtBp-i4f.d.mts → server.BeJithK4.d.mts} +12 -14
  27. package/dist/shared/{server.V6zT5iYQ.mjs → server.BtxZnWJ9.mjs} +155 -160
  28. package/dist/shared/server.Bz_xNBjz.d.mts +8 -0
  29. package/dist/shared/{server.BHIDiY4a.mjs → server.DoP20NVH.mjs} +6 -5
  30. package/dist/shared/server.EhgR_5_I.d.ts +8 -0
  31. package/dist/shared/{server.DKrKGnk2.mjs → server.jG7ZuX3S.mjs} +11 -14
  32. package/package.json +7 -7
  33. package/dist/shared/server.ptXwNGQr.d.mts +0 -158
  34. package/dist/shared/server.ptXwNGQr.d.ts +0 -158
@@ -1,13 +1,14 @@
1
- import { RPCSerializer } from '@orpc/client/standard';
1
+ import { StandardRPCJsonSerializer, StandardRPCSerializer } from '@orpc/client/standard';
2
2
  import { toStandardLazyRequest, toFetchResponse } from '@orpc/standard-server-fetch';
3
- import { S as StandardHandler, a as RPCMatcher, R as RPCCodec } from './server.DKrKGnk2.mjs';
3
+ import { S as StandardHandler, b as StandardRPCMatcher, a as StandardRPCCodec } from './server.jG7ZuX3S.mjs';
4
4
 
5
5
  class RPCHandler {
6
6
  standardHandler;
7
7
  constructor(router, options = {}) {
8
- const serializer = new RPCSerializer();
9
- const matcher = new RPCMatcher();
10
- const codec = new RPCCodec(serializer);
8
+ const jsonSerializer = new StandardRPCJsonSerializer(options);
9
+ const serializer = new StandardRPCSerializer(jsonSerializer);
10
+ const matcher = new StandardRPCMatcher();
11
+ const codec = new StandardRPCCodec(serializer);
11
12
  this.standardHandler = new StandardHandler(router, matcher, codec, options);
12
13
  }
13
14
  async handle(request, ...[
@@ -0,0 +1,8 @@
1
+ import { StandardRPCJsonSerializerOptions } from '@orpc/client/standard';
2
+ import { C as Context } from './server.BYTulgUc.js';
3
+ import { a as StandardHandlerOptions } from './server.B3Tm0IXY.js';
4
+
5
+ interface StandardRPCHandlerOptions<T extends Context> extends StandardHandlerOptions<T>, StandardRPCJsonSerializerOptions {
6
+ }
7
+
8
+ export type { StandardRPCHandlerOptions as S };
@@ -1,7 +1,7 @@
1
1
  import { ORPCError, toORPCError } from '@orpc/client';
2
2
  import { intercept, trim, parseEmptyableJSON } from '@orpc/shared';
3
3
  import { C as CompositePlugin } from './server.Q6ZmnTgO.mjs';
4
- import { c as createProcedureClient, e as eachContractProcedure, a as convertPathToHttpPath, i as isProcedure, u as unlazy, g as getRouterChild, b as createContractedProcedure } from './server.V6zT5iYQ.mjs';
4
+ import { c as createProcedureClient, t as traverseContractProcedures, a as toHttpPath, i as isProcedure, u as unlazy, g as getRouter, b as createContractedProcedure } from './server.BtxZnWJ9.mjs';
5
5
 
6
6
  class StandardHandler {
7
7
  constructor(router, matcher, codec, options) {
@@ -69,7 +69,7 @@ class StandardHandler {
69
69
  }
70
70
  }
71
71
 
72
- class RPCCodec {
72
+ class StandardRPCCodec {
73
73
  constructor(serializer) {
74
74
  this.serializer = serializer;
75
75
  }
@@ -93,15 +93,12 @@ class RPCCodec {
93
93
  }
94
94
  }
95
95
 
96
- class RPCMatcher {
96
+ class StandardRPCMatcher {
97
97
  tree = {};
98
98
  pendingRouters = [];
99
99
  init(router, path = []) {
100
- const laziedOptions = eachContractProcedure({
101
- router,
102
- path
103
- }, ({ path: path2, contract }) => {
104
- const httpPath = convertPathToHttpPath(path2);
100
+ const laziedOptions = traverseContractProcedures({ router, path }, ({ path: path2, contract }) => {
101
+ const httpPath = toHttpPath(path2);
105
102
  if (isProcedure(contract)) {
106
103
  this.tree[httpPath] = {
107
104
  path: path2,
@@ -121,7 +118,7 @@ class RPCMatcher {
121
118
  });
122
119
  this.pendingRouters.push(...laziedOptions.map((option) => ({
123
120
  ...option,
124
- httpPathPrefix: convertPathToHttpPath(option.path)
121
+ httpPathPrefix: toHttpPath(option.path)
125
122
  })));
126
123
  }
127
124
  async match(_method, pathname) {
@@ -129,7 +126,7 @@ class RPCMatcher {
129
126
  const newPendingRouters = [];
130
127
  for (const pendingRouter of this.pendingRouters) {
131
128
  if (pathname.startsWith(pendingRouter.httpPathPrefix)) {
132
- const { default: router } = await unlazy(pendingRouter.lazied);
129
+ const { default: router } = await unlazy(pendingRouter.router);
133
130
  this.init(router, pendingRouter.path);
134
131
  } else {
135
132
  newPendingRouters.push(pendingRouter);
@@ -142,14 +139,14 @@ class RPCMatcher {
142
139
  return void 0;
143
140
  }
144
141
  if (!match.procedure) {
145
- const { default: maybeProcedure } = await unlazy(getRouterChild(match.router, ...match.path));
142
+ const { default: maybeProcedure } = await unlazy(getRouter(match.router, match.path));
146
143
  if (!isProcedure(maybeProcedure)) {
147
144
  throw new Error(`
148
- [Contract-First] Missing or invalid implementation for procedure at path: ${convertPathToHttpPath(match.path)}.
145
+ [Contract-First] Missing or invalid implementation for procedure at path: ${toHttpPath(match.path)}.
149
146
  Ensure that the procedure is correctly defined and matches the expected contract.
150
147
  `);
151
148
  }
152
- match.procedure = createContractedProcedure(match.contract, maybeProcedure);
149
+ match.procedure = createContractedProcedure(maybeProcedure, match.contract);
153
150
  }
154
151
  return {
155
152
  path: match.path,
@@ -158,4 +155,4 @@ class RPCMatcher {
158
155
  }
159
156
  }
160
157
 
161
- export { RPCCodec as R, StandardHandler as S, RPCMatcher as a };
158
+ export { StandardHandler as S, StandardRPCCodec as a, StandardRPCMatcher as b };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@orpc/server",
3
3
  "type": "module",
4
- "version": "0.47.0",
4
+ "version": "1.0.0-beta.1",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -58,12 +58,12 @@
58
58
  "next": ">=14.0.0"
59
59
  },
60
60
  "dependencies": {
61
- "@orpc/client": "0.47.0",
62
- "@orpc/contract": "0.47.0",
63
- "@orpc/standard-server": "0.47.0",
64
- "@orpc/shared": "0.47.0",
65
- "@orpc/standard-server-node": "0.47.0",
66
- "@orpc/standard-server-fetch": "0.47.0"
61
+ "@orpc/client": "1.0.0-beta.1",
62
+ "@orpc/shared": "1.0.0-beta.1",
63
+ "@orpc/standard-server": "1.0.0-beta.1",
64
+ "@orpc/standard-server-fetch": "1.0.0-beta.1",
65
+ "@orpc/standard-server-node": "1.0.0-beta.1",
66
+ "@orpc/contract": "1.0.0-beta.1"
67
67
  },
68
68
  "devDependencies": {
69
69
  "light-my-request": "^6.5.1"
@@ -1,158 +0,0 @@
1
- import { ORPCErrorCode, ORPCErrorOptions, ORPCError, ClientContext, Client } from '@orpc/client';
2
- import { IsNever, MaybeOptionalOptions, Promisable, Interceptor, Value } from '@orpc/shared';
3
- import { ErrorMap, ErrorMapItem, SchemaInput, HTTPPath, Meta, Schema, ContractProcedureDef, SchemaOutput, ErrorFromErrorMap, AnyContractRouter, ContractProcedure, MergedErrorMap } from '@orpc/contract';
4
-
5
- type Context = Record<string, any>;
6
- type MergedContext<T extends Context, U extends Context> = T & U;
7
- declare function mergeContext<T extends Context, U extends Context>(context: T, other: U): MergedContext<T, U>;
8
- type ConflictContextGuard<T extends Context> = true extends IsNever<T> | {
9
- [K in keyof T]: IsNever<T[K]>;
10
- }[keyof T] ? never : unknown;
11
-
12
- type ORPCErrorConstructorMapItemOptions<TData> = Omit<ORPCErrorOptions<TData>, 'defined' | 'status'>;
13
- type ORPCErrorConstructorMapItem<TCode extends ORPCErrorCode, TInData> = (...rest: MaybeOptionalOptions<ORPCErrorConstructorMapItemOptions<TInData>>) => ORPCError<TCode, TInData>;
14
- type ORPCErrorConstructorMap<T extends ErrorMap> = {
15
- [K in keyof T]: K extends ORPCErrorCode ? T[K] extends ErrorMapItem<infer UInputSchema> ? ORPCErrorConstructorMapItem<K, SchemaInput<UInputSchema>> : never : never;
16
- };
17
-
18
- declare const LAZY_LOADER_SYMBOL: unique symbol;
19
- interface LazyMeta {
20
- prefix?: HTTPPath;
21
- }
22
- interface Lazy<T> {
23
- [LAZY_LOADER_SYMBOL](): Promise<{
24
- default: T;
25
- }>;
26
- }
27
- type Lazyable<T> = T | Lazy<T>;
28
- interface LazyOptions {
29
- prefix?: HTTPPath;
30
- }
31
- declare function lazy<T>(loader: () => Promise<{
32
- default: T;
33
- }>): Lazy<T>;
34
- declare function isLazy(item: unknown): item is Lazy<any>;
35
- declare function unlazy<T extends Lazyable<any>>(lazied: T): Promise<{
36
- default: T extends Lazy<infer U> ? U : T;
37
- }>;
38
-
39
- type MiddlewareResult<TOutContext extends Context, TOutput> = Promisable<{
40
- output: TOutput;
41
- context: TOutContext;
42
- }>;
43
- type MiddlewareNextFnOptions<TOutContext extends Context> = Record<never, never> extends TOutContext ? {
44
- context?: TOutContext;
45
- } : {
46
- context: TOutContext;
47
- };
48
- interface MiddlewareNextFn<TInContext extends Context, TOutput> {
49
- <U extends Context & Partial<TInContext> = Record<never, never>>(...rest: MaybeOptionalOptions<MiddlewareNextFnOptions<U>>): MiddlewareResult<U, TOutput>;
50
- }
51
- interface MiddlewareOutputFn<TOutput> {
52
- (output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
53
- }
54
- interface MiddlewareOptions<TInContext extends Context, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
55
- context: TInContext;
56
- path: string[];
57
- procedure: Procedure<Context, Context, Schema, Schema, unknown, ErrorMap, TMeta>;
58
- signal?: AbortSignal;
59
- lastEventId: string | undefined;
60
- next: MiddlewareNextFn<TInContext, TOutput>;
61
- errors: TErrorConstructorMap;
62
- }
63
- interface Middleware<TInContext extends Context, TOutContext extends Context, TInput, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
64
- (options: MiddlewareOptions<TInContext, TOutput, TErrorConstructorMap, TMeta>, input: TInput, output: MiddlewareOutputFn<TOutput>): Promisable<MiddlewareResult<TOutContext, TOutput>>;
65
- }
66
- type AnyMiddleware = Middleware<any, any, any, any, any, any>;
67
- interface MapInputMiddleware<TInput, TMappedInput> {
68
- (input: TInput): TMappedInput;
69
- }
70
- declare function middlewareOutputFn<TOutput>(output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
71
-
72
- interface ProcedureHandlerOptions<TCurrentContext extends Context, TInput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
73
- context: TCurrentContext;
74
- input: TInput;
75
- path: string[];
76
- procedure: Procedure<Context, Context, Schema, Schema, unknown, ErrorMap, TMeta>;
77
- signal?: AbortSignal;
78
- lastEventId: string | undefined;
79
- errors: TErrorConstructorMap;
80
- }
81
- interface ProcedureHandler<TCurrentContext extends Context, TInput, THandlerOutput, TErrorMap extends ErrorMap, TMeta extends Meta> {
82
- (opt: ProcedureHandlerOptions<TCurrentContext, TInput, ORPCErrorConstructorMap<TErrorMap>, TMeta>): Promisable<THandlerOutput>;
83
- }
84
- interface ProcedureDef<TInitialContext extends Context, TCurrentContext extends Context, TInputSchema extends Schema, TOutputSchema extends Schema, THandlerOutput, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap, TMeta> {
85
- __initialContext?: (type: TInitialContext) => unknown;
86
- middlewares: AnyMiddleware[];
87
- inputValidationIndex: number;
88
- outputValidationIndex: number;
89
- handler: ProcedureHandler<TCurrentContext, any, THandlerOutput, any, any>;
90
- }
91
- declare class Procedure<TInitialContext extends Context, TCurrentContext extends Context, TInputSchema extends Schema, TOutputSchema extends Schema, THandlerOutput, TErrorMap extends ErrorMap, TMeta extends Meta> {
92
- '~orpc': ProcedureDef<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap, TMeta>;
93
- constructor(def: ProcedureDef<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap, TMeta>);
94
- }
95
- type AnyProcedure = Procedure<any, any, any, any, any, any, any>;
96
- declare function isProcedure(item: unknown): item is AnyProcedure;
97
-
98
- type ProcedureClient<TClientContext extends ClientContext, TInputSchema extends Schema, TOutputSchema extends Schema, THandlerOutput, TErrorMap extends ErrorMap> = Client<TClientContext, SchemaInput<TInputSchema>, SchemaOutput<TOutputSchema, THandlerOutput>, ErrorFromErrorMap<TErrorMap>>;
99
- interface ProcedureClientInterceptorOptions<TInitialContext extends Context, TInputSchema extends Schema, TErrorMap extends ErrorMap, TMeta extends Meta> {
100
- context: TInitialContext;
101
- input: SchemaInput<TInputSchema>;
102
- errors: ORPCErrorConstructorMap<TErrorMap>;
103
- path: string[];
104
- procedure: Procedure<Context, Context, Schema, Schema, unknown, ErrorMap, TMeta>;
105
- signal?: AbortSignal;
106
- lastEventId: string | undefined;
107
- }
108
- /**
109
- * Options for creating a procedure caller with comprehensive type safety
110
- */
111
- type CreateProcedureClientOptions<TInitialContext extends Context, TInputSchema extends Schema, TOutputSchema extends Schema, THandlerOutput, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext> = {
112
- /**
113
- * This is helpful for logging and analytics.
114
- */
115
- path?: string[];
116
- interceptors?: Interceptor<ProcedureClientInterceptorOptions<TInitialContext, TInputSchema, TErrorMap, TMeta>, SchemaOutput<TOutputSchema, THandlerOutput>, ErrorFromErrorMap<TErrorMap>>[];
117
- } & (Record<never, never> extends TInitialContext ? {
118
- context?: Value<TInitialContext, [clientContext: TClientContext]>;
119
- } : {
120
- context: Value<TInitialContext, [clientContext: TClientContext]>;
121
- });
122
- declare function createProcedureClient<TInitialContext extends Context, TInputSchema extends Schema, TOutputSchema extends Schema, THandlerOutput, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext>(lazyableProcedure: Lazyable<Procedure<TInitialContext, any, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap, TMeta>>, ...[options]: MaybeOptionalOptions<CreateProcedureClientOptions<TInitialContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap, TMeta, TClientContext>>): ProcedureClient<TClientContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap>;
123
-
124
- type AccessibleLazyRouter<T extends AnyRouter | undefined | Lazy<AnyRouter | undefined>> = T extends Lazy<infer U extends AnyRouter | undefined | Lazy<AnyRouter | undefined>> ? AccessibleLazyRouter<U> : Lazy<T> & (T extends AnyProcedure | undefined ? unknown : {
125
- [K in keyof T]: T[K] extends AnyRouter ? AccessibleLazyRouter<T[K]> : never;
126
- });
127
- declare function createAccessibleLazyRouter<T extends Lazy<AnyRouter | undefined>>(lazied: T): AccessibleLazyRouter<T>;
128
-
129
- type Router<TInitialContext extends Context, TContract extends AnyContractRouter> = Lazyable<TContract extends ContractProcedure<infer UInputSchema, infer UOutputSchema, infer UErrorMap, infer UMeta> ? Procedure<TInitialContext, any, UInputSchema, UOutputSchema, any, UErrorMap, UMeta> : {
130
- [K in keyof TContract]: TContract[K] extends AnyContractRouter ? Router<TInitialContext, TContract[K]> : never;
131
- }>;
132
- type AnyRouter = Router<any, any>;
133
- type InferRouterInitialContext<T extends AnyRouter> = T extends Router<infer UInitialContext, any> ? UInitialContext : never;
134
- type InferRouterInitialContexts<T extends AnyRouter> = T extends Lazy<infer U extends AnyRouter> ? InferRouterInitialContexts<U> : T extends Procedure<infer UInitialContext, any, any, any, any, any, any> ? UInitialContext : {
135
- [K in keyof T]: T[K] extends AnyRouter ? InferRouterInitialContexts<T[K]> : never;
136
- };
137
- type InferRouterCurrentContexts<T extends AnyRouter> = T extends Lazy<infer U extends AnyRouter> ? InferRouterCurrentContexts<U> : T extends Procedure<any, infer UCurrentContext, any, any, any, any, any> ? UCurrentContext : {
138
- [K in keyof T]: T[K] extends AnyRouter ? InferRouterCurrentContexts<T[K]> : never;
139
- };
140
- type InferRouterInputs<T extends AnyRouter> = T extends Lazy<infer U extends AnyRouter> ? InferRouterInputs<U> : T extends Procedure<any, any, infer UInputSchema, any, any, any, any> ? SchemaInput<UInputSchema> : {
141
- [K in keyof T]: T[K] extends AnyRouter ? InferRouterInputs<T[K]> : never;
142
- };
143
- type InferRouterOutputs<T extends AnyRouter> = T extends Lazy<infer U extends AnyRouter> ? InferRouterOutputs<U> : T extends Procedure<any, any, any, infer UOutputSchema, infer UFuncOutput, any, any> ? SchemaOutput<UOutputSchema, UFuncOutput> : {
144
- [K in keyof T]: T[K] extends AnyRouter ? InferRouterOutputs<T[K]> : never;
145
- };
146
- type AdaptedRouter<TRouter extends AnyRouter, TInitialContext extends Context, TErrorMap extends ErrorMap> = TRouter extends Lazy<infer U extends AnyRouter> ? AccessibleLazyRouter<AdaptedRouter<U, TInitialContext, TErrorMap>> : TRouter extends Procedure<any, infer UCurrentContext, infer UInputSchema, infer UOutputSchema, infer UFuncOutput, infer UErrorMap, infer UMeta> ? Procedure<TInitialContext, UCurrentContext, UInputSchema, UOutputSchema, UFuncOutput, MergedErrorMap<TErrorMap, UErrorMap>, UMeta> : {
147
- [K in keyof TRouter]: TRouter[K] extends AnyRouter ? AdaptedRouter<TRouter[K], TInitialContext, TErrorMap> : never;
148
- };
149
- interface AdaptRouterOptions<TErrorMap extends ErrorMap> {
150
- middlewares: AnyMiddleware[];
151
- tags?: readonly string[];
152
- prefix?: HTTPPath;
153
- errorMap: TErrorMap;
154
- }
155
- declare function adaptRouter<TRouter extends AnyRouter, TInitialContext extends Context, TErrorMap extends ErrorMap>(router: TRouter, options: AdaptRouterOptions<TErrorMap>): AdaptedRouter<TRouter, TInitialContext, TErrorMap>;
156
- declare function getRouterChild<T extends AnyRouter | Lazy<undefined>>(router: T, ...path: string[]): T extends Lazy<any> ? Lazy<AnyProcedure> | Lazy<Record<string, AnyRouter>> | Lazy<undefined> : AnyRouter | Lazy<undefined> | undefined;
157
-
158
- export { type AnyProcedure as A, type ProcedureDef as B, type Context as C, isProcedure as D, createProcedureClient as E, type InferRouterInitialContexts as F, type InferRouterCurrentContexts as G, type InferRouterInputs as H, type InferRouterInitialContext as I, type InferRouterOutputs as J, adaptRouter as K, type Lazy as L, type Middleware as M, getRouterChild as N, type ORPCErrorConstructorMap as O, type ProcedureClientInterceptorOptions as P, type AccessibleLazyRouter as Q, type Router as R, createAccessibleLazyRouter as S, type AnyRouter as a, Procedure as b, type ConflictContextGuard as c, type MergedContext as d, type MapInputMiddleware as e, type CreateProcedureClientOptions as f, type ProcedureClient as g, type ProcedureHandler as h, type AdaptRouterOptions as i, type AdaptedRouter as j, type AnyMiddleware as k, type Lazyable as l, mergeContext as m, LAZY_LOADER_SYMBOL as n, type LazyMeta as o, type LazyOptions as p, lazy as q, isLazy as r, type MiddlewareResult as s, type MiddlewareNextFnOptions as t, unlazy as u, type MiddlewareNextFn as v, type MiddlewareOutputFn as w, type MiddlewareOptions as x, middlewareOutputFn as y, type ProcedureHandlerOptions as z };
@@ -1,158 +0,0 @@
1
- import { ORPCErrorCode, ORPCErrorOptions, ORPCError, ClientContext, Client } from '@orpc/client';
2
- import { IsNever, MaybeOptionalOptions, Promisable, Interceptor, Value } from '@orpc/shared';
3
- import { ErrorMap, ErrorMapItem, SchemaInput, HTTPPath, Meta, Schema, ContractProcedureDef, SchemaOutput, ErrorFromErrorMap, AnyContractRouter, ContractProcedure, MergedErrorMap } from '@orpc/contract';
4
-
5
- type Context = Record<string, any>;
6
- type MergedContext<T extends Context, U extends Context> = T & U;
7
- declare function mergeContext<T extends Context, U extends Context>(context: T, other: U): MergedContext<T, U>;
8
- type ConflictContextGuard<T extends Context> = true extends IsNever<T> | {
9
- [K in keyof T]: IsNever<T[K]>;
10
- }[keyof T] ? never : unknown;
11
-
12
- type ORPCErrorConstructorMapItemOptions<TData> = Omit<ORPCErrorOptions<TData>, 'defined' | 'status'>;
13
- type ORPCErrorConstructorMapItem<TCode extends ORPCErrorCode, TInData> = (...rest: MaybeOptionalOptions<ORPCErrorConstructorMapItemOptions<TInData>>) => ORPCError<TCode, TInData>;
14
- type ORPCErrorConstructorMap<T extends ErrorMap> = {
15
- [K in keyof T]: K extends ORPCErrorCode ? T[K] extends ErrorMapItem<infer UInputSchema> ? ORPCErrorConstructorMapItem<K, SchemaInput<UInputSchema>> : never : never;
16
- };
17
-
18
- declare const LAZY_LOADER_SYMBOL: unique symbol;
19
- interface LazyMeta {
20
- prefix?: HTTPPath;
21
- }
22
- interface Lazy<T> {
23
- [LAZY_LOADER_SYMBOL](): Promise<{
24
- default: T;
25
- }>;
26
- }
27
- type Lazyable<T> = T | Lazy<T>;
28
- interface LazyOptions {
29
- prefix?: HTTPPath;
30
- }
31
- declare function lazy<T>(loader: () => Promise<{
32
- default: T;
33
- }>): Lazy<T>;
34
- declare function isLazy(item: unknown): item is Lazy<any>;
35
- declare function unlazy<T extends Lazyable<any>>(lazied: T): Promise<{
36
- default: T extends Lazy<infer U> ? U : T;
37
- }>;
38
-
39
- type MiddlewareResult<TOutContext extends Context, TOutput> = Promisable<{
40
- output: TOutput;
41
- context: TOutContext;
42
- }>;
43
- type MiddlewareNextFnOptions<TOutContext extends Context> = Record<never, never> extends TOutContext ? {
44
- context?: TOutContext;
45
- } : {
46
- context: TOutContext;
47
- };
48
- interface MiddlewareNextFn<TInContext extends Context, TOutput> {
49
- <U extends Context & Partial<TInContext> = Record<never, never>>(...rest: MaybeOptionalOptions<MiddlewareNextFnOptions<U>>): MiddlewareResult<U, TOutput>;
50
- }
51
- interface MiddlewareOutputFn<TOutput> {
52
- (output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
53
- }
54
- interface MiddlewareOptions<TInContext extends Context, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
55
- context: TInContext;
56
- path: string[];
57
- procedure: Procedure<Context, Context, Schema, Schema, unknown, ErrorMap, TMeta>;
58
- signal?: AbortSignal;
59
- lastEventId: string | undefined;
60
- next: MiddlewareNextFn<TInContext, TOutput>;
61
- errors: TErrorConstructorMap;
62
- }
63
- interface Middleware<TInContext extends Context, TOutContext extends Context, TInput, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
64
- (options: MiddlewareOptions<TInContext, TOutput, TErrorConstructorMap, TMeta>, input: TInput, output: MiddlewareOutputFn<TOutput>): Promisable<MiddlewareResult<TOutContext, TOutput>>;
65
- }
66
- type AnyMiddleware = Middleware<any, any, any, any, any, any>;
67
- interface MapInputMiddleware<TInput, TMappedInput> {
68
- (input: TInput): TMappedInput;
69
- }
70
- declare function middlewareOutputFn<TOutput>(output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
71
-
72
- interface ProcedureHandlerOptions<TCurrentContext extends Context, TInput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
73
- context: TCurrentContext;
74
- input: TInput;
75
- path: string[];
76
- procedure: Procedure<Context, Context, Schema, Schema, unknown, ErrorMap, TMeta>;
77
- signal?: AbortSignal;
78
- lastEventId: string | undefined;
79
- errors: TErrorConstructorMap;
80
- }
81
- interface ProcedureHandler<TCurrentContext extends Context, TInput, THandlerOutput, TErrorMap extends ErrorMap, TMeta extends Meta> {
82
- (opt: ProcedureHandlerOptions<TCurrentContext, TInput, ORPCErrorConstructorMap<TErrorMap>, TMeta>): Promisable<THandlerOutput>;
83
- }
84
- interface ProcedureDef<TInitialContext extends Context, TCurrentContext extends Context, TInputSchema extends Schema, TOutputSchema extends Schema, THandlerOutput, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap, TMeta> {
85
- __initialContext?: (type: TInitialContext) => unknown;
86
- middlewares: AnyMiddleware[];
87
- inputValidationIndex: number;
88
- outputValidationIndex: number;
89
- handler: ProcedureHandler<TCurrentContext, any, THandlerOutput, any, any>;
90
- }
91
- declare class Procedure<TInitialContext extends Context, TCurrentContext extends Context, TInputSchema extends Schema, TOutputSchema extends Schema, THandlerOutput, TErrorMap extends ErrorMap, TMeta extends Meta> {
92
- '~orpc': ProcedureDef<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap, TMeta>;
93
- constructor(def: ProcedureDef<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap, TMeta>);
94
- }
95
- type AnyProcedure = Procedure<any, any, any, any, any, any, any>;
96
- declare function isProcedure(item: unknown): item is AnyProcedure;
97
-
98
- type ProcedureClient<TClientContext extends ClientContext, TInputSchema extends Schema, TOutputSchema extends Schema, THandlerOutput, TErrorMap extends ErrorMap> = Client<TClientContext, SchemaInput<TInputSchema>, SchemaOutput<TOutputSchema, THandlerOutput>, ErrorFromErrorMap<TErrorMap>>;
99
- interface ProcedureClientInterceptorOptions<TInitialContext extends Context, TInputSchema extends Schema, TErrorMap extends ErrorMap, TMeta extends Meta> {
100
- context: TInitialContext;
101
- input: SchemaInput<TInputSchema>;
102
- errors: ORPCErrorConstructorMap<TErrorMap>;
103
- path: string[];
104
- procedure: Procedure<Context, Context, Schema, Schema, unknown, ErrorMap, TMeta>;
105
- signal?: AbortSignal;
106
- lastEventId: string | undefined;
107
- }
108
- /**
109
- * Options for creating a procedure caller with comprehensive type safety
110
- */
111
- type CreateProcedureClientOptions<TInitialContext extends Context, TInputSchema extends Schema, TOutputSchema extends Schema, THandlerOutput, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext> = {
112
- /**
113
- * This is helpful for logging and analytics.
114
- */
115
- path?: string[];
116
- interceptors?: Interceptor<ProcedureClientInterceptorOptions<TInitialContext, TInputSchema, TErrorMap, TMeta>, SchemaOutput<TOutputSchema, THandlerOutput>, ErrorFromErrorMap<TErrorMap>>[];
117
- } & (Record<never, never> extends TInitialContext ? {
118
- context?: Value<TInitialContext, [clientContext: TClientContext]>;
119
- } : {
120
- context: Value<TInitialContext, [clientContext: TClientContext]>;
121
- });
122
- declare function createProcedureClient<TInitialContext extends Context, TInputSchema extends Schema, TOutputSchema extends Schema, THandlerOutput, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext>(lazyableProcedure: Lazyable<Procedure<TInitialContext, any, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap, TMeta>>, ...[options]: MaybeOptionalOptions<CreateProcedureClientOptions<TInitialContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap, TMeta, TClientContext>>): ProcedureClient<TClientContext, TInputSchema, TOutputSchema, THandlerOutput, TErrorMap>;
123
-
124
- type AccessibleLazyRouter<T extends AnyRouter | undefined | Lazy<AnyRouter | undefined>> = T extends Lazy<infer U extends AnyRouter | undefined | Lazy<AnyRouter | undefined>> ? AccessibleLazyRouter<U> : Lazy<T> & (T extends AnyProcedure | undefined ? unknown : {
125
- [K in keyof T]: T[K] extends AnyRouter ? AccessibleLazyRouter<T[K]> : never;
126
- });
127
- declare function createAccessibleLazyRouter<T extends Lazy<AnyRouter | undefined>>(lazied: T): AccessibleLazyRouter<T>;
128
-
129
- type Router<TInitialContext extends Context, TContract extends AnyContractRouter> = Lazyable<TContract extends ContractProcedure<infer UInputSchema, infer UOutputSchema, infer UErrorMap, infer UMeta> ? Procedure<TInitialContext, any, UInputSchema, UOutputSchema, any, UErrorMap, UMeta> : {
130
- [K in keyof TContract]: TContract[K] extends AnyContractRouter ? Router<TInitialContext, TContract[K]> : never;
131
- }>;
132
- type AnyRouter = Router<any, any>;
133
- type InferRouterInitialContext<T extends AnyRouter> = T extends Router<infer UInitialContext, any> ? UInitialContext : never;
134
- type InferRouterInitialContexts<T extends AnyRouter> = T extends Lazy<infer U extends AnyRouter> ? InferRouterInitialContexts<U> : T extends Procedure<infer UInitialContext, any, any, any, any, any, any> ? UInitialContext : {
135
- [K in keyof T]: T[K] extends AnyRouter ? InferRouterInitialContexts<T[K]> : never;
136
- };
137
- type InferRouterCurrentContexts<T extends AnyRouter> = T extends Lazy<infer U extends AnyRouter> ? InferRouterCurrentContexts<U> : T extends Procedure<any, infer UCurrentContext, any, any, any, any, any> ? UCurrentContext : {
138
- [K in keyof T]: T[K] extends AnyRouter ? InferRouterCurrentContexts<T[K]> : never;
139
- };
140
- type InferRouterInputs<T extends AnyRouter> = T extends Lazy<infer U extends AnyRouter> ? InferRouterInputs<U> : T extends Procedure<any, any, infer UInputSchema, any, any, any, any> ? SchemaInput<UInputSchema> : {
141
- [K in keyof T]: T[K] extends AnyRouter ? InferRouterInputs<T[K]> : never;
142
- };
143
- type InferRouterOutputs<T extends AnyRouter> = T extends Lazy<infer U extends AnyRouter> ? InferRouterOutputs<U> : T extends Procedure<any, any, any, infer UOutputSchema, infer UFuncOutput, any, any> ? SchemaOutput<UOutputSchema, UFuncOutput> : {
144
- [K in keyof T]: T[K] extends AnyRouter ? InferRouterOutputs<T[K]> : never;
145
- };
146
- type AdaptedRouter<TRouter extends AnyRouter, TInitialContext extends Context, TErrorMap extends ErrorMap> = TRouter extends Lazy<infer U extends AnyRouter> ? AccessibleLazyRouter<AdaptedRouter<U, TInitialContext, TErrorMap>> : TRouter extends Procedure<any, infer UCurrentContext, infer UInputSchema, infer UOutputSchema, infer UFuncOutput, infer UErrorMap, infer UMeta> ? Procedure<TInitialContext, UCurrentContext, UInputSchema, UOutputSchema, UFuncOutput, MergedErrorMap<TErrorMap, UErrorMap>, UMeta> : {
147
- [K in keyof TRouter]: TRouter[K] extends AnyRouter ? AdaptedRouter<TRouter[K], TInitialContext, TErrorMap> : never;
148
- };
149
- interface AdaptRouterOptions<TErrorMap extends ErrorMap> {
150
- middlewares: AnyMiddleware[];
151
- tags?: readonly string[];
152
- prefix?: HTTPPath;
153
- errorMap: TErrorMap;
154
- }
155
- declare function adaptRouter<TRouter extends AnyRouter, TInitialContext extends Context, TErrorMap extends ErrorMap>(router: TRouter, options: AdaptRouterOptions<TErrorMap>): AdaptedRouter<TRouter, TInitialContext, TErrorMap>;
156
- declare function getRouterChild<T extends AnyRouter | Lazy<undefined>>(router: T, ...path: string[]): T extends Lazy<any> ? Lazy<AnyProcedure> | Lazy<Record<string, AnyRouter>> | Lazy<undefined> : AnyRouter | Lazy<undefined> | undefined;
157
-
158
- export { type AnyProcedure as A, type ProcedureDef as B, type Context as C, isProcedure as D, createProcedureClient as E, type InferRouterInitialContexts as F, type InferRouterCurrentContexts as G, type InferRouterInputs as H, type InferRouterInitialContext as I, type InferRouterOutputs as J, adaptRouter as K, type Lazy as L, type Middleware as M, getRouterChild as N, type ORPCErrorConstructorMap as O, type ProcedureClientInterceptorOptions as P, type AccessibleLazyRouter as Q, type Router as R, createAccessibleLazyRouter as S, type AnyRouter as a, Procedure as b, type ConflictContextGuard as c, type MergedContext as d, type MapInputMiddleware as e, type CreateProcedureClientOptions as f, type ProcedureClient as g, type ProcedureHandler as h, type AdaptRouterOptions as i, type AdaptedRouter as j, type AnyMiddleware as k, type Lazyable as l, mergeContext as m, LAZY_LOADER_SYMBOL as n, type LazyMeta as o, type LazyOptions as p, lazy as q, isLazy as r, type MiddlewareResult as s, type MiddlewareNextFnOptions as t, unlazy as u, type MiddlewareNextFn as v, type MiddlewareOutputFn as w, type MiddlewareOptions as x, middlewareOutputFn as y, type ProcedureHandlerOptions as z };