@trpc/client 10.43.0 → 11.0.0-next.91

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trpc/client",
3
- "version": "10.43.0",
3
+ "version": "11.0.0-next.91+ca021e61",
4
4
  "description": "The tRPC client library",
5
5
  "author": "KATT",
6
6
  "license": "MIT",
@@ -76,11 +76,11 @@
76
76
  "!**/*.test.*"
77
77
  ],
78
78
  "peerDependencies": {
79
- "@trpc/server": "10.43.0"
79
+ "@trpc/server": "11.0.0-next.91+ca021e61"
80
80
  },
81
81
  "devDependencies": {
82
82
  "@testing-library/dom": "^9.0.0",
83
- "@trpc/server": "10.43.0",
83
+ "@trpc/server": "11.0.0-next.91+ca021e61",
84
84
  "@types/isomorphic-fetch": "^0.0.38",
85
85
  "@types/node": "^18.16.16",
86
86
  "eslint": "^8.40.0",
@@ -97,5 +97,5 @@
97
97
  "funding": [
98
98
  "https://trpc.io/sponsor"
99
99
  ],
100
- "gitHead": "0d505fd7415f537bd3e263971686b13a78584167"
100
+ "gitHead": "ca021e61ba8e6eb6e1715f124489cda75a00b5aa"
101
101
  }
@@ -1,29 +1,15 @@
1
- import {
2
- AnyProcedure,
3
- AnyRouter,
4
- DefaultErrorShape,
5
- inferRouterError,
6
- Maybe,
7
- } from '@trpc/server';
8
- import { TRPCErrorResponse, TRPCErrorShape } from '@trpc/server/rpc';
1
+ import { DefaultErrorShape, Maybe } from '@trpc/server';
2
+ import { TRPCErrorResponse } from '@trpc/server/rpc';
3
+ import { inferErrorShape, TRPCInferrable } from '@trpc/server/shared';
9
4
  import { isObject } from './internals/isObject';
10
5
 
11
- type ErrorInferrable = AnyProcedure | AnyRouter | TRPCErrorShape<number>;
12
-
13
- type inferErrorShape<TInferrable extends ErrorInferrable> =
14
- TInferrable extends AnyRouter
15
- ? inferRouterError<TInferrable>
16
- : TInferrable extends AnyProcedure
17
- ? TInferrable['_def']['_config']['$types']['errorShape']
18
- : TInferrable;
19
-
20
6
  export interface TRPCClientErrorBase<TShape extends DefaultErrorShape> {
21
7
  readonly message: string;
22
8
  readonly shape: Maybe<TShape>;
23
9
  readonly data: Maybe<TShape['data']>;
24
10
  }
25
- export type TRPCClientErrorLike<TRouterOrProcedure extends ErrorInferrable> =
26
- TRPCClientErrorBase<inferErrorShape<TRouterOrProcedure>>;
11
+ export type TRPCClientErrorLike<TInferrable extends TRPCInferrable> =
12
+ TRPCClientErrorBase<inferErrorShape<TInferrable>>;
27
13
 
28
14
  function isTRPCClientError(cause: unknown): cause is TRPCClientError<any> {
29
15
  return (
@@ -45,7 +31,7 @@ function isTRPCErrorResponse(obj: unknown): obj is TRPCErrorResponse<any> {
45
31
  );
46
32
  }
47
33
 
48
- export class TRPCClientError<TRouterOrProcedure extends ErrorInferrable>
34
+ export class TRPCClientError<TRouterOrProcedure extends TRPCInferrable>
49
35
  extends Error
50
36
  implements TRPCClientErrorBase<inferErrorShape<TRouterOrProcedure>>
51
37
  {
@@ -83,7 +69,7 @@ export class TRPCClientError<TRouterOrProcedure extends ErrorInferrable>
83
69
  Object.setPrototypeOf(this, TRPCClientError.prototype);
84
70
  }
85
71
 
86
- public static from<TRouterOrProcedure extends ErrorInferrable>(
72
+ public static from<TRouterOrProcedure extends TRPCInferrable>(
87
73
  _cause: Error | TRPCErrorResponse<any>,
88
74
  opts: { meta?: Record<string, unknown> } = {},
89
75
  ): TRPCClientError<TRouterOrProcedure> {
@@ -1,64 +1,156 @@
1
+ /* eslint-disable @typescript-eslint/no-non-null-assertion */
1
2
  import type {
3
+ AnyMutationProcedure,
4
+ AnyProcedure,
5
+ AnyQueryProcedure,
6
+ AnyRootConfig,
2
7
  AnyRouter,
3
- inferProcedureInput,
4
- inferProcedureOutput,
5
- inferSubscriptionOutput,
8
+ AnySubscriptionProcedure,
9
+ IntersectionError,
10
+ ProcedureArgs,
11
+ ProcedureRouterRecord,
12
+ ProcedureType,
6
13
  } from '@trpc/server';
7
- import { Unsubscribable } from '@trpc/server/observable';
8
- import { inferTransformedProcedureOutput } from '@trpc/server/shared';
14
+ import type { Unsubscribable } from '@trpc/server/observable';
15
+ import {
16
+ createFlatProxy,
17
+ createRecursiveProxy,
18
+ inferTransformedProcedureOutput,
19
+ inferTransformedSubscriptionOutput,
20
+ } from '@trpc/server/shared';
21
+ import { CreateTRPCClientOptions } from './createTRPCUntypedClient';
9
22
  import {
10
- CreateTRPCClientOptions,
11
- TRPCRequestOptions,
12
23
  TRPCSubscriptionObserver,
13
24
  TRPCUntypedClient,
25
+ UntypedClientProperties,
14
26
  } from './internals/TRPCUntypedClient';
15
- import { TRPCClientRuntime } from './links';
16
27
  import { TRPCClientError } from './TRPCClientError';
17
28
 
18
29
  /**
19
- * @deprecated
30
+ * @public
31
+ **/
32
+ export type inferRouterClient<TRouter extends AnyRouter> =
33
+ DecoratedProcedureRecord<TRouter, TRouter['_def']['record']>;
34
+
35
+ /** @internal */
36
+ export type Resolver<
37
+ TConfig extends AnyRootConfig,
38
+ TProcedure extends AnyProcedure,
39
+ > = (
40
+ ...args: ProcedureArgs<TProcedure['_def']>
41
+ ) => Promise<inferTransformedProcedureOutput<TConfig, TProcedure>>;
42
+
43
+ type SubscriptionResolver<
44
+ TConfig extends AnyRootConfig,
45
+ TProcedure extends AnyProcedure,
46
+ > = (
47
+ ...args: [
48
+ input: ProcedureArgs<TProcedure['_def']>[0],
49
+ opts: Partial<
50
+ TRPCSubscriptionObserver<
51
+ inferTransformedSubscriptionOutput<TConfig, TProcedure>,
52
+ TRPCClientError<TConfig>
53
+ >
54
+ > &
55
+ ProcedureArgs<TProcedure['_def']>[1],
56
+ ]
57
+ ) => Unsubscribable;
58
+
59
+ type DecorateProcedure<
60
+ TConfig extends AnyRootConfig,
61
+ TProcedure extends AnyProcedure,
62
+ > = TProcedure extends AnyQueryProcedure
63
+ ? {
64
+ query: Resolver<TConfig, TProcedure>;
65
+ }
66
+ : TProcedure extends AnyMutationProcedure
67
+ ? {
68
+ mutate: Resolver<TConfig, TProcedure>;
69
+ }
70
+ : TProcedure extends AnySubscriptionProcedure
71
+ ? {
72
+ subscribe: SubscriptionResolver<TConfig, TProcedure>;
73
+ }
74
+ : never;
75
+
76
+ /**
77
+ * @internal
20
78
  */
21
- export interface TRPCClient<TRouter extends AnyRouter> {
22
- readonly runtime: TRPCClientRuntime;
23
- query<
24
- TQueries extends TRouter['_def']['queries'],
25
- TPath extends string & keyof TQueries,
26
- TInput extends inferProcedureInput<TQueries[TPath]>,
27
- >(
28
- path: TPath,
29
- input?: TInput,
30
- opts?: TRPCRequestOptions,
31
- ): Promise<inferProcedureOutput<TQueries[TPath]>>;
79
+ type DecoratedProcedureRecord<
80
+ TRouter extends AnyRouter,
81
+ TProcedures extends ProcedureRouterRecord,
82
+ > = {
83
+ [TKey in keyof TProcedures]: TProcedures[TKey] extends AnyRouter
84
+ ? DecoratedProcedureRecord<TRouter, TProcedures[TKey]['_def']['record']>
85
+ : TProcedures[TKey] extends AnyProcedure
86
+ ? DecorateProcedure<TRouter['_def']['_config'], TProcedures[TKey]>
87
+ : never;
88
+ };
32
89
 
33
- mutation<
34
- TMutations extends TRouter['_def']['mutations'],
35
- TPath extends string & keyof TMutations,
36
- TInput extends inferProcedureInput<TMutations[TPath]>,
37
- >(
38
- path: TPath,
39
- input?: TInput,
40
- opts?: TRPCRequestOptions,
41
- ): Promise<inferTransformedProcedureOutput<TMutations[TPath]>>;
90
+ const clientCallTypeMap: Record<
91
+ keyof DecorateProcedure<any, any>,
92
+ ProcedureType
93
+ > = {
94
+ query: 'query',
95
+ mutate: 'mutation',
96
+ subscribe: 'subscription',
97
+ };
98
+
99
+ /** @internal */
100
+ export const clientCallTypeToProcedureType = (
101
+ clientCallType: string,
102
+ ): ProcedureType => {
103
+ return clientCallTypeMap[clientCallType as keyof typeof clientCallTypeMap];
104
+ };
42
105
 
43
- subscription<
44
- TSubscriptions extends TRouter['_def']['subscriptions'],
45
- TPath extends string & keyof TSubscriptions,
46
- // TODO - this should probably be updated to use inferTransformedProcedureOutput but this is only hit for legacy clients
47
- TOutput extends inferSubscriptionOutput<TRouter, TPath>,
48
- TInput extends inferProcedureInput<TSubscriptions[TPath]>,
49
- >(
50
- path: TPath,
51
- input: TInput,
52
- opts: Partial<TRPCSubscriptionObserver<TOutput, TRPCClientError<TRouter>>> &
53
- TRPCRequestOptions,
54
- ): Unsubscribable;
55
- }
56
106
  /**
57
- * @deprecated use `createTRPCProxyClient` instead
107
+ * Creates a proxy client and shows type errors if you have query names that collide with built-in properties
58
108
  */
109
+ export type CreateTRPCClient<TRouter extends AnyRouter> =
110
+ inferRouterClient<TRouter> extends infer $ProcedureRecord
111
+ ? UntypedClientProperties & keyof $ProcedureRecord extends never
112
+ ? inferRouterClient<TRouter>
113
+ : IntersectionError<UntypedClientProperties & keyof $ProcedureRecord>
114
+ : never;
115
+
116
+ /**
117
+ * @internal
118
+ */
119
+ export function createTRPCClientProxy<TRouter extends AnyRouter>(
120
+ client: TRPCUntypedClient<TRouter>,
121
+ ): CreateTRPCClient<TRouter> {
122
+ return createFlatProxy<CreateTRPCClient<TRouter>>((key) => {
123
+ if (client.hasOwnProperty(key)) {
124
+ return (client as any)[key as any];
125
+ }
126
+ if (key === '__untypedClient') {
127
+ return client;
128
+ }
129
+ return createRecursiveProxy(({ path, args }) => {
130
+ const pathCopy = [key, ...path];
131
+ const procedureType = clientCallTypeToProcedureType(pathCopy.pop()!);
132
+
133
+ const fullPath = pathCopy.join('.');
134
+
135
+ return (client as any)[procedureType](fullPath, ...args);
136
+ });
137
+ });
138
+ }
139
+
59
140
  export function createTRPCClient<TRouter extends AnyRouter>(
60
141
  opts: CreateTRPCClientOptions<TRouter>,
61
- ) {
142
+ ): CreateTRPCClient<TRouter> {
62
143
  const client = new TRPCUntypedClient(opts);
63
- return client as TRPCClient<TRouter>;
144
+ const proxy = createTRPCClientProxy<TRouter>(client);
145
+ return proxy;
146
+ }
147
+
148
+ /**
149
+ * Get an untyped client from a proxy client
150
+ * @internal
151
+ */
152
+ export function getUntypedClient<TRouter extends AnyRouter>(
153
+ client: inferRouterClient<TRouter>,
154
+ ): TRPCUntypedClient<TRouter> {
155
+ return (client as any).__untypedClient;
64
156
  }
package/src/index.ts CHANGED
@@ -1,6 +1,20 @@
1
1
  export * from './createTRPCUntypedClient';
2
2
  export * from './createTRPCClient';
3
- export * from './createTRPCClientProxy';
4
3
  export * from './getFetch';
5
4
  export * from './TRPCClientError';
6
5
  export * from './links';
6
+
7
+ export {
8
+ /**
9
+ * @deprecated - use `createTRPCClient` instead
10
+ */
11
+ createTRPCClient as createTRPCProxyClient,
12
+ /**
13
+ * @deprecated - use `CreateTRPCClient` instead
14
+ */
15
+ type CreateTRPCClient as CreateTRPCProxyClient,
16
+ /**
17
+ * @deprecated - use `inferRouterClient` instead
18
+ */
19
+ type inferRouterClient as inferRouterProxyClient,
20
+ } from './createTRPCClient';
@@ -14,8 +14,3 @@ export interface HTTPBatchLinkOptions extends HTTPLinkBaseOptions {
14
14
  opList: NonEmptyArray<Operation>;
15
15
  }) => HTTPHeaders | Promise<HTTPHeaders>);
16
16
  }
17
- /**
18
- * @alias HttpBatchLinkOptions
19
- * @deprecated use `HTTPBatchLinkOptions` instead
20
- */
21
- export interface HttpBatchLinkOptions extends HTTPBatchLinkOptions {}