@trpc/client 10.0.0-alpha.16 → 10.0.0-alpha.17
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 +586 -586
- package/dist/index.mjs +585 -585
- package/package.json +4 -44
- package/src/internals/TRPCClient.ts +1 -1
- package/src/links/splitLink.test.ts +2 -2
package/dist/index.js
CHANGED
|
@@ -6,24 +6,24 @@ var _classCallCheck = require('@babel/runtime/helpers/classCallCheck');
|
|
|
6
6
|
var _createClass = require('@babel/runtime/helpers/createClass');
|
|
7
7
|
var tap = require('./tap-fcd41ae6.js');
|
|
8
8
|
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
|
|
9
|
+
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
|
|
9
10
|
var _assertThisInitialized = require('@babel/runtime/helpers/assertThisInitialized');
|
|
10
11
|
var _inherits = require('@babel/runtime/helpers/inherits');
|
|
11
12
|
var _possibleConstructorReturn = require('@babel/runtime/helpers/possibleConstructorReturn');
|
|
12
13
|
var _getPrototypeOf = require('@babel/runtime/helpers/getPrototypeOf');
|
|
13
14
|
var _wrapNativeSuper = require('@babel/runtime/helpers/wrapNativeSuper');
|
|
14
|
-
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
|
|
15
15
|
|
|
16
16
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
17
17
|
|
|
18
18
|
var _classCallCheck__default = /*#__PURE__*/_interopDefaultLegacy(_classCallCheck);
|
|
19
19
|
var _createClass__default = /*#__PURE__*/_interopDefaultLegacy(_createClass);
|
|
20
20
|
var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
|
|
21
|
+
var _slicedToArray__default = /*#__PURE__*/_interopDefaultLegacy(_slicedToArray);
|
|
21
22
|
var _assertThisInitialized__default = /*#__PURE__*/_interopDefaultLegacy(_assertThisInitialized);
|
|
22
23
|
var _inherits__default = /*#__PURE__*/_interopDefaultLegacy(_inherits);
|
|
23
24
|
var _possibleConstructorReturn__default = /*#__PURE__*/_interopDefaultLegacy(_possibleConstructorReturn);
|
|
24
25
|
var _getPrototypeOf__default = /*#__PURE__*/_interopDefaultLegacy(_getPrototypeOf);
|
|
25
26
|
var _wrapNativeSuper__default = /*#__PURE__*/_interopDefaultLegacy(_wrapNativeSuper);
|
|
26
|
-
var _slicedToArray__default = /*#__PURE__*/_interopDefaultLegacy(_slicedToArray);
|
|
27
27
|
|
|
28
28
|
function getWindow() {
|
|
29
29
|
if (typeof window !== 'undefined') {
|
|
@@ -51,6 +51,59 @@ function getFetch(f) {
|
|
|
51
51
|
throw new Error('No fetch implementation found');
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
+
function dedupeLink() {
|
|
55
|
+
// initialized config
|
|
56
|
+
return function () {
|
|
57
|
+
// initialized in app
|
|
58
|
+
var pending = {};
|
|
59
|
+
return function (_ref) {
|
|
60
|
+
var op = _ref.op,
|
|
61
|
+
next = _ref.next;
|
|
62
|
+
|
|
63
|
+
// initialized for request
|
|
64
|
+
if (op.type !== 'query') {
|
|
65
|
+
// pass through
|
|
66
|
+
return next(op);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
var key = JSON.stringify([op.path, op.input]);
|
|
70
|
+
|
|
71
|
+
if (pending[key]) {
|
|
72
|
+
// console.log('hooking into pending', { op });
|
|
73
|
+
return tap.observable(function (observer) {
|
|
74
|
+
return pending[key].subscribe(observer);
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
var shared$ = tap.observable(function (observer) {
|
|
79
|
+
function reset() {
|
|
80
|
+
delete pending[key];
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
var subscription = next(op).subscribe({
|
|
84
|
+
next: function next(v) {
|
|
85
|
+
observer.next(v);
|
|
86
|
+
},
|
|
87
|
+
error: function error(e) {
|
|
88
|
+
reset();
|
|
89
|
+
observer.error(e);
|
|
90
|
+
},
|
|
91
|
+
complete: function complete() {
|
|
92
|
+
reset();
|
|
93
|
+
observer.complete();
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
return function () {
|
|
97
|
+
reset();
|
|
98
|
+
subscription.unsubscribe();
|
|
99
|
+
};
|
|
100
|
+
}).pipe(tap.share());
|
|
101
|
+
pending[key] = shared$;
|
|
102
|
+
return shared$;
|
|
103
|
+
};
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
54
107
|
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; } } }; }
|
|
55
108
|
|
|
56
109
|
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); }
|
|
@@ -325,6 +378,196 @@ function httpBatchLink(opts) {
|
|
|
325
378
|
};
|
|
326
379
|
}
|
|
327
380
|
|
|
381
|
+
function httpLink(opts) {
|
|
382
|
+
var url = opts.url;
|
|
383
|
+
return function (runtime) {
|
|
384
|
+
return function (_ref) {
|
|
385
|
+
var op = _ref.op;
|
|
386
|
+
return tap.observable(function (observer) {
|
|
387
|
+
var path = op.path,
|
|
388
|
+
input = op.input,
|
|
389
|
+
type = op.type;
|
|
390
|
+
|
|
391
|
+
var _httpRequest = httpRequest({
|
|
392
|
+
runtime: runtime,
|
|
393
|
+
path: path,
|
|
394
|
+
input: input,
|
|
395
|
+
type: type,
|
|
396
|
+
url: url
|
|
397
|
+
}),
|
|
398
|
+
promise = _httpRequest.promise,
|
|
399
|
+
cancel = _httpRequest.cancel;
|
|
400
|
+
|
|
401
|
+
promise.then(function (res) {
|
|
402
|
+
observer.next({
|
|
403
|
+
context: res.meta,
|
|
404
|
+
data: res.json
|
|
405
|
+
});
|
|
406
|
+
observer.complete();
|
|
407
|
+
}).catch(observer.error);
|
|
408
|
+
return function () {
|
|
409
|
+
cancel();
|
|
410
|
+
};
|
|
411
|
+
});
|
|
412
|
+
};
|
|
413
|
+
};
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
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; }
|
|
417
|
+
|
|
418
|
+
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__default["default"](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; }
|
|
419
|
+
var palette = {
|
|
420
|
+
query: ['72e3ff', '3fb0d8'],
|
|
421
|
+
mutation: ['c5a3fc', '904dfc'],
|
|
422
|
+
subscription: ['ff49e1', 'd83fbe']
|
|
423
|
+
};
|
|
424
|
+
|
|
425
|
+
// maybe this should be moved to it's own package
|
|
426
|
+
var defaultLogger = function defaultLogger() {
|
|
427
|
+
var c = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : console;
|
|
428
|
+
return function (props) {
|
|
429
|
+
var direction = props.direction,
|
|
430
|
+
input = props.input,
|
|
431
|
+
type = props.type,
|
|
432
|
+
path = props.path,
|
|
433
|
+
context = props.context,
|
|
434
|
+
id = props.id;
|
|
435
|
+
|
|
436
|
+
var _palette$type = _slicedToArray__default["default"](palette[type], 2),
|
|
437
|
+
light = _palette$type[0],
|
|
438
|
+
dark = _palette$type[1];
|
|
439
|
+
|
|
440
|
+
var css = "\n background-color: #".concat(direction === 'up' ? light : dark, "; \n color: ").concat(direction === 'up' ? 'black' : 'white', ";\n padding: 2px;\n ");
|
|
441
|
+
var parts = ['%c', direction === 'up' ? '>>' : '<<', type, "#".concat(id), "%c".concat(path, "%c"), '%O'];
|
|
442
|
+
var args = [css, "".concat(css, "; font-weight: bold;"), "".concat(css, "; font-weight: normal;")];
|
|
443
|
+
|
|
444
|
+
if (props.direction === 'up') {
|
|
445
|
+
args.push({
|
|
446
|
+
input: input,
|
|
447
|
+
context: context
|
|
448
|
+
});
|
|
449
|
+
} else {
|
|
450
|
+
args.push({
|
|
451
|
+
input: input,
|
|
452
|
+
result: props.result,
|
|
453
|
+
elapsedMs: props.elapsedMs,
|
|
454
|
+
context: context
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
var fn = props.direction === 'down' && props.result && (props.result instanceof Error || 'error' in props.result.data) ? 'error' : 'log';
|
|
459
|
+
c[fn].apply(null, [parts.join(' ')].concat(args));
|
|
460
|
+
};
|
|
461
|
+
};
|
|
462
|
+
|
|
463
|
+
function loggerLink() {
|
|
464
|
+
var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
465
|
+
var _opts$enabled = opts.enabled,
|
|
466
|
+
enabled = _opts$enabled === void 0 ? function () {
|
|
467
|
+
return true;
|
|
468
|
+
} : _opts$enabled;
|
|
469
|
+
var _opts$logger = opts.logger,
|
|
470
|
+
logger = _opts$logger === void 0 ? defaultLogger(opts.console) : _opts$logger;
|
|
471
|
+
return function () {
|
|
472
|
+
return function (_ref) {
|
|
473
|
+
var op = _ref.op,
|
|
474
|
+
next = _ref.next;
|
|
475
|
+
return tap.observable(function (observer) {
|
|
476
|
+
// ->
|
|
477
|
+
enabled(_objectSpread$2(_objectSpread$2({}, op), {}, {
|
|
478
|
+
direction: 'up'
|
|
479
|
+
})) && logger(_objectSpread$2(_objectSpread$2({}, op), {}, {
|
|
480
|
+
direction: 'up'
|
|
481
|
+
}));
|
|
482
|
+
var requestStartTime = Date.now();
|
|
483
|
+
|
|
484
|
+
function logResult(result) {
|
|
485
|
+
var elapsedMs = Date.now() - requestStartTime;
|
|
486
|
+
enabled(_objectSpread$2(_objectSpread$2({}, op), {}, {
|
|
487
|
+
direction: 'down',
|
|
488
|
+
result: result
|
|
489
|
+
})) && logger(_objectSpread$2(_objectSpread$2({}, op), {}, {
|
|
490
|
+
direction: 'down',
|
|
491
|
+
elapsedMs: elapsedMs,
|
|
492
|
+
result: result
|
|
493
|
+
}));
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
return next(op).pipe(tap.tap({
|
|
497
|
+
next: function next(result) {
|
|
498
|
+
logResult(result);
|
|
499
|
+
},
|
|
500
|
+
error: function error(result) {
|
|
501
|
+
logResult(result);
|
|
502
|
+
}
|
|
503
|
+
})).subscribe(observer);
|
|
504
|
+
});
|
|
505
|
+
};
|
|
506
|
+
};
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
function retryLink(opts) {
|
|
510
|
+
// initialized config
|
|
511
|
+
return function () {
|
|
512
|
+
// initialized in app
|
|
513
|
+
return function (_ref) {
|
|
514
|
+
var op = _ref.op,
|
|
515
|
+
next = _ref.next;
|
|
516
|
+
// initialized for request
|
|
517
|
+
return tap.observable(function (observer) {
|
|
518
|
+
var next$ = null;
|
|
519
|
+
var attempts = 0;
|
|
520
|
+
var isDone = false;
|
|
521
|
+
|
|
522
|
+
function attempt() {
|
|
523
|
+
var _next$;
|
|
524
|
+
|
|
525
|
+
attempts++;
|
|
526
|
+
(_next$ = next$) === null || _next$ === void 0 ? void 0 : _next$.unsubscribe();
|
|
527
|
+
next$ = next(op).subscribe({
|
|
528
|
+
error: function error(_error) {
|
|
529
|
+
if (attempts >= opts.attempts) {
|
|
530
|
+
observer.error(_error);
|
|
531
|
+
return;
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
attempt();
|
|
535
|
+
},
|
|
536
|
+
next: function next(result) {
|
|
537
|
+
if ('result' in result.data) {
|
|
538
|
+
isDone = true;
|
|
539
|
+
observer.next(result);
|
|
540
|
+
return;
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
if (attempts >= opts.attempts) {
|
|
544
|
+
isDone = true;
|
|
545
|
+
observer.next(result);
|
|
546
|
+
return;
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
attempt();
|
|
550
|
+
},
|
|
551
|
+
complete: function complete() {
|
|
552
|
+
if (isDone) {
|
|
553
|
+
observer.complete();
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
});
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
attempt();
|
|
560
|
+
return function () {
|
|
561
|
+
var _next$2;
|
|
562
|
+
|
|
563
|
+
isDone = true;
|
|
564
|
+
(_next$2 = next$) === null || _next$2 === void 0 ? void 0 : _next$2.unsubscribe();
|
|
565
|
+
};
|
|
566
|
+
});
|
|
567
|
+
};
|
|
568
|
+
};
|
|
569
|
+
}
|
|
570
|
+
|
|
328
571
|
/** @internal */
|
|
329
572
|
function createChain(opts) {
|
|
330
573
|
return tap.observable(function (observer) {
|
|
@@ -347,576 +590,9 @@ function createChain(opts) {
|
|
|
347
590
|
});
|
|
348
591
|
}
|
|
349
592
|
|
|
350
|
-
function
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
function _createSuper$2(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct$2(); return function _createSuperInternal() { var Super = _getPrototypeOf__default["default"](Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf__default["default"](this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn__default["default"](this, result); }; }
|
|
355
|
-
|
|
356
|
-
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; } }
|
|
357
|
-
|
|
358
|
-
var TRPCClientError = /*#__PURE__*/function (_Error) {
|
|
359
|
-
_inherits__default["default"](TRPCClientError, _Error);
|
|
360
|
-
|
|
361
|
-
var _super = /*#__PURE__*/_createSuper$2(TRPCClientError);
|
|
362
|
-
|
|
363
|
-
/**
|
|
364
|
-
* @deprecated use `cause`
|
|
365
|
-
*/
|
|
366
|
-
function TRPCClientError(message, opts) {
|
|
367
|
-
var _opts$cause, _opts$result, _opts$result2;
|
|
368
|
-
|
|
369
|
-
var _this;
|
|
370
|
-
|
|
371
|
-
_classCallCheck__default["default"](this, TRPCClientError);
|
|
372
|
-
|
|
373
|
-
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
|
|
374
|
-
// @ts-ignore https://github.com/tc39/proposal-error-cause
|
|
375
|
-
|
|
376
|
-
_this = _super.call(this, message, {
|
|
377
|
-
cause: cause
|
|
378
|
-
});
|
|
379
|
-
_this.originalError = void 0;
|
|
380
|
-
_this.cause = void 0;
|
|
381
|
-
_this.shape = void 0;
|
|
382
|
-
_this.data = void 0;
|
|
383
|
-
_this.meta = void 0;
|
|
384
|
-
_this.meta = opts === null || opts === void 0 ? void 0 : opts.meta;
|
|
385
|
-
_this.cause = _this.originalError = cause;
|
|
386
|
-
_this.shape = opts === null || opts === void 0 ? void 0 : (_opts$result = opts.result) === null || _opts$result === void 0 ? void 0 : _opts$result.error;
|
|
387
|
-
_this.data = opts === null || opts === void 0 ? void 0 : (_opts$result2 = opts.result) === null || _opts$result2 === void 0 ? void 0 : _opts$result2.error.data;
|
|
388
|
-
_this.name = 'TRPCClientError';
|
|
389
|
-
Object.setPrototypeOf(_assertThisInitialized__default["default"](_this), TRPCClientError.prototype);
|
|
390
|
-
return _this;
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
_createClass__default["default"](TRPCClientError, null, [{
|
|
394
|
-
key: "from",
|
|
395
|
-
value: function from(cause) {
|
|
396
|
-
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
397
|
-
|
|
398
|
-
if (!(cause instanceof Error)) {
|
|
399
|
-
var _message;
|
|
400
|
-
|
|
401
|
-
return new TRPCClientError((_message = cause.error.message) !== null && _message !== void 0 ? _message : '', _objectSpread$2(_objectSpread$2({}, opts), {}, {
|
|
402
|
-
cause: null,
|
|
403
|
-
result: cause
|
|
404
|
-
}));
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
if (cause.name === 'TRPCClientError') {
|
|
408
|
-
return cause;
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
return new TRPCClientError(cause.message, _objectSpread$2(_objectSpread$2({}, opts), {}, {
|
|
412
|
-
cause: cause,
|
|
413
|
-
result: null
|
|
414
|
-
}));
|
|
415
|
-
}
|
|
416
|
-
}]);
|
|
417
|
-
|
|
418
|
-
return TRPCClientError;
|
|
419
|
-
}( /*#__PURE__*/_wrapNativeSuper__default["default"](Error));
|
|
420
|
-
|
|
421
|
-
/** @internal */
|
|
422
|
-
function transformOperationResult$1(result) {
|
|
423
|
-
var context = result.context;
|
|
424
|
-
|
|
425
|
-
if ('error' in result.data) {
|
|
426
|
-
var error = TRPCClientError.from(result.data);
|
|
427
|
-
return {
|
|
428
|
-
ok: false,
|
|
429
|
-
error: error,
|
|
430
|
-
context: context
|
|
431
|
-
};
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
var data = result.data.result.data;
|
|
435
|
-
return {
|
|
436
|
-
ok: true,
|
|
437
|
-
data: data,
|
|
438
|
-
context: context
|
|
439
|
-
};
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
function _createSuper$1(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct$1(); return function _createSuperInternal() { var Super = _getPrototypeOf__default["default"](Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf__default["default"](this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn__default["default"](this, result); }; }
|
|
443
|
-
|
|
444
|
-
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; } }
|
|
445
|
-
|
|
446
|
-
var ObservableAbortError = /*#__PURE__*/function (_Error) {
|
|
447
|
-
_inherits__default["default"](ObservableAbortError, _Error);
|
|
448
|
-
|
|
449
|
-
var _super = /*#__PURE__*/_createSuper$1(ObservableAbortError);
|
|
450
|
-
|
|
451
|
-
function ObservableAbortError(message) {
|
|
452
|
-
var _this;
|
|
453
|
-
|
|
454
|
-
_classCallCheck__default["default"](this, ObservableAbortError);
|
|
455
|
-
|
|
456
|
-
_this = _super.call(this, message);
|
|
457
|
-
_this.name = 'ObservableAbortError';
|
|
458
|
-
Object.setPrototypeOf(_assertThisInitialized__default["default"](_this), ObservableAbortError.prototype);
|
|
459
|
-
return _this;
|
|
460
|
-
}
|
|
461
|
-
|
|
462
|
-
return _createClass__default["default"](ObservableAbortError);
|
|
463
|
-
}( /*#__PURE__*/_wrapNativeSuper__default["default"](Error));
|
|
464
|
-
/** @internal */
|
|
465
|
-
|
|
466
|
-
function observableToPromise(observable) {
|
|
467
|
-
var abort;
|
|
468
|
-
var promise = new Promise(function (resolve, reject) {
|
|
469
|
-
var isDone = false;
|
|
470
|
-
|
|
471
|
-
function onDone() {
|
|
472
|
-
if (isDone) {
|
|
473
|
-
return;
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
isDone = true;
|
|
477
|
-
reject(new ObservableAbortError('This operation was cancelled.'));
|
|
478
|
-
obs$.unsubscribe();
|
|
479
|
-
}
|
|
480
|
-
|
|
481
|
-
var obs$ = observable.subscribe({
|
|
482
|
-
next: function next(data) {
|
|
483
|
-
isDone = true;
|
|
484
|
-
resolve(data);
|
|
485
|
-
onDone();
|
|
486
|
-
},
|
|
487
|
-
error: function error(data) {
|
|
488
|
-
isDone = true;
|
|
489
|
-
reject(data);
|
|
490
|
-
onDone();
|
|
491
|
-
},
|
|
492
|
-
complete: function complete() {
|
|
493
|
-
isDone = true;
|
|
494
|
-
onDone();
|
|
495
|
-
}
|
|
496
|
-
});
|
|
497
|
-
abort = onDone;
|
|
498
|
-
});
|
|
499
|
-
return {
|
|
500
|
-
promise: promise,
|
|
501
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
502
|
-
abort: abort
|
|
503
|
-
};
|
|
504
|
-
}
|
|
505
|
-
|
|
506
|
-
var idCounter = 0;
|
|
507
|
-
|
|
508
|
-
function getRequestId() {
|
|
509
|
-
return ++idCounter;
|
|
510
|
-
}
|
|
511
|
-
|
|
512
|
-
var TRPCClient = /*#__PURE__*/function () {
|
|
513
|
-
function TRPCClient(opts) {
|
|
514
|
-
var _this = this;
|
|
515
|
-
|
|
516
|
-
_classCallCheck__default["default"](this, TRPCClient);
|
|
517
|
-
|
|
518
|
-
this.links = void 0;
|
|
519
|
-
this.runtime = void 0;
|
|
520
|
-
|
|
521
|
-
var _fetch = getFetch(opts === null || opts === void 0 ? void 0 : opts.fetch);
|
|
522
|
-
|
|
523
|
-
var AC = getAbortController(opts === null || opts === void 0 ? void 0 : opts.AbortController);
|
|
524
|
-
|
|
525
|
-
function getHeadersFn() {
|
|
526
|
-
if (opts.headers) {
|
|
527
|
-
var headers = opts.headers;
|
|
528
|
-
return typeof headers === 'function' ? headers : function () {
|
|
529
|
-
return headers;
|
|
530
|
-
};
|
|
531
|
-
}
|
|
532
|
-
|
|
533
|
-
return function () {
|
|
534
|
-
return {};
|
|
535
|
-
};
|
|
536
|
-
}
|
|
537
|
-
|
|
538
|
-
this.runtime = {
|
|
539
|
-
AbortController: AC,
|
|
540
|
-
fetch: _fetch,
|
|
541
|
-
headers: getHeadersFn()
|
|
542
|
-
};
|
|
543
|
-
|
|
544
|
-
if ('links' in opts) {
|
|
545
|
-
this.links = opts.links.map(function (link) {
|
|
546
|
-
return link(_this.runtime);
|
|
547
|
-
});
|
|
548
|
-
} else {
|
|
549
|
-
this.links = [httpBatchLink({
|
|
550
|
-
url: opts.url
|
|
551
|
-
})(this.runtime)];
|
|
552
|
-
}
|
|
553
|
-
}
|
|
554
|
-
|
|
555
|
-
_createClass__default["default"](TRPCClient, [{
|
|
556
|
-
key: "$request",
|
|
557
|
-
value: function $request(_ref) {
|
|
558
|
-
var type = _ref.type,
|
|
559
|
-
input = _ref.input,
|
|
560
|
-
path = _ref.path,
|
|
561
|
-
_ref$context = _ref.context,
|
|
562
|
-
context = _ref$context === void 0 ? {} : _ref$context;
|
|
563
|
-
var chain$ = createChain({
|
|
564
|
-
links: this.links,
|
|
565
|
-
op: {
|
|
566
|
-
id: getRequestId(),
|
|
567
|
-
type: type,
|
|
568
|
-
path: path,
|
|
569
|
-
input: input,
|
|
570
|
-
context: context
|
|
571
|
-
}
|
|
572
|
-
});
|
|
573
|
-
return chain$.pipe(tap.share());
|
|
574
|
-
}
|
|
575
|
-
}, {
|
|
576
|
-
key: "requestAsPromise",
|
|
577
|
-
value: function requestAsPromise(opts) {
|
|
578
|
-
var req$ = this.$request(opts);
|
|
579
|
-
|
|
580
|
-
var _observableToPromise = observableToPromise(req$),
|
|
581
|
-
promise = _observableToPromise.promise,
|
|
582
|
-
abort = _observableToPromise.abort;
|
|
583
|
-
|
|
584
|
-
var cancellablePromise = new Promise(function (resolve, reject) {
|
|
585
|
-
promise.then(function (result) {
|
|
586
|
-
var transformed = transformOperationResult$1(result);
|
|
587
|
-
|
|
588
|
-
if (transformed.ok) {
|
|
589
|
-
resolve(transformed.data);
|
|
590
|
-
return;
|
|
591
|
-
}
|
|
592
|
-
|
|
593
|
-
reject(transformed.error);
|
|
594
|
-
}).catch(function (err) {
|
|
595
|
-
reject(TRPCClientError.from(err));
|
|
596
|
-
});
|
|
597
|
-
});
|
|
598
|
-
cancellablePromise.cancel = abort;
|
|
599
|
-
return cancellablePromise;
|
|
600
|
-
}
|
|
601
|
-
}, {
|
|
602
|
-
key: "query",
|
|
603
|
-
value: function query(path) {
|
|
604
|
-
var _ref2;
|
|
605
|
-
|
|
606
|
-
var context = (_ref2 = arguments.length <= 2 ? undefined : arguments[2]) === null || _ref2 === void 0 ? void 0 : _ref2.context;
|
|
607
|
-
return this.requestAsPromise({
|
|
608
|
-
type: 'query',
|
|
609
|
-
path: path,
|
|
610
|
-
input: arguments.length <= 1 ? undefined : arguments[1],
|
|
611
|
-
context: context
|
|
612
|
-
});
|
|
613
|
-
}
|
|
614
|
-
}, {
|
|
615
|
-
key: "mutation",
|
|
616
|
-
value: function mutation(path) {
|
|
617
|
-
var _ref3;
|
|
618
|
-
|
|
619
|
-
var context = (_ref3 = arguments.length <= 2 ? undefined : arguments[2]) === null || _ref3 === void 0 ? void 0 : _ref3.context;
|
|
620
|
-
return this.requestAsPromise({
|
|
621
|
-
type: 'mutation',
|
|
622
|
-
path: path,
|
|
623
|
-
input: arguments.length <= 1 ? undefined : arguments[1],
|
|
624
|
-
context: context
|
|
625
|
-
});
|
|
626
|
-
}
|
|
627
|
-
}, {
|
|
628
|
-
key: "subscription",
|
|
629
|
-
value: function subscription(path, input, opts) {
|
|
630
|
-
var observable$ = this.$request({
|
|
631
|
-
type: 'subscription',
|
|
632
|
-
path: path,
|
|
633
|
-
input: input,
|
|
634
|
-
context: opts.context
|
|
635
|
-
});
|
|
636
|
-
return observable$.subscribe({
|
|
637
|
-
next: function next(result) {
|
|
638
|
-
var _opts$next;
|
|
639
|
-
|
|
640
|
-
if ('error' in result.data) {
|
|
641
|
-
var _opts$error;
|
|
642
|
-
|
|
643
|
-
var err = TRPCClientError.from(result.data, {
|
|
644
|
-
meta: result.context
|
|
645
|
-
});
|
|
646
|
-
(_opts$error = opts.error) === null || _opts$error === void 0 ? void 0 : _opts$error.call(opts, err);
|
|
647
|
-
return;
|
|
648
|
-
}
|
|
649
|
-
|
|
650
|
-
(_opts$next = opts.next) === null || _opts$next === void 0 ? void 0 : _opts$next.call(opts, result.data.result);
|
|
651
|
-
},
|
|
652
|
-
error: function error(err) {
|
|
653
|
-
var _opts$error2;
|
|
654
|
-
|
|
655
|
-
(_opts$error2 = opts.error) === null || _opts$error2 === void 0 ? void 0 : _opts$error2.call(opts, err);
|
|
656
|
-
},
|
|
657
|
-
complete: function complete() {
|
|
658
|
-
var _opts$complete;
|
|
659
|
-
|
|
660
|
-
(_opts$complete = opts.complete) === null || _opts$complete === void 0 ? void 0 : _opts$complete.call(opts);
|
|
661
|
-
}
|
|
662
|
-
});
|
|
663
|
-
}
|
|
664
|
-
}]);
|
|
665
|
-
|
|
666
|
-
return TRPCClient;
|
|
667
|
-
}();
|
|
668
|
-
|
|
669
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
670
|
-
function createTRPCClient(opts) {
|
|
671
|
-
return new TRPCClient(opts);
|
|
672
|
-
}
|
|
673
|
-
|
|
674
|
-
function dedupeLink() {
|
|
675
|
-
// initialized config
|
|
676
|
-
return function () {
|
|
677
|
-
// initialized in app
|
|
678
|
-
var pending = {};
|
|
679
|
-
return function (_ref) {
|
|
680
|
-
var op = _ref.op,
|
|
681
|
-
next = _ref.next;
|
|
682
|
-
|
|
683
|
-
// initialized for request
|
|
684
|
-
if (op.type !== 'query') {
|
|
685
|
-
// pass through
|
|
686
|
-
return next(op);
|
|
687
|
-
}
|
|
688
|
-
|
|
689
|
-
var key = JSON.stringify([op.path, op.input]);
|
|
690
|
-
|
|
691
|
-
if (pending[key]) {
|
|
692
|
-
// console.log('hooking into pending', { op });
|
|
693
|
-
return tap.observable(function (observer) {
|
|
694
|
-
return pending[key].subscribe(observer);
|
|
695
|
-
});
|
|
696
|
-
}
|
|
697
|
-
|
|
698
|
-
var shared$ = tap.observable(function (observer) {
|
|
699
|
-
function reset() {
|
|
700
|
-
delete pending[key];
|
|
701
|
-
}
|
|
702
|
-
|
|
703
|
-
var subscription = next(op).subscribe({
|
|
704
|
-
next: function next(v) {
|
|
705
|
-
observer.next(v);
|
|
706
|
-
},
|
|
707
|
-
error: function error(e) {
|
|
708
|
-
reset();
|
|
709
|
-
observer.error(e);
|
|
710
|
-
},
|
|
711
|
-
complete: function complete() {
|
|
712
|
-
reset();
|
|
713
|
-
observer.complete();
|
|
714
|
-
}
|
|
715
|
-
});
|
|
716
|
-
return function () {
|
|
717
|
-
reset();
|
|
718
|
-
subscription.unsubscribe();
|
|
719
|
-
};
|
|
720
|
-
}).pipe(tap.share());
|
|
721
|
-
pending[key] = shared$;
|
|
722
|
-
return shared$;
|
|
723
|
-
};
|
|
724
|
-
};
|
|
725
|
-
}
|
|
726
|
-
|
|
727
|
-
function httpLink(opts) {
|
|
728
|
-
var url = opts.url;
|
|
729
|
-
return function (runtime) {
|
|
730
|
-
return function (_ref) {
|
|
731
|
-
var op = _ref.op;
|
|
732
|
-
return tap.observable(function (observer) {
|
|
733
|
-
var path = op.path,
|
|
734
|
-
input = op.input,
|
|
735
|
-
type = op.type;
|
|
736
|
-
|
|
737
|
-
var _httpRequest = httpRequest({
|
|
738
|
-
runtime: runtime,
|
|
739
|
-
path: path,
|
|
740
|
-
input: input,
|
|
741
|
-
type: type,
|
|
742
|
-
url: url
|
|
743
|
-
}),
|
|
744
|
-
promise = _httpRequest.promise,
|
|
745
|
-
cancel = _httpRequest.cancel;
|
|
746
|
-
|
|
747
|
-
promise.then(function (res) {
|
|
748
|
-
observer.next({
|
|
749
|
-
context: res.meta,
|
|
750
|
-
data: res.json
|
|
751
|
-
});
|
|
752
|
-
observer.complete();
|
|
753
|
-
}).catch(observer.error);
|
|
754
|
-
return function () {
|
|
755
|
-
cancel();
|
|
756
|
-
};
|
|
757
|
-
});
|
|
758
|
-
};
|
|
759
|
-
};
|
|
760
|
-
}
|
|
761
|
-
|
|
762
|
-
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; }
|
|
763
|
-
|
|
764
|
-
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__default["default"](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; }
|
|
765
|
-
var palette = {
|
|
766
|
-
query: ['72e3ff', '3fb0d8'],
|
|
767
|
-
mutation: ['c5a3fc', '904dfc'],
|
|
768
|
-
subscription: ['ff49e1', 'd83fbe']
|
|
769
|
-
};
|
|
770
|
-
|
|
771
|
-
// maybe this should be moved to it's own package
|
|
772
|
-
var defaultLogger = function defaultLogger() {
|
|
773
|
-
var c = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : console;
|
|
774
|
-
return function (props) {
|
|
775
|
-
var direction = props.direction,
|
|
776
|
-
input = props.input,
|
|
777
|
-
type = props.type,
|
|
778
|
-
path = props.path,
|
|
779
|
-
context = props.context,
|
|
780
|
-
id = props.id;
|
|
781
|
-
|
|
782
|
-
var _palette$type = _slicedToArray__default["default"](palette[type], 2),
|
|
783
|
-
light = _palette$type[0],
|
|
784
|
-
dark = _palette$type[1];
|
|
785
|
-
|
|
786
|
-
var css = "\n background-color: #".concat(direction === 'up' ? light : dark, "; \n color: ").concat(direction === 'up' ? 'black' : 'white', ";\n padding: 2px;\n ");
|
|
787
|
-
var parts = ['%c', direction === 'up' ? '>>' : '<<', type, "#".concat(id), "%c".concat(path, "%c"), '%O'];
|
|
788
|
-
var args = [css, "".concat(css, "; font-weight: bold;"), "".concat(css, "; font-weight: normal;")];
|
|
789
|
-
|
|
790
|
-
if (props.direction === 'up') {
|
|
791
|
-
args.push({
|
|
792
|
-
input: input,
|
|
793
|
-
context: context
|
|
794
|
-
});
|
|
795
|
-
} else {
|
|
796
|
-
args.push({
|
|
797
|
-
input: input,
|
|
798
|
-
result: props.result,
|
|
799
|
-
elapsedMs: props.elapsedMs,
|
|
800
|
-
context: context
|
|
801
|
-
});
|
|
802
|
-
}
|
|
803
|
-
|
|
804
|
-
var fn = props.direction === 'down' && props.result && (props.result instanceof Error || 'error' in props.result.data) ? 'error' : 'log';
|
|
805
|
-
c[fn].apply(null, [parts.join(' ')].concat(args));
|
|
806
|
-
};
|
|
807
|
-
};
|
|
808
|
-
|
|
809
|
-
function loggerLink() {
|
|
810
|
-
var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
811
|
-
var _opts$enabled = opts.enabled,
|
|
812
|
-
enabled = _opts$enabled === void 0 ? function () {
|
|
813
|
-
return true;
|
|
814
|
-
} : _opts$enabled;
|
|
815
|
-
var _opts$logger = opts.logger,
|
|
816
|
-
logger = _opts$logger === void 0 ? defaultLogger(opts.console) : _opts$logger;
|
|
817
|
-
return function () {
|
|
818
|
-
return function (_ref) {
|
|
819
|
-
var op = _ref.op,
|
|
820
|
-
next = _ref.next;
|
|
821
|
-
return tap.observable(function (observer) {
|
|
822
|
-
// ->
|
|
823
|
-
enabled(_objectSpread$1(_objectSpread$1({}, op), {}, {
|
|
824
|
-
direction: 'up'
|
|
825
|
-
})) && logger(_objectSpread$1(_objectSpread$1({}, op), {}, {
|
|
826
|
-
direction: 'up'
|
|
827
|
-
}));
|
|
828
|
-
var requestStartTime = Date.now();
|
|
829
|
-
|
|
830
|
-
function logResult(result) {
|
|
831
|
-
var elapsedMs = Date.now() - requestStartTime;
|
|
832
|
-
enabled(_objectSpread$1(_objectSpread$1({}, op), {}, {
|
|
833
|
-
direction: 'down',
|
|
834
|
-
result: result
|
|
835
|
-
})) && logger(_objectSpread$1(_objectSpread$1({}, op), {}, {
|
|
836
|
-
direction: 'down',
|
|
837
|
-
elapsedMs: elapsedMs,
|
|
838
|
-
result: result
|
|
839
|
-
}));
|
|
840
|
-
}
|
|
841
|
-
|
|
842
|
-
return next(op).pipe(tap.tap({
|
|
843
|
-
next: function next(result) {
|
|
844
|
-
logResult(result);
|
|
845
|
-
},
|
|
846
|
-
error: function error(result) {
|
|
847
|
-
logResult(result);
|
|
848
|
-
}
|
|
849
|
-
})).subscribe(observer);
|
|
850
|
-
});
|
|
851
|
-
};
|
|
852
|
-
};
|
|
853
|
-
}
|
|
854
|
-
|
|
855
|
-
function retryLink(opts) {
|
|
856
|
-
// initialized config
|
|
857
|
-
return function () {
|
|
858
|
-
// initialized in app
|
|
859
|
-
return function (_ref) {
|
|
860
|
-
var op = _ref.op,
|
|
861
|
-
next = _ref.next;
|
|
862
|
-
// initialized for request
|
|
863
|
-
return tap.observable(function (observer) {
|
|
864
|
-
var next$ = null;
|
|
865
|
-
var attempts = 0;
|
|
866
|
-
var isDone = false;
|
|
867
|
-
|
|
868
|
-
function attempt() {
|
|
869
|
-
var _next$;
|
|
870
|
-
|
|
871
|
-
attempts++;
|
|
872
|
-
(_next$ = next$) === null || _next$ === void 0 ? void 0 : _next$.unsubscribe();
|
|
873
|
-
next$ = next(op).subscribe({
|
|
874
|
-
error: function error(_error) {
|
|
875
|
-
if (attempts >= opts.attempts) {
|
|
876
|
-
observer.error(_error);
|
|
877
|
-
return;
|
|
878
|
-
}
|
|
879
|
-
|
|
880
|
-
attempt();
|
|
881
|
-
},
|
|
882
|
-
next: function next(result) {
|
|
883
|
-
if ('result' in result.data) {
|
|
884
|
-
isDone = true;
|
|
885
|
-
observer.next(result);
|
|
886
|
-
return;
|
|
887
|
-
}
|
|
888
|
-
|
|
889
|
-
if (attempts >= opts.attempts) {
|
|
890
|
-
isDone = true;
|
|
891
|
-
observer.next(result);
|
|
892
|
-
return;
|
|
893
|
-
}
|
|
894
|
-
|
|
895
|
-
attempt();
|
|
896
|
-
},
|
|
897
|
-
complete: function complete() {
|
|
898
|
-
if (isDone) {
|
|
899
|
-
observer.complete();
|
|
900
|
-
}
|
|
901
|
-
}
|
|
902
|
-
});
|
|
903
|
-
}
|
|
904
|
-
|
|
905
|
-
attempt();
|
|
906
|
-
return function () {
|
|
907
|
-
var _next$2;
|
|
908
|
-
|
|
909
|
-
isDone = true;
|
|
910
|
-
(_next$2 = next$) === null || _next$2 === void 0 ? void 0 : _next$2.unsubscribe();
|
|
911
|
-
};
|
|
912
|
-
});
|
|
913
|
-
};
|
|
914
|
-
};
|
|
915
|
-
}
|
|
916
|
-
|
|
917
|
-
function asArray(value) {
|
|
918
|
-
return Array.isArray(value) ? value : [value];
|
|
919
|
-
}
|
|
593
|
+
function asArray(value) {
|
|
594
|
+
return Array.isArray(value) ? value : [value];
|
|
595
|
+
}
|
|
920
596
|
|
|
921
597
|
function splitLink(opts) {
|
|
922
598
|
return function (rt) {
|
|
@@ -938,17 +614,17 @@ function splitLink(opts) {
|
|
|
938
614
|
};
|
|
939
615
|
}
|
|
940
616
|
|
|
941
|
-
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; }
|
|
617
|
+
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; }
|
|
942
618
|
|
|
943
|
-
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__default["default"](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; }
|
|
619
|
+
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__default["default"](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; }
|
|
944
620
|
|
|
945
621
|
/**
|
|
946
622
|
* @internal
|
|
947
623
|
*/
|
|
948
|
-
function transformOperationResult(result, transformer) {
|
|
624
|
+
function transformOperationResult$1(result, transformer) {
|
|
949
625
|
if ('error' in result.data) {
|
|
950
|
-
return _objectSpread(_objectSpread({}, result), {}, {
|
|
951
|
-
data: _objectSpread(_objectSpread({}, result.data), {}, {
|
|
626
|
+
return _objectSpread$1(_objectSpread$1({}, result), {}, {
|
|
627
|
+
data: _objectSpread$1(_objectSpread$1({}, result.data), {}, {
|
|
952
628
|
error: transformer.deserialize(result.data.error)
|
|
953
629
|
})
|
|
954
630
|
});
|
|
@@ -958,9 +634,9 @@ function transformOperationResult(result, transformer) {
|
|
|
958
634
|
return result;
|
|
959
635
|
}
|
|
960
636
|
|
|
961
|
-
return _objectSpread(_objectSpread({}, result), {}, {
|
|
962
|
-
data: _objectSpread(_objectSpread({}, result.data), {}, {
|
|
963
|
-
result: _objectSpread(_objectSpread({}, result.data.result), {}, {
|
|
637
|
+
return _objectSpread$1(_objectSpread$1({}, result), {}, {
|
|
638
|
+
data: _objectSpread$1(_objectSpread$1({}, result.data), {}, {
|
|
639
|
+
result: _objectSpread$1(_objectSpread$1({}, result.data.result), {}, {
|
|
964
640
|
data: transformer.deserialize(result.data.result.data)
|
|
965
641
|
})
|
|
966
642
|
})
|
|
@@ -985,11 +661,11 @@ function transformerLink(transformer) {
|
|
|
985
661
|
var input = _transformer.serialize(props.op.input);
|
|
986
662
|
|
|
987
663
|
return tap.observable(function (observer) {
|
|
988
|
-
var subscription = props.next(_objectSpread(_objectSpread({}, props.op), {}, {
|
|
664
|
+
var subscription = props.next(_objectSpread$1(_objectSpread$1({}, props.op), {}, {
|
|
989
665
|
input: input
|
|
990
666
|
})).subscribe({
|
|
991
667
|
next: function next(value) {
|
|
992
|
-
var transformed = transformOperationResult(value, _transformer);
|
|
668
|
+
var transformed = transformOperationResult$1(value, _transformer);
|
|
993
669
|
observer.next(transformed);
|
|
994
670
|
},
|
|
995
671
|
error: function error(err) {
|
|
@@ -1010,9 +686,80 @@ var retryDelay = function retryDelay(attemptIndex) {
|
|
|
1010
686
|
return attemptIndex === 0 ? 0 : Math.min(1000 * Math.pow(2, attemptIndex), 30000);
|
|
1011
687
|
};
|
|
1012
688
|
|
|
1013
|
-
function
|
|
689
|
+
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; }
|
|
690
|
+
|
|
691
|
+
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__default["default"](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; }
|
|
692
|
+
|
|
693
|
+
function _createSuper$2(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct$2(); return function _createSuperInternal() { var Super = _getPrototypeOf__default["default"](Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf__default["default"](this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn__default["default"](this, result); }; }
|
|
694
|
+
|
|
695
|
+
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; } }
|
|
696
|
+
|
|
697
|
+
var TRPCClientError = /*#__PURE__*/function (_Error) {
|
|
698
|
+
_inherits__default["default"](TRPCClientError, _Error);
|
|
699
|
+
|
|
700
|
+
var _super = /*#__PURE__*/_createSuper$2(TRPCClientError);
|
|
701
|
+
|
|
702
|
+
/**
|
|
703
|
+
* @deprecated use `cause`
|
|
704
|
+
*/
|
|
705
|
+
function TRPCClientError(message, opts) {
|
|
706
|
+
var _opts$cause, _opts$result, _opts$result2;
|
|
707
|
+
|
|
708
|
+
var _this;
|
|
709
|
+
|
|
710
|
+
_classCallCheck__default["default"](this, TRPCClientError);
|
|
711
|
+
|
|
712
|
+
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
|
|
713
|
+
// @ts-ignore https://github.com/tc39/proposal-error-cause
|
|
714
|
+
|
|
715
|
+
_this = _super.call(this, message, {
|
|
716
|
+
cause: cause
|
|
717
|
+
});
|
|
718
|
+
_this.originalError = void 0;
|
|
719
|
+
_this.cause = void 0;
|
|
720
|
+
_this.shape = void 0;
|
|
721
|
+
_this.data = void 0;
|
|
722
|
+
_this.meta = void 0;
|
|
723
|
+
_this.meta = opts === null || opts === void 0 ? void 0 : opts.meta;
|
|
724
|
+
_this.cause = _this.originalError = cause;
|
|
725
|
+
_this.shape = opts === null || opts === void 0 ? void 0 : (_opts$result = opts.result) === null || _opts$result === void 0 ? void 0 : _opts$result.error;
|
|
726
|
+
_this.data = opts === null || opts === void 0 ? void 0 : (_opts$result2 = opts.result) === null || _opts$result2 === void 0 ? void 0 : _opts$result2.error.data;
|
|
727
|
+
_this.name = 'TRPCClientError';
|
|
728
|
+
Object.setPrototypeOf(_assertThisInitialized__default["default"](_this), TRPCClientError.prototype);
|
|
729
|
+
return _this;
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
_createClass__default["default"](TRPCClientError, null, [{
|
|
733
|
+
key: "from",
|
|
734
|
+
value: function from(cause) {
|
|
735
|
+
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
736
|
+
|
|
737
|
+
if (!(cause instanceof Error)) {
|
|
738
|
+
var _message;
|
|
739
|
+
|
|
740
|
+
return new TRPCClientError((_message = cause.error.message) !== null && _message !== void 0 ? _message : '', _objectSpread(_objectSpread({}, opts), {}, {
|
|
741
|
+
cause: null,
|
|
742
|
+
result: cause
|
|
743
|
+
}));
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
if (cause.name === 'TRPCClientError') {
|
|
747
|
+
return cause;
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
return new TRPCClientError(cause.message, _objectSpread(_objectSpread({}, opts), {}, {
|
|
751
|
+
cause: cause,
|
|
752
|
+
result: null
|
|
753
|
+
}));
|
|
754
|
+
}
|
|
755
|
+
}]);
|
|
756
|
+
|
|
757
|
+
return TRPCClientError;
|
|
758
|
+
}( /*#__PURE__*/_wrapNativeSuper__default["default"](Error));
|
|
759
|
+
|
|
760
|
+
function _createSuper$1(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct$1(); return function _createSuperInternal() { var Super = _getPrototypeOf__default["default"](Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf__default["default"](this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn__default["default"](this, result); }; }
|
|
1014
761
|
|
|
1015
|
-
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; } }
|
|
762
|
+
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; } }
|
|
1016
763
|
function createWSClient(opts) {
|
|
1017
764
|
var url = opts.url,
|
|
1018
765
|
_opts$WebSocket = opts.WebSocket,
|
|
@@ -1276,7 +1023,7 @@ function createWSClient(opts) {
|
|
|
1276
1023
|
var TRPCWebSocketClosedError = /*#__PURE__*/function (_Error) {
|
|
1277
1024
|
_inherits__default["default"](TRPCWebSocketClosedError, _Error);
|
|
1278
1025
|
|
|
1279
|
-
var _super = /*#__PURE__*/_createSuper(TRPCWebSocketClosedError);
|
|
1026
|
+
var _super = /*#__PURE__*/_createSuper$1(TRPCWebSocketClosedError);
|
|
1280
1027
|
|
|
1281
1028
|
function TRPCWebSocketClosedError(message) {
|
|
1282
1029
|
var _this;
|
|
@@ -1295,7 +1042,7 @@ var TRPCWebSocketClosedError = /*#__PURE__*/function (_Error) {
|
|
|
1295
1042
|
var TRPCSubscriptionEndedError = /*#__PURE__*/function (_Error2) {
|
|
1296
1043
|
_inherits__default["default"](TRPCSubscriptionEndedError, _Error2);
|
|
1297
1044
|
|
|
1298
|
-
var _super2 = /*#__PURE__*/_createSuper(TRPCSubscriptionEndedError);
|
|
1045
|
+
var _super2 = /*#__PURE__*/_createSuper$1(TRPCSubscriptionEndedError);
|
|
1299
1046
|
|
|
1300
1047
|
function TRPCSubscriptionEndedError(message) {
|
|
1301
1048
|
var _this2;
|
|
@@ -1363,6 +1110,259 @@ function wsLink(opts) {
|
|
|
1363
1110
|
};
|
|
1364
1111
|
}
|
|
1365
1112
|
|
|
1113
|
+
/** @internal */
|
|
1114
|
+
function transformOperationResult(result) {
|
|
1115
|
+
var context = result.context;
|
|
1116
|
+
|
|
1117
|
+
if ('error' in result.data) {
|
|
1118
|
+
var error = TRPCClientError.from(result.data);
|
|
1119
|
+
return {
|
|
1120
|
+
ok: false,
|
|
1121
|
+
error: error,
|
|
1122
|
+
context: context
|
|
1123
|
+
};
|
|
1124
|
+
}
|
|
1125
|
+
|
|
1126
|
+
var data = result.data.result.data;
|
|
1127
|
+
return {
|
|
1128
|
+
ok: true,
|
|
1129
|
+
data: data,
|
|
1130
|
+
context: context
|
|
1131
|
+
};
|
|
1132
|
+
}
|
|
1133
|
+
|
|
1134
|
+
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf__default["default"](Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf__default["default"](this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn__default["default"](this, result); }; }
|
|
1135
|
+
|
|
1136
|
+
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; } }
|
|
1137
|
+
|
|
1138
|
+
var ObservableAbortError = /*#__PURE__*/function (_Error) {
|
|
1139
|
+
_inherits__default["default"](ObservableAbortError, _Error);
|
|
1140
|
+
|
|
1141
|
+
var _super = /*#__PURE__*/_createSuper(ObservableAbortError);
|
|
1142
|
+
|
|
1143
|
+
function ObservableAbortError(message) {
|
|
1144
|
+
var _this;
|
|
1145
|
+
|
|
1146
|
+
_classCallCheck__default["default"](this, ObservableAbortError);
|
|
1147
|
+
|
|
1148
|
+
_this = _super.call(this, message);
|
|
1149
|
+
_this.name = 'ObservableAbortError';
|
|
1150
|
+
Object.setPrototypeOf(_assertThisInitialized__default["default"](_this), ObservableAbortError.prototype);
|
|
1151
|
+
return _this;
|
|
1152
|
+
}
|
|
1153
|
+
|
|
1154
|
+
return _createClass__default["default"](ObservableAbortError);
|
|
1155
|
+
}( /*#__PURE__*/_wrapNativeSuper__default["default"](Error));
|
|
1156
|
+
/** @internal */
|
|
1157
|
+
|
|
1158
|
+
function observableToPromise(observable) {
|
|
1159
|
+
var abort;
|
|
1160
|
+
var promise = new Promise(function (resolve, reject) {
|
|
1161
|
+
var isDone = false;
|
|
1162
|
+
|
|
1163
|
+
function onDone() {
|
|
1164
|
+
if (isDone) {
|
|
1165
|
+
return;
|
|
1166
|
+
}
|
|
1167
|
+
|
|
1168
|
+
isDone = true;
|
|
1169
|
+
reject(new ObservableAbortError('This operation was cancelled.'));
|
|
1170
|
+
obs$.unsubscribe();
|
|
1171
|
+
}
|
|
1172
|
+
|
|
1173
|
+
var obs$ = observable.subscribe({
|
|
1174
|
+
next: function next(data) {
|
|
1175
|
+
isDone = true;
|
|
1176
|
+
resolve(data);
|
|
1177
|
+
onDone();
|
|
1178
|
+
},
|
|
1179
|
+
error: function error(data) {
|
|
1180
|
+
isDone = true;
|
|
1181
|
+
reject(data);
|
|
1182
|
+
onDone();
|
|
1183
|
+
},
|
|
1184
|
+
complete: function complete() {
|
|
1185
|
+
isDone = true;
|
|
1186
|
+
onDone();
|
|
1187
|
+
}
|
|
1188
|
+
});
|
|
1189
|
+
abort = onDone;
|
|
1190
|
+
});
|
|
1191
|
+
return {
|
|
1192
|
+
promise: promise,
|
|
1193
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
1194
|
+
abort: abort
|
|
1195
|
+
};
|
|
1196
|
+
}
|
|
1197
|
+
|
|
1198
|
+
var idCounter = 0;
|
|
1199
|
+
|
|
1200
|
+
function getRequestId() {
|
|
1201
|
+
return ++idCounter;
|
|
1202
|
+
}
|
|
1203
|
+
|
|
1204
|
+
var TRPCClient = /*#__PURE__*/function () {
|
|
1205
|
+
function TRPCClient(opts) {
|
|
1206
|
+
var _this = this;
|
|
1207
|
+
|
|
1208
|
+
_classCallCheck__default["default"](this, TRPCClient);
|
|
1209
|
+
|
|
1210
|
+
this.links = void 0;
|
|
1211
|
+
this.runtime = void 0;
|
|
1212
|
+
|
|
1213
|
+
var _fetch = getFetch(opts === null || opts === void 0 ? void 0 : opts.fetch);
|
|
1214
|
+
|
|
1215
|
+
var AC = getAbortController(opts === null || opts === void 0 ? void 0 : opts.AbortController);
|
|
1216
|
+
|
|
1217
|
+
function getHeadersFn() {
|
|
1218
|
+
if (opts.headers) {
|
|
1219
|
+
var headers = opts.headers;
|
|
1220
|
+
return typeof headers === 'function' ? headers : function () {
|
|
1221
|
+
return headers;
|
|
1222
|
+
};
|
|
1223
|
+
}
|
|
1224
|
+
|
|
1225
|
+
return function () {
|
|
1226
|
+
return {};
|
|
1227
|
+
};
|
|
1228
|
+
}
|
|
1229
|
+
|
|
1230
|
+
this.runtime = {
|
|
1231
|
+
AbortController: AC,
|
|
1232
|
+
fetch: _fetch,
|
|
1233
|
+
headers: getHeadersFn()
|
|
1234
|
+
};
|
|
1235
|
+
|
|
1236
|
+
if ('links' in opts) {
|
|
1237
|
+
this.links = opts.links.map(function (link) {
|
|
1238
|
+
return link(_this.runtime);
|
|
1239
|
+
});
|
|
1240
|
+
} else {
|
|
1241
|
+
this.links = [httpBatchLink({
|
|
1242
|
+
url: opts.url
|
|
1243
|
+
})(this.runtime)];
|
|
1244
|
+
}
|
|
1245
|
+
}
|
|
1246
|
+
|
|
1247
|
+
_createClass__default["default"](TRPCClient, [{
|
|
1248
|
+
key: "$request",
|
|
1249
|
+
value: function $request(_ref) {
|
|
1250
|
+
var type = _ref.type,
|
|
1251
|
+
input = _ref.input,
|
|
1252
|
+
path = _ref.path,
|
|
1253
|
+
_ref$context = _ref.context,
|
|
1254
|
+
context = _ref$context === void 0 ? {} : _ref$context;
|
|
1255
|
+
var chain$ = createChain({
|
|
1256
|
+
links: this.links,
|
|
1257
|
+
op: {
|
|
1258
|
+
id: getRequestId(),
|
|
1259
|
+
type: type,
|
|
1260
|
+
path: path,
|
|
1261
|
+
input: input,
|
|
1262
|
+
context: context
|
|
1263
|
+
}
|
|
1264
|
+
});
|
|
1265
|
+
return chain$.pipe(tap.share());
|
|
1266
|
+
}
|
|
1267
|
+
}, {
|
|
1268
|
+
key: "requestAsPromise",
|
|
1269
|
+
value: function requestAsPromise(opts) {
|
|
1270
|
+
var req$ = this.$request(opts);
|
|
1271
|
+
|
|
1272
|
+
var _observableToPromise = observableToPromise(req$),
|
|
1273
|
+
promise = _observableToPromise.promise,
|
|
1274
|
+
abort = _observableToPromise.abort;
|
|
1275
|
+
|
|
1276
|
+
var cancellablePromise = new Promise(function (resolve, reject) {
|
|
1277
|
+
promise.then(function (result) {
|
|
1278
|
+
var transformed = transformOperationResult(result);
|
|
1279
|
+
|
|
1280
|
+
if (transformed.ok) {
|
|
1281
|
+
resolve(transformed.data);
|
|
1282
|
+
return;
|
|
1283
|
+
}
|
|
1284
|
+
|
|
1285
|
+
reject(transformed.error);
|
|
1286
|
+
}).catch(function (err) {
|
|
1287
|
+
reject(TRPCClientError.from(err));
|
|
1288
|
+
});
|
|
1289
|
+
});
|
|
1290
|
+
cancellablePromise.cancel = abort;
|
|
1291
|
+
return cancellablePromise;
|
|
1292
|
+
}
|
|
1293
|
+
}, {
|
|
1294
|
+
key: "query",
|
|
1295
|
+
value: function query(path) {
|
|
1296
|
+
var _ref2;
|
|
1297
|
+
|
|
1298
|
+
var context = (_ref2 = arguments.length <= 2 ? undefined : arguments[2]) === null || _ref2 === void 0 ? void 0 : _ref2.context;
|
|
1299
|
+
return this.requestAsPromise({
|
|
1300
|
+
type: 'query',
|
|
1301
|
+
path: path,
|
|
1302
|
+
input: arguments.length <= 1 ? undefined : arguments[1],
|
|
1303
|
+
context: context
|
|
1304
|
+
});
|
|
1305
|
+
}
|
|
1306
|
+
}, {
|
|
1307
|
+
key: "mutation",
|
|
1308
|
+
value: function mutation(path) {
|
|
1309
|
+
var _ref3;
|
|
1310
|
+
|
|
1311
|
+
var context = (_ref3 = arguments.length <= 2 ? undefined : arguments[2]) === null || _ref3 === void 0 ? void 0 : _ref3.context;
|
|
1312
|
+
return this.requestAsPromise({
|
|
1313
|
+
type: 'mutation',
|
|
1314
|
+
path: path,
|
|
1315
|
+
input: arguments.length <= 1 ? undefined : arguments[1],
|
|
1316
|
+
context: context
|
|
1317
|
+
});
|
|
1318
|
+
}
|
|
1319
|
+
}, {
|
|
1320
|
+
key: "subscription",
|
|
1321
|
+
value: function subscription(path, input, opts) {
|
|
1322
|
+
var observable$ = this.$request({
|
|
1323
|
+
type: 'subscription',
|
|
1324
|
+
path: path,
|
|
1325
|
+
input: input,
|
|
1326
|
+
context: opts.context
|
|
1327
|
+
});
|
|
1328
|
+
return observable$.subscribe({
|
|
1329
|
+
next: function next(result) {
|
|
1330
|
+
var _opts$next;
|
|
1331
|
+
|
|
1332
|
+
if ('error' in result.data) {
|
|
1333
|
+
var _opts$error;
|
|
1334
|
+
|
|
1335
|
+
var err = TRPCClientError.from(result.data, {
|
|
1336
|
+
meta: result.context
|
|
1337
|
+
});
|
|
1338
|
+
(_opts$error = opts.error) === null || _opts$error === void 0 ? void 0 : _opts$error.call(opts, err);
|
|
1339
|
+
return;
|
|
1340
|
+
}
|
|
1341
|
+
|
|
1342
|
+
(_opts$next = opts.next) === null || _opts$next === void 0 ? void 0 : _opts$next.call(opts, result.data.result);
|
|
1343
|
+
},
|
|
1344
|
+
error: function error(err) {
|
|
1345
|
+
var _opts$error2;
|
|
1346
|
+
|
|
1347
|
+
(_opts$error2 = opts.error) === null || _opts$error2 === void 0 ? void 0 : _opts$error2.call(opts, err);
|
|
1348
|
+
},
|
|
1349
|
+
complete: function complete() {
|
|
1350
|
+
var _opts$complete;
|
|
1351
|
+
|
|
1352
|
+
(_opts$complete = opts.complete) === null || _opts$complete === void 0 ? void 0 : _opts$complete.call(opts);
|
|
1353
|
+
}
|
|
1354
|
+
});
|
|
1355
|
+
}
|
|
1356
|
+
}]);
|
|
1357
|
+
|
|
1358
|
+
return TRPCClient;
|
|
1359
|
+
}();
|
|
1360
|
+
|
|
1361
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
1362
|
+
function createTRPCClient(opts) {
|
|
1363
|
+
return new TRPCClient(opts);
|
|
1364
|
+
}
|
|
1365
|
+
|
|
1366
1366
|
exports.TRPCClientError = TRPCClientError;
|
|
1367
1367
|
exports.createTRPCClient = createTRPCClient;
|
|
1368
1368
|
exports.createWSClient = createWSClient;
|