@trpc/client 10.0.0-alpha.10 → 10.0.0-alpha.13

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 (150) 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 -18
  6. package/dist/index.mjs +198 -13
  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/observable.d.ts +4 -0
  42. package/dist/observable/observable.d.ts.map +1 -0
  43. package/dist/observable/operators/index.d.ts +4 -0
  44. package/dist/observable/operators/index.d.ts.map +1 -0
  45. package/dist/observable/operators/map.d.ts +3 -0
  46. package/dist/observable/operators/map.d.ts.map +1 -0
  47. package/dist/observable/operators/share.d.ts +6 -0
  48. package/dist/observable/operators/share.d.ts.map +1 -0
  49. package/dist/observable/operators/tap.d.ts +3 -0
  50. package/dist/observable/operators/tap.d.ts.map +1 -0
  51. package/dist/observable/types.d.ts +29 -0
  52. package/dist/observable/types.d.ts.map +1 -0
  53. package/dist/observable/util/identity.d.ts +2 -0
  54. package/dist/observable/util/identity.d.ts.map +1 -0
  55. package/dist/observable/util/observableToPromise.d.ts +10 -0
  56. package/dist/observable/util/observableToPromise.d.ts.map +1 -0
  57. package/dist/observable/util/pipe.d.ts +4 -0
  58. package/dist/observable/util/pipe.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 -5
  72. package/src/links/dedupeLink.test.ts +3 -3
  73. package/src/links/dedupeLink.ts +27 -28
  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/observable.test.ts +103 -0
  89. package/src/observable/observable.ts +70 -0
  90. package/src/observable/operators/index.ts +3 -0
  91. package/src/observable/operators/map.test.ts +92 -0
  92. package/src/observable/operators/map.ts +25 -0
  93. package/src/observable/operators/share.test.ts +60 -0
  94. package/src/observable/operators/share.ts +67 -0
  95. package/src/observable/operators/tap.ts +26 -0
  96. package/src/observable/types.ts +69 -0
  97. package/src/observable/util/identity.ts +3 -0
  98. package/src/{rx.ts → observable/util/observableToPromise.ts} +6 -7
  99. package/src/observable/util/pipe.ts +22 -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 -11
  106. package/dist/rx.d.ts.map +0 -1
  107. package/dist/rx.js +0 -88
  108. package/dist/rx.mjs +0 -73
  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
@@ -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;
@@ -1,210 +0,0 @@
1
- import _createForOfIteratorHelper from '@babel/runtime/helpers/esm/createForOfIteratorHelper';
2
- import { T as TRPCAbortError, t as transformRPCResponse, h as httpRequest } from '../../../dist/transformRPCResponse-f7f8b0e1.esm.js';
3
- import { T as TRPCClientError } from '../../../dist/TRPCClientError-09b8a26b.esm.js';
4
- import '@babel/runtime/helpers/createClass';
5
- import '@babel/runtime/helpers/classCallCheck';
6
- import '@babel/runtime/helpers/assertThisInitialized';
7
- import '@babel/runtime/helpers/inherits';
8
- import '@babel/runtime/helpers/createSuper';
9
- import '@babel/runtime/helpers/wrapNativeSuper';
10
- import '@babel/runtime/helpers/objectSpread2';
11
-
12
- /**
13
- * Dataloader that's very inspired by https://github.com/graphql/dataloader
14
- * Less configuration, no caching, and allows you to cancel requests
15
- * When cancelling a single fetch the whole batch will be cancelled only when _all_ items are cancelled
16
- */
17
- function dataLoader(fetchMany, opts) {
18
- var batch = null;
19
- var dispatchTimer = null;
20
-
21
- var destroyTimerAndBatch = function destroyTimerAndBatch() {
22
- clearTimeout(dispatchTimer);
23
- dispatchTimer = null;
24
- batch = null;
25
- };
26
-
27
- function dispatch() {
28
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
29
- var batchCopy = batch;
30
- destroyTimerAndBatch();
31
-
32
- var _fetchMany = fetchMany(batchCopy.items.map(function (v) {
33
- return v.key;
34
- })),
35
- promise = _fetchMany.promise,
36
- cancel = _fetchMany.cancel;
37
-
38
- batchCopy.cancel = cancel;
39
- promise.then(function (result) {
40
- for (var i = 0; i < result.length; i++) {
41
- var _value = result[i];
42
- batchCopy.items[i].resolve(_value);
43
- }
44
- }).catch(function (cause) {
45
- var _iterator = _createForOfIteratorHelper(batchCopy.items),
46
- _step;
47
-
48
- try {
49
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
50
- var item = _step.value;
51
- item.reject(cause);
52
- }
53
- } catch (err) {
54
- _iterator.e(err);
55
- } finally {
56
- _iterator.f();
57
- }
58
- });
59
- }
60
-
61
- function load(key) {
62
- var batchItem = {
63
- cancelled: false,
64
- key: key
65
- };
66
-
67
- if (!batch) {
68
- batch = {
69
- items: [],
70
- cancel: function cancel() {
71
- destroyTimerAndBatch();
72
- }
73
- };
74
- }
75
-
76
- var thisBatch = batch;
77
- var promise = new Promise(function (resolve, reject) {
78
- var item = batchItem;
79
- item.reject = reject;
80
- item.resolve = resolve;
81
- thisBatch.items.push(item);
82
-
83
- if (typeof (opts === null || opts === void 0 ? void 0 : opts.maxBatchSize) !== 'undefined' && thisBatch.items.length >= opts.maxBatchSize) {
84
- dispatch();
85
- return;
86
- }
87
- });
88
-
89
- if (!dispatchTimer) {
90
- dispatchTimer = setTimeout(dispatch);
91
- }
92
-
93
- var cancel = function cancel() {
94
- batchItem.cancelled = true;
95
-
96
- if (thisBatch.items.some(function (item) {
97
- return !item.cancelled;
98
- })) {
99
- // there are still things that can be resolved
100
- return;
101
- }
102
-
103
- thisBatch.cancel();
104
- };
105
-
106
- return {
107
- promise: promise,
108
- cancel: cancel
109
- };
110
- }
111
-
112
- return {
113
- load: load
114
- };
115
- }
116
-
117
- function httpBatchLink(opts) {
118
- var url = opts.url,
119
- maxBatchSize = opts.maxBatchSize; // initialized config
120
-
121
- return function (runtime) {
122
- // initialized in app
123
- var fetcher = function fetcher(type) {
124
- return function (keyInputPairs) {
125
- var path = keyInputPairs.map(function (op) {
126
- return op.path;
127
- }).join(',');
128
- var inputs = keyInputPairs.map(function (op) {
129
- return op.input;
130
- });
131
-
132
- var _httpRequest = httpRequest({
133
- url: url,
134
- inputs: inputs,
135
- path: path,
136
- runtime: runtime,
137
- type: type
138
- }),
139
- promise = _httpRequest.promise,
140
- cancel = _httpRequest.cancel;
141
-
142
- return {
143
- promise: promise.then(function (res) {
144
- if (!Array.isArray(res)) {
145
- return keyInputPairs.map(function () {
146
- return res;
147
- });
148
- }
149
-
150
- return res;
151
- }),
152
- cancel: cancel
153
- };
154
- };
155
- };
156
-
157
- var query = dataLoader(fetcher('query'), {
158
- maxBatchSize: maxBatchSize
159
- });
160
- var mutation = dataLoader(fetcher('mutation'), {
161
- maxBatchSize: maxBatchSize
162
- });
163
- var subscription = dataLoader(fetcher('subscription'), {
164
- maxBatchSize: maxBatchSize
165
- });
166
- var loaders = {
167
- query: query,
168
- subscription: subscription,
169
- mutation: mutation
170
- };
171
- return function (_ref) {
172
- var op = _ref.op,
173
- prev = _ref.prev,
174
- onDestroy = _ref.onDestroy;
175
- var loader = loaders[op.type];
176
-
177
- var _loader$load = loader.load(op),
178
- promise = _loader$load.promise,
179
- cancel = _loader$load.cancel;
180
-
181
- var isDone = false;
182
-
183
- var prevOnce = function prevOnce(result) {
184
- if (isDone) {
185
- return;
186
- }
187
-
188
- isDone = true;
189
- prev(result);
190
- };
191
-
192
- onDestroy(function () {
193
- prevOnce(TRPCClientError.from(new TRPCAbortError(), {
194
- isDone: true
195
- }));
196
- cancel();
197
- });
198
- promise.then(function (envelope) {
199
- prevOnce(transformRPCResponse({
200
- envelope: envelope,
201
- runtime: runtime
202
- }));
203
- }).catch(function (cause) {
204
- prevOnce(TRPCClientError.from(cause));
205
- });
206
- };
207
- };
208
- }
209
-
210
- export { httpBatchLink };
@@ -1 +0,0 @@
1
- export * from '../../dist/links/httpBatchLink';
@@ -1 +0,0 @@
1
- module.exports = require('../../dist/links/httpBatchLink');
@@ -1 +0,0 @@
1
- export * from "../../../dist/declarations/src/links/httpLink";