@trpc/client 10.0.0-alpha.3 → 10.0.0-alpha.30

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 (169) hide show
  1. package/dist/TRPCClientError.d.ts +3 -11
  2. package/dist/TRPCClientError.d.ts.map +1 -1
  3. package/dist/createTRPCClient.d.ts +7 -4
  4. package/dist/createTRPCClient.d.ts.map +1 -1
  5. package/dist/{httpUtils-c09d7b79.mjs → httpUtils-089853f6.mjs} +38 -30
  6. package/dist/{httpUtils-1f9e4e3c.js → httpUtils-a0169eef.js} +38 -29
  7. package/dist/index.d.ts +1 -1
  8. package/dist/index.d.ts.map +1 -1
  9. package/dist/index.js +257 -103
  10. package/dist/index.mjs +246 -95
  11. package/dist/internals/TRPCClient.d.ts +14 -17
  12. package/dist/internals/TRPCClient.d.ts.map +1 -1
  13. package/dist/internals/dataLoader.d.ts +7 -4
  14. package/dist/internals/dataLoader.d.ts.map +1 -1
  15. package/dist/links/dedupeLink.d.ts.map +1 -1
  16. package/dist/links/httpBatchLink.d.ts +5 -2
  17. package/dist/links/httpBatchLink.d.ts.map +1 -1
  18. package/dist/links/httpBatchLink.js +197 -69
  19. package/dist/links/httpBatchLink.mjs +197 -69
  20. package/dist/links/httpLink.d.ts +1 -1
  21. package/dist/links/httpLink.d.ts.map +1 -1
  22. package/dist/links/httpLink.js +5 -5
  23. package/dist/links/httpLink.mjs +5 -5
  24. package/dist/links/index.d.ts +9 -0
  25. package/dist/links/index.d.ts.map +1 -0
  26. package/dist/links/internals/httpUtils.d.ts +30 -0
  27. package/dist/links/internals/httpUtils.d.ts.map +1 -0
  28. package/dist/links/internals/transformOperationResult.d.ts +17 -3
  29. package/dist/links/internals/transformOperationResult.d.ts.map +1 -1
  30. package/dist/links/loggerLink.d.ts.map +1 -1
  31. package/dist/links/loggerLink.js +2 -3
  32. package/dist/links/loggerLink.mjs +1 -2
  33. package/dist/links/retryLink.d.ts.map +1 -1
  34. package/dist/links/splitLink.d.ts +1 -1
  35. package/dist/links/splitLink.d.ts.map +1 -1
  36. package/dist/links/splitLink.js +3 -25
  37. package/dist/links/splitLink.mjs +2 -28
  38. package/dist/links/types.d.ts +12 -9
  39. package/dist/links/types.d.ts.map +1 -1
  40. package/dist/links/wsLink.d.ts +3 -3
  41. package/dist/links/wsLink.d.ts.map +1 -1
  42. package/dist/links/wsLink.js +11 -376
  43. package/dist/links/wsLink.mjs +9 -369
  44. package/dist/splitLink-62ff9db0.mjs +49 -0
  45. package/dist/splitLink-8202386e.js +52 -0
  46. package/dist/wsLink-69f5adb8.js +451 -0
  47. package/dist/wsLink-9468a563.mjs +436 -0
  48. package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.cjs.dev.js +81 -15
  49. package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.cjs.prod.js +81 -15
  50. package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.esm.js +77 -15
  51. package/links/httpBatchLink/index.d.ts +1 -1
  52. package/links/httpBatchLink/index.js +1 -1
  53. package/links/httpLink/dist/trpc-client-links-httpLink.cjs.dev.js +76 -9
  54. package/links/httpLink/dist/trpc-client-links-httpLink.cjs.prod.js +76 -9
  55. package/links/httpLink/dist/trpc-client-links-httpLink.esm.js +72 -9
  56. package/links/httpLink/index.d.ts +1 -1
  57. package/links/httpLink/index.js +1 -1
  58. package/links/loggerLink/index.d.ts +1 -1
  59. package/links/loggerLink/index.js +1 -1
  60. package/links/splitLink/index.d.ts +1 -1
  61. package/links/splitLink/index.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/links/wsLink/index.d.ts +1 -1
  66. package/links/wsLink/index.js +1 -1
  67. package/package.json +23 -30
  68. package/src/TRPCClientError.ts +5 -13
  69. package/src/createTRPCClient.ts +57 -3
  70. package/src/index.ts +1 -1
  71. package/src/internals/TRPCClient.ts +52 -36
  72. package/src/internals/dataLoader.ts +120 -45
  73. package/src/links/dedupeLink.test.ts +1 -1
  74. package/src/links/dedupeLink.ts +3 -5
  75. package/src/links/httpBatchLink.ts +64 -31
  76. package/src/links/httpLink.ts +4 -4
  77. package/src/links/index.ts +8 -0
  78. package/src/links/internals/createChain.test.ts +5 -5
  79. package/src/links/internals/createChain.ts +3 -3
  80. package/src/links/internals/httpUtils.ts +116 -0
  81. package/src/links/internals/transformOperationResult.ts +40 -12
  82. package/src/links/loggerLink.ts +1 -2
  83. package/src/links/retryLink.ts +1 -2
  84. package/src/links/splitLink.test.ts +3 -3
  85. package/src/links/splitLink.ts +5 -5
  86. package/src/links/types.ts +14 -9
  87. package/src/links/wsLink.ts +16 -14
  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/internals/transformRPCResponse.d.ts +0 -15
  93. package/dist/internals/transformRPCResponse.d.ts.map +0 -1
  94. package/dist/links/httpUtils.d.ts +0 -22
  95. package/dist/links/httpUtils.d.ts.map +0 -1
  96. package/dist/links/transformerLink.d.ts +0 -4
  97. package/dist/links/transformerLink.d.ts.map +0 -1
  98. package/dist/links/transformerLink.js +0 -75
  99. package/dist/links/transformerLink.mjs +0 -67
  100. package/dist/links/transformerLink.ts.js +0 -1
  101. package/dist/observable-7e66cb56.mjs +0 -102
  102. package/dist/observable-85669100.js +0 -104
  103. package/dist/rx/index.d.ts +0 -3
  104. package/dist/rx/index.d.ts.map +0 -1
  105. package/dist/rx/index.js +0 -9
  106. package/dist/rx/index.mjs +0 -1
  107. package/dist/rx/index.ts.js +0 -1
  108. package/dist/rx/observable.d.ts +0 -4
  109. package/dist/rx/observable.d.ts.map +0 -1
  110. package/dist/rx/operators/index.d.ts +0 -4
  111. package/dist/rx/operators/index.d.ts.map +0 -1
  112. package/dist/rx/operators/index.js +0 -38
  113. package/dist/rx/operators/index.mjs +0 -32
  114. package/dist/rx/operators/index.ts.js +0 -1
  115. package/dist/rx/operators/map.d.ts +0 -3
  116. package/dist/rx/operators/map.d.ts.map +0 -1
  117. package/dist/rx/operators/share.d.ts +0 -6
  118. package/dist/rx/operators/share.d.ts.map +0 -1
  119. package/dist/rx/operators/tap.d.ts +0 -3
  120. package/dist/rx/operators/tap.d.ts.map +0 -1
  121. package/dist/rx/types.d.ts +0 -29
  122. package/dist/rx/types.d.ts.map +0 -1
  123. package/dist/rx/util/identity.d.ts +0 -2
  124. package/dist/rx/util/identity.d.ts.map +0 -1
  125. package/dist/rx/util/observableToPromise.d.ts +0 -10
  126. package/dist/rx/util/observableToPromise.d.ts.map +0 -1
  127. package/dist/rx/util/pipe.d.ts +0 -4
  128. package/dist/rx/util/pipe.d.ts.map +0 -1
  129. package/dist/share-4d79dc49.js +0 -110
  130. package/dist/share-671b63fa.mjs +0 -108
  131. package/dist/tap-538fa07f.mjs +0 -30
  132. package/dist/tap-799a2d33.js +0 -32
  133. package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.d.ts +0 -1
  134. package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.dev.js +0 -67
  135. package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.js +0 -7
  136. package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.prod.js +0 -67
  137. package/links/transformerLink/dist/trpc-client-links-transformerLink.esm.js +0 -63
  138. package/links/transformerLink/index.d.ts +0 -1
  139. package/links/transformerLink/index.js +0 -1
  140. package/rx/dist/trpc-client-rx.cjs.d.ts +0 -1
  141. package/rx/dist/trpc-client-rx.cjs.dev.js +0 -9
  142. package/rx/dist/trpc-client-rx.cjs.js +0 -7
  143. package/rx/dist/trpc-client-rx.cjs.prod.js +0 -9
  144. package/rx/dist/trpc-client-rx.esm.js +0 -1
  145. package/rx/index.d.ts +0 -1
  146. package/rx/index.js +0 -1
  147. package/rx/operators/dist/trpc-client-rx-operators.cjs.d.ts +0 -1
  148. package/rx/operators/dist/trpc-client-rx-operators.cjs.dev.js +0 -39
  149. package/rx/operators/dist/trpc-client-rx-operators.cjs.js +0 -7
  150. package/rx/operators/dist/trpc-client-rx-operators.cjs.prod.js +0 -39
  151. package/rx/operators/dist/trpc-client-rx-operators.esm.js +0 -33
  152. package/rx/operators/index.d.ts +0 -1
  153. package/rx/operators/index.js +0 -1
  154. package/src/internals/transformRPCResponse.ts +0 -25
  155. package/src/links/httpUtils.ts +0 -94
  156. package/src/links/transformerLink.ts +0 -70
  157. package/src/rx/index.ts +0 -2
  158. package/src/rx/observable.test.ts +0 -83
  159. package/src/rx/observable.ts +0 -70
  160. package/src/rx/operators/index.ts +0 -3
  161. package/src/rx/operators/map.test.ts +0 -92
  162. package/src/rx/operators/map.ts +0 -25
  163. package/src/rx/operators/share.test.ts +0 -60
  164. package/src/rx/operators/share.ts +0 -67
  165. package/src/rx/operators/tap.ts +0 -26
  166. package/src/rx/types.ts +0 -69
  167. package/src/rx/util/identity.ts +0 -3
  168. package/src/rx/util/observableToPromise.ts +0 -49
  169. package/src/rx/util/pipe.ts +0 -22
@@ -2,8 +2,8 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var observable = require('../observable-85669100.js');
6
- var httpUtils = require('../httpUtils-1f9e4e3c.js');
5
+ var observable = require('@trpc/server/observable');
6
+ var httpUtils = require('../httpUtils-a0169eef.js');
7
7
  require('@babel/runtime/helpers/defineProperty');
8
8
 
9
9
  function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
@@ -12,76 +12,174 @@ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o =
12
12
 
13
13
  function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
14
14
 
15
+ /* eslint-disable @typescript-eslint/no-non-null-assertion */
16
+
17
+ /**
18
+ * A function that should never be called unless we messed something up.
19
+ */
20
+ var throwFatalError = function throwFatalError() {
21
+ throw new Error('Something went wrong. Please submit an issue at https://github.com/trpc/trpc/issues/new');
22
+ };
15
23
  /**
16
24
  * Dataloader that's very inspired by https://github.com/graphql/dataloader
17
25
  * Less configuration, no caching, and allows you to cancel requests
18
26
  * When cancelling a single fetch the whole batch will be cancelled only when _all_ items are cancelled
19
27
  */
20
- function dataLoader(fetchMany) {
21
- var batch = null;
28
+
29
+
30
+ function dataLoader(batchLoader) {
31
+ var pendingItems = null;
22
32
  var dispatchTimer = null;
23
33
 
24
- var destroyTimerAndBatch = function destroyTimerAndBatch() {
34
+ var destroyTimerAndPendingItems = function destroyTimerAndPendingItems() {
25
35
  clearTimeout(dispatchTimer);
26
36
  dispatchTimer = null;
27
- batch = null;
37
+ pendingItems = null;
28
38
  };
39
+ /**
40
+ * Iterate through the items and split them into groups based on the `batchLoader`'s validate function
41
+ */
29
42
 
30
- function dispatch() {
31
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
32
- var batchCopy = batch;
33
- destroyTimerAndBatch();
34
-
35
- var _fetchMany = fetchMany(batchCopy.items.map(function (v) {
36
- return v.key;
37
- })),
38
- promise = _fetchMany.promise,
39
- cancel = _fetchMany.cancel;
40
-
41
- batchCopy.cancel = cancel;
42
- promise.then(function (result) {
43
- for (var i = 0; i < result.length; i++) {
44
- var _value = result[i];
45
- batchCopy.items[i].resolve(_value);
43
+
44
+ function groupItems(items) {
45
+ var groupedItems = [[]];
46
+ var index = 0;
47
+
48
+ while (true) {
49
+ var item = items[index];
50
+
51
+ if (!item) {
52
+ // we're done
53
+ break;
46
54
  }
47
- }).catch(function (cause) {
48
- var _iterator = _createForOfIteratorHelper(batchCopy.items),
49
- _step;
50
-
51
- try {
52
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
53
- var item = _step.value;
54
- item.reject(cause);
55
- }
56
- } catch (err) {
57
- _iterator.e(err);
58
- } finally {
59
- _iterator.f();
55
+
56
+ var lastGroup = groupedItems[groupedItems.length - 1];
57
+
58
+ if (item.aborted) {
59
+ // Item was aborted before it was dispatched
60
+ item.reject(new Error('Aborted'));
61
+ index++;
62
+ continue;
60
63
  }
61
- });
64
+
65
+ var isValid = batchLoader.validate(lastGroup.concat(item).map(function (it) {
66
+ return it.key;
67
+ }));
68
+
69
+ if (isValid) {
70
+ lastGroup.push(item);
71
+ index++;
72
+ continue;
73
+ }
74
+
75
+ if (lastGroup.length === 0) {
76
+ item.reject(new Error('Input is too big for a single dispatch'));
77
+ index++;
78
+ continue;
79
+ } // Create new group, next iteration will try to add the item to that
80
+
81
+
82
+ groupedItems.push([]);
83
+ }
84
+
85
+ return groupedItems;
62
86
  }
63
87
 
64
- function load(key) {
65
- var batchItem = {
66
- cancelled: false,
67
- key: key
68
- };
88
+ function dispatch() {
89
+ var groupedItems = groupItems(pendingItems);
90
+ destroyTimerAndPendingItems(); // Create batches for each group of items
91
+
92
+ var _iterator = _createForOfIteratorHelper(groupedItems),
93
+ _step;
94
+
95
+ try {
96
+ var _loop = function _loop() {
97
+ var items = _step.value;
98
+
99
+ if (!items.length) {
100
+ return "continue";
101
+ }
102
+
103
+ var batch = {
104
+ items: items,
105
+ cancel: throwFatalError
106
+ };
107
+
108
+ var _iterator2 = _createForOfIteratorHelper(items),
109
+ _step2;
69
110
 
70
- if (!batch) {
71
- batch = {
72
- items: [],
73
- cancel: function cancel() {
74
- destroyTimerAndBatch();
111
+ try {
112
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
113
+ var item = _step2.value;
114
+ item.batch = batch;
115
+ }
116
+ } catch (err) {
117
+ _iterator2.e(err);
118
+ } finally {
119
+ _iterator2.f();
75
120
  }
121
+
122
+ var _batchLoader$fetch = batchLoader.fetch(batch.items.map(function (_item) {
123
+ return _item.key;
124
+ })),
125
+ promise = _batchLoader$fetch.promise,
126
+ cancel = _batchLoader$fetch.cancel;
127
+
128
+ batch.cancel = cancel;
129
+ promise.then(function (result) {
130
+ for (var i = 0; i < result.length; i++) {
131
+ var _value = result[i];
132
+ var item = batch.items[i];
133
+ item.resolve(_value);
134
+ item.batch = null;
135
+ }
136
+ }).catch(function (cause) {
137
+ var _iterator3 = _createForOfIteratorHelper(batch.items),
138
+ _step3;
139
+
140
+ try {
141
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
142
+ var item = _step3.value;
143
+ item.reject(cause);
144
+ item.batch = null;
145
+ }
146
+ } catch (err) {
147
+ _iterator3.e(err);
148
+ } finally {
149
+ _iterator3.f();
150
+ }
151
+ });
76
152
  };
153
+
154
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
155
+ var _ret = _loop();
156
+
157
+ if (_ret === "continue") continue;
158
+ }
159
+ } catch (err) {
160
+ _iterator.e(err);
161
+ } finally {
162
+ _iterator.f();
77
163
  }
164
+ }
78
165
 
79
- var thisBatch = batch;
166
+ function load(key) {
167
+ var item = {
168
+ aborted: false,
169
+ key: key,
170
+ batch: null,
171
+ resolve: throwFatalError,
172
+ reject: throwFatalError
173
+ };
80
174
  var promise = new Promise(function (resolve, reject) {
81
- var item = batchItem;
82
175
  item.reject = reject;
83
176
  item.resolve = resolve;
84
- thisBatch.items.push(item);
177
+
178
+ if (!pendingItems) {
179
+ pendingItems = [];
180
+ }
181
+
182
+ pendingItems.push(item);
85
183
  });
86
184
 
87
185
  if (!dispatchTimer) {
@@ -89,16 +187,17 @@ function dataLoader(fetchMany) {
89
187
  }
90
188
 
91
189
  var cancel = function cancel() {
92
- batchItem.cancelled = true;
190
+ var _item$batch;
191
+
192
+ item.aborted = true;
93
193
 
94
- if (thisBatch.items.some(function (item) {
95
- return !item.cancelled;
194
+ if ((_item$batch = item.batch) !== null && _item$batch !== void 0 && _item$batch.items.every(function (item) {
195
+ return item.aborted;
96
196
  })) {
97
- // there are still things that can be resolved
98
- return;
197
+ // All items in the batch have been cancelled
198
+ item.batch.cancel();
199
+ item.batch = null;
99
200
  }
100
-
101
- thisBatch.cancel();
102
201
  };
103
202
 
104
203
  return {
@@ -113,30 +212,54 @@ function dataLoader(fetchMany) {
113
212
  }
114
213
 
115
214
  function httpBatchLink(opts) {
116
- var url = opts.url;
215
+ // initialized config
117
216
  return function (runtime) {
118
- var fetcher = function fetcher(type) {
119
- return function (keyInputPairs) {
120
- var path = keyInputPairs.map(function (op) {
217
+ var maxURLLength = opts.maxURLLength || Infinity;
218
+
219
+ var batchLoader = function batchLoader(type) {
220
+ var validate = function validate(batchOps) {
221
+ if (maxURLLength === Infinity) {
222
+ // escape hatch for quick calcs
223
+ return true;
224
+ }
225
+
226
+ var path = batchOps.map(function (op) {
121
227
  return op.path;
122
228
  }).join(',');
123
- var inputs = keyInputPairs.map(function (op) {
229
+ var inputs = batchOps.map(function (op) {
230
+ return op.input;
231
+ });
232
+ var url = httpUtils.getUrl({
233
+ url: opts.url,
234
+ runtime: runtime,
235
+ type: type,
236
+ path: path,
237
+ inputs: inputs
238
+ });
239
+ return url.length <= maxURLLength;
240
+ };
241
+
242
+ var fetch = function fetch(batchOps) {
243
+ var path = batchOps.map(function (op) {
244
+ return op.path;
245
+ }).join(',');
246
+ var inputs = batchOps.map(function (op) {
124
247
  return op.input;
125
248
  });
126
249
 
127
250
  var _httpRequest = httpUtils.httpRequest({
128
- url: url,
129
- inputs: inputs,
130
- path: path,
251
+ url: opts.url,
131
252
  runtime: runtime,
132
- type: type
253
+ type: type,
254
+ path: path,
255
+ inputs: inputs
133
256
  }),
134
257
  promise = _httpRequest.promise,
135
258
  cancel = _httpRequest.cancel;
136
259
 
137
260
  return {
138
261
  promise: promise.then(function (res) {
139
- var resJSON = Array.isArray(res.json) ? res.json : keyInputPairs.map(function () {
262
+ var resJSON = Array.isArray(res.json) ? res.json : batchOps.map(function () {
140
263
  return res.json;
141
264
  });
142
265
  var result = resJSON.map(function (item) {
@@ -150,11 +273,16 @@ function httpBatchLink(opts) {
150
273
  cancel: cancel
151
274
  };
152
275
  };
276
+
277
+ return {
278
+ validate: validate,
279
+ fetch: fetch
280
+ };
153
281
  };
154
282
 
155
- var query = dataLoader(fetcher('query'));
156
- var mutation = dataLoader(fetcher('mutation'));
157
- var subscription = dataLoader(fetcher('subscription'));
283
+ var query = dataLoader(batchLoader('query'));
284
+ var mutation = dataLoader(batchLoader('mutation'));
285
+ var subscription = dataLoader(batchLoader('subscription'));
158
286
  var loaders = {
159
287
  query: query,
160
288
  subscription: subscription,
@@ -1,5 +1,5 @@
1
- import { o as observable } from '../observable-7e66cb56.mjs';
2
- import { h as httpRequest } from '../httpUtils-c09d7b79.mjs';
1
+ import { observable } from '@trpc/server/observable';
2
+ import { g as getUrl, h as httpRequest } from '../httpUtils-089853f6.mjs';
3
3
  import '@babel/runtime/helpers/defineProperty';
4
4
 
5
5
  function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
@@ -8,76 +8,174 @@ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o =
8
8
 
9
9
  function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
10
10
 
11
+ /* eslint-disable @typescript-eslint/no-non-null-assertion */
12
+
13
+ /**
14
+ * A function that should never be called unless we messed something up.
15
+ */
16
+ var throwFatalError = function throwFatalError() {
17
+ throw new Error('Something went wrong. Please submit an issue at https://github.com/trpc/trpc/issues/new');
18
+ };
11
19
  /**
12
20
  * Dataloader that's very inspired by https://github.com/graphql/dataloader
13
21
  * Less configuration, no caching, and allows you to cancel requests
14
22
  * When cancelling a single fetch the whole batch will be cancelled only when _all_ items are cancelled
15
23
  */
16
- function dataLoader(fetchMany) {
17
- var batch = null;
24
+
25
+
26
+ function dataLoader(batchLoader) {
27
+ var pendingItems = null;
18
28
  var dispatchTimer = null;
19
29
 
20
- var destroyTimerAndBatch = function destroyTimerAndBatch() {
30
+ var destroyTimerAndPendingItems = function destroyTimerAndPendingItems() {
21
31
  clearTimeout(dispatchTimer);
22
32
  dispatchTimer = null;
23
- batch = null;
33
+ pendingItems = null;
24
34
  };
35
+ /**
36
+ * Iterate through the items and split them into groups based on the `batchLoader`'s validate function
37
+ */
25
38
 
26
- function dispatch() {
27
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
28
- var batchCopy = batch;
29
- destroyTimerAndBatch();
30
-
31
- var _fetchMany = fetchMany(batchCopy.items.map(function (v) {
32
- return v.key;
33
- })),
34
- promise = _fetchMany.promise,
35
- cancel = _fetchMany.cancel;
36
-
37
- batchCopy.cancel = cancel;
38
- promise.then(function (result) {
39
- for (var i = 0; i < result.length; i++) {
40
- var _value = result[i];
41
- batchCopy.items[i].resolve(_value);
39
+
40
+ function groupItems(items) {
41
+ var groupedItems = [[]];
42
+ var index = 0;
43
+
44
+ while (true) {
45
+ var item = items[index];
46
+
47
+ if (!item) {
48
+ // we're done
49
+ break;
42
50
  }
43
- }).catch(function (cause) {
44
- var _iterator = _createForOfIteratorHelper(batchCopy.items),
45
- _step;
46
-
47
- try {
48
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
49
- var item = _step.value;
50
- item.reject(cause);
51
- }
52
- } catch (err) {
53
- _iterator.e(err);
54
- } finally {
55
- _iterator.f();
51
+
52
+ var lastGroup = groupedItems[groupedItems.length - 1];
53
+
54
+ if (item.aborted) {
55
+ // Item was aborted before it was dispatched
56
+ item.reject(new Error('Aborted'));
57
+ index++;
58
+ continue;
56
59
  }
57
- });
60
+
61
+ var isValid = batchLoader.validate(lastGroup.concat(item).map(function (it) {
62
+ return it.key;
63
+ }));
64
+
65
+ if (isValid) {
66
+ lastGroup.push(item);
67
+ index++;
68
+ continue;
69
+ }
70
+
71
+ if (lastGroup.length === 0) {
72
+ item.reject(new Error('Input is too big for a single dispatch'));
73
+ index++;
74
+ continue;
75
+ } // Create new group, next iteration will try to add the item to that
76
+
77
+
78
+ groupedItems.push([]);
79
+ }
80
+
81
+ return groupedItems;
58
82
  }
59
83
 
60
- function load(key) {
61
- var batchItem = {
62
- cancelled: false,
63
- key: key
64
- };
84
+ function dispatch() {
85
+ var groupedItems = groupItems(pendingItems);
86
+ destroyTimerAndPendingItems(); // Create batches for each group of items
87
+
88
+ var _iterator = _createForOfIteratorHelper(groupedItems),
89
+ _step;
90
+
91
+ try {
92
+ var _loop = function _loop() {
93
+ var items = _step.value;
94
+
95
+ if (!items.length) {
96
+ return "continue";
97
+ }
98
+
99
+ var batch = {
100
+ items: items,
101
+ cancel: throwFatalError
102
+ };
103
+
104
+ var _iterator2 = _createForOfIteratorHelper(items),
105
+ _step2;
65
106
 
66
- if (!batch) {
67
- batch = {
68
- items: [],
69
- cancel: function cancel() {
70
- destroyTimerAndBatch();
107
+ try {
108
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
109
+ var item = _step2.value;
110
+ item.batch = batch;
111
+ }
112
+ } catch (err) {
113
+ _iterator2.e(err);
114
+ } finally {
115
+ _iterator2.f();
71
116
  }
117
+
118
+ var _batchLoader$fetch = batchLoader.fetch(batch.items.map(function (_item) {
119
+ return _item.key;
120
+ })),
121
+ promise = _batchLoader$fetch.promise,
122
+ cancel = _batchLoader$fetch.cancel;
123
+
124
+ batch.cancel = cancel;
125
+ promise.then(function (result) {
126
+ for (var i = 0; i < result.length; i++) {
127
+ var _value = result[i];
128
+ var item = batch.items[i];
129
+ item.resolve(_value);
130
+ item.batch = null;
131
+ }
132
+ }).catch(function (cause) {
133
+ var _iterator3 = _createForOfIteratorHelper(batch.items),
134
+ _step3;
135
+
136
+ try {
137
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
138
+ var item = _step3.value;
139
+ item.reject(cause);
140
+ item.batch = null;
141
+ }
142
+ } catch (err) {
143
+ _iterator3.e(err);
144
+ } finally {
145
+ _iterator3.f();
146
+ }
147
+ });
72
148
  };
149
+
150
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
151
+ var _ret = _loop();
152
+
153
+ if (_ret === "continue") continue;
154
+ }
155
+ } catch (err) {
156
+ _iterator.e(err);
157
+ } finally {
158
+ _iterator.f();
73
159
  }
160
+ }
74
161
 
75
- var thisBatch = batch;
162
+ function load(key) {
163
+ var item = {
164
+ aborted: false,
165
+ key: key,
166
+ batch: null,
167
+ resolve: throwFatalError,
168
+ reject: throwFatalError
169
+ };
76
170
  var promise = new Promise(function (resolve, reject) {
77
- var item = batchItem;
78
171
  item.reject = reject;
79
172
  item.resolve = resolve;
80
- thisBatch.items.push(item);
173
+
174
+ if (!pendingItems) {
175
+ pendingItems = [];
176
+ }
177
+
178
+ pendingItems.push(item);
81
179
  });
82
180
 
83
181
  if (!dispatchTimer) {
@@ -85,16 +183,17 @@ function dataLoader(fetchMany) {
85
183
  }
86
184
 
87
185
  var cancel = function cancel() {
88
- batchItem.cancelled = true;
186
+ var _item$batch;
187
+
188
+ item.aborted = true;
89
189
 
90
- if (thisBatch.items.some(function (item) {
91
- return !item.cancelled;
190
+ if ((_item$batch = item.batch) !== null && _item$batch !== void 0 && _item$batch.items.every(function (item) {
191
+ return item.aborted;
92
192
  })) {
93
- // there are still things that can be resolved
94
- return;
193
+ // All items in the batch have been cancelled
194
+ item.batch.cancel();
195
+ item.batch = null;
95
196
  }
96
-
97
- thisBatch.cancel();
98
197
  };
99
198
 
100
199
  return {
@@ -109,30 +208,54 @@ function dataLoader(fetchMany) {
109
208
  }
110
209
 
111
210
  function httpBatchLink(opts) {
112
- var url = opts.url;
211
+ // initialized config
113
212
  return function (runtime) {
114
- var fetcher = function fetcher(type) {
115
- return function (keyInputPairs) {
116
- var path = keyInputPairs.map(function (op) {
213
+ var maxURLLength = opts.maxURLLength || Infinity;
214
+
215
+ var batchLoader = function batchLoader(type) {
216
+ var validate = function validate(batchOps) {
217
+ if (maxURLLength === Infinity) {
218
+ // escape hatch for quick calcs
219
+ return true;
220
+ }
221
+
222
+ var path = batchOps.map(function (op) {
117
223
  return op.path;
118
224
  }).join(',');
119
- var inputs = keyInputPairs.map(function (op) {
225
+ var inputs = batchOps.map(function (op) {
226
+ return op.input;
227
+ });
228
+ var url = getUrl({
229
+ url: opts.url,
230
+ runtime: runtime,
231
+ type: type,
232
+ path: path,
233
+ inputs: inputs
234
+ });
235
+ return url.length <= maxURLLength;
236
+ };
237
+
238
+ var fetch = function fetch(batchOps) {
239
+ var path = batchOps.map(function (op) {
240
+ return op.path;
241
+ }).join(',');
242
+ var inputs = batchOps.map(function (op) {
120
243
  return op.input;
121
244
  });
122
245
 
123
246
  var _httpRequest = httpRequest({
124
- url: url,
125
- inputs: inputs,
126
- path: path,
247
+ url: opts.url,
127
248
  runtime: runtime,
128
- type: type
249
+ type: type,
250
+ path: path,
251
+ inputs: inputs
129
252
  }),
130
253
  promise = _httpRequest.promise,
131
254
  cancel = _httpRequest.cancel;
132
255
 
133
256
  return {
134
257
  promise: promise.then(function (res) {
135
- var resJSON = Array.isArray(res.json) ? res.json : keyInputPairs.map(function () {
258
+ var resJSON = Array.isArray(res.json) ? res.json : batchOps.map(function () {
136
259
  return res.json;
137
260
  });
138
261
  var result = resJSON.map(function (item) {
@@ -146,11 +269,16 @@ function httpBatchLink(opts) {
146
269
  cancel: cancel
147
270
  };
148
271
  };
272
+
273
+ return {
274
+ validate: validate,
275
+ fetch: fetch
276
+ };
149
277
  };
150
278
 
151
- var query = dataLoader(fetcher('query'));
152
- var mutation = dataLoader(fetcher('mutation'));
153
- var subscription = dataLoader(fetcher('subscription'));
279
+ var query = dataLoader(batchLoader('query'));
280
+ var mutation = dataLoader(batchLoader('mutation'));
281
+ var subscription = dataLoader(batchLoader('subscription'));
154
282
  var loaders = {
155
283
  query: query,
156
284
  subscription: subscription,
@@ -1,5 +1,5 @@
1
1
  import { AnyRouter } from '@trpc/server';
2
+ import { HTTPLinkOptions } from './internals/httpUtils';
2
3
  import { TRPCLink } from './types';
3
- import { HTTPLinkOptions } from './httpUtils';
4
4
  export declare function httpLink<TRouter extends AnyRouter>(opts: HTTPLinkOptions): TRPCLink<TRouter>;
5
5
  //# sourceMappingURL=httpLink.d.ts.map