@trpc/client 11.33.0 → 11.35.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 (66) hide show
  1. package/dist/{httpBatchLink-_bkMj4mk.d.cts → httpBatchLink-50xXDyIp.d.cts} +3 -3
  2. package/dist/{httpBatchLink-DaVHWlLU.d.mts.map → httpBatchLink-50xXDyIp.d.cts.map} +1 -1
  3. package/dist/{httpBatchLink-DaVHWlLU.d.mts → httpBatchLink-D4shlyQy.d.mts} +3 -3
  4. package/dist/{httpBatchLink-_bkMj4mk.d.cts.map → httpBatchLink-D4shlyQy.d.mts.map} +1 -1
  5. package/dist/{httpLink-DN_Ana7a.d.mts → httpLink-Bb6hOyBN.d.mts} +3 -3
  6. package/dist/{httpLink-DN_Ana7a.d.mts.map → httpLink-Bb6hOyBN.d.mts.map} +1 -1
  7. package/dist/{httpUtils-BAs5frdP.d.mts → httpUtils-BbinScxw.d.mts} +2 -2
  8. package/dist/{httpUtils-1z6dzl3V.d.cts.map → httpUtils-BbinScxw.d.mts.map} +1 -1
  9. package/dist/{httpUtils-1z6dzl3V.d.cts → httpUtils-dB1XeokQ.d.cts} +2 -2
  10. package/dist/{httpUtils-BAs5frdP.d.mts.map → httpUtils-dB1XeokQ.d.cts.map} +1 -1
  11. package/dist/index.cjs +4 -3
  12. package/dist/index.d.cts +37 -11
  13. package/dist/index.d.cts.map +1 -1
  14. package/dist/index.d.mts +40 -14
  15. package/dist/index.d.mts.map +1 -1
  16. package/dist/index.mjs +4 -3
  17. package/dist/index.mjs.map +1 -1
  18. package/dist/links/httpBatchLink.d.cts +1 -1
  19. package/dist/links/httpBatchLink.d.mts +1 -1
  20. package/dist/links/httpLink.d.cts +2 -2
  21. package/dist/links/httpLink.d.mts +1 -1
  22. package/dist/links/loggerLink.d.cts +1 -1
  23. package/dist/links/loggerLink.d.mts +1 -1
  24. package/dist/links/splitLink.cjs +1 -1
  25. package/dist/links/splitLink.d.cts +29 -8
  26. package/dist/links/splitLink.d.cts.map +1 -1
  27. package/dist/links/splitLink.d.mts +1 -1
  28. package/dist/links/splitLink.mjs +1 -1
  29. package/dist/links/wsLink/wsLink.d.cts +1 -1
  30. package/dist/links/wsLink/wsLink.d.mts +1 -1
  31. package/dist/{loggerLink-CiVinJOG.d.mts → loggerLink-y1x7WTWX.d.mts} +2 -2
  32. package/dist/{loggerLink-CiVinJOG.d.mts.map → loggerLink-y1x7WTWX.d.mts.map} +1 -1
  33. package/dist/splitLink-BGLTgm1a.d.mts +56 -0
  34. package/dist/splitLink-BGLTgm1a.d.mts.map +1 -0
  35. package/dist/{splitLink-IUbsOwO6.mjs → splitLink-C8U8vvLl.mjs} +10 -12
  36. package/dist/splitLink-C8U8vvLl.mjs.map +1 -0
  37. package/dist/{splitLink-BQoYcpdp.cjs → splitLink-NqS85b92.cjs} +9 -11
  38. package/dist/{types-DW0iJUBU.d.mts → types-BZH1P86I.d.mts} +3 -3
  39. package/dist/types-BZH1P86I.d.mts.map +1 -0
  40. package/dist/{types-DMe6B4SV.d.cts → types-CGS36fjk.d.cts} +3 -3
  41. package/dist/{types-DMe6B4SV.d.cts.map → types-CGS36fjk.d.cts.map} +1 -1
  42. package/dist/{wsLink-DX5fv7-H.d.mts → wsLink-CFh-pvQG.d.mts} +2 -2
  43. package/dist/{wsLink-DX5fv7-H.d.mts.map → wsLink-CFh-pvQG.d.mts.map} +1 -1
  44. package/dist/{wsLink-DuyXOEPt.d.cts → wsLink-CJT5pqFt.d.cts} +2 -2
  45. package/dist/{wsLink-DuyXOEPt.d.cts.map → wsLink-CJT5pqFt.d.cts.map} +1 -1
  46. package/package.json +3 -3
  47. package/skills/client-setup/SKILL.md +15 -10
  48. package/skills/links/SKILL.md +9 -7
  49. package/skills/links/references/client-links-httpBatchLink.md +12 -8
  50. package/skills/links/references/client-links-httpBatchStreamLink.md +9 -6
  51. package/skills/links/references/client-links-httpLink.md +3 -2
  52. package/skills/links/references/client-links-httpSubscriptionLink.md +12 -4
  53. package/skills/links/references/client-links-localLink.md +3 -2
  54. package/skills/links/references/client-links-loggerLink.md +3 -2
  55. package/skills/links/references/client-links-retryLink.md +31 -2
  56. package/skills/links/references/client-links-splitLink.md +22 -6
  57. package/skills/links/references/client-links-wsLink.md +3 -2
  58. package/skills/superjson/SKILL.md +10 -6
  59. package/src/createTRPCClient.ts +8 -2
  60. package/src/links/retryLink.ts +41 -10
  61. package/src/links/splitLink.ts +85 -14
  62. package/src/links/types.ts +5 -2
  63. package/dist/splitLink-ClFN_nbg.d.mts +0 -35
  64. package/dist/splitLink-ClFN_nbg.d.mts.map +0 -1
  65. package/dist/splitLink-IUbsOwO6.mjs.map +0 -1
  66. package/dist/types-DW0iJUBU.d.mts.map +0 -1
@@ -15,10 +15,11 @@ From https://trpc.io/docs/client/links/retryLink
15
15
  You can import and add the `retryLink` to the `links` array when creating your tRPC client. This link can be placed before or after other links in your setup, depending on your requirements.
16
16
 
17
17
  ```ts no-check
18
- import { createTRPCClient, httpBatchLink, retryLink } from '@trpc/client';
18
+ import { createTRPCClient, httpBatchLink, retryLink, routerType } from '@trpc/client';
19
19
  import type { AppRouter } from './server';
20
20
 
21
- const client = createTRPCClient<AppRouter>({
21
+ const client = createTRPCClient({
22
+ router: routerType<AppRouter>(),
22
23
  links: [
23
24
  retryLink({
24
25
  retry(opts) {
@@ -52,6 +53,34 @@ In the example above, we add the `retryLink` before the `httpBatchLink`. The `re
52
53
  - Retry the request if the error is a `TRPCClientError` with a status code of 500 or if we couldn't get a valid tRPC error.
53
54
  - Retry the request up to 3 times.
54
55
 
56
+ ## Changing retries for one call
57
+
58
+ Pass a `retry` function in a call's `context` to use it instead of the link's `retry` for that call. With `router: routerType<AppRouter>()`, it's typed like the link's.
59
+
60
+ ```ts no-check
61
+ import {
62
+ createTRPCClient,
63
+ httpBatchLink,
64
+ retryLink,
65
+ routerType,
66
+ } from '@trpc/client';
67
+ import type { AppRouter } from './server';
68
+
69
+ const client = createTRPCClient({
70
+ router: routerType<AppRouter>(),
71
+ links: [
72
+ // retry queries and mutations
73
+ retryLink({ retry: (opts) => opts.attempts <= 3 }),
74
+ httpBatchLink({ url: 'http://localhost:3000' }),
75
+ ],
76
+ });
77
+
78
+ // never send a payment twice
79
+ await client.createPayment.mutate(undefined, {
80
+ context: { retry: () => false },
81
+ });
82
+ ```
83
+
55
84
  ## Options
56
85
 
57
86
  ```ts no-check
@@ -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`.
@@ -11,14 +11,15 @@ From https://trpc.io/docs/client/links/wsLink
11
11
  To use `wsLink`, you need to pass it a `TRPCWebSocketClient`, which you can create with `createWSClient`:
12
12
 
13
13
  ```ts no-check title="client/index.ts"
14
- import { createTRPCClient, createWSClient, wsLink } from '@trpc/client';
14
+ import { createTRPCClient, createWSClient, wsLink, routerType } from '@trpc/client';
15
15
  import type { AppRouter } from './server';
16
16
 
17
17
  const wsClient = createWSClient({
18
18
  url: 'ws://localhost:3000',
19
19
  });
20
20
 
21
- const trpcClient = createTRPCClient<AppRouter>({
21
+ const trpcClient = createTRPCClient({
22
+ router: routerType<AppRouter>(),
22
23
  links: [wsLink<AppRouter>({ client: wsClient })],
23
24
  });
24
25
  ```
@@ -9,7 +9,7 @@ sources:
9
9
  metadata:
10
10
  type: composition
11
11
  library: trpc
12
- library_version: 11.33.0
12
+ library_version: 11.35.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. -->
@@ -54,11 +54,12 @@ export const t = initTRPC.create({
54
54
  `createTRPCClient()`:
55
55
 
56
56
  ```ts no-check title="src/app/_trpc/client.ts"
57
- import { createTRPCClient, httpLink } from '@trpc/client';
57
+ import { createTRPCClient, httpLink, routerType } from '@trpc/client';
58
58
  import type { AppRouter } from './server';
59
59
  import superjson from 'superjson';
60
60
 
61
- export const client = createTRPCClient<AppRouter>({
61
+ export const client = createTRPCClient({
62
+ router: routerType<AppRouter>(),
62
63
  links: [
63
64
  httpLink({
64
65
  url: 'http://localhost:3000',
@@ -76,11 +77,13 @@ import {
76
77
  httpBatchLink,
77
78
  httpSubscriptionLink,
78
79
  splitLink,
80
+ routerType,
79
81
  } from '@trpc/client';
80
82
  import type { AppRouter } from './server';
81
83
  import superjson from 'superjson';
82
84
 
83
- export const client = createTRPCClient<AppRouter>({
85
+ export const client = createTRPCClient({
86
+ router: routerType<AppRouter>(),
84
87
  links: [
85
88
  splitLink({
86
89
  condition: (op) => op.type === 'subscription',
@@ -149,11 +152,12 @@ export const t = initTRPC.create({
149
152
  `createTRPCClient()`:
150
153
 
151
154
  ```ts no-check title="src/app/_trpc/client.ts"
152
- import { createTRPCClient, httpLink } from '@trpc/client';
155
+ import { createTRPCClient, httpLink, routerType } from '@trpc/client';
153
156
  import type { AppRouter } from './server/routers/_app';
154
157
  import { transformer } from './utils/trpc';
155
158
 
156
- export const client = createTRPCClient<AppRouter>({
159
+ export const client = createTRPCClient({
160
+ router: routerType<AppRouter>(),
157
161
  links: [
158
162
  httpLink({
159
163
  url: 'http://localhost:3000',
@@ -238,9 +238,12 @@ export type CreateTRPCClientWithRouterOptions<
238
238
  * ```ts
239
239
  * const client = createTRPCClient({
240
240
  * router: routerType<AppRouter>(),
241
- * links: [httpBatchLink({ url: 'http://localhost:3000/trpc' })],
241
+ * links: [retryLink({ retry: () => false }), httpBatchLink({ url })],
242
242
  * });
243
- * const post = await client.post.byId.query({ id: '1' });
243
+ * const post = await client.post.byId.query(
244
+ * { id: '1' },
245
+ * { context: { retry: (opts) => opts.attempts <= 3 } },
246
+ * );
244
247
  * ```
245
248
  * @see https://trpc.io/docs/client/vanilla/setup#3-initialize-the-trpc-client
246
249
  */
@@ -262,6 +265,9 @@ export function createTRPCClient<
262
265
  * const post = await client.post.byId.query({ id: '1' });
263
266
  * ```
264
267
  * @see https://trpc.io/docs/client/vanilla/setup#3-initialize-the-trpc-client
268
+ * @deprecated Pass the router's type as `router: routerType<AppRouter>()`
269
+ * instead of a type argument, so the links can type each call's `context`.
270
+ * The type argument form is removed in v12.
265
271
  */
266
272
  // Kept last: `ReturnType<typeof createTRPCClient>` reads the last overload.
267
273
  export function createTRPCClient<TRouter extends AnyRouter>(
@@ -7,18 +7,47 @@ import { inputWithTrackedEventId } from '../internals/inputWithTrackedEventId';
7
7
  import type { TRPCClientError } from '../TRPCClientError';
8
8
  import type { AugmentedOperationContext, Operation, TRPCLink } from './types';
9
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
+ };
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,17 +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<
45
- TInferrable,
46
- // its callbacks may read the fields added to `OperationContext`
47
- { context: AugmentedOperationContext }
48
- > {
73
+ ): TRPCLink<TInferrable, { context: RetryLinkContext<TInferrable> }> {
49
74
  // initialized config
50
75
  return () => {
51
76
  // initialized in app
@@ -83,7 +108,13 @@ export function retryLink<TInferrable extends InferrableClientTypes>(
83
108
 
84
109
  const subscription = callOpts.next(op).subscribe({
85
110
  error(error) {
86
- 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({
87
118
  op,
88
119
  attempts,
89
120
  error,
@@ -5,6 +5,7 @@ import type {
5
5
  AugmentedOperationContext,
6
6
  inferLinksDeclaredContext,
7
7
  Operation,
8
+ OperationContext,
8
9
  TRPCLink,
9
10
  } from './types';
10
11
 
@@ -12,6 +13,19 @@ function asArray<TType>(value: TType | readonly TType[]): readonly TType[] {
12
13
  return Array.isArray(value) ? value : [value as TType];
13
14
  }
14
15
 
16
+ // a field the call leaves out, or sets to `undefined`, takes its default
17
+ function withDefaults(
18
+ defaults: Record<PropertyKey, unknown>,
19
+ context: OperationContext,
20
+ ): OperationContext {
21
+ // spreading copies symbol keys too
22
+ const merged: Record<PropertyKey, unknown> = { ...defaults, ...context };
23
+ for (const key of Reflect.ownKeys(defaults)) {
24
+ if (merged[key] === undefined) merged[key] = defaults[key];
25
+ }
26
+ return merged;
27
+ }
28
+
15
29
  /**
16
30
  * A `splitLink` branch: one link or a chain of them.
17
31
  * @internal
@@ -29,11 +43,12 @@ interface SplitLinkOptions<
29
43
  TRouter extends AnyRouter,
30
44
  TTrue extends SplitLinkBranch<TRouter>,
31
45
  TFalse extends SplitLinkBranch<TRouter>,
46
+ TContext extends object,
32
47
  > {
33
48
  /**
34
49
  * Picks the chain for an operation.
35
50
  */
36
- condition: (op: Operation) => boolean;
51
+ condition: (op: Operation<unknown, TContext>) => boolean;
37
52
  /**
38
53
  * The link to execute next if the test function returns `true`.
39
54
  */
@@ -44,11 +59,41 @@ interface SplitLinkOptions<
44
59
  false: TFalse;
45
60
  }
46
61
 
62
+ // the types the branches' links read the defaults' fields as, so a
63
+ // default that a branch would read as another type is an error
64
+ type fitBranchContext<TContext, TTrue, TFalse> =
65
+ inferLinksDeclaredContext<
66
+ [...asLinks<TTrue>, ...asLinks<TFalse>]
67
+ > extends infer $Branches
68
+ ? { [TKey in keyof TContext & keyof $Branches]: $Branches[TKey] }
69
+ : never;
70
+
71
+ type SplitLink<
72
+ TRouter extends AnyRouter,
73
+ TTrue extends SplitLinkBranch<TRouter>,
74
+ TFalse extends SplitLinkBranch<TRouter>,
75
+ TContext extends object,
76
+ > = TRPCLink<
77
+ TRouter,
78
+ {
79
+ context: TContext &
80
+ inferLinksDeclaredContext<[...asLinks<TTrue>, ...asLinks<TFalse>]>;
81
+ }
82
+ >;
83
+
47
84
  /**
48
85
  * Sends each operation down one of two link chains, based on `condition`.
49
86
  *
50
- * Calls take the `context` both chains read. `condition` may read the
51
- * fields added to `OperationContext`.
87
+ * Calls take the `context` both chains read. Give `context` defaults to
88
+ * declare the fields `condition` reads; without them, `condition` reads
89
+ * the fields added to `OperationContext`.
90
+ * @example
91
+ * splitLink({
92
+ * context: { skipBatch: false },
93
+ * condition: (op) => op.context.skipBatch,
94
+ * true: httpLink({ url }),
95
+ * false: httpBatchLink({ url }),
96
+ * })
52
97
  *
53
98
  * @see https://trpc.io/docs/client/links/splitLink
54
99
  */
@@ -58,22 +103,48 @@ export function splitLink<
58
103
  const TTrue extends SplitLinkBranch<TRouter> = SplitLinkBranch<TRouter>,
59
104
  const TFalse extends SplitLinkBranch<TRouter> = SplitLinkBranch<TRouter>,
60
105
  >(
61
- opts: SplitLinkOptions<TRouter, TTrue, TFalse>,
62
- ): TRPCLink<
63
- TRouter,
64
- {
65
- // `condition` may read the fields added to `OperationContext`
66
- context: AugmentedOperationContext &
67
- inferLinksDeclaredContext<[...asLinks<TTrue>, ...asLinks<TFalse>]>;
68
- }
69
- > {
106
+ opts: SplitLinkOptions<TRouter, TTrue, TFalse, OperationContext> & {
107
+ context?: never;
108
+ },
109
+ // `condition` may read the fields added to `OperationContext`
110
+ ): SplitLink<TRouter, TTrue, TFalse, AugmentedOperationContext>;
111
+ export function splitLink<
112
+ TRouter extends AnyRouter,
113
+ const TTrue extends SplitLinkBranch<TRouter>,
114
+ const TFalse extends SplitLinkBranch<TRouter>,
115
+ TContext extends object,
116
+ >(
117
+ opts: SplitLinkOptions<TRouter, TTrue, TFalse, TContext> & {
118
+ /**
119
+ * Values for the `context` fields `condition` reads, used when a call
120
+ * leaves one out. Their types are the fields calls can pass. A field a
121
+ * call passes replaces its default whole: nested objects aren't merged.
122
+ * A field the branches' links also read must fit their type.
123
+ */
124
+ context: TContext & fitBranchContext<TContext, TTrue, TFalse>;
125
+ },
126
+ ): SplitLink<TRouter, TTrue, TFalse, TContext>;
127
+ export function splitLink(
128
+ opts: SplitLinkOptions<
129
+ AnyRouter,
130
+ SplitLinkBranch<AnyRouter>,
131
+ SplitLinkBranch<AnyRouter>,
132
+ any
133
+ > & { context?: Record<PropertyKey, unknown> },
134
+ ): TRPCLink<AnyRouter> {
70
135
  return (runtime) => {
71
136
  const yes = asArray(opts.true).map((link) => link(runtime));
72
137
  const no = asArray(opts.false).map((link) => link(runtime));
73
138
  return (props) => {
74
139
  return observable((observer) => {
75
- const links = opts.condition(props.op) ? yes : no;
76
- return createChain({ op: props.op, links }).subscribe(observer);
140
+ const op = opts.context
141
+ ? {
142
+ ...props.op,
143
+ context: withDefaults(opts.context, props.op.context),
144
+ }
145
+ : props.op;
146
+ const links = opts.condition(op) ? yes : no;
147
+ return createChain({ op, links }).subscribe(observer);
77
148
  });
78
149
  };
79
150
  };
@@ -40,13 +40,16 @@ export type AugmentedOperationContext = {
40
40
  * A procedure call as it goes through the links.
41
41
  * @internal
42
42
  */
43
- export type Operation<TInput = unknown> = {
43
+ export type Operation<
44
+ TInput = unknown,
45
+ TContext extends object = OperationContext,
46
+ > = {
44
47
  /** A number unique to the call within its client. */
45
48
  id: number;
46
49
  type: 'mutation' | 'query' | 'subscription';
47
50
  input: TInput;
48
51
  path: string;
49
- context: OperationContext;
52
+ context: TContext;
50
53
  signal: Maybe<AbortSignal>;
51
54
  };
52
55
 
@@ -1,35 +0,0 @@
1
- import { a as Operation, h as inferLinksDeclaredContext, p as TRPCLink, t as AugmentedOperationContext } from "./types-DW0iJUBU.mjs";
2
- import { AnyRouter } from "@trpc/server/unstable-core-do-not-import";
3
- /**
4
- * A `splitLink` branch: one link or a chain of them.
5
- * @internal
6
- */
7
- type SplitLinkBranch<TRouter extends AnyRouter> = TRPCLink<TRouter, any> | readonly TRPCLink<TRouter, any>[];
8
- type asLinks<TBranch> = TBranch extends readonly unknown[] ? TBranch : [TBranch];
9
- interface SplitLinkOptions<TRouter extends AnyRouter, TTrue extends SplitLinkBranch<TRouter>, TFalse extends SplitLinkBranch<TRouter>> {
10
- /**
11
- * Picks the chain for an operation.
12
- */
13
- condition: (op: Operation) => boolean;
14
- /**
15
- * The link to execute next if the test function returns `true`.
16
- */
17
- true: TTrue;
18
- /**
19
- * The link to execute next if the test function returns `false`.
20
- */
21
- false: TFalse;
22
- }
23
- /**
24
- * Sends each operation down one of two link chains, based on `condition`.
25
- *
26
- * Calls take the `context` both chains read. `condition` may read the
27
- * fields added to `OperationContext`.
28
- *
29
- * @see https://trpc.io/docs/client/links/splitLink
30
- */
31
- declare function splitLink<TRouter extends AnyRouter = AnyRouter, const TTrue extends SplitLinkBranch<TRouter> = SplitLinkBranch<TRouter>, const TFalse extends SplitLinkBranch<TRouter> = SplitLinkBranch<TRouter>>(opts: SplitLinkOptions<TRouter, TTrue, TFalse>): TRPCLink<TRouter, {
32
- context: AugmentedOperationContext & inferLinksDeclaredContext<[...asLinks<TTrue>, ...asLinks<TFalse>]>;
33
- }>;
34
- export { splitLink as t };
35
- //# sourceMappingURL=splitLink-ClFN_nbg.d.mts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"splitLink-ClFN_nbg.d.mts","names":[],"sources":["../src/links/splitLink.ts"],"mappings":";;;;;;KAkBK,gBAAgB,gBAAgB,aACjC,SAAS,yBACA,SAAS;KAGjB,QAAQ,WAAW,qCACpB,WACC;UAEK,iBACR,gBAAgB,WAChB,cAAc,gBAAgB,UAC9B,eAAe,gBAAgB;;;;EAK/B,YAAY,IAAI;;;;EAIhB,MAAM;;;;EAIN,OAAO;;;;;;;;;;iBAWO,UACd,gBAAgB,YAAY,iBAEtB,cAAc,gBAAgB,WAAW,gBAAgB,gBACzD,eAAe,gBAAgB,WAAW,gBAAgB,UAEhE,MAAM,iBAAiB,SAAS,OAAO,UACtC,SACD;EAGE,SAAS,4BACP,8BAA8B,QAAQ,WAAW,QAAQ"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"splitLink-IUbsOwO6.mjs","names":[],"sources":["../src/links/internals/createChain.ts","../src/links/splitLink.ts"],"sourcesContent":["import { observable } from '@trpc/server/observable';\nimport type { AnyRouter } from '@trpc/server/unstable-core-do-not-import';\nimport type {\n Operation,\n OperationLink,\n OperationResultObservable,\n} from '../types';\n\n/** @internal */\nexport function createChain<\n TRouter extends AnyRouter,\n TInput = unknown,\n TOutput = unknown,\n>(opts: {\n links: OperationLink<TRouter, TInput, TOutput>[];\n op: Operation<TInput>;\n}): OperationResultObservable<TRouter, TOutput> {\n return observable((observer) => {\n function execute(index = 0, op = opts.op) {\n const next = opts.links[index];\n if (!next) {\n throw new Error(\n 'No more links to execute - did you forget to add an ending link?',\n );\n }\n const subscription = next({\n op,\n next(nextOp) {\n const nextObserver = execute(index + 1, nextOp);\n\n return nextObserver;\n },\n });\n return subscription;\n }\n\n const obs$ = execute();\n return obs$.subscribe(observer);\n });\n}\n","import { observable } from '@trpc/server/observable';\nimport type { AnyRouter } from '@trpc/server/unstable-core-do-not-import';\nimport { createChain } from './internals/createChain';\nimport type {\n AugmentedOperationContext,\n inferLinksDeclaredContext,\n Operation,\n TRPCLink,\n} from './types';\n\nfunction asArray<TType>(value: TType | readonly TType[]): readonly TType[] {\n return Array.isArray(value) ? value : [value as TType];\n}\n\n/**\n * A `splitLink` branch: one link or a chain of them.\n * @internal\n */\ntype SplitLinkBranch<TRouter extends AnyRouter> =\n | TRPCLink<TRouter, any>\n | readonly TRPCLink<TRouter, any>[];\n\n// a tuple, so links whose declarations nest aren't merged into one\ntype asLinks<TBranch> = TBranch extends readonly unknown[]\n ? TBranch\n : [TBranch];\n\ninterface SplitLinkOptions<\n TRouter extends AnyRouter,\n TTrue extends SplitLinkBranch<TRouter>,\n TFalse extends SplitLinkBranch<TRouter>,\n> {\n /**\n * Picks the chain for an operation.\n */\n condition: (op: Operation) => boolean;\n /**\n * The link to execute next if the test function returns `true`.\n */\n true: TTrue;\n /**\n * The link to execute next if the test function returns `false`.\n */\n false: TFalse;\n}\n\n/**\n * Sends each operation down one of two link chains, based on `condition`.\n *\n * Calls take the `context` both chains read. `condition` may read the\n * fields added to `OperationContext`.\n *\n * @see https://trpc.io/docs/client/links/splitLink\n */\nexport function splitLink<\n TRouter extends AnyRouter = AnyRouter,\n // with an explicit router type, a branch is still a link or an array\n const TTrue extends SplitLinkBranch<TRouter> = SplitLinkBranch<TRouter>,\n const TFalse extends SplitLinkBranch<TRouter> = SplitLinkBranch<TRouter>,\n>(\n opts: SplitLinkOptions<TRouter, TTrue, TFalse>,\n): TRPCLink<\n TRouter,\n {\n // `condition` may read the fields added to `OperationContext`\n context: AugmentedOperationContext &\n inferLinksDeclaredContext<[...asLinks<TTrue>, ...asLinks<TFalse>]>;\n }\n> {\n return (runtime) => {\n const yes = asArray(opts.true).map((link) => link(runtime));\n const no = asArray(opts.false).map((link) => link(runtime));\n return (props) => {\n return observable((observer) => {\n const links = opts.condition(props.op) ? yes : no;\n return createChain({ op: props.op, links }).subscribe(observer);\n });\n };\n };\n}\n"],"mappings":";;;AASA,SAAgB,YAId,MAG8C;CAC9C,OAAO,YAAY,aAAa;EAC9B,SAAS,QAAQ,QAAQ,GAAG,KAAK,KAAK,IAAI;GACxC,MAAM,OAAO,KAAK,MAAM;GACxB,IAAI,CAAC,MACH,MAAM,IAAI,MACR,kEACF;GAUF,OARqB,KAAK;IACxB;IACA,KAAK,QAAQ;KAGX,OAFqB,QAAQ,QAAQ,GAAG,MAEtB;IACpB;GACF,CACkB;EACpB;EAGA,OADa,QACH,CAAC,CAAC,UAAU,QAAQ;CAChC,CAAC;AACH;;AC7BA,SAAS,QAAe,OAAmD;CACzE,OAAO,MAAM,QAAQ,KAAK,IAAI,QAAQ,CAAC,KAAc;AACvD;;;;;;;;;AA0CA,SAAgB,UAMd,MAQA;CACA,QAAQ,YAAY;EAClB,MAAM,MAAM,QAAQ,KAAK,IAAI,CAAC,CAAC,KAAK,SAAS,KAAK,OAAO,CAAC;EAC1D,MAAM,KAAK,QAAQ,KAAK,KAAK,CAAC,CAAC,KAAK,SAAS,KAAK,OAAO,CAAC;EAC1D,QAAQ,UAAU;GAChB,OAAO,YAAY,aAAa;IAC9B,MAAM,QAAQ,KAAK,UAAU,MAAM,EAAE,IAAI,MAAM;IAC/C,OAAO,YAAY;KAAE,IAAI,MAAM;KAAI;IAAM,CAAC,CAAC,CAAC,UAAU,QAAQ;GAChE,CAAC;EACH;CACF;AACF"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"types-DW0iJUBU.d.mts","names":[],"sources":["../src/internals/types.ts","../src/TRPCClientError.ts","../src/links/types.ts"],"mappings":";;;;;;;;;;KAOY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