@trpc/client 10.0.0-alpha.8 → 10.0.0-proxy-alpha.53

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 (180) hide show
  1. package/README.md +58 -1
  2. package/dist/TRPCClientError.d.ts +16 -20
  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 +37 -0
  7. package/dist/createTRPCClientProxy.d.ts.map +1 -0
  8. package/dist/httpUtils-44781613.mjs +76 -0
  9. package/dist/httpUtils-801112a6.js +79 -0
  10. package/dist/index.d.ts +2 -1
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +260 -280
  13. package/dist/index.mjs +253 -271
  14. package/dist/internals/TRPCClient.d.ts +39 -21
  15. package/dist/internals/TRPCClient.d.ts.map +1 -1
  16. package/dist/internals/dataLoader.d.ts +7 -6
  17. package/dist/internals/dataLoader.d.ts.map +1 -1
  18. package/dist/internals/fetchHelpers.d.ts +1 -1
  19. package/dist/internals/fetchHelpers.d.ts.map +1 -1
  20. package/dist/internals/retryDelay.d.ts.map +1 -1
  21. package/dist/links/dedupeLink.d.ts.map +1 -1
  22. package/dist/links/httpBatchLink.d.ts +2 -2
  23. package/dist/links/httpBatchLink.d.ts.map +1 -1
  24. package/dist/links/httpBatchLink.js +189 -172
  25. package/dist/links/httpBatchLink.mjs +189 -172
  26. package/dist/links/httpLink.d.ts +1 -1
  27. package/dist/links/httpLink.d.ts.map +1 -1
  28. package/dist/links/httpLink.js +31 -35
  29. package/dist/links/httpLink.mjs +31 -35
  30. package/dist/links/index.d.ts +9 -0
  31. package/dist/links/index.d.ts.map +1 -0
  32. package/dist/links/internals/createChain.d.ts.map +1 -1
  33. package/dist/links/internals/httpUtils.d.ts +31 -0
  34. package/dist/links/internals/httpUtils.d.ts.map +1 -0
  35. package/dist/links/internals/transformResult.d.ts +28 -0
  36. package/dist/links/internals/transformResult.d.ts.map +1 -0
  37. package/dist/links/loggerLink.d.ts +3 -3
  38. package/dist/links/loggerLink.d.ts.map +1 -1
  39. package/dist/links/loggerLink.js +90 -96
  40. package/dist/links/loggerLink.mjs +90 -91
  41. package/dist/links/retryLink.d.ts.map +1 -1
  42. package/dist/links/splitLink.d.ts +1 -1
  43. package/dist/links/splitLink.d.ts.map +1 -1
  44. package/dist/links/splitLink.js +3 -25
  45. package/dist/links/splitLink.mjs +2 -28
  46. package/dist/links/types.d.ts +13 -12
  47. package/dist/links/types.d.ts.map +1 -1
  48. package/dist/links/wsLink.d.ts +12 -5
  49. package/dist/links/wsLink.d.ts.map +1 -1
  50. package/dist/links/wsLink.js +250 -358
  51. package/dist/links/wsLink.mjs +250 -348
  52. package/dist/splitLink-695ae288.js +48 -0
  53. package/dist/splitLink-ad44a55d.mjs +45 -0
  54. package/dist/transformResult-106791d7.mjs +62 -0
  55. package/dist/transformResult-e15ccdf6.js +65 -0
  56. package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.cjs.dev.js +15 -13
  57. package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.cjs.prod.js +15 -13
  58. package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.esm.js +12 -10
  59. package/links/httpLink/dist/trpc-client-links-httpLink.cjs.dev.js +4 -4
  60. package/links/httpLink/dist/trpc-client-links-httpLink.cjs.prod.js +4 -4
  61. package/links/httpLink/dist/trpc-client-links-httpLink.esm.js +1 -1
  62. package/links/wsLink/dist/trpc-client-links-wsLink.cjs.dev.js +3 -1
  63. package/links/wsLink/dist/trpc-client-links-wsLink.cjs.prod.js +3 -1
  64. package/links/wsLink/dist/trpc-client-links-wsLink.esm.js +3 -1
  65. package/package.json +23 -34
  66. package/src/TRPCClientError.ts +38 -28
  67. package/src/createTRPCClient.ts +8 -3
  68. package/src/createTRPCClientProxy.ts +113 -0
  69. package/src/index.ts +2 -1
  70. package/src/internals/TRPCClient.ts +118 -77
  71. package/src/internals/dataLoader.ts +116 -53
  72. package/src/internals/fetchHelpers.ts +2 -2
  73. package/src/links/dedupeLink.test.ts +9 -15
  74. package/src/links/dedupeLink.ts +6 -7
  75. package/src/links/httpBatchLink.ts +73 -36
  76. package/src/links/httpLink.ts +18 -6
  77. package/src/links/index.ts +8 -0
  78. package/src/links/internals/createChain.test.ts +23 -38
  79. package/src/links/internals/createChain.ts +8 -3
  80. package/src/links/internals/httpUtils.ts +117 -0
  81. package/src/links/internals/transformResult.ts +40 -0
  82. package/src/links/loggerLink.ts +6 -7
  83. package/src/links/retryLink.ts +2 -3
  84. package/src/links/splitLink.test.ts +3 -3
  85. package/src/links/splitLink.ts +5 -5
  86. package/src/links/types.ts +15 -12
  87. package/src/links/wsLink.ts +57 -23
  88. package/dist/TRPCClientError-17b117e1.mjs +0 -81
  89. package/dist/TRPCClientError-ec6bd2ca.js +0 -94
  90. package/dist/createChain-389cc116.mjs +0 -25
  91. package/dist/createChain-864dbb30.js +0 -27
  92. package/dist/httpUtils-1f9e4e3c.js +0 -97
  93. package/dist/httpUtils-c09d7b79.mjs +0 -91
  94. package/dist/index.ts.js +0 -1
  95. package/dist/internals/transformRPCResponse.d.ts +0 -15
  96. package/dist/internals/transformRPCResponse.d.ts.map +0 -1
  97. package/dist/links/httpBatchLink.ts.js +0 -1
  98. package/dist/links/httpLink.ts.js +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/internals/transformOperationResult.d.ts +0 -16
  102. package/dist/links/internals/transformOperationResult.d.ts.map +0 -1
  103. package/dist/links/loggerLink.ts.js +0 -1
  104. package/dist/links/splitLink.ts.js +0 -1
  105. package/dist/links/transformerLink.d.ts +0 -4
  106. package/dist/links/transformerLink.d.ts.map +0 -1
  107. package/dist/links/transformerLink.js +0 -75
  108. package/dist/links/transformerLink.mjs +0 -67
  109. package/dist/links/transformerLink.ts.js +0 -1
  110. package/dist/links/wsLink.ts.js +0 -1
  111. package/dist/observable-7e66cb56.mjs +0 -102
  112. package/dist/observable-85669100.js +0 -104
  113. package/dist/rx/index.d.ts +0 -3
  114. package/dist/rx/index.d.ts.map +0 -1
  115. package/dist/rx/index.js +0 -9
  116. package/dist/rx/index.mjs +0 -1
  117. package/dist/rx/index.ts.js +0 -1
  118. package/dist/rx/observable.d.ts +0 -4
  119. package/dist/rx/observable.d.ts.map +0 -1
  120. package/dist/rx/operators/index.d.ts +0 -4
  121. package/dist/rx/operators/index.d.ts.map +0 -1
  122. package/dist/rx/operators/index.js +0 -38
  123. package/dist/rx/operators/index.mjs +0 -32
  124. package/dist/rx/operators/index.ts.js +0 -1
  125. package/dist/rx/operators/map.d.ts +0 -3
  126. package/dist/rx/operators/map.d.ts.map +0 -1
  127. package/dist/rx/operators/share.d.ts +0 -6
  128. package/dist/rx/operators/share.d.ts.map +0 -1
  129. package/dist/rx/operators/tap.d.ts +0 -3
  130. package/dist/rx/operators/tap.d.ts.map +0 -1
  131. package/dist/rx/types.d.ts +0 -29
  132. package/dist/rx/types.d.ts.map +0 -1
  133. package/dist/rx/util/identity.d.ts +0 -2
  134. package/dist/rx/util/identity.d.ts.map +0 -1
  135. package/dist/rx/util/observableToPromise.d.ts +0 -10
  136. package/dist/rx/util/observableToPromise.d.ts.map +0 -1
  137. package/dist/rx/util/pipe.d.ts +0 -4
  138. package/dist/rx/util/pipe.d.ts.map +0 -1
  139. package/dist/share-4d79dc49.js +0 -110
  140. package/dist/share-671b63fa.mjs +0 -108
  141. package/dist/tap-538fa07f.mjs +0 -30
  142. package/dist/tap-799a2d33.js +0 -32
  143. package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.d.ts +0 -1
  144. package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.dev.js +0 -67
  145. package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.js +0 -7
  146. package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.prod.js +0 -67
  147. package/links/transformerLink/dist/trpc-client-links-transformerLink.esm.js +0 -63
  148. package/links/transformerLink/index.d.ts +0 -1
  149. package/links/transformerLink/index.js +0 -1
  150. package/rx/dist/trpc-client-rx.cjs.d.ts +0 -1
  151. package/rx/dist/trpc-client-rx.cjs.dev.js +0 -9
  152. package/rx/dist/trpc-client-rx.cjs.js +0 -7
  153. package/rx/dist/trpc-client-rx.cjs.prod.js +0 -9
  154. package/rx/dist/trpc-client-rx.esm.js +0 -1
  155. package/rx/index.d.ts +0 -1
  156. package/rx/index.js +0 -1
  157. package/rx/operators/dist/trpc-client-rx-operators.cjs.d.ts +0 -1
  158. package/rx/operators/dist/trpc-client-rx-operators.cjs.dev.js +0 -39
  159. package/rx/operators/dist/trpc-client-rx-operators.cjs.js +0 -7
  160. package/rx/operators/dist/trpc-client-rx-operators.cjs.prod.js +0 -39
  161. package/rx/operators/dist/trpc-client-rx-operators.esm.js +0 -33
  162. package/rx/operators/index.d.ts +0 -1
  163. package/rx/operators/index.js +0 -1
  164. package/src/internals/transformRPCResponse.ts +0 -25
  165. package/src/links/httpUtils.ts +0 -94
  166. package/src/links/internals/transformOperationResult.ts +0 -25
  167. package/src/links/transformerLink.ts +0 -70
  168. package/src/rx/index.ts +0 -2
  169. package/src/rx/observable.test.ts +0 -83
  170. package/src/rx/observable.ts +0 -70
  171. package/src/rx/operators/index.ts +0 -3
  172. package/src/rx/operators/map.test.ts +0 -92
  173. package/src/rx/operators/map.ts +0 -25
  174. package/src/rx/operators/share.test.ts +0 -60
  175. package/src/rx/operators/share.ts +0 -67
  176. package/src/rx/operators/tap.ts +0 -26
  177. package/src/rx/types.ts +0 -69
  178. package/src/rx/util/identity.ts +0 -3
  179. package/src/rx/util/observableToPromise.ts +0 -49
  180. package/src/rx/util/pipe.ts +0 -22
package/dist/index.js CHANGED
@@ -2,313 +2,293 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var _classCallCheck = require('@babel/runtime/helpers/classCallCheck');
6
- var _createClass = require('@babel/runtime/helpers/createClass');
5
+ var observable = require('@trpc/server/observable');
6
+ var transformResult = require('./transformResult-e15ccdf6.js');
7
7
  var links_httpBatchLink = require('./links/httpBatchLink.js');
8
- var createChain = require('./createChain-864dbb30.js');
9
- var TRPCClientError = require('./TRPCClientError-ec6bd2ca.js');
10
- var share = require('./share-4d79dc49.js');
11
- var _assertThisInitialized = require('@babel/runtime/helpers/assertThisInitialized');
12
- var _inherits = require('@babel/runtime/helpers/inherits');
13
- var _possibleConstructorReturn = require('@babel/runtime/helpers/possibleConstructorReturn');
14
- var _getPrototypeOf = require('@babel/runtime/helpers/getPrototypeOf');
15
- var _wrapNativeSuper = require('@babel/runtime/helpers/wrapNativeSuper');
16
- require('./observable-85669100.js');
17
- require('./httpUtils-1f9e4e3c.js');
18
- require('@babel/runtime/helpers/defineProperty');
19
-
20
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
21
-
22
- var _classCallCheck__default = /*#__PURE__*/_interopDefaultLegacy(_classCallCheck);
23
- var _createClass__default = /*#__PURE__*/_interopDefaultLegacy(_createClass);
24
- var _assertThisInitialized__default = /*#__PURE__*/_interopDefaultLegacy(_assertThisInitialized);
25
- var _inherits__default = /*#__PURE__*/_interopDefaultLegacy(_inherits);
26
- var _possibleConstructorReturn__default = /*#__PURE__*/_interopDefaultLegacy(_possibleConstructorReturn);
27
- var _getPrototypeOf__default = /*#__PURE__*/_interopDefaultLegacy(_getPrototypeOf);
28
- var _wrapNativeSuper__default = /*#__PURE__*/_interopDefaultLegacy(_wrapNativeSuper);
8
+ var links_splitLink = require('./splitLink-695ae288.js');
9
+ var shared = require('@trpc/server/shared');
10
+ var links_httpLink = require('./links/httpLink.js');
11
+ var links_loggerLink = require('./links/loggerLink.js');
12
+ var links_wsLink = require('./links/wsLink.js');
13
+ require('./httpUtils-801112a6.js');
29
14
 
30
15
  function getWindow() {
31
- if (typeof window !== 'undefined') {
32
- return window;
33
- }
34
-
35
- return global;
16
+ if (typeof window !== 'undefined') {
17
+ return window;
18
+ }
19
+ return global;
36
20
  }
37
21
  function getAbortController(ac) {
38
- var win = getWindow();
39
- return ac || win.AbortController || null;
22
+ const win = getWindow();
23
+ return (ac ?? win.AbortController) ?? undefined;
40
24
  }
41
25
 
42
26
  function getFetch(f) {
43
- if (f) {
44
- return f;
45
- }
46
-
47
- var win = getWindow();
48
-
49
- if (win.fetch) {
50
- return typeof win.fetch.bind === 'function' ? win.fetch.bind(win) : win.fetch;
51
- }
52
-
53
- throw new Error('No fetch implementation found');
54
- }
55
-
56
- /** @internal */
57
- function transformOperationResult(result) {
58
- var context = result.context;
59
-
60
- if ('error' in result.data) {
61
- var error = TRPCClientError.TRPCClientError.from(result.data);
62
- return {
63
- ok: false,
64
- error: error,
65
- context: context
66
- };
67
- }
68
-
69
- var data = result.data.result.data;
70
- return {
71
- ok: true,
72
- data: data,
73
- context: context
74
- };
75
- }
76
-
77
- function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf__default["default"](Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf__default["default"](this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn__default["default"](this, result); }; }
78
-
79
- function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
80
-
81
- var ObservableAbortError = /*#__PURE__*/function (_Error) {
82
- _inherits__default["default"](ObservableAbortError, _Error);
83
-
84
- var _super = /*#__PURE__*/_createSuper(ObservableAbortError);
85
-
86
- function ObservableAbortError(message) {
87
- var _this;
88
-
89
- _classCallCheck__default["default"](this, ObservableAbortError);
90
-
91
- _this = _super.call(this, message);
92
- _this.name = 'ObservableAbortError';
93
- Object.setPrototypeOf(_assertThisInitialized__default["default"](_this), ObservableAbortError.prototype);
94
- return _this;
95
- }
96
-
97
- return _createClass__default["default"](ObservableAbortError);
98
- }( /*#__PURE__*/_wrapNativeSuper__default["default"](Error));
99
- /** @internal */
100
-
101
- function observableToPromise(observable) {
102
- var abort;
103
- var promise = new Promise(function (resolve, reject) {
104
- var isDone = false;
105
-
106
- function onDone() {
107
- if (isDone) {
108
- return;
109
- }
110
-
111
- isDone = true;
112
- reject(new ObservableAbortError('This operation was cancelled.'));
113
- obs$.unsubscribe();
27
+ if (f) {
28
+ return f;
114
29
  }
115
-
116
- var obs$ = observable.subscribe({
117
- next: function next(data) {
118
- isDone = true;
119
- resolve(data);
120
- onDone();
121
- },
122
- error: function error(data) {
123
- isDone = true;
124
- reject(data);
125
- onDone();
126
- },
127
- complete: function complete() {
128
- isDone = true;
129
- onDone();
130
- }
131
- });
132
- abort = onDone;
133
- });
134
- return {
135
- promise: promise,
136
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
137
- abort: abort
138
- };
30
+ const win = getWindow();
31
+ if (win.fetch) {
32
+ return typeof win.fetch.bind === 'function' ? win.fetch.bind(win) : win.fetch;
33
+ }
34
+ throw new Error('No fetch implementation found');
139
35
  }
140
36
 
141
- var idCounter = 0;
142
-
143
- function getRequestId() {
144
- return ++idCounter;
37
+ function dedupeLink() {
38
+ // initialized config
39
+ return ()=>{
40
+ // initialized in app
41
+ const pending = {};
42
+ return ({ op , next })=>{
43
+ // initialized for request
44
+ if (op.type !== 'query') {
45
+ // pass through
46
+ return next(op);
47
+ }
48
+ const key = JSON.stringify([
49
+ op.path,
50
+ op.input
51
+ ]);
52
+ const obs$ = pending[key];
53
+ if (obs$) {
54
+ // console.log('hooking into pending', { op });
55
+ return observable.observable((observer)=>obs$.subscribe(observer));
56
+ }
57
+ const shared$ = observable.observable((observer)=>{
58
+ function reset() {
59
+ delete pending[key];
60
+ }
61
+ const subscription = next(op).subscribe({
62
+ next (v) {
63
+ observer.next(v);
64
+ },
65
+ error (e) {
66
+ reset();
67
+ observer.error(e);
68
+ },
69
+ complete () {
70
+ reset();
71
+ observer.complete();
72
+ }
73
+ });
74
+ return ()=>{
75
+ reset();
76
+ subscription.unsubscribe();
77
+ };
78
+ }).pipe(observable.share());
79
+ pending[key] = shared$;
80
+ return shared$;
81
+ };
82
+ };
145
83
  }
146
84
 
147
- var TRPCClient = /*#__PURE__*/function () {
148
- function TRPCClient(opts) {
149
- var _this = this;
150
-
151
- _classCallCheck__default["default"](this, TRPCClient);
152
-
153
- this.links = void 0;
154
- this.runtime = void 0;
155
-
156
- var _fetch = getFetch(opts === null || opts === void 0 ? void 0 : opts.fetch);
157
-
158
- var AC = getAbortController(opts === null || opts === void 0 ? void 0 : opts.AbortController);
159
-
160
- function getHeadersFn() {
161
- if (opts.headers) {
162
- var headers = opts.headers;
163
- return typeof headers === 'function' ? headers : function () {
164
- return headers;
85
+ function retryLink(opts) {
86
+ // initialized config
87
+ return ()=>{
88
+ // initialized in app
89
+ return ({ op , next })=>{
90
+ // initialized for request
91
+ return observable.observable((observer)=>{
92
+ let next$ = null;
93
+ let attempts = 0;
94
+ let isDone = false;
95
+ function attempt() {
96
+ attempts++;
97
+ next$?.unsubscribe();
98
+ next$ = next(op).subscribe({
99
+ error (error) {
100
+ if (attempts >= opts.attempts) {
101
+ observer.error(error);
102
+ return;
103
+ }
104
+ attempt();
105
+ },
106
+ next (result) {
107
+ if ('result' in result.result) {
108
+ isDone = true;
109
+ observer.next(result);
110
+ return;
111
+ }
112
+ if (attempts >= opts.attempts) {
113
+ isDone = true;
114
+ observer.next(result);
115
+ return;
116
+ }
117
+ attempt();
118
+ },
119
+ complete () {
120
+ if (isDone) {
121
+ observer.complete();
122
+ }
123
+ }
124
+ });
125
+ }
126
+ attempt();
127
+ return ()=>{
128
+ isDone = true;
129
+ next$?.unsubscribe();
130
+ };
131
+ });
165
132
  };
166
- }
167
-
168
- return function () {
169
- return {};
170
- };
171
- }
172
-
173
- this.runtime = {
174
- AbortController: AC,
175
- fetch: _fetch,
176
- headers: getHeadersFn()
177
133
  };
134
+ }
178
135
 
179
- if ('links' in opts) {
180
- this.links = opts.links.map(function (link) {
181
- return link(_this.runtime);
182
- });
183
- } else {
184
- this.links = [links_httpBatchLink.httpBatchLink({
185
- url: opts.url
186
- })(this.runtime)];
136
+ class TRPCClient {
137
+ getRequestId() {
138
+ return ++this.requestId;
187
139
  }
188
- }
189
-
190
- _createClass__default["default"](TRPCClient, [{
191
- key: "$request",
192
- value: function $request(_ref) {
193
- var type = _ref.type,
194
- input = _ref.input,
195
- path = _ref.path,
196
- _ref$context = _ref.context,
197
- context = _ref$context === void 0 ? {} : _ref$context;
198
- var chain$ = createChain.createChain({
199
- links: this.links,
200
- op: {
201
- id: getRequestId(),
202
- type: type,
203
- path: path,
204
- input: input,
205
- context: context
206
- }
207
- });
208
- return chain$.pipe(share.share());
140
+ $request({ type , input , path , context ={} }) {
141
+ const chain$ = links_splitLink.createChain({
142
+ links: this.links,
143
+ op: {
144
+ id: this.getRequestId(),
145
+ type,
146
+ path,
147
+ input,
148
+ context
149
+ }
150
+ });
151
+ return chain$.pipe(observable.share());
209
152
  }
210
- }, {
211
- key: "requestAsPromise",
212
- value: function requestAsPromise(opts) {
213
- var req$ = this.$request(opts);
214
-
215
- var _observableToPromise = observableToPromise(req$),
216
- promise = _observableToPromise.promise,
217
- abort = _observableToPromise.abort;
218
-
219
- var cancellablePromise = new Promise(function (resolve, reject) {
220
- promise.then(function (result) {
221
- var transformed = transformOperationResult(result);
222
-
223
- if (transformed.ok) {
224
- resolve(transformed.data);
225
- return;
226
- }
227
-
228
- reject(transformed.error);
229
- }).catch(function (err) {
230
- reject(TRPCClientError.TRPCClientError.from(err));
153
+ requestAsPromise(opts) {
154
+ const req$ = this.$request(opts);
155
+ const { promise , abort } = observable.observableToPromise(req$);
156
+ const abortablePromise = new Promise((resolve, reject)=>{
157
+ opts.signal?.addEventListener('abort', abort);
158
+ promise.then((envelope)=>{
159
+ resolve(envelope.result.data);
160
+ }).catch((err)=>{
161
+ reject(transformResult.TRPCClientError.from(err));
162
+ });
231
163
  });
232
- });
233
- cancellablePromise.cancel = abort;
234
- return cancellablePromise;
164
+ return abortablePromise;
235
165
  }
236
- }, {
237
- key: "query",
238
- value: function query(path) {
239
- var _ref2;
240
-
241
- var context = (_ref2 = arguments.length <= 2 ? undefined : arguments[2]) === null || _ref2 === void 0 ? void 0 : _ref2.context;
242
- return this.requestAsPromise({
243
- type: 'query',
244
- path: path,
245
- input: arguments.length <= 1 ? undefined : arguments[1],
246
- context: context
247
- });
166
+ query(path, ...args) {
167
+ // FIXME: Should be inferred from `args`
168
+ const context = args[1]?.context;
169
+ const signal = args[1]?.signal;
170
+ return this.requestAsPromise({
171
+ type: 'query',
172
+ path,
173
+ input: args[0],
174
+ context,
175
+ signal
176
+ });
248
177
  }
249
- }, {
250
- key: "mutation",
251
- value: function mutation(path) {
252
- var _ref3;
253
-
254
- var context = (_ref3 = arguments.length <= 2 ? undefined : arguments[2]) === null || _ref3 === void 0 ? void 0 : _ref3.context;
255
- return this.requestAsPromise({
256
- type: 'mutation',
257
- path: path,
258
- input: arguments.length <= 1 ? undefined : arguments[1],
259
- context: context
260
- });
178
+ mutation(path, ...args) {
179
+ // FIXME: Should be inferred from `args`
180
+ const context = args[1]?.context;
181
+ const signal = args[1]?.signal;
182
+ return this.requestAsPromise({
183
+ type: 'mutation',
184
+ path,
185
+ input: args[0],
186
+ context,
187
+ signal
188
+ });
261
189
  }
262
- }, {
263
- key: "subscription",
264
- value: function subscription(path, input, opts) {
265
- var observable$ = this.$request({
266
- type: 'subscription',
267
- path: path,
268
- input: input,
269
- context: opts.context
270
- });
271
- var observer = observable$.subscribe({
272
- next: function next(result) {
273
- var _opts$next;
274
-
275
- if ('error' in result.data) {
276
- var _opts$error;
277
-
278
- var err = TRPCClientError.TRPCClientError.from(result.data, {
279
- meta: result.context
280
- });
281
- (_opts$error = opts.error) === null || _opts$error === void 0 ? void 0 : _opts$error.call(opts, err);
282
- return;
283
- }
284
-
285
- (_opts$next = opts.next) === null || _opts$next === void 0 ? void 0 : _opts$next.call(opts, result.data.result);
286
- },
287
- error: function error(err) {
288
- var _opts$error2;
289
-
290
- (_opts$error2 = opts.error) === null || _opts$error2 === void 0 ? void 0 : _opts$error2.call(opts, err);
291
- },
292
- complete: function complete() {
293
- var _opts$complete;
294
-
295
- (_opts$complete = opts.complete) === null || _opts$complete === void 0 ? void 0 : _opts$complete.call(opts);
190
+ subscription(path, input, opts) {
191
+ const observable$ = this.$request({
192
+ type: 'subscription',
193
+ path,
194
+ input,
195
+ context: opts?.context
196
+ });
197
+ return observable$.subscribe({
198
+ next (envelope) {
199
+ if (envelope.result.type === 'started') {
200
+ opts.onStarted?.();
201
+ } else if (envelope.result.type === 'stopped') {
202
+ opts.onStopped?.();
203
+ } else {
204
+ opts.onData?.(envelope.result.data);
205
+ }
206
+ },
207
+ error (err) {
208
+ opts.onError?.(err);
209
+ },
210
+ complete () {
211
+ opts.onComplete?.();
212
+ }
213
+ });
214
+ }
215
+ constructor(opts){
216
+ const _fetch = getFetch(opts?.fetch);
217
+ const AC = getAbortController(opts?.AbortController);
218
+ this.requestId = 0;
219
+ function getHeadersFn() {
220
+ if (opts.headers) {
221
+ const headers = opts.headers;
222
+ return typeof headers === 'function' ? headers : ()=>headers;
223
+ }
224
+ return ()=>({});
225
+ }
226
+ const transformer = opts.transformer ? 'input' in opts.transformer ? {
227
+ serialize: opts.transformer.input.serialize,
228
+ deserialize: opts.transformer.output.deserialize
229
+ } : opts.transformer : {
230
+ serialize: (data)=>data,
231
+ deserialize: (data)=>data
232
+ };
233
+ this.runtime = {
234
+ AbortController: AC,
235
+ fetch: _fetch,
236
+ headers: getHeadersFn(),
237
+ transformer
238
+ };
239
+ if ('links' in opts) {
240
+ this.links = opts.links.map((link)=>link(this.runtime));
241
+ } else {
242
+ this.links = [
243
+ links_httpBatchLink.httpBatchLink({
244
+ url: opts.url
245
+ })(this.runtime),
246
+ ];
296
247
  }
297
- });
298
- return function () {
299
- observer.unsubscribe();
300
- };
301
248
  }
302
- }]);
303
-
304
- return TRPCClient;
305
- }();
249
+ }
306
250
 
307
- /* eslint-disable @typescript-eslint/no-explicit-any */
308
251
  function createTRPCClient(opts) {
309
- return new TRPCClient(opts);
252
+ const client = new TRPCClient(opts);
253
+ return client;
254
+ }
255
+
256
+ const clientCallTypeMap = {
257
+ query: 'query',
258
+ mutate: 'mutation',
259
+ subscribe: 'subscription'
260
+ };
261
+ /**
262
+ * @deprecated use createTRPCProxyClient instead
263
+ * @internal
264
+ */ function createTRPCClientProxy(client) {
265
+ const proxy = shared.createProxy(({ path , args })=>{
266
+ const pathCopy = [
267
+ ...path
268
+ ];
269
+ const clientCallType = pathCopy.pop();
270
+ const procedureType = clientCallTypeMap[clientCallType];
271
+ const fullPath = pathCopy.join('.');
272
+ return client[procedureType](fullPath, ...args);
273
+ });
274
+ return proxy;
275
+ }
276
+ function createTRPCProxyClient(opts) {
277
+ const client = createTRPCClient(opts);
278
+ const proxy = createTRPCClientProxy(client);
279
+ return proxy;
310
280
  }
311
281
 
312
- exports.TRPCClientError = TRPCClientError.TRPCClientError;
282
+ exports.TRPCClientError = transformResult.TRPCClientError;
283
+ exports.httpBatchLink = links_httpBatchLink.httpBatchLink;
284
+ exports.splitLink = links_splitLink.splitLink;
285
+ exports.httpLink = links_httpLink.httpLink;
286
+ exports.loggerLink = links_loggerLink.loggerLink;
287
+ exports.createWSClient = links_wsLink.createWSClient;
288
+ exports.wsLink = links_wsLink.wsLink;
313
289
  exports.createTRPCClient = createTRPCClient;
290
+ exports.createTRPCClientProxy = createTRPCClientProxy;
291
+ exports.createTRPCProxyClient = createTRPCProxyClient;
292
+ exports.dedupeLink = dedupeLink;
314
293
  exports.getFetch = getFetch;
294
+ exports.retryLink = retryLink;