@trpc/client 10.0.0-alpha.4 → 10.0.0-alpha.42

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 (174) hide show
  1. package/README.md +58 -1
  2. package/dist/TRPCClientError.d.ts +3 -11
  3. package/dist/TRPCClientError.d.ts.map +1 -1
  4. package/dist/createTRPCClient.d.ts +2 -3
  5. package/dist/createTRPCClient.d.ts.map +1 -1
  6. package/dist/createTRPCClientProxy.d.ts +31 -0
  7. package/dist/createTRPCClientProxy.d.ts.map +1 -0
  8. package/dist/{httpUtils-c09d7b79.mjs → httpUtils-089853f6.mjs} +38 -30
  9. package/dist/{httpUtils-1f9e4e3c.js → httpUtils-a0169eef.js} +38 -29
  10. package/dist/index.d.ts +2 -1
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +256 -107
  13. package/dist/index.mjs +244 -99
  14. package/dist/internals/TRPCClient.d.ts +21 -18
  15. package/dist/internals/TRPCClient.d.ts.map +1 -1
  16. package/dist/internals/dataLoader.d.ts +7 -4
  17. package/dist/internals/dataLoader.d.ts.map +1 -1
  18. package/dist/links/dedupeLink.d.ts.map +1 -1
  19. package/dist/links/httpBatchLink.d.ts +5 -2
  20. package/dist/links/httpBatchLink.d.ts.map +1 -1
  21. package/dist/links/httpBatchLink.js +197 -69
  22. package/dist/links/httpBatchLink.mjs +197 -69
  23. package/dist/links/httpLink.d.ts +1 -1
  24. package/dist/links/httpLink.d.ts.map +1 -1
  25. package/dist/links/httpLink.js +5 -5
  26. package/dist/links/httpLink.mjs +5 -5
  27. package/dist/links/index.d.ts +9 -0
  28. package/dist/links/index.d.ts.map +1 -0
  29. package/dist/links/internals/createChain.d.ts.map +1 -1
  30. package/dist/links/internals/httpUtils.d.ts +30 -0
  31. package/dist/links/internals/httpUtils.d.ts.map +1 -0
  32. package/dist/links/internals/transformOperationResult.d.ts +17 -3
  33. package/dist/links/internals/transformOperationResult.d.ts.map +1 -1
  34. package/dist/links/loggerLink.d.ts.map +1 -1
  35. package/dist/links/loggerLink.js +2 -3
  36. package/dist/links/loggerLink.mjs +1 -2
  37. package/dist/links/retryLink.d.ts.map +1 -1
  38. package/dist/links/splitLink.d.ts +1 -1
  39. package/dist/links/splitLink.d.ts.map +1 -1
  40. package/dist/links/splitLink.js +3 -25
  41. package/dist/links/splitLink.mjs +2 -28
  42. package/dist/links/types.d.ts +12 -9
  43. package/dist/links/types.d.ts.map +1 -1
  44. package/dist/links/wsLink.d.ts +5 -3
  45. package/dist/links/wsLink.d.ts.map +1 -1
  46. package/dist/links/wsLink.js +12 -376
  47. package/dist/links/wsLink.mjs +10 -369
  48. package/dist/splitLink-0202cd7b.mjs +54 -0
  49. package/dist/splitLink-23c5ce5c.js +57 -0
  50. package/dist/wsLink-27b93bd0.mjs +451 -0
  51. package/dist/wsLink-44703a83.js +467 -0
  52. package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.cjs.dev.js +4 -4
  53. package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.cjs.prod.js +4 -4
  54. package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.esm.js +4 -4
  55. package/links/httpBatchLink/index.d.ts +1 -1
  56. package/links/httpBatchLink/index.js +1 -1
  57. package/links/httpLink/dist/trpc-client-links-httpLink.cjs.dev.js +1 -1
  58. package/links/httpLink/dist/trpc-client-links-httpLink.cjs.prod.js +1 -1
  59. package/links/httpLink/dist/trpc-client-links-httpLink.esm.js +1 -1
  60. package/links/httpLink/index.d.ts +1 -1
  61. package/links/httpLink/index.js +1 -1
  62. package/links/loggerLink/index.d.ts +1 -1
  63. package/links/loggerLink/index.js +1 -1
  64. package/links/splitLink/index.d.ts +1 -1
  65. package/links/splitLink/index.js +1 -1
  66. package/links/wsLink/dist/trpc-client-links-wsLink.cjs.dev.js +3 -1
  67. package/links/wsLink/dist/trpc-client-links-wsLink.cjs.prod.js +3 -1
  68. package/links/wsLink/dist/trpc-client-links-wsLink.esm.js +3 -1
  69. package/links/wsLink/index.d.ts +1 -1
  70. package/links/wsLink/index.js +1 -1
  71. package/package.json +23 -30
  72. package/src/TRPCClientError.ts +5 -13
  73. package/src/createTRPCClient.ts +7 -3
  74. package/src/createTRPCClientProxy.ts +98 -0
  75. package/src/index.ts +2 -1
  76. package/src/internals/TRPCClient.ts +70 -42
  77. package/src/internals/dataLoader.ts +120 -45
  78. package/src/links/dedupeLink.test.ts +1 -1
  79. package/src/links/dedupeLink.ts +6 -7
  80. package/src/links/httpBatchLink.ts +64 -31
  81. package/src/links/httpLink.ts +4 -4
  82. package/src/links/index.ts +8 -0
  83. package/src/links/internals/createChain.test.ts +5 -5
  84. package/src/links/internals/createChain.ts +8 -3
  85. package/src/links/internals/httpUtils.ts +116 -0
  86. package/src/links/internals/transformOperationResult.ts +40 -12
  87. package/src/links/loggerLink.ts +1 -2
  88. package/src/links/retryLink.ts +1 -2
  89. package/src/links/splitLink.test.ts +3 -3
  90. package/src/links/splitLink.ts +5 -5
  91. package/src/links/types.ts +14 -9
  92. package/src/links/wsLink.ts +31 -16
  93. package/dist/TRPCClientError-17b117e1.mjs +0 -81
  94. package/dist/TRPCClientError-ec6bd2ca.js +0 -94
  95. package/dist/createChain-389cc116.mjs +0 -25
  96. package/dist/createChain-864dbb30.js +0 -27
  97. package/dist/internals/transformRPCResponse.d.ts +0 -15
  98. package/dist/internals/transformRPCResponse.d.ts.map +0 -1
  99. package/dist/links/httpUtils.d.ts +0 -22
  100. package/dist/links/httpUtils.d.ts.map +0 -1
  101. package/dist/links/transformerLink.d.ts +0 -4
  102. package/dist/links/transformerLink.d.ts.map +0 -1
  103. package/dist/links/transformerLink.js +0 -75
  104. package/dist/links/transformerLink.mjs +0 -67
  105. package/dist/links/transformerLink.ts.js +0 -1
  106. package/dist/observable-7e66cb56.mjs +0 -102
  107. package/dist/observable-85669100.js +0 -104
  108. package/dist/rx/index.d.ts +0 -3
  109. package/dist/rx/index.d.ts.map +0 -1
  110. package/dist/rx/index.js +0 -9
  111. package/dist/rx/index.mjs +0 -1
  112. package/dist/rx/index.ts.js +0 -1
  113. package/dist/rx/observable.d.ts +0 -4
  114. package/dist/rx/observable.d.ts.map +0 -1
  115. package/dist/rx/operators/index.d.ts +0 -4
  116. package/dist/rx/operators/index.d.ts.map +0 -1
  117. package/dist/rx/operators/index.js +0 -38
  118. package/dist/rx/operators/index.mjs +0 -32
  119. package/dist/rx/operators/index.ts.js +0 -1
  120. package/dist/rx/operators/map.d.ts +0 -3
  121. package/dist/rx/operators/map.d.ts.map +0 -1
  122. package/dist/rx/operators/share.d.ts +0 -6
  123. package/dist/rx/operators/share.d.ts.map +0 -1
  124. package/dist/rx/operators/tap.d.ts +0 -3
  125. package/dist/rx/operators/tap.d.ts.map +0 -1
  126. package/dist/rx/types.d.ts +0 -29
  127. package/dist/rx/types.d.ts.map +0 -1
  128. package/dist/rx/util/identity.d.ts +0 -2
  129. package/dist/rx/util/identity.d.ts.map +0 -1
  130. package/dist/rx/util/observableToPromise.d.ts +0 -10
  131. package/dist/rx/util/observableToPromise.d.ts.map +0 -1
  132. package/dist/rx/util/pipe.d.ts +0 -4
  133. package/dist/rx/util/pipe.d.ts.map +0 -1
  134. package/dist/share-4d79dc49.js +0 -110
  135. package/dist/share-671b63fa.mjs +0 -108
  136. package/dist/tap-538fa07f.mjs +0 -30
  137. package/dist/tap-799a2d33.js +0 -32
  138. package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.d.ts +0 -1
  139. package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.dev.js +0 -67
  140. package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.js +0 -7
  141. package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.prod.js +0 -67
  142. package/links/transformerLink/dist/trpc-client-links-transformerLink.esm.js +0 -63
  143. package/links/transformerLink/index.d.ts +0 -1
  144. package/links/transformerLink/index.js +0 -1
  145. package/rx/dist/trpc-client-rx.cjs.d.ts +0 -1
  146. package/rx/dist/trpc-client-rx.cjs.dev.js +0 -9
  147. package/rx/dist/trpc-client-rx.cjs.js +0 -7
  148. package/rx/dist/trpc-client-rx.cjs.prod.js +0 -9
  149. package/rx/dist/trpc-client-rx.esm.js +0 -1
  150. package/rx/index.d.ts +0 -1
  151. package/rx/index.js +0 -1
  152. package/rx/operators/dist/trpc-client-rx-operators.cjs.d.ts +0 -1
  153. package/rx/operators/dist/trpc-client-rx-operators.cjs.dev.js +0 -39
  154. package/rx/operators/dist/trpc-client-rx-operators.cjs.js +0 -7
  155. package/rx/operators/dist/trpc-client-rx-operators.cjs.prod.js +0 -39
  156. package/rx/operators/dist/trpc-client-rx-operators.esm.js +0 -33
  157. package/rx/operators/index.d.ts +0 -1
  158. package/rx/operators/index.js +0 -1
  159. package/src/internals/transformRPCResponse.ts +0 -25
  160. package/src/links/httpUtils.ts +0 -94
  161. package/src/links/transformerLink.ts +0 -70
  162. package/src/rx/index.ts +0 -2
  163. package/src/rx/observable.test.ts +0 -83
  164. package/src/rx/observable.ts +0 -70
  165. package/src/rx/operators/index.ts +0 -3
  166. package/src/rx/operators/map.test.ts +0 -92
  167. package/src/rx/operators/map.ts +0 -25
  168. package/src/rx/operators/share.test.ts +0 -60
  169. package/src/rx/operators/share.ts +0 -67
  170. package/src/rx/operators/tap.ts +0 -26
  171. package/src/rx/types.ts +0 -69
  172. package/src/rx/util/identity.ts +0 -3
  173. package/src/rx/util/observableToPromise.ts +0 -49
  174. package/src/rx/util/pipe.ts +0 -22
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trpc/client",
3
- "version": "10.0.0-alpha.4",
3
+ "version": "10.0.0-alpha.42",
4
4
  "description": "tRPC Client lib",
5
5
  "author": "KATT",
6
6
  "license": "MIT",
@@ -10,6 +10,14 @@
10
10
  "url": "git+https://github.com/trpc/trpc.git",
11
11
  "directory": "packages/client"
12
12
  },
13
+ "eslintConfig": {
14
+ "rules": {
15
+ "no-restricted-imports": [
16
+ "error",
17
+ "@trpc/client"
18
+ ]
19
+ }
20
+ },
13
21
  "main": "dist/index.js",
14
22
  "module": "dist/index.mjs",
15
23
  "typings": "dist/index.d.ts",
@@ -19,68 +27,53 @@
19
27
  "require": "./dist/index.js",
20
28
  "default": "./dist/index.js"
21
29
  },
22
- "./links/httpBatchLink": {
23
- "import": "./dist/links/httpBatchLink.mjs",
24
- "require": "./dist/links/httpBatchLink.js",
25
- "default": "./dist/links/httpBatchLink.js"
26
- },
27
30
  "./links/httpLink": {
28
31
  "import": "./dist/links/httpLink.mjs",
29
32
  "require": "./dist/links/httpLink.js",
30
33
  "default": "./dist/links/httpLink.js"
31
34
  },
32
- "./links/loggerLink": {
33
- "import": "./dist/links/loggerLink.mjs",
34
- "require": "./dist/links/loggerLink.js",
35
- "default": "./dist/links/loggerLink.js"
35
+ "./links/httpBatchLink": {
36
+ "import": "./dist/links/httpBatchLink.mjs",
37
+ "require": "./dist/links/httpBatchLink.js",
38
+ "default": "./dist/links/httpBatchLink.js"
36
39
  },
37
40
  "./links/splitLink": {
38
41
  "import": "./dist/links/splitLink.mjs",
39
42
  "require": "./dist/links/splitLink.js",
40
43
  "default": "./dist/links/splitLink.js"
41
44
  },
42
- "./links/transformerLink": {
43
- "import": "./dist/links/transformerLink.mjs",
44
- "require": "./dist/links/transformerLink.js",
45
- "default": "./dist/links/transformerLink.js"
45
+ "./links/loggerLink": {
46
+ "import": "./dist/links/loggerLink.mjs",
47
+ "require": "./dist/links/loggerLink.js",
48
+ "default": "./dist/links/loggerLink.js"
46
49
  },
47
50
  "./links/wsLink": {
48
51
  "import": "./dist/links/wsLink.mjs",
49
52
  "require": "./dist/links/wsLink.js",
50
53
  "default": "./dist/links/wsLink.js"
51
- },
52
- "./rx": {
53
- "import": "./dist/rx/index.mjs",
54
- "require": "./dist/rx/index.js",
55
- "default": "./dist/rx/index.js"
56
- },
57
- "./rx/operators": {
58
- "import": "./dist/rx/operators/index.mjs",
59
- "require": "./dist/rx/operators/index.js",
60
- "default": "./dist/rx/operators/index.js"
61
54
  }
62
55
  },
63
56
  "files": [
64
57
  "README.md",
65
58
  "dist",
66
59
  "links",
67
- "src",
68
- "rx"
60
+ "src"
69
61
  ],
70
62
  "scripts": {
71
- "build": "rollup -c"
63
+ "build": "rollup -c",
64
+ "ts-watch": "tsc --project tsconfig.watch.json"
72
65
  },
73
66
  "dependencies": {
74
67
  "@babel/runtime": "^7.9.0"
75
68
  },
76
69
  "peerDependencies": {
77
- "@trpc/server": "10.0.0-alpha.4"
70
+ "@trpc/server": "10.0.0-alpha.42"
78
71
  },
79
72
  "devDependencies": {
80
- "@trpc/server": "10.0.0-alpha.4"
73
+ "@trpc/server": "10.0.0-alpha.42"
81
74
  },
82
75
  "publishConfig": {
83
76
  "access": "public"
84
77
  },
85
- "gitHead": "ef9f7f1d36a26339b96069bba001ea0fe5768bb3"
78
+ "gitHead": "d5909785e8d024ef7869d9f5423dbe3bfd80cd59"
86
79
  }
@@ -1,4 +1,4 @@
1
- import { AnyRouter, inferRouterError, Maybe } from '@trpc/server';
1
+ import { AnyRouter, Maybe, inferRouterError } from '@trpc/server';
2
2
  import { TRPCErrorResponse } from '@trpc/server/rpc';
3
3
 
4
4
  export interface TRPCClientErrorLike<TRouter extends AnyRouter> {
@@ -11,10 +11,6 @@ export class TRPCClientError<TRouter extends AnyRouter>
11
11
  extends Error
12
12
  implements TRPCClientErrorLike<TRouter>
13
13
  {
14
- /**
15
- * @deprecated use `cause`
16
- */
17
- public readonly originalError;
18
14
  public readonly cause;
19
15
  public readonly shape: Maybe<inferRouterError<TRouter>>;
20
16
  public readonly data: Maybe<inferRouterError<TRouter>['data']>;
@@ -24,15 +20,11 @@ export class TRPCClientError<TRouter extends AnyRouter>
24
20
  message: string,
25
21
  opts?: {
26
22
  result: Maybe<TRPCErrorResponse<inferRouterError<TRouter>>>;
27
- /**
28
- * @deprecated use `cause`
29
- **/
30
- originalError?: Maybe<Error>;
31
- cause?: Maybe<Error>;
23
+ cause?: Error;
32
24
  meta?: Record<string, unknown>;
33
25
  },
34
26
  ) {
35
- const cause = opts?.cause ?? opts?.originalError;
27
+ const cause = opts?.cause;
36
28
 
37
29
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
38
30
  // @ts-ignore https://github.com/tc39/proposal-error-cause
@@ -40,7 +32,7 @@ export class TRPCClientError<TRouter extends AnyRouter>
40
32
 
41
33
  this.meta = opts?.meta;
42
34
 
43
- this.cause = this.originalError = cause;
35
+ this.cause = cause;
44
36
  this.shape = opts?.result?.error;
45
37
  this.data = opts?.result?.error.data;
46
38
  this.name = 'TRPCClientError';
@@ -55,7 +47,7 @@ export class TRPCClientError<TRouter extends AnyRouter>
55
47
  if (!(cause instanceof Error)) {
56
48
  return new TRPCClientError<TRouter>((cause.error as any).message ?? '', {
57
49
  ...opts,
58
- cause: null,
50
+ cause: undefined,
59
51
  result: cause,
60
52
  });
61
53
  }
@@ -1,19 +1,23 @@
1
+ /* eslint-disable @typescript-eslint/no-non-null-assertion */
2
+
1
3
  /* eslint-disable @typescript-eslint/no-explicit-any */
2
4
  import type { AnyRouter } from '@trpc/server';
3
5
  import {
4
- CreateTRPCClientOptions,
5
6
  TRPCClient as Client,
7
+ CreateTRPCClientOptions,
6
8
  } from './internals/TRPCClient';
7
9
 
8
10
  export function createTRPCClient<TRouter extends AnyRouter>(
9
11
  opts: CreateTRPCClientOptions<TRouter>,
10
12
  ) {
11
- return new Client<TRouter>(opts);
13
+ const client = new Client<TRouter>(opts);
14
+ return client;
12
15
  }
13
16
 
14
- export type TRPCClient<TRouter extends AnyRouter> = Client<TRouter>;
17
+ // Also the client created above needs to somehow be like `TRPCClient<Router> & Omit<Router, 'createCaller' | 'createProcedure' | '_def' | 'transformer' | 'errorFormatter' | 'getErrorShape>`
15
18
 
16
19
  export type {
17
20
  TRPCRequestOptions,
18
21
  CreateTRPCClientOptions,
22
+ TRPCClient,
19
23
  } from './internals/TRPCClient';
@@ -0,0 +1,98 @@
1
+ /* eslint-disable @typescript-eslint/no-non-null-assertion */
2
+
3
+ /* eslint-disable @typescript-eslint/no-explicit-any */
4
+ import type {
5
+ AnyRouter,
6
+ OmitNeverKeys,
7
+ Procedure,
8
+ ProcedureArgs,
9
+ ProcedureRouterRecord,
10
+ ProcedureType,
11
+ inferProcedureOutput,
12
+ } from '@trpc/server';
13
+ import type {
14
+ Unsubscribable,
15
+ inferObservableValue,
16
+ } from '@trpc/server/observable';
17
+ import type { TRPCResultMessage } from '@trpc/server/rpc';
18
+ import { createProxy } from '@trpc/server/shared';
19
+ import { TRPCClientError } from './TRPCClientError';
20
+ import {
21
+ TRPCClient as Client,
22
+ TRPCSubscriptionObserver,
23
+ } from './internals/TRPCClient';
24
+
25
+ type Resolver<TProcedure extends Procedure<any>> = (
26
+ ...args: ProcedureArgs<TProcedure['_def']>
27
+ ) => Promise<inferProcedureOutput<TProcedure>>;
28
+
29
+ type SubscriptionResolver<
30
+ TProcedure extends Procedure<any>,
31
+ TRouter extends AnyRouter,
32
+ > = (
33
+ ...args: [
34
+ input: ProcedureArgs<TProcedure['_def']>[0],
35
+ opts: ProcedureArgs<TProcedure['_def']>[1] &
36
+ Partial<
37
+ TRPCSubscriptionObserver<
38
+ TRPCResultMessage<
39
+ inferObservableValue<inferProcedureOutput<TProcedure>>
40
+ >,
41
+ TRPCClientError<TRouter>
42
+ >
43
+ >,
44
+ ]
45
+ ) => Unsubscribable;
46
+
47
+ type DecorateProcedure<
48
+ TProcedure extends Procedure<any>,
49
+ TRouter extends AnyRouter,
50
+ > = OmitNeverKeys<{
51
+ query: TProcedure extends { _query: true } ? Resolver<TProcedure> : never;
52
+
53
+ mutate: TProcedure extends { _mutation: true } ? Resolver<TProcedure> : never;
54
+
55
+ subscribe: TProcedure extends { _subscription: true }
56
+ ? SubscriptionResolver<TProcedure, TRouter>
57
+ : never;
58
+ }>;
59
+
60
+ type assertProcedure<T> = T extends Procedure<any> ? T : never;
61
+
62
+ /**
63
+ * @internal
64
+ */
65
+ type DecoratedProcedureRecord<
66
+ TProcedures extends ProcedureRouterRecord,
67
+ TRouter extends AnyRouter,
68
+ > = {
69
+ [TKey in keyof TProcedures]: TProcedures[TKey] extends AnyRouter
70
+ ? DecoratedProcedureRecord<
71
+ TProcedures[TKey]['_def']['record'],
72
+ TProcedures[TKey]
73
+ >
74
+ : DecorateProcedure<assertProcedure<TProcedures[TKey]>, TRouter>;
75
+ };
76
+
77
+ const clientCallTypeMap: Record<
78
+ keyof DecorateProcedure<any, any>,
79
+ ProcedureType
80
+ > = {
81
+ query: 'query',
82
+ mutate: 'mutation',
83
+ subscribe: 'subscription',
84
+ };
85
+
86
+ export function createTRPCClientProxy<TRouter extends AnyRouter>(
87
+ client: Client<TRouter>,
88
+ ) {
89
+ const proxy = createProxy(({ path, args }) => {
90
+ const pathCopy = [...path];
91
+ const clientCallType = pathCopy.pop()! as keyof DecorateProcedure<any, any>;
92
+ const procedureType = clientCallTypeMap[clientCallType];
93
+
94
+ const fullPath = pathCopy.join('.');
95
+ return (client as any)[procedureType](fullPath, ...args);
96
+ });
97
+ return proxy as DecoratedProcedureRecord<TRouter['_def']['record'], TRouter>;
98
+ }
package/src/index.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './createTRPCClient';
2
+ export * from './createTRPCClientProxy';
2
3
  export * from './getFetch';
3
- export * from './links/types';
4
4
  export * from './TRPCClientError';
5
+ export * from './links';
@@ -1,42 +1,40 @@
1
1
  import {
2
2
  AnyRouter,
3
+ ClientDataTransformerOptions,
4
+ DataTransformer,
3
5
  inferHandlerInput,
4
6
  inferProcedureInput,
5
7
  inferProcedureOutput,
6
8
  inferSubscriptionOutput,
7
9
  } from '@trpc/server';
8
- import { TRPCResult } from '@trpc/server/rpc';
10
+ import {
11
+ Unsubscribable,
12
+ inferObservableValue,
13
+ observableToPromise,
14
+ share,
15
+ } from '@trpc/server/observable';
9
16
  import { CancelFn } from '..';
17
+ import { TRPCClientError } from '../TRPCClientError';
10
18
  import { getFetch } from '../getFetch';
11
- import { httpBatchLink } from '../links/httpBatchLink';
19
+ import { httpBatchLink } from '../links';
12
20
  import { createChain } from '../links/internals/createChain';
13
- import { transformOperationResult } from '../links/internals/transformOperationResult';
21
+ import {
22
+ transformOperationResult,
23
+ transformSubscriptionOperationResult,
24
+ } from '../links/internals/transformOperationResult';
14
25
  import {
15
26
  HTTPHeaders,
16
- LinkRuntime,
17
27
  OperationContext,
18
28
  OperationLink,
29
+ TRPCClientRuntime,
19
30
  TRPCLink,
20
31
  } from '../links/types';
21
- import { UnsubscribeFn } from '../rx';
22
- import { inferObservableValue } from '../rx/observable';
23
- import { share } from '../rx/operators';
24
- import { Observer } from '../rx/types';
25
- import { observableToPromise } from '../rx/util/observableToPromise';
26
- import { TRPCClientError } from '../TRPCClientError';
27
32
  import { getAbortController } from './fetchHelpers';
28
33
 
29
34
  type CancellablePromise<T = unknown> = Promise<T> & {
30
35
  cancel: CancelFn;
31
36
  };
32
37
 
33
- /**
34
- * @deprecated no longer used
35
- */
36
- export interface FetchOptions {
37
- fetch?: typeof fetch;
38
- AbortController?: typeof AbortController;
39
- }
40
38
  let idCounter = 0;
41
39
  function getRequestId() {
42
40
  return ++idCounter;
@@ -54,6 +52,11 @@ interface CreateTRPCClientBaseOptions {
54
52
  * headers to be set on outgoing requests / callback that of said headers
55
53
  */
56
54
  headers?: HTTPHeaders | (() => HTTPHeaders | Promise<HTTPHeaders>);
55
+ /**
56
+ * Data transformer
57
+ * @link https://trpc.io/docs/data-transformers
58
+ **/
59
+ transformer?: ClientDataTransformerOptions;
57
60
  }
58
61
 
59
62
  /** @internal */
@@ -69,7 +72,7 @@ export interface CreateTRPCClientWithURLOptions
69
72
  export interface CreateTRPCClientWithLinksOptions<TRouter extends AnyRouter>
70
73
  extends CreateTRPCClientBaseOptions {
71
74
  /**
72
- * @link http://localhost:3000/docs/links
75
+ * @link https://trpc.io/docs/links
73
76
  **/
74
77
  links: TRPCLink<TRouter>[];
75
78
  }
@@ -79,7 +82,15 @@ export interface TRPCRequestOptions {
79
82
  /**
80
83
  * Pass additional context to links
81
84
  */
82
- context?: OperationContext;
85
+ requestContext?: OperationContext;
86
+ }
87
+
88
+ export interface TRPCSubscriptionObserver<TValue, TError> {
89
+ onStarted: () => void;
90
+ onData: (value: TValue) => void;
91
+ onError: (err: TError) => void;
92
+ onStopped: () => void;
93
+ onComplete: () => void;
83
94
  }
84
95
 
85
96
  /** @internal */
@@ -88,23 +99,37 @@ export type CreateTRPCClientOptions<TRouter extends AnyRouter> =
88
99
  | CreateTRPCClientWithURLOptions;
89
100
  export class TRPCClient<TRouter extends AnyRouter> {
90
101
  private readonly links: OperationLink<TRouter>[];
91
- public readonly runtime: LinkRuntime;
102
+ public readonly runtime: TRPCClientRuntime;
92
103
 
93
104
  constructor(opts: CreateTRPCClientOptions<TRouter>) {
94
105
  const _fetch = getFetch(opts?.fetch);
95
106
  const AC = getAbortController(opts?.AbortController);
96
107
 
97
- function getHeadersFn(): LinkRuntime['headers'] {
108
+ function getHeadersFn(): TRPCClientRuntime['headers'] {
98
109
  if (opts.headers) {
99
110
  const headers = opts.headers;
100
111
  return typeof headers === 'function' ? headers : () => headers;
101
112
  }
102
113
  return () => ({});
103
114
  }
115
+
116
+ const transformer: DataTransformer = opts.transformer
117
+ ? 'input' in opts.transformer
118
+ ? {
119
+ serialize: opts.transformer.input.serialize,
120
+ deserialize: opts.transformer.output.deserialize,
121
+ }
122
+ : opts.transformer
123
+ : {
124
+ serialize: (data) => data,
125
+ deserialize: (data) => data,
126
+ };
127
+
104
128
  this.runtime = {
105
129
  AbortController: AC as any,
106
130
  fetch: _fetch,
107
131
  headers: getHeadersFn(),
132
+ transformer,
108
133
  };
109
134
 
110
135
  if ('links' in opts) {
@@ -155,9 +180,9 @@ export class TRPCClient<TRouter extends AnyRouter> {
155
180
  (resolve, reject) => {
156
181
  promise
157
182
  .then((result) => {
158
- const transformed = transformOperationResult(result);
183
+ const transformed = transformOperationResult(result, this.runtime);
159
184
  if (transformed.ok) {
160
- resolve(transformed.data as any);
185
+ resolve(transformed.data);
161
186
  return;
162
187
  }
163
188
  reject(transformed.error);
@@ -178,7 +203,7 @@ export class TRPCClient<TRouter extends AnyRouter> {
178
203
  path: TPath,
179
204
  ...args: [...inferHandlerInput<TQueries[TPath]>, TRPCRequestOptions?]
180
205
  ) {
181
- const context = (args[1] as TRPCRequestOptions | undefined)?.context;
206
+ const context = (args[1] as TRPCRequestOptions | undefined)?.requestContext;
182
207
  return this.requestAsPromise<
183
208
  inferHandlerInput<TQueries[TPath]>,
184
209
  inferProcedureOutput<TQueries[TPath]>
@@ -189,7 +214,6 @@ export class TRPCClient<TRouter extends AnyRouter> {
189
214
  context,
190
215
  });
191
216
  }
192
-
193
217
  public mutation<
194
218
  TMutations extends TRouter['_def']['mutations'],
195
219
  TPath extends string & keyof TMutations,
@@ -197,7 +221,7 @@ export class TRPCClient<TRouter extends AnyRouter> {
197
221
  path: TPath,
198
222
  ...args: [...inferHandlerInput<TMutations[TPath]>, TRPCRequestOptions?]
199
223
  ) {
200
- const context = (args[1] as TRPCRequestOptions | undefined)?.context;
224
+ const context = (args[1] as TRPCRequestOptions | undefined)?.requestContext;
201
225
  return this.requestAsPromise<
202
226
  inferHandlerInput<TMutations[TPath]>,
203
227
  inferProcedureOutput<TMutations[TPath]>
@@ -217,35 +241,39 @@ export class TRPCClient<TRouter extends AnyRouter> {
217
241
  path: TPath,
218
242
  input: TInput,
219
243
  opts: TRPCRequestOptions &
220
- Partial<Observer<TRPCResult<TOutput>, TRPCClientError<TRouter>>>,
221
- ): UnsubscribeFn {
244
+ Partial<TRPCSubscriptionObserver<TOutput, TRPCClientError<TRouter>>>,
245
+ ): Unsubscribable {
222
246
  const observable$ = this.$request<TInput, TOutput>({
223
247
  type: 'subscription',
224
248
  path,
225
249
  input,
226
- context: opts.context,
250
+ context: opts.requestContext,
227
251
  });
228
- const observer = observable$.subscribe({
252
+ const runtime = this.runtime;
253
+ return observable$.subscribe({
229
254
  next(result) {
230
- if ('error' in result.data) {
231
- const err = TRPCClientError.from(result.data, {
232
- meta: result.context,
233
- });
234
-
235
- opts.error?.(err);
255
+ const transformed = transformSubscriptionOperationResult(
256
+ result,
257
+ runtime,
258
+ );
259
+ if (transformed.ok) {
260
+ if (transformed.data.type === 'started') {
261
+ opts.onStarted?.();
262
+ } else if (transformed.data.type === 'stopped') {
263
+ opts.onStopped?.();
264
+ } else if (transformed.data.type === 'data') {
265
+ opts.onData?.(transformed.data.data);
266
+ }
236
267
  return;
237
268
  }
238
- opts.next?.(result.data.result);
269
+ opts.onError?.(transformed.error);
239
270
  },
240
271
  error(err) {
241
- opts.error?.(err);
272
+ opts.onError?.(err);
242
273
  },
243
274
  complete() {
244
- opts.complete?.();
275
+ opts.onComplete?.();
245
276
  },
246
277
  });
247
- return () => {
248
- observer.unsubscribe();
249
- };
250
278
  }
251
279
  }