@trpc/client 10.0.0-alpha.17 → 10.0.0-alpha.18
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.
- package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.cjs.d.ts +1 -11
- package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.cjs.dev.js +214 -0
- package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.cjs.js +6 -15
- package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.cjs.prod.js +214 -0
- package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.esm.js +210 -0
- package/links/httpLink/dist/trpc-client-links-httpLink.cjs.d.ts +1 -11
- package/links/httpLink/dist/trpc-client-links-httpLink.cjs.dev.js +67 -0
- package/links/httpLink/dist/trpc-client-links-httpLink.cjs.js +6 -15
- package/links/httpLink/dist/trpc-client-links-httpLink.cjs.prod.js +67 -0
- package/links/httpLink/dist/trpc-client-links-httpLink.esm.js +63 -0
- package/links/loggerLink/dist/trpc-client-links-loggerLink.cjs.d.ts +1 -11
- package/links/loggerLink/dist/trpc-client-links-loggerLink.cjs.dev.js +89 -0
- package/links/loggerLink/dist/trpc-client-links-loggerLink.cjs.js +6 -15
- package/links/loggerLink/dist/trpc-client-links-loggerLink.cjs.prod.js +89 -0
- package/links/loggerLink/dist/trpc-client-links-loggerLink.esm.js +85 -0
- package/links/splitLink/dist/trpc-client-links-splitLink.cjs.d.ts +1 -11
- package/links/splitLink/dist/trpc-client-links-splitLink.cjs.dev.js +19 -0
- package/links/splitLink/dist/trpc-client-links-splitLink.cjs.js +6 -15
- package/links/splitLink/dist/trpc-client-links-splitLink.cjs.prod.js +19 -0
- package/links/splitLink/dist/trpc-client-links-splitLink.esm.js +15 -0
- package/links/wsLink/dist/trpc-client-links-wsLink.cjs.d.ts +1 -11
- package/links/wsLink/dist/trpc-client-links-wsLink.cjs.dev.js +395 -0
- package/links/wsLink/dist/trpc-client-links-wsLink.cjs.js +6 -15
- package/links/wsLink/dist/trpc-client-links-wsLink.cjs.prod.js +395 -0
- package/links/wsLink/dist/trpc-client-links-wsLink.esm.js +390 -0
- package/package.json +9 -4
|
@@ -1,11 +1 @@
|
|
|
1
|
-
|
|
2
|
-
// you should run `yarn` or `yarn preconstruct dev` if preconstruct dev isn't in your postinstall hook
|
|
3
|
-
|
|
4
|
-
// curious why you need to?
|
|
5
|
-
// this file exists so that you can import from the entrypoint normally
|
|
6
|
-
// except that it points to your source file and you don't need to run build constantly
|
|
7
|
-
// which means we need to re-export all of the modules from your source file
|
|
8
|
-
// and since export * doesn't include default exports, we need to read your source file
|
|
9
|
-
// to check for a default export and re-export it if it exists
|
|
10
|
-
// it's not ideal, but it works pretty well ¯\_(ツ)_/¯
|
|
11
|
-
export * from "../../../src/links/httpBatchLink";
|
|
1
|
+
export * from "../../../dist/declarations/src/links/httpBatchLink";
|
|
@@ -0,0 +1,214 @@
|
|
|
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,16 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
// this file might look strange and you might be wondering what it's for
|
|
3
|
-
// it's lets you import your source files by importing this entrypoint
|
|
4
|
-
// as you would import it if it was built with preconstruct build
|
|
5
|
-
// this file is slightly different to some others though
|
|
6
|
-
// it has a require hook which compiles your code with Babel
|
|
7
|
-
// this means that you don't have to set up @babel/register or anything like that
|
|
8
|
-
// but you can still require this module and it'll be compiled
|
|
1
|
+
'use strict';
|
|
9
2
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
unregister();
|
|
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
|
+
}
|
|
@@ -0,0 +1,214 @@
|
|
|
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;
|
|
@@ -0,0 +1,210 @@
|
|
|
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,11 +1 @@
|
|
|
1
|
-
|
|
2
|
-
// you should run `yarn` or `yarn preconstruct dev` if preconstruct dev isn't in your postinstall hook
|
|
3
|
-
|
|
4
|
-
// curious why you need to?
|
|
5
|
-
// this file exists so that you can import from the entrypoint normally
|
|
6
|
-
// except that it points to your source file and you don't need to run build constantly
|
|
7
|
-
// which means we need to re-export all of the modules from your source file
|
|
8
|
-
// and since export * doesn't include default exports, we need to read your source file
|
|
9
|
-
// to check for a default export and re-export it if it exists
|
|
10
|
-
// it's not ideal, but it works pretty well ¯\_(ツ)_/¯
|
|
11
|
-
export * from "../../../src/links/httpLink";
|
|
1
|
+
export * from "../../../dist/declarations/src/links/httpLink";
|