@trpc/client 11.32.2 → 11.34.0

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 (78) hide show
  1. package/dist/{httpBatchLink-DbD4ZGAM.d.cts → httpBatchLink-50xXDyIp.d.cts} +6 -4
  2. package/dist/{httpBatchLink-DbD4ZGAM.d.cts.map → httpBatchLink-50xXDyIp.d.cts.map} +1 -1
  3. package/dist/{httpBatchLink-Dk3_n5pP.d.mts → httpBatchLink-D4shlyQy.d.mts} +6 -4
  4. package/dist/{httpBatchLink-Dk3_n5pP.d.mts.map → httpBatchLink-D4shlyQy.d.mts.map} +1 -1
  5. package/dist/httpBatchLink-DRitkRBC.mjs.map +1 -1
  6. package/dist/{httpLink-BjZdBzr6.d.mts → httpLink-Bb6hOyBN.d.mts} +6 -4
  7. package/dist/httpLink-Bb6hOyBN.d.mts.map +1 -0
  8. package/dist/httpLink-Cmg_mV7L.mjs.map +1 -1
  9. package/dist/{httpUtils-gZ7bAQ6T.d.mts → httpUtils-BbinScxw.d.mts} +2 -2
  10. package/dist/{httpUtils-CxgZ3W-1.d.cts.map → httpUtils-BbinScxw.d.mts.map} +1 -1
  11. package/dist/{httpUtils-CxgZ3W-1.d.cts → httpUtils-dB1XeokQ.d.cts} +2 -2
  12. package/dist/{httpUtils-gZ7bAQ6T.d.mts.map → httpUtils-dB1XeokQ.d.cts.map} +1 -1
  13. package/dist/index.cjs +25 -16
  14. package/dist/index.d.cts +120 -25
  15. package/dist/index.d.cts.map +1 -1
  16. package/dist/index.d.mts +123 -28
  17. package/dist/index.d.mts.map +1 -1
  18. package/dist/index.mjs +25 -17
  19. package/dist/index.mjs.map +1 -1
  20. package/dist/links/httpBatchLink.d.cts +1 -1
  21. package/dist/links/httpBatchLink.d.mts +1 -1
  22. package/dist/links/httpLink.d.cts +5 -3
  23. package/dist/links/httpLink.d.cts.map +1 -1
  24. package/dist/links/httpLink.d.mts +1 -1
  25. package/dist/links/loggerLink.d.cts +4 -2
  26. package/dist/links/loggerLink.d.cts.map +1 -1
  27. package/dist/links/loggerLink.d.mts +1 -1
  28. package/dist/links/loggerLink.mjs.map +1 -1
  29. package/dist/links/splitLink.cjs +1 -1
  30. package/dist/links/splitLink.d.cts +43 -9
  31. package/dist/links/splitLink.d.cts.map +1 -1
  32. package/dist/links/splitLink.d.mts +1 -1
  33. package/dist/links/splitLink.mjs +1 -1
  34. package/dist/links/wsLink/wsLink.d.cts +1 -1
  35. package/dist/links/wsLink/wsLink.d.mts +1 -1
  36. package/dist/{loggerLink-CBPx95zm.d.mts → loggerLink-y1x7WTWX.d.mts} +5 -3
  37. package/dist/loggerLink-y1x7WTWX.d.mts.map +1 -0
  38. package/dist/splitLink-BGLTgm1a.d.mts +56 -0
  39. package/dist/splitLink-BGLTgm1a.d.mts.map +1 -0
  40. package/dist/{splitLink-PFsRy7Mf.mjs → splitLink-C8U8vvLl.mjs} +10 -9
  41. package/dist/splitLink-C8U8vvLl.mjs.map +1 -0
  42. package/dist/{splitLink-BBLj14Dv.cjs → splitLink-NqS85b92.cjs} +9 -8
  43. package/dist/{types-BG-3kpeg.d.mts → types-BZH1P86I.d.mts} +67 -10
  44. package/dist/types-BZH1P86I.d.mts.map +1 -0
  45. package/dist/{types-BhHxuBsu.d.cts → types-CGS36fjk.d.cts} +67 -10
  46. package/dist/types-CGS36fjk.d.cts.map +1 -0
  47. package/dist/{wsLink-DU5wj6kH.d.mts → wsLink-CFh-pvQG.d.mts} +5 -3
  48. package/dist/{wsLink-BlAMQb0i.d.cts.map → wsLink-CFh-pvQG.d.mts.map} +1 -1
  49. package/dist/{wsLink-BlAMQb0i.d.cts → wsLink-CJT5pqFt.d.cts} +5 -3
  50. package/dist/{wsLink-DU5wj6kH.d.mts.map → wsLink-CJT5pqFt.d.cts.map} +1 -1
  51. package/dist/wsLink-C_Zg2yXV.mjs.map +1 -1
  52. package/package.json +3 -3
  53. package/skills/client-setup/SKILL.md +1 -1
  54. package/skills/links/SKILL.md +59 -2
  55. package/skills/links/references/client-links-retryLink.md +28 -0
  56. package/skills/links/references/client-links-splitLink.md +22 -6
  57. package/skills/superjson/SKILL.md +1 -1
  58. package/src/createTRPCClient.ts +91 -21
  59. package/src/index.ts +1 -0
  60. package/src/internals/types.ts +8 -5
  61. package/src/links/httpBatchLink.ts +6 -2
  62. package/src/links/httpBatchStreamLink.ts +6 -2
  63. package/src/links/httpLink.ts +6 -1
  64. package/src/links/httpSubscriptionLink.ts +6 -2
  65. package/src/links/localLink.ts +1 -1
  66. package/src/links/loggerLink.ts +11 -2
  67. package/src/links/retryLink.ts +42 -7
  68. package/src/links/splitLink.ts +126 -13
  69. package/src/links/types.ts +114 -5
  70. package/src/links/wsLink/wsLink.ts +1 -1
  71. package/src/routerType.ts +31 -0
  72. package/dist/httpLink-BjZdBzr6.d.mts.map +0 -1
  73. package/dist/loggerLink-CBPx95zm.d.mts.map +0 -1
  74. package/dist/splitLink-BtPz7lCB.d.mts +0 -23
  75. package/dist/splitLink-BtPz7lCB.d.mts.map +0 -1
  76. package/dist/splitLink-PFsRy7Mf.mjs.map +0 -1
  77. package/dist/types-BG-3kpeg.d.mts.map +0 -1
  78. package/dist/types-BhHxuBsu.d.cts.map +0 -1
@@ -20,24 +20,27 @@ Let's say you're using `httpBatchLink` as the terminating link in your tRPC clie
20
20
 
21
21
  #### 1. Configure client / `utils/trpc.ts`
22
22
 
23
+ `context` gives the fields `condition` reads, with their defaults:
24
+
23
25
  ```ts no-check title="client/index.ts"
24
26
  import {
25
27
  createTRPCClient,
26
28
  httpBatchLink,
27
29
  httpLink,
30
+ routerType,
28
31
  splitLink,
29
32
  } from '@trpc/client';
30
33
  import type { AppRouter } from './server';
31
34
 
32
35
  const url = `http://localhost:3000`;
33
36
 
34
- const client = createTRPCClient<AppRouter>({
37
+ const client = createTRPCClient({
38
+ router: routerType<AppRouter>(),
35
39
  links: [
36
40
  splitLink({
37
- condition(op) {
38
- // check for context property `skipBatch`
39
- return Boolean(op.context.skipBatch);
40
- },
41
+ // the `context` this condition reads, with its defaults
42
+ context: { skipBatch: false },
43
+ condition: (op) => op.context.skipBatch,
41
44
  // when condition is true, use normal request
42
45
  true: httpLink({
43
46
  url,
@@ -84,12 +87,25 @@ export function MyComponent() {
84
87
  }
85
88
  ```
86
89
 
90
+ ### Typed `context`
91
+
92
+ With `context: { skipBatch: false }` and `router: routerType<AppRouter>()`, calls take `skipBatch?: boolean`, so passing anything else is a type error. A call that leaves it out, or passes `undefined`, gets `false`. A field the call passes replaces its default whole: nested objects aren't merged. Without `context`, calls take only what the branches' links declare.
93
+
87
94
  ## `splitLink` Options
88
95
 
89
- The `splitLink` function takes an options object that has three fields: `condition`, `true`, and `false`.
96
+ The `splitLink` function takes an options object with `condition`, `true` and `false`, and optionally `context`.
90
97
 
91
98
  ```ts no-check
92
99
  declare function splitLink<TRouter extends AnyRouter = AnyRouter>(opts: {
100
+ /**
101
+ * Values for the `context` fields `condition` reads, used when a call
102
+ * leaves one out. Their types are the fields calls can pass. Without it,
103
+ * calls take only what the branches' links declare.
104
+ */
105
+ context?: Record<string, unknown>;
106
+ /**
107
+ * Picks the chain for an operation.
108
+ */
93
109
  condition: (op: Operation) => boolean;
94
110
  /**
95
111
  * The link to execute next if the test function returns `true`.
@@ -9,7 +9,7 @@ sources:
9
9
  metadata:
10
10
  type: composition
11
11
  library: trpc
12
- library_version: 11.32.2
12
+ library_version: 11.34.0
13
13
  ---
14
14
 
15
15
  <!-- Generated from www/v11/docs/server/data-transformers.md by www/build/skills.ts. Edit the docs instead. -->
@@ -24,6 +24,8 @@ import type {
24
24
  } from './internals/TRPCUntypedClient';
25
25
  import { TRPCUntypedClient } from './internals/TRPCUntypedClient';
26
26
  import type { TRPCProcedureOptions } from './internals/types';
27
+ import type { inferLinksContext, TRPCLink } from './links/types';
28
+ import type { RouterType } from './routerType';
27
29
  import type { TRPCTaggedPromise } from './safe';
28
30
  import type { TRPCClientError } from './TRPCClientError';
29
31
 
@@ -46,15 +48,22 @@ const untypedClientSymbol = Symbol.for('trpc_untypedClient');
46
48
  /**
47
49
  * A typed client for a router, as `createTRPCClient` returns it. Each
48
50
  * procedure has `.query()`, `.mutate()` or `.subscribe()`.
51
+ *
52
+ * `TContext` is the `context` calls take; `createTRPCClient({ router,
53
+ * links })` infers it from the links.
49
54
  * @see https://trpc.io/docs/client/vanilla/infer-types
50
55
  * @public
51
56
  **/
52
- export type TRPCClient<TRouter extends AnyRouter> = DecoratedProcedureRecord<
57
+ export type TRPCClient<
58
+ TRouter extends AnyRouter,
59
+ TContext extends object = Record<string, unknown>,
60
+ > = DecoratedProcedureRecord<
53
61
  {
54
62
  transformer: TRouter['_def']['_config']['$types']['transformer'];
55
63
  errorShape: TRouter['_def']['_config']['$types']['errorShape'];
56
64
  },
57
- TRouter['_def']['record']
65
+ TRouter['_def']['record'],
66
+ TContext
58
67
  > & {
59
68
  [untypedClientSymbol]: TRPCUntypedClient<TRouter>;
60
69
  };
@@ -79,9 +88,12 @@ type coerceAsyncGeneratorToIterable<T> =
79
88
  * The `.query()` or `.mutate()` of a procedure on the client.
80
89
  * @internal
81
90
  */
82
- export type Resolver<TDef extends TRPCResolverDef> = (
91
+ export type Resolver<
92
+ TDef extends TRPCResolverDef,
93
+ TContext extends object = Record<string, unknown>,
94
+ > = (
83
95
  input: TDef['input'],
84
- opts?: TRPCProcedureOptions,
96
+ opts?: TRPCProcedureOptions<TContext>,
85
97
  ) => TRPCTaggedPromise<
86
98
  coerceAsyncGeneratorToIterable<TDef['output']>,
87
99
  TDef['errorShape']
@@ -91,16 +103,19 @@ export type Resolver<TDef extends TRPCResolverDef> = (
91
103
  * The `.subscribe()` of a subscription procedure on the client.
92
104
  * @internal
93
105
  */
94
- export type SubscriptionResolver<TDef extends TRPCResolverDef> = {
106
+ export type SubscriptionResolver<
107
+ TDef extends TRPCResolverDef,
108
+ TContext extends object = Record<string, unknown>,
109
+ > = {
95
110
  (
96
111
  input: TDef['input'],
97
112
  opts: TRPCSubscriptionCallbacks<TDef['output'], TRPCClientError<TDef>> &
98
- TRPCProcedureOptions,
113
+ TRPCProcedureOptions<TContext>,
99
114
  ): Unsubscribable;
100
115
  <
101
- TOpts extends TRPCProcedureOptions &
116
+ TOpts extends TRPCProcedureOptions<TContext> &
102
117
  Partial<TRPCSubscriptionObserver<TDef['output'], TRPCClientError<TDef>>> =
103
- TRPCProcedureOptions,
118
+ TRPCProcedureOptions<TContext>,
104
119
  >(
105
120
  input: TDef['input'],
106
121
  opts?: TRPCSubscriptionWithoutCallbacks<TOpts>,
@@ -110,18 +125,13 @@ export type SubscriptionResolver<TDef extends TRPCResolverDef> = {
110
125
  type DecorateProcedure<
111
126
  TType extends ProcedureType,
112
127
  TDef extends TRPCResolverDef,
128
+ TContext extends object,
113
129
  > = TType extends 'query'
114
- ? {
115
- query: Resolver<TDef>;
116
- }
130
+ ? { query: Resolver<TDef, TContext> }
117
131
  : TType extends 'mutation'
118
- ? {
119
- mutate: Resolver<TDef>;
120
- }
132
+ ? { mutate: Resolver<TDef, TContext> }
121
133
  : TType extends 'subscription'
122
- ? {
123
- subscribe: SubscriptionResolver<TDef>;
124
- }
134
+ ? { subscribe: SubscriptionResolver<TDef, TContext> }
125
135
  : never;
126
136
 
127
137
  /**
@@ -130,6 +140,7 @@ type DecorateProcedure<
130
140
  type DecoratedProcedureRecord<
131
141
  TRoot extends InferrableClientTypes,
132
142
  TRecord extends RouterRecord,
143
+ TContext extends object,
133
144
  > = {
134
145
  [TKey in keyof TRecord]: TRecord[TKey] extends infer $Value
135
146
  ? $Value extends AnyProcedure
@@ -143,16 +154,17 @@ type DecoratedProcedureRecord<
143
154
  >;
144
155
  errorShape: inferProcedureErrorShape<TRoot, $Value>;
145
156
  transformer: inferClientTypes<TRoot>['transformer'];
146
- }
157
+ },
158
+ TContext
147
159
  >
148
160
  : $Value extends RouterRecord
149
- ? DecoratedProcedureRecord<TRoot, $Value>
161
+ ? DecoratedProcedureRecord<TRoot, $Value, TContext>
150
162
  : never
151
163
  : never;
152
164
  };
153
165
 
154
166
  const clientCallTypeMap: Record<
155
- keyof DecorateProcedure<any, any>,
167
+ keyof DecorateProcedure<any, any, any>,
156
168
  ProcedureType
157
169
  > = {
158
170
  query: 'query',
@@ -193,6 +205,55 @@ export function createTRPCClientProxy<TRouter extends AnyRouter>(
193
205
  });
194
206
  }
195
207
 
208
+ /**
209
+ * The options of `createTRPCClient` with the router's type passed as
210
+ * `router: routerType<AppRouter>()`.
211
+ * @public
212
+ */
213
+ export type CreateTRPCClientWithRouterOptions<
214
+ TRouter extends AnyRouter,
215
+ TLinks extends readonly TRPCLink<TRouter, any>[] = TRPCLink<TRouter>[],
216
+ > = Omit<CreateTRPCClientOptions<TRouter>, 'links'> & {
217
+ /**
218
+ * The router's type, from `routerType<AppRouter>()`. It's a type only:
219
+ * nothing about the router is sent or bundled.
220
+ */
221
+ router: RouterType<TRouter>;
222
+ /**
223
+ * The links each call goes through, in order. The last one must send
224
+ * the call, such as `httpBatchLink`. What they declare types each call's
225
+ * `context`, and which methods the client has. Pass them inline or
226
+ * `as const`: in an array declared without it, TypeScript can merge the
227
+ * links' types and drop what one declares.
228
+ * @see https://trpc.io/docs/client/links
229
+ */
230
+ links: TLinks;
231
+ };
232
+
233
+ /**
234
+ * Creates a typed client for a router. Pass the router's type with
235
+ * `routerType`, and the links that send its calls. What the links declare
236
+ * types each call's `context`.
237
+ * @example
238
+ * ```ts
239
+ * const client = createTRPCClient({
240
+ * router: routerType<AppRouter>(),
241
+ * links: [retryLink({ retry: () => false }), httpBatchLink({ url })],
242
+ * });
243
+ * const post = await client.post.byId.query(
244
+ * { id: '1' },
245
+ * { context: { retry: (opts) => opts.attempts <= 3 } },
246
+ * );
247
+ * ```
248
+ * @see https://trpc.io/docs/client/vanilla/setup#3-initialize-the-trpc-client
249
+ */
250
+ export function createTRPCClient<
251
+ TRouter extends AnyRouter,
252
+ // with an explicit router type, the links aren't inferred
253
+ const TLinks extends readonly TRPCLink<TRouter, any>[] = TRPCLink<TRouter>[],
254
+ >(
255
+ opts: CreateTRPCClientWithRouterOptions<TRouter, TLinks>,
256
+ ): TRPCClient<TRouter, inferLinksContext<TLinks>>;
196
257
  /**
197
258
  * Creates a typed client for a router. Pass the router's type, and the
198
259
  * links that send its calls.
@@ -205,10 +266,19 @@ export function createTRPCClientProxy<TRouter extends AnyRouter>(
205
266
  * ```
206
267
  * @see https://trpc.io/docs/client/vanilla/setup#3-initialize-the-trpc-client
207
268
  */
269
+ // Kept last: `ReturnType<typeof createTRPCClient>` reads the last overload.
208
270
  export function createTRPCClient<TRouter extends AnyRouter>(
209
271
  opts: CreateTRPCClientOptions<TRouter>,
272
+ ): TRPCClient<TRouter>;
273
+ export function createTRPCClient<TRouter extends AnyRouter>(
274
+ opts: Omit<CreateTRPCClientOptions<TRouter>, 'links'> & {
275
+ links: readonly TRPCLink<TRouter, any>[];
276
+ },
210
277
  ): TRPCClient<TRouter> {
211
- const client = new TRPCUntypedClient(opts);
278
+ // `router` is a type only, so the untyped client ignores it
279
+ const client = new TRPCUntypedClient<TRouter>({
280
+ links: [...opts.links],
281
+ });
212
282
  const proxy = createTRPCClientProxy<TRouter>(client);
213
283
  return proxy;
214
284
  }
package/src/index.ts CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  export * from './createTRPCUntypedClient';
4
4
  export * from './createTRPCClient';
5
+ export * from './routerType';
5
6
  export * from './getFetch';
6
7
  export * from './TRPCClientError';
7
8
  export * from './links';
@@ -88,19 +88,22 @@ export interface ResponseEsque {
88
88
  */
89
89
  export type NonEmptyArray<TItem> = [TItem, ...TItem[]];
90
90
 
91
- type ClientContext = Record<string, unknown>;
92
-
93
91
  /**
94
92
  * The options of a call made with the client, such as
95
- * `client.post.byId.query(input, opts)`.
93
+ * `client.post.byId.query(input, opts)`. `TContext` is what the client's
94
+ * links read, when it was created with `router: routerType<AppRouter>()`.
96
95
  * @public
97
96
  */
98
- export interface TRPCProcedureOptions {
97
+ // The default is written out, not aliased, so declaration files that name
98
+ // a client's type don't need a reference to an internal alias.
99
+ export interface TRPCProcedureOptions<
100
+ TContext extends object = Record<string, unknown>,
101
+ > {
99
102
  /**
100
103
  * Context for the links, such as a flag a `splitLink` reads.
101
104
  * @see https://trpc.io/docs/client/links#managing-context
102
105
  */
103
- context?: ClientContext;
106
+ context?: TContext;
104
107
  /**
105
108
  * Aborts the call. With `httpBatchLink`, the request is only aborted once
106
109
  * every call in its batch is, so an aborted call can still get a result.
@@ -16,7 +16,7 @@ import {
16
16
  jsonHttpRequester,
17
17
  resolveHTTPLinkOptions,
18
18
  } from './internals/httpUtils';
19
- import type { Operation, TRPCLink } from './types';
19
+ import type { AugmentedOperationContext, Operation, TRPCLink } from './types';
20
20
 
21
21
  /**
22
22
  * A terminating link that batches calls made in the same tick into one HTTP
@@ -26,7 +26,11 @@ import type { Operation, TRPCLink } from './types';
26
26
  */
27
27
  export function httpBatchLink<TRouter extends AnyRouter>(
28
28
  opts: HTTPBatchLinkOptions<TRouter['_def']['_config']['$types']>,
29
- ): TRPCLink<TRouter> {
29
+ ): TRPCLink<
30
+ TRouter,
31
+ // its callbacks may read the fields added to `OperationContext`
32
+ { context: AugmentedOperationContext }
33
+ > {
30
34
  const resolvedOpts = resolveHTTPLinkOptions(opts);
31
35
  const maxURLLength = opts.maxURLLength ?? Infinity;
32
36
  const maxItems = opts.maxItems ?? Infinity;
@@ -20,7 +20,7 @@ import {
20
20
  getUrl,
21
21
  resolveHTTPLinkOptions,
22
22
  } from './internals/httpUtils';
23
- import type { Operation, TRPCLink } from './types';
23
+ import type { AugmentedOperationContext, Operation, TRPCLink } from './types';
24
24
 
25
25
  /**
26
26
  * Options for {@link httpBatchStreamLink}. Takes the same options as
@@ -53,7 +53,11 @@ type BatchStreamOperation = Operation & {
53
53
  */
54
54
  export function httpBatchStreamLink<TRouter extends AnyRouter>(
55
55
  opts: HTTPBatchStreamLinkOptions<TRouter['_def']['_config']['$types']>,
56
- ): TRPCLink<TRouter> {
56
+ ): TRPCLink<
57
+ TRouter,
58
+ // its callbacks may read the fields added to `OperationContext`
59
+ { context: AugmentedOperationContext }
60
+ > {
57
61
  const resolvedOpts = resolveHTTPLinkOptions(opts);
58
62
  const maxURLLength = opts.maxURLLength ?? Infinity;
59
63
  const maxItems = opts.maxItems ?? Infinity;
@@ -22,6 +22,7 @@ import {
22
22
  isOctetType,
23
23
  type HTTPHeaders,
24
24
  type Operation,
25
+ type AugmentedOperationContext,
25
26
  type TRPCLink,
26
27
  } from './types';
27
28
 
@@ -82,7 +83,11 @@ const universalRequester: Requester = (opts) => {
82
83
  */
83
84
  export function httpLink<TRouter extends AnyRouter = AnyRouter>(
84
85
  opts: HTTPLinkOptions<TRouter['_def']['_config']['$types']>,
85
- ): TRPCLink<TRouter> {
86
+ ): TRPCLink<
87
+ TRouter,
88
+ // its callbacks may read the fields added to `OperationContext`
89
+ { context: AugmentedOperationContext }
90
+ > {
86
91
  const resolvedOpts = resolveHTTPLinkOptions(opts);
87
92
  return () => {
88
93
  return (operationOpts) => {
@@ -22,7 +22,7 @@ import {
22
22
  resultOf,
23
23
  type UrlOptionsWithConnectionParams,
24
24
  } from './internals/urlWithConnectionParams';
25
- import type { Operation, TRPCLink } from './types';
25
+ import type { AugmentedOperationContext, Operation, TRPCLink } from './types';
26
26
 
27
27
  async function urlWithConnectionParams(
28
28
  opts: UrlOptionsWithConnectionParams,
@@ -79,7 +79,11 @@ export function httpSubscriptionLink<
79
79
  inferClientTypes<TInferrable>,
80
80
  TEventSource
81
81
  >,
82
- ): TRPCLink<TInferrable> {
82
+ ): TRPCLink<
83
+ TInferrable,
84
+ // its callbacks may read the fields added to `OperationContext`
85
+ { context: AugmentedOperationContext }
86
+ > {
83
87
  const transformer = getTransformer(opts.transformer);
84
88
  const pathSeparator = resolvePathSeparator(opts.pathSeparator);
85
89
 
@@ -86,7 +86,7 @@ export type LocalLinkOptions<TRouter extends AnyRouter> = {
86
86
  */
87
87
  export function localLink<TRouter extends AnyRouter>(
88
88
  opts: LocalLinkOptions<TRouter>,
89
- ): TRPCLink<TRouter> {
89
+ ): TRPCLink<TRouter, { context: {} }> {
90
90
  const transformer = getTransformer(opts.transformer);
91
91
 
92
92
  const transformChunk = (chunk: unknown) => {
@@ -11,7 +11,12 @@ import type {
11
11
  InferrableClientTypes,
12
12
  } from '@trpc/server/unstable-core-do-not-import';
13
13
  import type { TRPCClientError } from '../TRPCClientError';
14
- import type { Operation, OperationResultEnvelope, TRPCLink } from './types';
14
+ import type {
15
+ Operation,
16
+ AugmentedOperationContext,
17
+ OperationResultEnvelope,
18
+ TRPCLink,
19
+ } from './types';
15
20
 
16
21
  type ConsoleEsque = {
17
22
  log: (...args: any[]) => void;
@@ -229,7 +234,11 @@ const defaultLogger =
229
234
  */
230
235
  export function loggerLink<TRouter extends AnyRouter = AnyRouter>(
231
236
  opts: LoggerLinkOptions<TRouter> = {},
232
- ): TRPCLink<TRouter> {
237
+ ): TRPCLink<
238
+ TRouter,
239
+ // its callbacks may read the fields added to `OperationContext`
240
+ { context: AugmentedOperationContext }
241
+ > {
233
242
  const { enabled = () => true } = opts;
234
243
 
235
244
  const colorMode =
@@ -5,20 +5,49 @@ import { observable } from '@trpc/server/observable';
5
5
  import type { InferrableClientTypes } from '@trpc/server/unstable-core-do-not-import';
6
6
  import { inputWithTrackedEventId } from '../internals/inputWithTrackedEventId';
7
7
  import type { TRPCClientError } from '../TRPCClientError';
8
- import type { Operation, TRPCLink } from './types';
8
+ import type { AugmentedOperationContext, Operation, TRPCLink } from './types';
9
+
10
+ type RetryFn<TInferrable extends InferrableClientTypes> = (
11
+ opts: RetryFnOptions<TInferrable>,
12
+ ) => boolean;
13
+
14
+ /**
15
+ * The `context` calls can pass through `retryLink`: the fields added to
16
+ * `OperationContext` (`TAugmented`), which its callbacks may read, and
17
+ * `retry`.
18
+ * @public
19
+ */
20
+ export type RetryLinkContext<
21
+ TInferrable extends InferrableClientTypes,
22
+ TAugmented extends object = AugmentedOperationContext,
23
+ > = Omit<TAugmented, 'retry'> & {
24
+ /**
25
+ * Decides whether to retry this call, in place of the link's `retry`.
26
+ * Anything else, such as a flag added to `OperationContext` that the
27
+ * link's `retry` reads, is left to the link.
28
+ * @example client.post.byId.query(input, { context: { retry: (opts) => opts.attempts <= 3 } })
29
+ */
30
+ retry?:
31
+ | RetryFn<TInferrable>
32
+ | ('retry' extends keyof TAugmented ? TAugmented['retry'] : never);
33
+ };
9
34
 
10
35
  interface RetryLinkOptions<TInferrable extends InferrableClientTypes> {
11
36
  /**
12
- * The retry function
37
+ * The retry function. A call's `context.retry` replaces it for that call.
13
38
  */
14
- retry: (opts: RetryFnOptions<TInferrable>) => boolean;
39
+ retry: RetryFn<TInferrable>;
15
40
  /**
16
41
  * The delay between retries in ms (defaults to 0)
17
42
  */
18
43
  retryDelayMs?: (attempt: number) => number;
19
44
  }
20
45
 
21
- interface RetryFnOptions<TInferrable extends InferrableClientTypes> {
46
+ /**
47
+ * What a `retry` function is given.
48
+ * @public
49
+ */
50
+ export interface RetryFnOptions<TInferrable extends InferrableClientTypes> {
22
51
  /**
23
52
  * The operation that failed
24
53
  */
@@ -35,13 +64,13 @@ interface RetryFnOptions<TInferrable extends InferrableClientTypes> {
35
64
 
36
65
  /**
37
66
  * Retries failed operations. Subscriptions resume from the last tracked event
38
- * id.
67
+ * id. A call can pass its own `retry` in `context`.
39
68
  *
40
69
  * @see https://trpc.io/docs/client/links/retryLink
41
70
  */
42
71
  export function retryLink<TInferrable extends InferrableClientTypes>(
43
72
  opts: RetryLinkOptions<TInferrable>,
44
- ): TRPCLink<TInferrable> {
73
+ ): TRPCLink<TInferrable, { context: RetryLinkContext<TInferrable> }> {
45
74
  // initialized config
46
75
  return () => {
47
76
  // initialized in app
@@ -79,7 +108,13 @@ export function retryLink<TInferrable extends InferrableClientTypes>(
79
108
 
80
109
  const subscription = callOpts.next(op).subscribe({
81
110
  error(error) {
82
- const shouldRetry = opts.retry({
111
+ const callRetry = op.context['retry'];
112
+ // a `retry` that isn't a function is a flag for the link's own `retry`
113
+ const retry =
114
+ typeof callRetry === 'function'
115
+ ? (callRetry as RetryFn<TInferrable>)
116
+ : opts.retry;
117
+ const shouldRetry = retry({
83
118
  op,
84
119
  attempts,
85
120
  error,