@trpc/client 10.0.0-alpha.13 → 10.0.0-alpha.16
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/dist/index.js +972 -35
- package/dist/index.mjs +953 -21
- package/dist/links/wsLink.d.ts.map +1 -1
- package/dist/observable/index.js +3 -5
- package/dist/observable/index.mjs +1 -3
- package/dist/observable/{util → internals}/identity.d.ts +0 -0
- package/dist/observable/internals/identity.d.ts.map +1 -0
- package/dist/observable/{util → internals}/observableToPromise.d.ts +0 -0
- package/dist/observable/internals/observableToPromise.d.ts.map +1 -0
- package/dist/observable/{util → internals}/pipe.d.ts +0 -0
- package/dist/observable/internals/pipe.d.ts.map +1 -0
- package/dist/{share-9f12d26c.mjs → tap-4bc60fb0.mjs} +131 -1
- package/dist/{share-4afc084f.js → tap-fcd41ae6.js} +132 -0
- package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.cjs.d.ts +11 -0
- package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.cjs.js +16 -0
- package/links/httpLink/dist/trpc-client-links-httpLink.cjs.d.ts +11 -0
- package/links/httpLink/dist/trpc-client-links-httpLink.cjs.js +16 -0
- package/links/loggerLink/dist/trpc-client-links-loggerLink.cjs.d.ts +11 -0
- package/links/loggerLink/dist/trpc-client-links-loggerLink.cjs.js +16 -0
- package/links/splitLink/dist/trpc-client-links-splitLink.cjs.d.ts +11 -0
- package/links/splitLink/dist/trpc-client-links-splitLink.cjs.js +16 -0
- package/links/wsLink/dist/trpc-client-links-wsLink.cjs.d.ts +11 -0
- package/links/wsLink/dist/trpc-client-links-wsLink.cjs.js +16 -0
- package/package.json +43 -8
- package/src/internals/TRPCClient.ts +1 -1
- package/src/links/wsLink.ts +1 -2
- package/src/observable/{util → internals}/identity.ts +0 -0
- package/src/observable/{util → internals}/observableToPromise.ts +0 -0
- package/src/observable/{util → internals}/pipe.ts +0 -0
- package/src/observable/observable.ts +1 -1
- package/dist/httpUtils-27640b42.js +0 -97
- package/dist/httpUtils-b92552af.mjs +0 -91
- package/dist/links/httpBatchLink.js +0 -195
- package/dist/links/httpBatchLink.mjs +0 -191
- package/dist/links/httpBatchLink.ts.js +0 -1
- package/dist/links/httpLink.js +0 -44
- package/dist/links/httpLink.mjs +0 -40
- package/dist/links/httpLink.ts.js +0 -1
- package/dist/links/loggerLink.js +0 -108
- package/dist/links/loggerLink.mjs +0 -99
- package/dist/links/loggerLink.ts.js +0 -1
- package/dist/links/splitLink.js +0 -10
- package/dist/links/splitLink.mjs +0 -2
- package/dist/links/splitLink.ts.js +0 -1
- package/dist/links/transformerLink.js +0 -79
- package/dist/links/transformerLink.mjs +0 -71
- package/dist/links/transformerLink.ts.js +0 -1
- package/dist/links/wsLink.js +0 -19
- package/dist/links/wsLink.mjs +0 -10
- package/dist/links/wsLink.ts.js +0 -1
- package/dist/observable/util/identity.d.ts.map +0 -1
- package/dist/observable/util/observableToPromise.d.ts.map +0 -1
- package/dist/observable/util/pipe.d.ts.map +0 -1
- package/dist/observable-16fde241.mjs +0 -102
- package/dist/observable-9aaca086.js +0 -104
- package/dist/splitLink-8635d455.js +0 -52
- package/dist/splitLink-a736378a.mjs +0 -49
- package/dist/tap-aacf31bf.js +0 -32
- package/dist/tap-f567ea97.mjs +0 -30
- package/dist/wsLink-a6a0f3a8.mjs +0 -440
- package/dist/wsLink-c9323097.js +0 -455
package/dist/index.mjs
CHANGED
|
@@ -1,25 +1,13 @@
|
|
|
1
1
|
import _classCallCheck from '@babel/runtime/helpers/classCallCheck';
|
|
2
2
|
import _createClass from '@babel/runtime/helpers/createClass';
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
import { c as createChain } from './splitLink-a736378a.mjs';
|
|
6
|
-
export { s as splitLink } from './splitLink-a736378a.mjs';
|
|
7
|
-
import { T as TRPCClientError } from './wsLink-a6a0f3a8.mjs';
|
|
8
|
-
export { T as TRPCClientError, c as createWSClient, w as wsLink } from './wsLink-a6a0f3a8.mjs';
|
|
9
|
-
import { s as share } from './share-9f12d26c.mjs';
|
|
3
|
+
import { o as observable, s as share, t as tap } from './tap-4bc60fb0.mjs';
|
|
4
|
+
import _defineProperty from '@babel/runtime/helpers/defineProperty';
|
|
10
5
|
import _assertThisInitialized from '@babel/runtime/helpers/assertThisInitialized';
|
|
11
6
|
import _inherits from '@babel/runtime/helpers/inherits';
|
|
12
7
|
import _possibleConstructorReturn from '@babel/runtime/helpers/possibleConstructorReturn';
|
|
13
8
|
import _getPrototypeOf from '@babel/runtime/helpers/getPrototypeOf';
|
|
14
9
|
import _wrapNativeSuper from '@babel/runtime/helpers/wrapNativeSuper';
|
|
15
|
-
import
|
|
16
|
-
export { httpLink } from './links/httpLink.mjs';
|
|
17
|
-
export { loggerLink } from './links/loggerLink.mjs';
|
|
18
|
-
export { transformerLink } from './links/transformerLink.mjs';
|
|
19
|
-
import './httpUtils-b92552af.mjs';
|
|
20
|
-
import '@babel/runtime/helpers/defineProperty';
|
|
21
|
-
import '@babel/runtime/helpers/slicedToArray';
|
|
22
|
-
import './tap-f567ea97.mjs';
|
|
10
|
+
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
|
|
23
11
|
|
|
24
12
|
function getWindow() {
|
|
25
13
|
if (typeof window !== 'undefined') {
|
|
@@ -47,8 +35,375 @@ function getFetch(f) {
|
|
|
47
35
|
throw new Error('No fetch implementation found');
|
|
48
36
|
}
|
|
49
37
|
|
|
38
|
+
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; } } }; }
|
|
39
|
+
|
|
40
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
41
|
+
|
|
42
|
+
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; }
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Dataloader that's very inspired by https://github.com/graphql/dataloader
|
|
46
|
+
* Less configuration, no caching, and allows you to cancel requests
|
|
47
|
+
* When cancelling a single fetch the whole batch will be cancelled only when _all_ items are cancelled
|
|
48
|
+
*/
|
|
49
|
+
function dataLoader(fetchMany, opts) {
|
|
50
|
+
var batch = null;
|
|
51
|
+
var dispatchTimer = null;
|
|
52
|
+
|
|
53
|
+
var destroyTimerAndBatch = function destroyTimerAndBatch() {
|
|
54
|
+
clearTimeout(dispatchTimer);
|
|
55
|
+
dispatchTimer = null;
|
|
56
|
+
batch = null;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
function dispatch() {
|
|
60
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
61
|
+
var batchCopy = batch;
|
|
62
|
+
destroyTimerAndBatch();
|
|
63
|
+
|
|
64
|
+
var _fetchMany = fetchMany(batchCopy.items.map(function (v) {
|
|
65
|
+
return v.key;
|
|
66
|
+
})),
|
|
67
|
+
promise = _fetchMany.promise,
|
|
68
|
+
cancel = _fetchMany.cancel;
|
|
69
|
+
|
|
70
|
+
batchCopy.cancel = cancel;
|
|
71
|
+
promise.then(function (result) {
|
|
72
|
+
for (var i = 0; i < result.length; i++) {
|
|
73
|
+
var _value = result[i];
|
|
74
|
+
batchCopy.items[i].resolve(_value);
|
|
75
|
+
}
|
|
76
|
+
}).catch(function (cause) {
|
|
77
|
+
var _iterator = _createForOfIteratorHelper(batchCopy.items),
|
|
78
|
+
_step;
|
|
79
|
+
|
|
80
|
+
try {
|
|
81
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
82
|
+
var item = _step.value;
|
|
83
|
+
item.reject(cause);
|
|
84
|
+
}
|
|
85
|
+
} catch (err) {
|
|
86
|
+
_iterator.e(err);
|
|
87
|
+
} finally {
|
|
88
|
+
_iterator.f();
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function load(key) {
|
|
94
|
+
var batchItem = {
|
|
95
|
+
cancelled: false,
|
|
96
|
+
key: key
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
if (!batch) {
|
|
100
|
+
batch = {
|
|
101
|
+
items: [],
|
|
102
|
+
cancel: function cancel() {
|
|
103
|
+
destroyTimerAndBatch();
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
var thisBatch = batch;
|
|
109
|
+
var promise = new Promise(function (resolve, reject) {
|
|
110
|
+
var item = batchItem;
|
|
111
|
+
item.reject = reject;
|
|
112
|
+
item.resolve = resolve;
|
|
113
|
+
thisBatch.items.push(item);
|
|
114
|
+
|
|
115
|
+
if (typeof (opts === null || opts === void 0 ? void 0 : opts.maxBatchSize) !== 'undefined' && thisBatch.items.length >= opts.maxBatchSize) {
|
|
116
|
+
dispatch();
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
if (!dispatchTimer) {
|
|
122
|
+
dispatchTimer = setTimeout(dispatch);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
var cancel = function cancel() {
|
|
126
|
+
batchItem.cancelled = true;
|
|
127
|
+
|
|
128
|
+
if (thisBatch.items.some(function (item) {
|
|
129
|
+
return !item.cancelled;
|
|
130
|
+
})) {
|
|
131
|
+
// there are still things that can be resolved
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
thisBatch.cancel();
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
return {
|
|
139
|
+
promise: promise,
|
|
140
|
+
cancel: cancel
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return {
|
|
145
|
+
load: load
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function ownKeys$3(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
150
|
+
|
|
151
|
+
function _objectSpread$3(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys$3(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys$3(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
152
|
+
|
|
153
|
+
// https://github.com/trpc/trpc/pull/669
|
|
154
|
+
function arrayToDict(array) {
|
|
155
|
+
var dict = {};
|
|
156
|
+
|
|
157
|
+
for (var index = 0; index < array.length; index++) {
|
|
158
|
+
var element = array[index];
|
|
159
|
+
dict[index] = element;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return dict;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
var METHOD = {
|
|
166
|
+
query: 'GET',
|
|
167
|
+
mutation: 'POST',
|
|
168
|
+
subscription: 'PATCH'
|
|
169
|
+
};
|
|
170
|
+
function httpRequest(props) {
|
|
171
|
+
var type = props.type,
|
|
172
|
+
rt = props.runtime,
|
|
173
|
+
path = props.path;
|
|
174
|
+
var ac = rt.AbortController ? new rt.AbortController() : null;
|
|
175
|
+
var input = 'input' in props ? props.input : arrayToDict(props.inputs);
|
|
176
|
+
|
|
177
|
+
function getUrl() {
|
|
178
|
+
var url = props.url + '/' + path;
|
|
179
|
+
var queryParts = [];
|
|
180
|
+
|
|
181
|
+
if ('inputs' in props) {
|
|
182
|
+
queryParts.push('batch=1');
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
if (type === 'query' && input !== undefined) {
|
|
186
|
+
queryParts.push("input=".concat(encodeURIComponent(JSON.stringify(input))));
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
if (queryParts.length) {
|
|
190
|
+
url += '?' + queryParts.join('&');
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
return url;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function getBody() {
|
|
197
|
+
if (type === 'query') {
|
|
198
|
+
return undefined;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
return input !== undefined ? JSON.stringify(input) : undefined;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
var promise = new Promise(function (resolve, reject) {
|
|
205
|
+
var url = getUrl();
|
|
206
|
+
var meta = {};
|
|
207
|
+
Promise.resolve(rt.headers()).then(function (headers) {
|
|
208
|
+
return rt.fetch(url, {
|
|
209
|
+
method: METHOD[type],
|
|
210
|
+
signal: ac === null || ac === void 0 ? void 0 : ac.signal,
|
|
211
|
+
body: getBody(),
|
|
212
|
+
headers: _objectSpread$3({
|
|
213
|
+
'content-type': 'application/json'
|
|
214
|
+
}, headers)
|
|
215
|
+
});
|
|
216
|
+
}).then(function (_res) {
|
|
217
|
+
meta.response = _res;
|
|
218
|
+
return _res.json();
|
|
219
|
+
}).then(function (json) {
|
|
220
|
+
resolve({
|
|
221
|
+
json: json,
|
|
222
|
+
meta: meta
|
|
223
|
+
});
|
|
224
|
+
}).catch(reject);
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
var cancel = function cancel() {
|
|
228
|
+
ac === null || ac === void 0 ? void 0 : ac.abort();
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
return {
|
|
232
|
+
promise: promise,
|
|
233
|
+
cancel: cancel
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function httpBatchLink(opts) {
|
|
238
|
+
var url = opts.url; // initialized config
|
|
239
|
+
|
|
240
|
+
return function (runtime) {
|
|
241
|
+
var fetcher = function fetcher(type) {
|
|
242
|
+
return function (keyInputPairs) {
|
|
243
|
+
var path = keyInputPairs.map(function (op) {
|
|
244
|
+
return op.path;
|
|
245
|
+
}).join(',');
|
|
246
|
+
var inputs = keyInputPairs.map(function (op) {
|
|
247
|
+
return op.input;
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
var _httpRequest = httpRequest({
|
|
251
|
+
url: url,
|
|
252
|
+
inputs: inputs,
|
|
253
|
+
path: path,
|
|
254
|
+
runtime: runtime,
|
|
255
|
+
type: type
|
|
256
|
+
}),
|
|
257
|
+
promise = _httpRequest.promise,
|
|
258
|
+
cancel = _httpRequest.cancel;
|
|
259
|
+
|
|
260
|
+
return {
|
|
261
|
+
promise: promise.then(function (res) {
|
|
262
|
+
var resJSON = Array.isArray(res.json) ? res.json : keyInputPairs.map(function () {
|
|
263
|
+
return res.json;
|
|
264
|
+
});
|
|
265
|
+
var result = resJSON.map(function (item) {
|
|
266
|
+
return {
|
|
267
|
+
meta: res.meta,
|
|
268
|
+
json: item
|
|
269
|
+
};
|
|
270
|
+
});
|
|
271
|
+
return result;
|
|
272
|
+
}),
|
|
273
|
+
cancel: cancel
|
|
274
|
+
};
|
|
275
|
+
};
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
var query = dataLoader(fetcher('query'), opts);
|
|
279
|
+
var mutation = dataLoader(fetcher('mutation'), opts);
|
|
280
|
+
var subscription = dataLoader(fetcher('subscription'), opts);
|
|
281
|
+
var loaders = {
|
|
282
|
+
query: query,
|
|
283
|
+
subscription: subscription,
|
|
284
|
+
mutation: mutation
|
|
285
|
+
};
|
|
286
|
+
return function (_ref) {
|
|
287
|
+
var op = _ref.op;
|
|
288
|
+
return observable(function (observer) {
|
|
289
|
+
var loader = loaders[op.type];
|
|
290
|
+
|
|
291
|
+
var _loader$load = loader.load(op),
|
|
292
|
+
promise = _loader$load.promise,
|
|
293
|
+
cancel = _loader$load.cancel;
|
|
294
|
+
|
|
295
|
+
promise.then(function (res) {
|
|
296
|
+
observer.next({
|
|
297
|
+
context: res.meta,
|
|
298
|
+
data: res.json
|
|
299
|
+
});
|
|
300
|
+
observer.complete();
|
|
301
|
+
}).catch(function (err) {
|
|
302
|
+
return observer.error(err);
|
|
303
|
+
});
|
|
304
|
+
return function () {
|
|
305
|
+
cancel();
|
|
306
|
+
};
|
|
307
|
+
});
|
|
308
|
+
};
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/** @internal */
|
|
313
|
+
function createChain(opts) {
|
|
314
|
+
return observable(function (observer) {
|
|
315
|
+
function execute() {
|
|
316
|
+
var index = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
|
|
317
|
+
var op = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : opts.op;
|
|
318
|
+
var next = opts.links[index];
|
|
319
|
+
var subscription = next({
|
|
320
|
+
op: op,
|
|
321
|
+
next: function next(nextOp) {
|
|
322
|
+
var nextObserver = execute(index + 1, nextOp);
|
|
323
|
+
return nextObserver;
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
return subscription;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
var obs$ = execute();
|
|
330
|
+
return obs$.subscribe(observer);
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
function ownKeys$2(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
335
|
+
|
|
336
|
+
function _objectSpread$2(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys$2(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys$2(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
337
|
+
|
|
338
|
+
function _createSuper$2(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct$2(); 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); }; }
|
|
339
|
+
|
|
340
|
+
function _isNativeReflectConstruct$2() { 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; } }
|
|
341
|
+
|
|
342
|
+
var TRPCClientError = /*#__PURE__*/function (_Error) {
|
|
343
|
+
_inherits(TRPCClientError, _Error);
|
|
344
|
+
|
|
345
|
+
var _super = /*#__PURE__*/_createSuper$2(TRPCClientError);
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* @deprecated use `cause`
|
|
349
|
+
*/
|
|
350
|
+
function TRPCClientError(message, opts) {
|
|
351
|
+
var _opts$cause, _opts$result, _opts$result2;
|
|
352
|
+
|
|
353
|
+
var _this;
|
|
354
|
+
|
|
355
|
+
_classCallCheck(this, TRPCClientError);
|
|
356
|
+
|
|
357
|
+
var cause = (_opts$cause = opts === null || opts === void 0 ? void 0 : opts.cause) !== null && _opts$cause !== void 0 ? _opts$cause : opts === null || opts === void 0 ? void 0 : opts.originalError; // eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
358
|
+
// @ts-ignore https://github.com/tc39/proposal-error-cause
|
|
359
|
+
|
|
360
|
+
_this = _super.call(this, message, {
|
|
361
|
+
cause: cause
|
|
362
|
+
});
|
|
363
|
+
_this.originalError = void 0;
|
|
364
|
+
_this.cause = void 0;
|
|
365
|
+
_this.shape = void 0;
|
|
366
|
+
_this.data = void 0;
|
|
367
|
+
_this.meta = void 0;
|
|
368
|
+
_this.meta = opts === null || opts === void 0 ? void 0 : opts.meta;
|
|
369
|
+
_this.cause = _this.originalError = cause;
|
|
370
|
+
_this.shape = opts === null || opts === void 0 ? void 0 : (_opts$result = opts.result) === null || _opts$result === void 0 ? void 0 : _opts$result.error;
|
|
371
|
+
_this.data = opts === null || opts === void 0 ? void 0 : (_opts$result2 = opts.result) === null || _opts$result2 === void 0 ? void 0 : _opts$result2.error.data;
|
|
372
|
+
_this.name = 'TRPCClientError';
|
|
373
|
+
Object.setPrototypeOf(_assertThisInitialized(_this), TRPCClientError.prototype);
|
|
374
|
+
return _this;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
_createClass(TRPCClientError, null, [{
|
|
378
|
+
key: "from",
|
|
379
|
+
value: function from(cause) {
|
|
380
|
+
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
381
|
+
|
|
382
|
+
if (!(cause instanceof Error)) {
|
|
383
|
+
var _message;
|
|
384
|
+
|
|
385
|
+
return new TRPCClientError((_message = cause.error.message) !== null && _message !== void 0 ? _message : '', _objectSpread$2(_objectSpread$2({}, opts), {}, {
|
|
386
|
+
cause: null,
|
|
387
|
+
result: cause
|
|
388
|
+
}));
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
if (cause.name === 'TRPCClientError') {
|
|
392
|
+
return cause;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
return new TRPCClientError(cause.message, _objectSpread$2(_objectSpread$2({}, opts), {}, {
|
|
396
|
+
cause: cause,
|
|
397
|
+
result: null
|
|
398
|
+
}));
|
|
399
|
+
}
|
|
400
|
+
}]);
|
|
401
|
+
|
|
402
|
+
return TRPCClientError;
|
|
403
|
+
}( /*#__PURE__*/_wrapNativeSuper(Error));
|
|
404
|
+
|
|
50
405
|
/** @internal */
|
|
51
|
-
function transformOperationResult(result) {
|
|
406
|
+
function transformOperationResult$1(result) {
|
|
52
407
|
var context = result.context;
|
|
53
408
|
|
|
54
409
|
if ('error' in result.data) {
|
|
@@ -68,14 +423,14 @@ function transformOperationResult(result) {
|
|
|
68
423
|
};
|
|
69
424
|
}
|
|
70
425
|
|
|
71
|
-
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); }; }
|
|
426
|
+
function _createSuper$1(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct$1(); 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); }; }
|
|
72
427
|
|
|
73
|
-
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; } }
|
|
428
|
+
function _isNativeReflectConstruct$1() { 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; } }
|
|
74
429
|
|
|
75
430
|
var ObservableAbortError = /*#__PURE__*/function (_Error) {
|
|
76
431
|
_inherits(ObservableAbortError, _Error);
|
|
77
432
|
|
|
78
|
-
var _super = /*#__PURE__*/_createSuper(ObservableAbortError);
|
|
433
|
+
var _super = /*#__PURE__*/_createSuper$1(ObservableAbortError);
|
|
79
434
|
|
|
80
435
|
function ObservableAbortError(message) {
|
|
81
436
|
var _this;
|
|
@@ -212,7 +567,7 @@ var TRPCClient = /*#__PURE__*/function () {
|
|
|
212
567
|
|
|
213
568
|
var cancellablePromise = new Promise(function (resolve, reject) {
|
|
214
569
|
promise.then(function (result) {
|
|
215
|
-
var transformed = transformOperationResult(result);
|
|
570
|
+
var transformed = transformOperationResult$1(result);
|
|
216
571
|
|
|
217
572
|
if (transformed.ok) {
|
|
218
573
|
resolve(transformed.data);
|
|
@@ -353,6 +708,134 @@ function dedupeLink() {
|
|
|
353
708
|
};
|
|
354
709
|
}
|
|
355
710
|
|
|
711
|
+
function httpLink(opts) {
|
|
712
|
+
var url = opts.url;
|
|
713
|
+
return function (runtime) {
|
|
714
|
+
return function (_ref) {
|
|
715
|
+
var op = _ref.op;
|
|
716
|
+
return observable(function (observer) {
|
|
717
|
+
var path = op.path,
|
|
718
|
+
input = op.input,
|
|
719
|
+
type = op.type;
|
|
720
|
+
|
|
721
|
+
var _httpRequest = httpRequest({
|
|
722
|
+
runtime: runtime,
|
|
723
|
+
path: path,
|
|
724
|
+
input: input,
|
|
725
|
+
type: type,
|
|
726
|
+
url: url
|
|
727
|
+
}),
|
|
728
|
+
promise = _httpRequest.promise,
|
|
729
|
+
cancel = _httpRequest.cancel;
|
|
730
|
+
|
|
731
|
+
promise.then(function (res) {
|
|
732
|
+
observer.next({
|
|
733
|
+
context: res.meta,
|
|
734
|
+
data: res.json
|
|
735
|
+
});
|
|
736
|
+
observer.complete();
|
|
737
|
+
}).catch(observer.error);
|
|
738
|
+
return function () {
|
|
739
|
+
cancel();
|
|
740
|
+
};
|
|
741
|
+
});
|
|
742
|
+
};
|
|
743
|
+
};
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
function ownKeys$1(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
747
|
+
|
|
748
|
+
function _objectSpread$1(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys$1(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys$1(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
749
|
+
var palette = {
|
|
750
|
+
query: ['72e3ff', '3fb0d8'],
|
|
751
|
+
mutation: ['c5a3fc', '904dfc'],
|
|
752
|
+
subscription: ['ff49e1', 'd83fbe']
|
|
753
|
+
};
|
|
754
|
+
|
|
755
|
+
// maybe this should be moved to it's own package
|
|
756
|
+
var defaultLogger = function defaultLogger() {
|
|
757
|
+
var c = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : console;
|
|
758
|
+
return function (props) {
|
|
759
|
+
var direction = props.direction,
|
|
760
|
+
input = props.input,
|
|
761
|
+
type = props.type,
|
|
762
|
+
path = props.path,
|
|
763
|
+
context = props.context,
|
|
764
|
+
id = props.id;
|
|
765
|
+
|
|
766
|
+
var _palette$type = _slicedToArray(palette[type], 2),
|
|
767
|
+
light = _palette$type[0],
|
|
768
|
+
dark = _palette$type[1];
|
|
769
|
+
|
|
770
|
+
var css = "\n background-color: #".concat(direction === 'up' ? light : dark, "; \n color: ").concat(direction === 'up' ? 'black' : 'white', ";\n padding: 2px;\n ");
|
|
771
|
+
var parts = ['%c', direction === 'up' ? '>>' : '<<', type, "#".concat(id), "%c".concat(path, "%c"), '%O'];
|
|
772
|
+
var args = [css, "".concat(css, "; font-weight: bold;"), "".concat(css, "; font-weight: normal;")];
|
|
773
|
+
|
|
774
|
+
if (props.direction === 'up') {
|
|
775
|
+
args.push({
|
|
776
|
+
input: input,
|
|
777
|
+
context: context
|
|
778
|
+
});
|
|
779
|
+
} else {
|
|
780
|
+
args.push({
|
|
781
|
+
input: input,
|
|
782
|
+
result: props.result,
|
|
783
|
+
elapsedMs: props.elapsedMs,
|
|
784
|
+
context: context
|
|
785
|
+
});
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
var fn = props.direction === 'down' && props.result && (props.result instanceof Error || 'error' in props.result.data) ? 'error' : 'log';
|
|
789
|
+
c[fn].apply(null, [parts.join(' ')].concat(args));
|
|
790
|
+
};
|
|
791
|
+
};
|
|
792
|
+
|
|
793
|
+
function loggerLink() {
|
|
794
|
+
var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
795
|
+
var _opts$enabled = opts.enabled,
|
|
796
|
+
enabled = _opts$enabled === void 0 ? function () {
|
|
797
|
+
return true;
|
|
798
|
+
} : _opts$enabled;
|
|
799
|
+
var _opts$logger = opts.logger,
|
|
800
|
+
logger = _opts$logger === void 0 ? defaultLogger(opts.console) : _opts$logger;
|
|
801
|
+
return function () {
|
|
802
|
+
return function (_ref) {
|
|
803
|
+
var op = _ref.op,
|
|
804
|
+
next = _ref.next;
|
|
805
|
+
return observable(function (observer) {
|
|
806
|
+
// ->
|
|
807
|
+
enabled(_objectSpread$1(_objectSpread$1({}, op), {}, {
|
|
808
|
+
direction: 'up'
|
|
809
|
+
})) && logger(_objectSpread$1(_objectSpread$1({}, op), {}, {
|
|
810
|
+
direction: 'up'
|
|
811
|
+
}));
|
|
812
|
+
var requestStartTime = Date.now();
|
|
813
|
+
|
|
814
|
+
function logResult(result) {
|
|
815
|
+
var elapsedMs = Date.now() - requestStartTime;
|
|
816
|
+
enabled(_objectSpread$1(_objectSpread$1({}, op), {}, {
|
|
817
|
+
direction: 'down',
|
|
818
|
+
result: result
|
|
819
|
+
})) && logger(_objectSpread$1(_objectSpread$1({}, op), {}, {
|
|
820
|
+
direction: 'down',
|
|
821
|
+
elapsedMs: elapsedMs,
|
|
822
|
+
result: result
|
|
823
|
+
}));
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
return next(op).pipe(tap({
|
|
827
|
+
next: function next(result) {
|
|
828
|
+
logResult(result);
|
|
829
|
+
},
|
|
830
|
+
error: function error(result) {
|
|
831
|
+
logResult(result);
|
|
832
|
+
}
|
|
833
|
+
})).subscribe(observer);
|
|
834
|
+
});
|
|
835
|
+
};
|
|
836
|
+
};
|
|
837
|
+
}
|
|
838
|
+
|
|
356
839
|
function retryLink(opts) {
|
|
357
840
|
// initialized config
|
|
358
841
|
return function () {
|
|
@@ -415,4 +898,453 @@ function retryLink(opts) {
|
|
|
415
898
|
};
|
|
416
899
|
}
|
|
417
900
|
|
|
418
|
-
|
|
901
|
+
function asArray(value) {
|
|
902
|
+
return Array.isArray(value) ? value : [value];
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
function splitLink(opts) {
|
|
906
|
+
return function (rt) {
|
|
907
|
+
var yes = asArray(opts.true).map(function (link) {
|
|
908
|
+
return link(rt);
|
|
909
|
+
});
|
|
910
|
+
var no = asArray(opts.false).map(function (link) {
|
|
911
|
+
return link(rt);
|
|
912
|
+
});
|
|
913
|
+
return function (props) {
|
|
914
|
+
return observable(function (observer) {
|
|
915
|
+
var links = opts.condition(props.op) ? yes : no;
|
|
916
|
+
return createChain({
|
|
917
|
+
op: props.op,
|
|
918
|
+
links: links
|
|
919
|
+
}).subscribe(observer);
|
|
920
|
+
});
|
|
921
|
+
};
|
|
922
|
+
};
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
926
|
+
|
|
927
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
928
|
+
|
|
929
|
+
/**
|
|
930
|
+
* @internal
|
|
931
|
+
*/
|
|
932
|
+
function transformOperationResult(result, transformer) {
|
|
933
|
+
if ('error' in result.data) {
|
|
934
|
+
return _objectSpread(_objectSpread({}, result), {}, {
|
|
935
|
+
data: _objectSpread(_objectSpread({}, result.data), {}, {
|
|
936
|
+
error: transformer.deserialize(result.data.error)
|
|
937
|
+
})
|
|
938
|
+
});
|
|
939
|
+
}
|
|
940
|
+
|
|
941
|
+
if (result.data.result.type !== 'data') {
|
|
942
|
+
return result;
|
|
943
|
+
}
|
|
944
|
+
|
|
945
|
+
return _objectSpread(_objectSpread({}, result), {}, {
|
|
946
|
+
data: _objectSpread(_objectSpread({}, result.data), {}, {
|
|
947
|
+
result: _objectSpread(_objectSpread({}, result.data.result), {}, {
|
|
948
|
+
data: transformer.deserialize(result.data.result.data)
|
|
949
|
+
})
|
|
950
|
+
})
|
|
951
|
+
});
|
|
952
|
+
}
|
|
953
|
+
|
|
954
|
+
function transformerLink(transformer) {
|
|
955
|
+
var _transformer = transformer ? 'input' in transformer ? {
|
|
956
|
+
serialize: transformer.input.serialize,
|
|
957
|
+
deserialize: transformer.output.deserialize
|
|
958
|
+
} : transformer : {
|
|
959
|
+
serialize: function serialize(data) {
|
|
960
|
+
return data;
|
|
961
|
+
},
|
|
962
|
+
deserialize: function deserialize(data) {
|
|
963
|
+
return data;
|
|
964
|
+
}
|
|
965
|
+
};
|
|
966
|
+
|
|
967
|
+
return function () {
|
|
968
|
+
return function (props) {
|
|
969
|
+
var input = _transformer.serialize(props.op.input);
|
|
970
|
+
|
|
971
|
+
return observable(function (observer) {
|
|
972
|
+
var subscription = props.next(_objectSpread(_objectSpread({}, props.op), {}, {
|
|
973
|
+
input: input
|
|
974
|
+
})).subscribe({
|
|
975
|
+
next: function next(value) {
|
|
976
|
+
var transformed = transformOperationResult(value, _transformer);
|
|
977
|
+
observer.next(transformed);
|
|
978
|
+
},
|
|
979
|
+
error: function error(err) {
|
|
980
|
+
observer.error(err);
|
|
981
|
+
},
|
|
982
|
+
complete: function complete() {
|
|
983
|
+
observer.complete();
|
|
984
|
+
}
|
|
985
|
+
});
|
|
986
|
+
return subscription;
|
|
987
|
+
});
|
|
988
|
+
};
|
|
989
|
+
};
|
|
990
|
+
}
|
|
991
|
+
|
|
992
|
+
/* istanbul ignore next */
|
|
993
|
+
var retryDelay = function retryDelay(attemptIndex) {
|
|
994
|
+
return attemptIndex === 0 ? 0 : Math.min(1000 * Math.pow(2, attemptIndex), 30000);
|
|
995
|
+
};
|
|
996
|
+
|
|
997
|
+
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); }; }
|
|
998
|
+
|
|
999
|
+
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; } }
|
|
1000
|
+
function createWSClient(opts) {
|
|
1001
|
+
var url = opts.url,
|
|
1002
|
+
_opts$WebSocket = opts.WebSocket,
|
|
1003
|
+
WebSocketImpl = _opts$WebSocket === void 0 ? WebSocket : _opts$WebSocket,
|
|
1004
|
+
_opts$retryDelayMs = opts.retryDelayMs,
|
|
1005
|
+
retryDelayFn = _opts$retryDelayMs === void 0 ? retryDelay : _opts$retryDelayMs;
|
|
1006
|
+
/* istanbul ignore next */
|
|
1007
|
+
|
|
1008
|
+
if (!WebSocketImpl) {
|
|
1009
|
+
throw new Error("No WebSocket implementation found - you probably don't want to use this on the server, but if you do you need to pass a `WebSocket`-ponyfill");
|
|
1010
|
+
}
|
|
1011
|
+
/**
|
|
1012
|
+
* outgoing messages buffer whilst not open
|
|
1013
|
+
*/
|
|
1014
|
+
|
|
1015
|
+
|
|
1016
|
+
var outgoing = [];
|
|
1017
|
+
/**
|
|
1018
|
+
* pending outgoing requests that are awaiting callback
|
|
1019
|
+
*/
|
|
1020
|
+
|
|
1021
|
+
var pendingRequests = Object.create(null);
|
|
1022
|
+
var connectAttempt = 0;
|
|
1023
|
+
var dispatchTimer = null;
|
|
1024
|
+
var connectTimer = null;
|
|
1025
|
+
var activeConnection = createWS();
|
|
1026
|
+
var state = 'connecting';
|
|
1027
|
+
/**
|
|
1028
|
+
* tries to send the list of messages
|
|
1029
|
+
*/
|
|
1030
|
+
|
|
1031
|
+
function dispatch() {
|
|
1032
|
+
if (state !== 'open' || dispatchTimer) {
|
|
1033
|
+
return;
|
|
1034
|
+
}
|
|
1035
|
+
|
|
1036
|
+
dispatchTimer = setTimeout(function () {
|
|
1037
|
+
dispatchTimer = null;
|
|
1038
|
+
|
|
1039
|
+
if (outgoing.length === 1) {
|
|
1040
|
+
// single send
|
|
1041
|
+
activeConnection.send(JSON.stringify(outgoing.pop()));
|
|
1042
|
+
} else {
|
|
1043
|
+
// batch send
|
|
1044
|
+
activeConnection.send(JSON.stringify(outgoing));
|
|
1045
|
+
} // clear
|
|
1046
|
+
|
|
1047
|
+
|
|
1048
|
+
outgoing = [];
|
|
1049
|
+
});
|
|
1050
|
+
}
|
|
1051
|
+
|
|
1052
|
+
function tryReconnect() {
|
|
1053
|
+
if (connectTimer || state === 'closed') {
|
|
1054
|
+
return;
|
|
1055
|
+
}
|
|
1056
|
+
|
|
1057
|
+
var timeout = retryDelayFn(connectAttempt++);
|
|
1058
|
+
reconnectInMs(timeout);
|
|
1059
|
+
}
|
|
1060
|
+
|
|
1061
|
+
function reconnect() {
|
|
1062
|
+
state = 'connecting';
|
|
1063
|
+
var oldConnection = activeConnection;
|
|
1064
|
+
activeConnection = createWS();
|
|
1065
|
+
closeIfNoPending(oldConnection);
|
|
1066
|
+
}
|
|
1067
|
+
|
|
1068
|
+
function reconnectInMs(ms) {
|
|
1069
|
+
if (connectTimer) {
|
|
1070
|
+
return;
|
|
1071
|
+
}
|
|
1072
|
+
|
|
1073
|
+
state = 'connecting';
|
|
1074
|
+
connectTimer = setTimeout(reconnect, ms);
|
|
1075
|
+
}
|
|
1076
|
+
|
|
1077
|
+
function closeIfNoPending(conn) {
|
|
1078
|
+
// disconnect as soon as there are are no pending result
|
|
1079
|
+
var hasPendingRequests = Object.values(pendingRequests).some(function (p) {
|
|
1080
|
+
return p.ws === conn;
|
|
1081
|
+
});
|
|
1082
|
+
|
|
1083
|
+
if (!hasPendingRequests) {
|
|
1084
|
+
conn.close();
|
|
1085
|
+
}
|
|
1086
|
+
}
|
|
1087
|
+
|
|
1088
|
+
function resumeSubscriptionOnReconnect(req) {
|
|
1089
|
+
if (outgoing.some(function (r) {
|
|
1090
|
+
return r.id === req.op.id;
|
|
1091
|
+
})) {
|
|
1092
|
+
return;
|
|
1093
|
+
}
|
|
1094
|
+
|
|
1095
|
+
request(req.op, req.callbacks);
|
|
1096
|
+
}
|
|
1097
|
+
|
|
1098
|
+
function createWS() {
|
|
1099
|
+
var conn = new WebSocket(url);
|
|
1100
|
+
clearTimeout(connectTimer);
|
|
1101
|
+
connectTimer = null;
|
|
1102
|
+
conn.addEventListener('open', function () {
|
|
1103
|
+
/* istanbul ignore next */
|
|
1104
|
+
if (conn !== activeConnection) {
|
|
1105
|
+
return;
|
|
1106
|
+
}
|
|
1107
|
+
|
|
1108
|
+
connectAttempt = 0;
|
|
1109
|
+
state = 'open';
|
|
1110
|
+
dispatch();
|
|
1111
|
+
});
|
|
1112
|
+
conn.addEventListener('error', function () {
|
|
1113
|
+
if (conn === activeConnection) {
|
|
1114
|
+
tryReconnect();
|
|
1115
|
+
}
|
|
1116
|
+
});
|
|
1117
|
+
|
|
1118
|
+
var handleIncomingRequest = function handleIncomingRequest(req) {
|
|
1119
|
+
if (req.method === 'reconnect' && conn === activeConnection) {
|
|
1120
|
+
reconnect(); // notify subscribers
|
|
1121
|
+
|
|
1122
|
+
for (var _i = 0, _Object$values = Object.values(pendingRequests); _i < _Object$values.length; _i++) {
|
|
1123
|
+
var pendingReq = _Object$values[_i];
|
|
1124
|
+
|
|
1125
|
+
if (pendingReq.type === 'subscription') {
|
|
1126
|
+
resumeSubscriptionOnReconnect(pendingReq);
|
|
1127
|
+
}
|
|
1128
|
+
}
|
|
1129
|
+
}
|
|
1130
|
+
};
|
|
1131
|
+
|
|
1132
|
+
var handleIncomingResponse = function handleIncomingResponse(data) {
|
|
1133
|
+
var _req$callbacks$next, _req$callbacks;
|
|
1134
|
+
|
|
1135
|
+
var req = data.id !== null && pendingRequests[data.id];
|
|
1136
|
+
|
|
1137
|
+
if (!req) {
|
|
1138
|
+
// do something?
|
|
1139
|
+
return;
|
|
1140
|
+
}
|
|
1141
|
+
|
|
1142
|
+
(_req$callbacks$next = (_req$callbacks = req.callbacks).next) === null || _req$callbacks$next === void 0 ? void 0 : _req$callbacks$next.call(_req$callbacks, {
|
|
1143
|
+
data: data
|
|
1144
|
+
});
|
|
1145
|
+
|
|
1146
|
+
if (req.ws !== activeConnection && conn === activeConnection) {
|
|
1147
|
+
var oldWs = req.ws; // gracefully replace old connection with this
|
|
1148
|
+
|
|
1149
|
+
req.ws = activeConnection;
|
|
1150
|
+
closeIfNoPending(oldWs);
|
|
1151
|
+
}
|
|
1152
|
+
|
|
1153
|
+
if ('result' in data && data.result.type === 'stopped' && conn === activeConnection) {
|
|
1154
|
+
req.callbacks.complete();
|
|
1155
|
+
}
|
|
1156
|
+
};
|
|
1157
|
+
|
|
1158
|
+
conn.addEventListener('message', function (_ref) {
|
|
1159
|
+
var data = _ref.data;
|
|
1160
|
+
var msg = JSON.parse(data);
|
|
1161
|
+
|
|
1162
|
+
if ('method' in msg) {
|
|
1163
|
+
handleIncomingRequest(msg);
|
|
1164
|
+
} else {
|
|
1165
|
+
handleIncomingResponse(msg);
|
|
1166
|
+
}
|
|
1167
|
+
|
|
1168
|
+
if (conn !== activeConnection || state === 'closed') {
|
|
1169
|
+
// when receiving a message, we close old connection that has no pending requests
|
|
1170
|
+
closeIfNoPending(conn);
|
|
1171
|
+
}
|
|
1172
|
+
});
|
|
1173
|
+
conn.addEventListener('close', function () {
|
|
1174
|
+
if (activeConnection === conn) {
|
|
1175
|
+
// connection might have been replaced already
|
|
1176
|
+
tryReconnect();
|
|
1177
|
+
}
|
|
1178
|
+
|
|
1179
|
+
for (var key in pendingRequests) {
|
|
1180
|
+
var _req$callbacks$error, _req$callbacks2;
|
|
1181
|
+
|
|
1182
|
+
var req = pendingRequests[key];
|
|
1183
|
+
|
|
1184
|
+
if (req.ws !== conn) {
|
|
1185
|
+
continue;
|
|
1186
|
+
}
|
|
1187
|
+
|
|
1188
|
+
(_req$callbacks$error = (_req$callbacks2 = req.callbacks).error) === null || _req$callbacks$error === void 0 ? void 0 : _req$callbacks$error.call(_req$callbacks2, TRPCClientError.from(new TRPCWebSocketClosedError('WebSocket closed prematurely')));
|
|
1189
|
+
|
|
1190
|
+
if (req.type !== 'subscription') {
|
|
1191
|
+
var _req$callbacks$comple, _req$callbacks3;
|
|
1192
|
+
|
|
1193
|
+
delete pendingRequests[key];
|
|
1194
|
+
(_req$callbacks$comple = (_req$callbacks3 = req.callbacks).complete) === null || _req$callbacks$comple === void 0 ? void 0 : _req$callbacks$comple.call(_req$callbacks3);
|
|
1195
|
+
} else if (state !== 'closed') {
|
|
1196
|
+
// request restart of sub with next connection
|
|
1197
|
+
resumeSubscriptionOnReconnect(req);
|
|
1198
|
+
}
|
|
1199
|
+
}
|
|
1200
|
+
});
|
|
1201
|
+
return conn;
|
|
1202
|
+
}
|
|
1203
|
+
|
|
1204
|
+
function request(op, callbacks) {
|
|
1205
|
+
var type = op.type,
|
|
1206
|
+
input = op.input,
|
|
1207
|
+
path = op.path,
|
|
1208
|
+
id = op.id;
|
|
1209
|
+
var envelope = {
|
|
1210
|
+
id: id,
|
|
1211
|
+
jsonrpc: '2.0',
|
|
1212
|
+
method: type,
|
|
1213
|
+
params: {
|
|
1214
|
+
input: input,
|
|
1215
|
+
path: path
|
|
1216
|
+
}
|
|
1217
|
+
};
|
|
1218
|
+
pendingRequests[id] = {
|
|
1219
|
+
ws: activeConnection,
|
|
1220
|
+
type: type,
|
|
1221
|
+
callbacks: callbacks,
|
|
1222
|
+
op: op
|
|
1223
|
+
}; // enqueue message
|
|
1224
|
+
|
|
1225
|
+
outgoing.push(envelope);
|
|
1226
|
+
dispatch();
|
|
1227
|
+
return function () {
|
|
1228
|
+
var _pendingRequests$id, _callbacks$complete;
|
|
1229
|
+
|
|
1230
|
+
var callbacks = (_pendingRequests$id = pendingRequests[id]) === null || _pendingRequests$id === void 0 ? void 0 : _pendingRequests$id.callbacks;
|
|
1231
|
+
delete pendingRequests[id];
|
|
1232
|
+
outgoing = outgoing.filter(function (msg) {
|
|
1233
|
+
return msg.id !== id;
|
|
1234
|
+
});
|
|
1235
|
+
callbacks === null || callbacks === void 0 ? void 0 : (_callbacks$complete = callbacks.complete) === null || _callbacks$complete === void 0 ? void 0 : _callbacks$complete.call(callbacks);
|
|
1236
|
+
|
|
1237
|
+
if (op.type === 'subscription') {
|
|
1238
|
+
outgoing.push({
|
|
1239
|
+
id: id,
|
|
1240
|
+
method: 'subscription.stop',
|
|
1241
|
+
params: undefined
|
|
1242
|
+
});
|
|
1243
|
+
dispatch();
|
|
1244
|
+
}
|
|
1245
|
+
};
|
|
1246
|
+
}
|
|
1247
|
+
|
|
1248
|
+
return {
|
|
1249
|
+
close: function close() {
|
|
1250
|
+
state = 'closed';
|
|
1251
|
+
closeIfNoPending(activeConnection);
|
|
1252
|
+
},
|
|
1253
|
+
request: request,
|
|
1254
|
+
getConnection: function getConnection() {
|
|
1255
|
+
return activeConnection;
|
|
1256
|
+
}
|
|
1257
|
+
};
|
|
1258
|
+
}
|
|
1259
|
+
|
|
1260
|
+
var TRPCWebSocketClosedError = /*#__PURE__*/function (_Error) {
|
|
1261
|
+
_inherits(TRPCWebSocketClosedError, _Error);
|
|
1262
|
+
|
|
1263
|
+
var _super = /*#__PURE__*/_createSuper(TRPCWebSocketClosedError);
|
|
1264
|
+
|
|
1265
|
+
function TRPCWebSocketClosedError(message) {
|
|
1266
|
+
var _this;
|
|
1267
|
+
|
|
1268
|
+
_classCallCheck(this, TRPCWebSocketClosedError);
|
|
1269
|
+
|
|
1270
|
+
_this = _super.call(this, message);
|
|
1271
|
+
_this.name = 'TRPCWebSocketClosedError';
|
|
1272
|
+
Object.setPrototypeOf(_assertThisInitialized(_this), TRPCWebSocketClosedError.prototype);
|
|
1273
|
+
return _this;
|
|
1274
|
+
}
|
|
1275
|
+
|
|
1276
|
+
return _createClass(TRPCWebSocketClosedError);
|
|
1277
|
+
}( /*#__PURE__*/_wrapNativeSuper(Error));
|
|
1278
|
+
|
|
1279
|
+
var TRPCSubscriptionEndedError = /*#__PURE__*/function (_Error2) {
|
|
1280
|
+
_inherits(TRPCSubscriptionEndedError, _Error2);
|
|
1281
|
+
|
|
1282
|
+
var _super2 = /*#__PURE__*/_createSuper(TRPCSubscriptionEndedError);
|
|
1283
|
+
|
|
1284
|
+
function TRPCSubscriptionEndedError(message) {
|
|
1285
|
+
var _this2;
|
|
1286
|
+
|
|
1287
|
+
_classCallCheck(this, TRPCSubscriptionEndedError);
|
|
1288
|
+
|
|
1289
|
+
_this2 = _super2.call(this, message);
|
|
1290
|
+
_this2.name = 'TRPCSubscriptionEndedError';
|
|
1291
|
+
Object.setPrototypeOf(_assertThisInitialized(_this2), TRPCSubscriptionEndedError.prototype);
|
|
1292
|
+
return _this2;
|
|
1293
|
+
}
|
|
1294
|
+
|
|
1295
|
+
return _createClass(TRPCSubscriptionEndedError);
|
|
1296
|
+
}( /*#__PURE__*/_wrapNativeSuper(Error));
|
|
1297
|
+
|
|
1298
|
+
function wsLink(opts) {
|
|
1299
|
+
return function () {
|
|
1300
|
+
var client = opts.client;
|
|
1301
|
+
return function (_ref2) {
|
|
1302
|
+
var op = _ref2.op;
|
|
1303
|
+
return observable(function (observer) {
|
|
1304
|
+
var type = op.type,
|
|
1305
|
+
input = op.input,
|
|
1306
|
+
path = op.path,
|
|
1307
|
+
id = op.id,
|
|
1308
|
+
context = op.context;
|
|
1309
|
+
var isDone = false;
|
|
1310
|
+
var unsub = client.request({
|
|
1311
|
+
type: type,
|
|
1312
|
+
path: path,
|
|
1313
|
+
input: input,
|
|
1314
|
+
id: id,
|
|
1315
|
+
context: context
|
|
1316
|
+
}, {
|
|
1317
|
+
error: function error(err) {
|
|
1318
|
+
isDone = true;
|
|
1319
|
+
observer.error(err);
|
|
1320
|
+
unsub();
|
|
1321
|
+
},
|
|
1322
|
+
complete: function complete() {
|
|
1323
|
+
if (!isDone) {
|
|
1324
|
+
isDone = true;
|
|
1325
|
+
observer.error(TRPCClientError.from(new TRPCSubscriptionEndedError('Operation ended prematurely')));
|
|
1326
|
+
} else {
|
|
1327
|
+
observer.complete();
|
|
1328
|
+
}
|
|
1329
|
+
},
|
|
1330
|
+
next: function next(result) {
|
|
1331
|
+
observer.next(result);
|
|
1332
|
+
|
|
1333
|
+
if (op.type !== 'subscription') {
|
|
1334
|
+
// if it isn't a subscription we don't care about next response
|
|
1335
|
+
isDone = true;
|
|
1336
|
+
unsub();
|
|
1337
|
+
observer.complete();
|
|
1338
|
+
}
|
|
1339
|
+
}
|
|
1340
|
+
});
|
|
1341
|
+
return function () {
|
|
1342
|
+
isDone = true;
|
|
1343
|
+
unsub();
|
|
1344
|
+
};
|
|
1345
|
+
});
|
|
1346
|
+
};
|
|
1347
|
+
};
|
|
1348
|
+
}
|
|
1349
|
+
|
|
1350
|
+
export { TRPCClientError, createTRPCClient, createWSClient, dedupeLink, getFetch, httpBatchLink, httpLink, loggerLink, retryLink, splitLink, transformerLink, wsLink };
|