@trpc/client 10.0.0-alpha.11 → 10.0.0-alpha.14

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 (151) hide show
  1. package/dist/{httpUtils-1f9e4e3c.js → httpUtils-27640b42.js} +0 -0
  2. package/dist/{httpUtils-c09d7b79.mjs → httpUtils-b92552af.mjs} +0 -0
  3. package/dist/index.d.ts +1 -1
  4. package/dist/index.d.ts.map +1 -1
  5. package/dist/index.js +215 -17
  6. package/dist/index.mjs +198 -12
  7. package/dist/internals/TRPCClient.d.ts +4 -2
  8. package/dist/internals/TRPCClient.d.ts.map +1 -1
  9. package/dist/links/dedupeLink.d.ts.map +1 -1
  10. package/dist/links/httpBatchLink.d.ts +1 -1
  11. package/dist/links/httpBatchLink.d.ts.map +1 -1
  12. package/dist/links/httpBatchLink.js +3 -3
  13. package/dist/links/httpBatchLink.mjs +3 -3
  14. package/dist/links/httpLink.d.ts +1 -1
  15. package/dist/links/httpLink.d.ts.map +1 -1
  16. package/dist/links/httpLink.js +3 -3
  17. package/dist/links/httpLink.mjs +3 -3
  18. package/dist/links/index.d.ts +11 -0
  19. package/dist/links/index.d.ts.map +1 -0
  20. package/dist/links/{httpUtils.d.ts → internals/httpUtils.d.ts} +1 -1
  21. package/dist/links/internals/httpUtils.d.ts.map +1 -0
  22. package/dist/links/loggerLink.d.ts.map +1 -1
  23. package/dist/links/loggerLink.js +4 -3
  24. package/dist/links/loggerLink.mjs +3 -2
  25. package/dist/links/retryLink.d.ts.map +1 -1
  26. package/dist/links/splitLink.js +3 -25
  27. package/dist/links/splitLink.mjs +2 -28
  28. package/dist/links/transformerLink.js +2 -2
  29. package/dist/links/transformerLink.mjs +2 -2
  30. package/dist/links/types.d.ts +4 -3
  31. package/dist/links/types.d.ts.map +1 -1
  32. package/dist/links/wsLink.d.ts +1 -2
  33. package/dist/links/wsLink.d.ts.map +1 -1
  34. package/dist/links/wsLink.js +11 -377
  35. package/dist/links/wsLink.mjs +9 -369
  36. package/dist/observable/index.d.ts +4 -0
  37. package/dist/observable/index.d.ts.map +1 -0
  38. package/dist/observable/index.js +40 -0
  39. package/dist/observable/index.mjs +33 -0
  40. package/dist/{rx.ts.js → observable/index.ts.js} +0 -0
  41. package/dist/observable/internals/identity.d.ts +2 -0
  42. package/dist/observable/internals/identity.d.ts.map +1 -0
  43. package/dist/observable/internals/observableToPromise.d.ts +10 -0
  44. package/dist/observable/internals/observableToPromise.d.ts.map +1 -0
  45. package/dist/observable/internals/pipe.d.ts +4 -0
  46. package/dist/observable/internals/pipe.d.ts.map +1 -0
  47. package/dist/observable/observable.d.ts +4 -0
  48. package/dist/observable/observable.d.ts.map +1 -0
  49. package/dist/observable/operators/index.d.ts +4 -0
  50. package/dist/observable/operators/index.d.ts.map +1 -0
  51. package/dist/observable/operators/map.d.ts +3 -0
  52. package/dist/observable/operators/map.d.ts.map +1 -0
  53. package/dist/observable/operators/share.d.ts +6 -0
  54. package/dist/observable/operators/share.d.ts.map +1 -0
  55. package/dist/observable/operators/tap.d.ts +3 -0
  56. package/dist/observable/operators/tap.d.ts.map +1 -0
  57. package/dist/observable/types.d.ts +29 -0
  58. package/dist/observable/types.d.ts.map +1 -0
  59. package/dist/observable-16fde241.mjs +102 -0
  60. package/dist/observable-9aaca086.js +104 -0
  61. package/dist/share-4afc084f.js +110 -0
  62. package/dist/share-9f12d26c.mjs +108 -0
  63. package/dist/splitLink-8635d455.js +52 -0
  64. package/dist/splitLink-a736378a.mjs +49 -0
  65. package/dist/tap-aacf31bf.js +32 -0
  66. package/dist/tap-f567ea97.mjs +30 -0
  67. package/dist/wsLink-a6a0f3a8.mjs +440 -0
  68. package/dist/wsLink-c9323097.js +455 -0
  69. package/package.json +11 -41
  70. package/src/index.ts +1 -1
  71. package/src/internals/TRPCClient.ts +8 -4
  72. package/src/links/dedupeLink.test.ts +3 -3
  73. package/src/links/dedupeLink.ts +27 -26
  74. package/src/links/httpBatchLink.ts +7 -3
  75. package/src/links/httpLink.ts +3 -3
  76. package/src/links/index.ts +10 -0
  77. package/src/links/internals/createChain.test.ts +7 -7
  78. package/src/links/internals/createChain.ts +4 -4
  79. package/src/links/{httpUtils.ts → internals/httpUtils.ts} +1 -1
  80. package/src/links/loggerLink.ts +3 -2
  81. package/src/links/retryLink.ts +3 -2
  82. package/src/links/splitLink.test.ts +3 -3
  83. package/src/links/splitLink.ts +2 -2
  84. package/src/links/transformerLink.ts +2 -2
  85. package/src/links/types.ts +4 -3
  86. package/src/links/wsLink.ts +3 -4
  87. package/src/observable/index.ts +3 -0
  88. package/src/observable/internals/identity.ts +3 -0
  89. package/src/observable/internals/observableToPromise.ts +49 -0
  90. package/src/observable/internals/pipe.ts +22 -0
  91. package/src/observable/observable.test.ts +103 -0
  92. package/src/observable/observable.ts +70 -0
  93. package/src/observable/operators/index.ts +3 -0
  94. package/src/observable/operators/map.test.ts +92 -0
  95. package/src/observable/operators/map.ts +25 -0
  96. package/src/observable/operators/share.test.ts +60 -0
  97. package/src/observable/operators/share.ts +67 -0
  98. package/src/observable/operators/tap.ts +26 -0
  99. package/src/observable/types.ts +69 -0
  100. package/dist/TRPCClientError-17b117e1.mjs +0 -81
  101. package/dist/TRPCClientError-ec6bd2ca.js +0 -94
  102. package/dist/createChain-3b458753.js +0 -27
  103. package/dist/createChain-b7cb1f5a.mjs +0 -25
  104. package/dist/links/httpUtils.d.ts.map +0 -1
  105. package/dist/rx.d.ts +0 -12
  106. package/dist/rx.d.ts.map +0 -1
  107. package/dist/rx.js +0 -130
  108. package/dist/rx.mjs +0 -114
  109. package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.cjs.d.ts +0 -1
  110. package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.cjs.dev.js +0 -214
  111. package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.cjs.js +0 -7
  112. package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.cjs.prod.js +0 -214
  113. package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.esm.js +0 -210
  114. package/links/httpBatchLink/index.d.ts +0 -1
  115. package/links/httpBatchLink/index.js +0 -1
  116. package/links/httpLink/dist/trpc-client-links-httpLink.cjs.d.ts +0 -1
  117. package/links/httpLink/dist/trpc-client-links-httpLink.cjs.dev.js +0 -67
  118. package/links/httpLink/dist/trpc-client-links-httpLink.cjs.js +0 -7
  119. package/links/httpLink/dist/trpc-client-links-httpLink.cjs.prod.js +0 -67
  120. package/links/httpLink/dist/trpc-client-links-httpLink.esm.js +0 -63
  121. package/links/httpLink/index.d.ts +0 -1
  122. package/links/httpLink/index.js +0 -1
  123. package/links/loggerLink/dist/trpc-client-links-loggerLink.cjs.d.ts +0 -1
  124. package/links/loggerLink/dist/trpc-client-links-loggerLink.cjs.dev.js +0 -89
  125. package/links/loggerLink/dist/trpc-client-links-loggerLink.cjs.js +0 -7
  126. package/links/loggerLink/dist/trpc-client-links-loggerLink.cjs.prod.js +0 -89
  127. package/links/loggerLink/dist/trpc-client-links-loggerLink.esm.js +0 -85
  128. package/links/loggerLink/index.d.ts +0 -1
  129. package/links/loggerLink/index.js +0 -1
  130. package/links/splitLink/dist/trpc-client-links-splitLink.cjs.d.ts +0 -1
  131. package/links/splitLink/dist/trpc-client-links-splitLink.cjs.dev.js +0 -19
  132. package/links/splitLink/dist/trpc-client-links-splitLink.cjs.js +0 -7
  133. package/links/splitLink/dist/trpc-client-links-splitLink.cjs.prod.js +0 -19
  134. package/links/splitLink/dist/trpc-client-links-splitLink.esm.js +0 -15
  135. package/links/splitLink/index.d.ts +0 -1
  136. package/links/splitLink/index.js +0 -1
  137. package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.d.ts +0 -1
  138. package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.dev.js +0 -67
  139. package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.js +0 -7
  140. package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.prod.js +0 -67
  141. package/links/transformerLink/dist/trpc-client-links-transformerLink.esm.js +0 -63
  142. package/links/transformerLink/index.d.ts +0 -1
  143. package/links/transformerLink/index.js +0 -1
  144. package/links/wsLink/dist/trpc-client-links-wsLink.cjs.d.ts +0 -1
  145. package/links/wsLink/dist/trpc-client-links-wsLink.cjs.dev.js +0 -395
  146. package/links/wsLink/dist/trpc-client-links-wsLink.cjs.js +0 -7
  147. package/links/wsLink/dist/trpc-client-links-wsLink.cjs.prod.js +0 -395
  148. package/links/wsLink/dist/trpc-client-links-wsLink.esm.js +0 -390
  149. package/links/wsLink/index.d.ts +0 -1
  150. package/links/wsLink/index.js +0 -1
  151. package/src/rx.ts +0 -82
package/dist/rx.mjs DELETED
@@ -1,114 +0,0 @@
1
- import _createClass from '@babel/runtime/helpers/createClass';
2
- import _classCallCheck from '@babel/runtime/helpers/classCallCheck';
3
- import _assertThisInitialized from '@babel/runtime/helpers/assertThisInitialized';
4
- import _inherits from '@babel/runtime/helpers/inherits';
5
- import _possibleConstructorReturn from '@babel/runtime/helpers/possibleConstructorReturn';
6
- import _getPrototypeOf from '@babel/runtime/helpers/getPrototypeOf';
7
- import _wrapNativeSuper from '@babel/runtime/helpers/wrapNativeSuper';
8
- import { Observable } from 'rxjs';
9
-
10
- function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
11
-
12
- 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; } }
13
- var ObservableAbortError = /*#__PURE__*/function (_Error) {
14
- _inherits(ObservableAbortError, _Error);
15
-
16
- var _super = /*#__PURE__*/_createSuper(ObservableAbortError);
17
-
18
- function ObservableAbortError(message) {
19
- var _this;
20
-
21
- _classCallCheck(this, ObservableAbortError);
22
-
23
- _this = _super.call(this, message);
24
- _this.name = 'ObservableAbortError';
25
- Object.setPrototypeOf(_assertThisInitialized(_this), ObservableAbortError.prototype);
26
- return _this;
27
- }
28
-
29
- return _createClass(ObservableAbortError);
30
- }( /*#__PURE__*/_wrapNativeSuper(Error));
31
- /** @internal */
32
-
33
- function observableToPromise(observable) {
34
- var abort;
35
- var promise = new Promise(function (resolve, reject) {
36
- var isDone = false;
37
-
38
- function onDone() {
39
- if (isDone) {
40
- return;
41
- }
42
-
43
- isDone = true;
44
- reject(new ObservableAbortError('This operation was cancelled.'));
45
- subscription.unsubscribe();
46
- }
47
-
48
- var subscription = observable.subscribe({
49
- next: function next(data) {
50
- isDone = true;
51
- resolve(data);
52
- onDone();
53
- },
54
- error: function error(data) {
55
- isDone = true;
56
- reject(data);
57
- onDone();
58
- },
59
- complete: function complete() {
60
- isDone = true;
61
- onDone();
62
- }
63
- });
64
- abort = onDone;
65
- });
66
- return {
67
- promise: promise,
68
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
69
- abort: abort
70
- };
71
- }
72
- function share() {
73
- var subscribers = new Set();
74
- var connection = null;
75
- return function (source) {
76
- return new Observable(function (subscriber) {
77
- subscribers.add(subscriber);
78
- subscriber.add(function () {
79
- subscribers.delete(subscriber);
80
-
81
- if (subscribers.size === 0) {
82
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
83
- connection.unsubscribe();
84
- connection = null;
85
- }
86
- });
87
-
88
- if (!connection) {
89
- connection = source.subscribe({
90
- next: function next(value) {
91
- return subscribers.forEach(function (s) {
92
- return s.next(value);
93
- });
94
- },
95
- error: function error(_error) {
96
- return subscribers.forEach(function (s) {
97
- return s.error(_error);
98
- });
99
- },
100
- complete: function complete() {
101
- return subscribers.forEach(function (s) {
102
- return s.complete();
103
- });
104
- }
105
- });
106
- connection.add(function () {
107
- return subscribers.clear();
108
- });
109
- }
110
- });
111
- };
112
- }
113
-
114
- export { ObservableAbortError, observableToPromise, share };
@@ -1 +0,0 @@
1
- export * from "../../../dist/declarations/src/links/httpBatchLink";
@@ -1,214 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var _createForOfIteratorHelper = require('@babel/runtime/helpers/createForOfIteratorHelper');
6
- var transformRPCResponse = require('../../../dist/transformRPCResponse-57e80136.cjs.dev.js');
7
- var TRPCClientError = require('../../../dist/TRPCClientError-bfee2bf5.cjs.dev.js');
8
- require('@babel/runtime/helpers/createClass');
9
- require('@babel/runtime/helpers/classCallCheck');
10
- require('@babel/runtime/helpers/assertThisInitialized');
11
- require('@babel/runtime/helpers/inherits');
12
- require('@babel/runtime/helpers/createSuper');
13
- require('@babel/runtime/helpers/wrapNativeSuper');
14
- require('@babel/runtime/helpers/objectSpread2');
15
-
16
- /**
17
- * Dataloader that's very inspired by https://github.com/graphql/dataloader
18
- * Less configuration, no caching, and allows you to cancel requests
19
- * When cancelling a single fetch the whole batch will be cancelled only when _all_ items are cancelled
20
- */
21
- function dataLoader(fetchMany, opts) {
22
- var batch = null;
23
- var dispatchTimer = null;
24
-
25
- var destroyTimerAndBatch = function destroyTimerAndBatch() {
26
- clearTimeout(dispatchTimer);
27
- dispatchTimer = null;
28
- batch = null;
29
- };
30
-
31
- function dispatch() {
32
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
33
- var batchCopy = batch;
34
- destroyTimerAndBatch();
35
-
36
- var _fetchMany = fetchMany(batchCopy.items.map(function (v) {
37
- return v.key;
38
- })),
39
- promise = _fetchMany.promise,
40
- cancel = _fetchMany.cancel;
41
-
42
- batchCopy.cancel = cancel;
43
- promise.then(function (result) {
44
- for (var i = 0; i < result.length; i++) {
45
- var _value = result[i];
46
- batchCopy.items[i].resolve(_value);
47
- }
48
- }).catch(function (cause) {
49
- var _iterator = _createForOfIteratorHelper(batchCopy.items),
50
- _step;
51
-
52
- try {
53
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
54
- var item = _step.value;
55
- item.reject(cause);
56
- }
57
- } catch (err) {
58
- _iterator.e(err);
59
- } finally {
60
- _iterator.f();
61
- }
62
- });
63
- }
64
-
65
- function load(key) {
66
- var batchItem = {
67
- cancelled: false,
68
- key: key
69
- };
70
-
71
- if (!batch) {
72
- batch = {
73
- items: [],
74
- cancel: function cancel() {
75
- destroyTimerAndBatch();
76
- }
77
- };
78
- }
79
-
80
- var thisBatch = batch;
81
- var promise = new Promise(function (resolve, reject) {
82
- var item = batchItem;
83
- item.reject = reject;
84
- item.resolve = resolve;
85
- thisBatch.items.push(item);
86
-
87
- if (typeof (opts === null || opts === void 0 ? void 0 : opts.maxBatchSize) !== 'undefined' && thisBatch.items.length >= opts.maxBatchSize) {
88
- dispatch();
89
- return;
90
- }
91
- });
92
-
93
- if (!dispatchTimer) {
94
- dispatchTimer = setTimeout(dispatch);
95
- }
96
-
97
- var cancel = function cancel() {
98
- batchItem.cancelled = true;
99
-
100
- if (thisBatch.items.some(function (item) {
101
- return !item.cancelled;
102
- })) {
103
- // there are still things that can be resolved
104
- return;
105
- }
106
-
107
- thisBatch.cancel();
108
- };
109
-
110
- return {
111
- promise: promise,
112
- cancel: cancel
113
- };
114
- }
115
-
116
- return {
117
- load: load
118
- };
119
- }
120
-
121
- function httpBatchLink(opts) {
122
- var url = opts.url,
123
- maxBatchSize = opts.maxBatchSize; // initialized config
124
-
125
- return function (runtime) {
126
- // initialized in app
127
- var fetcher = function fetcher(type) {
128
- return function (keyInputPairs) {
129
- var path = keyInputPairs.map(function (op) {
130
- return op.path;
131
- }).join(',');
132
- var inputs = keyInputPairs.map(function (op) {
133
- return op.input;
134
- });
135
-
136
- var _httpRequest = transformRPCResponse.httpRequest({
137
- url: url,
138
- inputs: inputs,
139
- path: path,
140
- runtime: runtime,
141
- type: type
142
- }),
143
- promise = _httpRequest.promise,
144
- cancel = _httpRequest.cancel;
145
-
146
- return {
147
- promise: promise.then(function (res) {
148
- if (!Array.isArray(res)) {
149
- return keyInputPairs.map(function () {
150
- return res;
151
- });
152
- }
153
-
154
- return res;
155
- }),
156
- cancel: cancel
157
- };
158
- };
159
- };
160
-
161
- var query = dataLoader(fetcher('query'), {
162
- maxBatchSize: maxBatchSize
163
- });
164
- var mutation = dataLoader(fetcher('mutation'), {
165
- maxBatchSize: maxBatchSize
166
- });
167
- var subscription = dataLoader(fetcher('subscription'), {
168
- maxBatchSize: maxBatchSize
169
- });
170
- var loaders = {
171
- query: query,
172
- subscription: subscription,
173
- mutation: mutation
174
- };
175
- return function (_ref) {
176
- var op = _ref.op,
177
- prev = _ref.prev,
178
- onDestroy = _ref.onDestroy;
179
- var loader = loaders[op.type];
180
-
181
- var _loader$load = loader.load(op),
182
- promise = _loader$load.promise,
183
- cancel = _loader$load.cancel;
184
-
185
- var isDone = false;
186
-
187
- var prevOnce = function prevOnce(result) {
188
- if (isDone) {
189
- return;
190
- }
191
-
192
- isDone = true;
193
- prev(result);
194
- };
195
-
196
- onDestroy(function () {
197
- prevOnce(TRPCClientError.TRPCClientError.from(new transformRPCResponse.TRPCAbortError(), {
198
- isDone: true
199
- }));
200
- cancel();
201
- });
202
- promise.then(function (envelope) {
203
- prevOnce(transformRPCResponse.transformRPCResponse({
204
- envelope: envelope,
205
- runtime: runtime
206
- }));
207
- }).catch(function (cause) {
208
- prevOnce(TRPCClientError.TRPCClientError.from(cause));
209
- });
210
- };
211
- };
212
- }
213
-
214
- exports.httpBatchLink = httpBatchLink;
@@ -1,7 +0,0 @@
1
- 'use strict';
2
-
3
- if (process.env.NODE_ENV === "production") {
4
- module.exports = require("./trpc-client-links-httpBatchLink.cjs.prod.js");
5
- } else {
6
- module.exports = require("./trpc-client-links-httpBatchLink.cjs.dev.js");
7
- }
@@ -1,214 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var _createForOfIteratorHelper = require('@babel/runtime/helpers/createForOfIteratorHelper');
6
- var transformRPCResponse = require('../../../dist/transformRPCResponse-739c13d0.cjs.prod.js');
7
- var TRPCClientError = require('../../../dist/TRPCClientError-e9bf96e9.cjs.prod.js');
8
- require('@babel/runtime/helpers/createClass');
9
- require('@babel/runtime/helpers/classCallCheck');
10
- require('@babel/runtime/helpers/assertThisInitialized');
11
- require('@babel/runtime/helpers/inherits');
12
- require('@babel/runtime/helpers/createSuper');
13
- require('@babel/runtime/helpers/wrapNativeSuper');
14
- require('@babel/runtime/helpers/objectSpread2');
15
-
16
- /**
17
- * Dataloader that's very inspired by https://github.com/graphql/dataloader
18
- * Less configuration, no caching, and allows you to cancel requests
19
- * When cancelling a single fetch the whole batch will be cancelled only when _all_ items are cancelled
20
- */
21
- function dataLoader(fetchMany, opts) {
22
- var batch = null;
23
- var dispatchTimer = null;
24
-
25
- var destroyTimerAndBatch = function destroyTimerAndBatch() {
26
- clearTimeout(dispatchTimer);
27
- dispatchTimer = null;
28
- batch = null;
29
- };
30
-
31
- function dispatch() {
32
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
33
- var batchCopy = batch;
34
- destroyTimerAndBatch();
35
-
36
- var _fetchMany = fetchMany(batchCopy.items.map(function (v) {
37
- return v.key;
38
- })),
39
- promise = _fetchMany.promise,
40
- cancel = _fetchMany.cancel;
41
-
42
- batchCopy.cancel = cancel;
43
- promise.then(function (result) {
44
- for (var i = 0; i < result.length; i++) {
45
- var _value = result[i];
46
- batchCopy.items[i].resolve(_value);
47
- }
48
- }).catch(function (cause) {
49
- var _iterator = _createForOfIteratorHelper(batchCopy.items),
50
- _step;
51
-
52
- try {
53
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
54
- var item = _step.value;
55
- item.reject(cause);
56
- }
57
- } catch (err) {
58
- _iterator.e(err);
59
- } finally {
60
- _iterator.f();
61
- }
62
- });
63
- }
64
-
65
- function load(key) {
66
- var batchItem = {
67
- cancelled: false,
68
- key: key
69
- };
70
-
71
- if (!batch) {
72
- batch = {
73
- items: [],
74
- cancel: function cancel() {
75
- destroyTimerAndBatch();
76
- }
77
- };
78
- }
79
-
80
- var thisBatch = batch;
81
- var promise = new Promise(function (resolve, reject) {
82
- var item = batchItem;
83
- item.reject = reject;
84
- item.resolve = resolve;
85
- thisBatch.items.push(item);
86
-
87
- if (typeof (opts === null || opts === void 0 ? void 0 : opts.maxBatchSize) !== 'undefined' && thisBatch.items.length >= opts.maxBatchSize) {
88
- dispatch();
89
- return;
90
- }
91
- });
92
-
93
- if (!dispatchTimer) {
94
- dispatchTimer = setTimeout(dispatch);
95
- }
96
-
97
- var cancel = function cancel() {
98
- batchItem.cancelled = true;
99
-
100
- if (thisBatch.items.some(function (item) {
101
- return !item.cancelled;
102
- })) {
103
- // there are still things that can be resolved
104
- return;
105
- }
106
-
107
- thisBatch.cancel();
108
- };
109
-
110
- return {
111
- promise: promise,
112
- cancel: cancel
113
- };
114
- }
115
-
116
- return {
117
- load: load
118
- };
119
- }
120
-
121
- function httpBatchLink(opts) {
122
- var url = opts.url,
123
- maxBatchSize = opts.maxBatchSize; // initialized config
124
-
125
- return function (runtime) {
126
- // initialized in app
127
- var fetcher = function fetcher(type) {
128
- return function (keyInputPairs) {
129
- var path = keyInputPairs.map(function (op) {
130
- return op.path;
131
- }).join(',');
132
- var inputs = keyInputPairs.map(function (op) {
133
- return op.input;
134
- });
135
-
136
- var _httpRequest = transformRPCResponse.httpRequest({
137
- url: url,
138
- inputs: inputs,
139
- path: path,
140
- runtime: runtime,
141
- type: type
142
- }),
143
- promise = _httpRequest.promise,
144
- cancel = _httpRequest.cancel;
145
-
146
- return {
147
- promise: promise.then(function (res) {
148
- if (!Array.isArray(res)) {
149
- return keyInputPairs.map(function () {
150
- return res;
151
- });
152
- }
153
-
154
- return res;
155
- }),
156
- cancel: cancel
157
- };
158
- };
159
- };
160
-
161
- var query = dataLoader(fetcher('query'), {
162
- maxBatchSize: maxBatchSize
163
- });
164
- var mutation = dataLoader(fetcher('mutation'), {
165
- maxBatchSize: maxBatchSize
166
- });
167
- var subscription = dataLoader(fetcher('subscription'), {
168
- maxBatchSize: maxBatchSize
169
- });
170
- var loaders = {
171
- query: query,
172
- subscription: subscription,
173
- mutation: mutation
174
- };
175
- return function (_ref) {
176
- var op = _ref.op,
177
- prev = _ref.prev,
178
- onDestroy = _ref.onDestroy;
179
- var loader = loaders[op.type];
180
-
181
- var _loader$load = loader.load(op),
182
- promise = _loader$load.promise,
183
- cancel = _loader$load.cancel;
184
-
185
- var isDone = false;
186
-
187
- var prevOnce = function prevOnce(result) {
188
- if (isDone) {
189
- return;
190
- }
191
-
192
- isDone = true;
193
- prev(result);
194
- };
195
-
196
- onDestroy(function () {
197
- prevOnce(TRPCClientError.TRPCClientError.from(new transformRPCResponse.TRPCAbortError(), {
198
- isDone: true
199
- }));
200
- cancel();
201
- });
202
- promise.then(function (envelope) {
203
- prevOnce(transformRPCResponse.transformRPCResponse({
204
- envelope: envelope,
205
- runtime: runtime
206
- }));
207
- }).catch(function (cause) {
208
- prevOnce(TRPCClientError.TRPCClientError.from(cause));
209
- });
210
- };
211
- };
212
- }
213
-
214
- exports.httpBatchLink = httpBatchLink;