@trpc/client 9.16.0 → 10.0.0-alpha.1
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/{TRPCClientError-bfee2bf5.cjs.dev.js → TRPCClientError-a5312a5f.cjs.dev.js} +14 -19
- package/dist/{TRPCClientError-09b8a26b.esm.js → TRPCClientError-e1da1ef0.esm.js} +14 -19
- package/dist/{TRPCClientError-e9bf96e9.cjs.prod.js → TRPCClientError-ea7d89b2.cjs.prod.js} +14 -19
- package/dist/createChain-93a38e1c.cjs.dev.js +27 -0
- package/dist/createChain-e45e052a.cjs.prod.js +27 -0
- package/dist/createChain-e45e6cf2.esm.js +25 -0
- package/dist/declarations/src/TRPCClientError.d.ts +6 -10
- package/dist/declarations/src/TRPCClientError.d.ts.map +1 -1
- package/dist/declarations/src/createTRPCClient.d.ts +1 -2
- package/dist/declarations/src/createTRPCClient.d.ts.map +1 -1
- package/dist/declarations/src/index.d.ts +1 -1
- package/dist/declarations/src/index.d.ts.map +1 -1
- package/dist/declarations/src/internals/TRPCClient.d.ts +17 -14
- package/dist/declarations/src/internals/TRPCClient.d.ts.map +1 -1
- package/dist/declarations/src/internals/dataLoader.d.ts +1 -1
- package/dist/declarations/src/internals/dataLoader.d.ts.map +1 -1
- package/dist/declarations/src/links/httpBatchLink.d.ts +2 -1
- package/dist/declarations/src/links/httpBatchLink.d.ts.map +1 -1
- package/dist/declarations/src/links/httpLink.d.ts +2 -1
- package/dist/declarations/src/links/httpLink.d.ts.map +1 -1
- package/dist/declarations/src/links/httpUtils.d.ts +22 -0
- package/dist/declarations/src/links/httpUtils.d.ts.map +1 -0
- package/dist/declarations/src/links/internals/createChain.d.ts +8 -0
- package/dist/declarations/src/links/internals/createChain.d.ts.map +1 -0
- package/dist/declarations/src/links/internals/transformOperationResult.d.ts +16 -0
- package/dist/declarations/src/links/internals/transformOperationResult.d.ts.map +1 -0
- package/dist/declarations/src/links/loggerLink.d.ts +4 -3
- package/dist/declarations/src/links/loggerLink.d.ts.map +1 -1
- package/dist/declarations/src/links/splitLink.d.ts +4 -14
- package/dist/declarations/src/links/splitLink.d.ts.map +1 -1
- package/dist/declarations/src/links/transformerLink.d.ts +4 -0
- package/dist/declarations/src/links/transformerLink.d.ts.map +1 -0
- package/dist/declarations/src/links/types.d.ts +40 -0
- package/dist/declarations/src/links/types.d.ts.map +1 -0
- package/dist/declarations/src/links/wsLink.d.ts +4 -11
- package/dist/declarations/src/links/wsLink.d.ts.map +1 -1
- package/dist/declarations/src/rx/index.d.ts +3 -0
- package/dist/declarations/src/rx/index.d.ts.map +1 -0
- package/dist/declarations/src/rx/observable.d.ts +4 -0
- package/dist/declarations/src/rx/observable.d.ts.map +1 -0
- package/dist/declarations/src/rx/operators/index.d.ts +4 -0
- package/dist/declarations/src/rx/operators/index.d.ts.map +1 -0
- package/dist/declarations/src/rx/operators/map.d.ts +3 -0
- package/dist/declarations/src/rx/operators/map.d.ts.map +1 -0
- package/dist/declarations/src/rx/operators/share.d.ts +6 -0
- package/dist/declarations/src/rx/operators/share.d.ts.map +1 -0
- package/dist/declarations/src/rx/operators/tap.d.ts +3 -0
- package/dist/declarations/src/rx/operators/tap.d.ts.map +1 -0
- package/dist/declarations/src/rx/types.d.ts +29 -0
- package/dist/declarations/src/rx/types.d.ts.map +1 -0
- package/dist/declarations/src/rx/util/identity.d.ts +2 -0
- package/dist/declarations/src/rx/util/identity.d.ts.map +1 -0
- package/dist/declarations/src/rx/util/observableToPromise.d.ts +10 -0
- package/dist/declarations/src/rx/util/observableToPromise.d.ts.map +1 -0
- package/dist/declarations/src/rx/util/pipe.d.ts +4 -0
- package/dist/declarations/src/rx/util/pipe.d.ts.map +1 -0
- package/dist/httpUtils-299a66f6.cjs.prod.js +89 -0
- package/dist/httpUtils-a1426e14.cjs.dev.js +89 -0
- package/dist/httpUtils-e45c9056.esm.js +87 -0
- package/dist/observable-01534ea8.esm.js +102 -0
- package/dist/observable-9aad8c3e.cjs.dev.js +104 -0
- package/dist/observable-ccb54234.cjs.prod.js +104 -0
- package/dist/share-5a8c2543.cjs.prod.js +106 -0
- package/dist/share-ca585761.esm.js +104 -0
- package/dist/share-f2ac9332.cjs.dev.js +106 -0
- package/dist/tap-2d565250.cjs.dev.js +32 -0
- package/dist/tap-606957cb.esm.js +30 -0
- package/dist/tap-d907998d.cjs.prod.js +32 -0
- package/dist/trpc-client.cjs.dev.js +136 -215
- package/dist/trpc-client.cjs.prod.js +136 -215
- package/dist/trpc-client.esm.js +137 -216
- package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.cjs.dev.js +34 -52
- package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.cjs.prod.js +34 -52
- package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.esm.js +33 -51
- package/links/httpLink/dist/trpc-client-links-httpLink.cjs.dev.js +29 -52
- package/links/httpLink/dist/trpc-client-links-httpLink.cjs.prod.js +29 -52
- package/links/httpLink/dist/trpc-client-links-httpLink.esm.js +29 -52
- package/links/loggerLink/dist/trpc-client-links-loggerLink.cjs.dev.js +31 -19
- package/links/loggerLink/dist/trpc-client-links-loggerLink.cjs.prod.js +31 -19
- package/links/loggerLink/dist/trpc-client-links-loggerLink.esm.js +31 -19
- package/links/splitLink/dist/trpc-client-links-splitLink.cjs.dev.js +20 -7
- package/links/splitLink/dist/trpc-client-links-splitLink.cjs.prod.js +20 -7
- package/links/splitLink/dist/trpc-client-links-splitLink.esm.js +20 -7
- package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.d.ts +1 -0
- package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.dev.js +67 -0
- package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.js +7 -0
- package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.prod.js +67 -0
- package/links/transformerLink/dist/trpc-client-links-transformerLink.esm.js +63 -0
- package/links/transformerLink/package.json +4 -0
- package/links/wsLink/dist/trpc-client-links-wsLink.cjs.dev.js +57 -81
- package/links/wsLink/dist/trpc-client-links-wsLink.cjs.prod.js +57 -81
- package/links/wsLink/dist/trpc-client-links-wsLink.esm.js +57 -81
- package/package.json +12 -8
- package/rx/dist/trpc-client-rx.cjs.d.ts +1 -0
- package/rx/dist/trpc-client-rx.cjs.dev.js +9 -0
- package/rx/dist/trpc-client-rx.cjs.js +7 -0
- package/rx/dist/trpc-client-rx.cjs.prod.js +9 -0
- package/rx/dist/trpc-client-rx.esm.js +1 -0
- package/rx/operators/dist/trpc-client-rx-operators.cjs.d.ts +1 -0
- package/rx/operators/dist/trpc-client-rx-operators.cjs.dev.js +39 -0
- package/rx/operators/dist/trpc-client-rx-operators.cjs.js +7 -0
- package/rx/operators/dist/trpc-client-rx-operators.cjs.prod.js +39 -0
- package/rx/operators/dist/trpc-client-rx-operators.esm.js +33 -0
- package/rx/operators/package.json +4 -0
- package/rx/package.json +4 -0
- package/src/TRPCClientError.ts +17 -21
- package/src/createTRPCClient.ts +4 -3
- package/src/index.ts +1 -1
- package/src/internals/TRPCClient.ts +80 -88
- package/src/internals/dataLoader.ts +1 -1
- package/src/internals/transformRPCResponse.ts +2 -2
- package/src/links/dedupeLink.test.ts +134 -0
- package/src/links/dedupeLink.ts +53 -0
- package/src/links/httpBatchLink.ts +37 -37
- package/src/links/httpLink.ts +26 -37
- package/src/{internals/httpRequest.ts → links/httpUtils.ts} +30 -21
- package/src/links/internals/createChain.test.ts +132 -0
- package/src/links/internals/createChain.ts +31 -0
- package/src/links/internals/transformOperationResult.ts +25 -0
- package/src/links/loggerLink.ts +40 -22
- package/src/links/retryLink.ts +45 -14
- package/src/links/splitLink.test.ts +56 -0
- package/src/links/splitLink.ts +23 -38
- package/src/links/transformerLink.ts +70 -0
- package/src/links/{core.ts → types.ts} +40 -46
- package/src/links/wsLink.ts +56 -77
- package/src/rx/index.ts +2 -0
- package/src/rx/observable.test.ts +83 -0
- package/src/rx/observable.ts +70 -0
- package/src/rx/operators/index.ts +3 -0
- package/src/rx/operators/map.test.ts +92 -0
- package/src/rx/operators/map.ts +25 -0
- package/src/rx/operators/share.test.ts +60 -0
- package/src/rx/operators/share.ts +67 -0
- package/src/rx/operators/tap.ts +26 -0
- package/src/rx/types.ts +69 -0
- package/src/rx/util/identity.ts +3 -0
- package/src/rx/util/observableToPromise.ts +49 -0
- package/src/rx/util/pipe.ts +22 -0
- package/dist/declarations/src/internals/TRPCAbortError.d.ts +0 -4
- package/dist/declarations/src/internals/TRPCAbortError.d.ts.map +0 -1
- package/dist/declarations/src/internals/executeChain.d.ts +0 -8
- package/dist/declarations/src/internals/executeChain.d.ts.map +0 -1
- package/dist/declarations/src/internals/httpRequest.d.ts +0 -14
- package/dist/declarations/src/internals/httpRequest.d.ts.map +0 -1
- package/dist/declarations/src/internals/observable.d.ts +0 -18
- package/dist/declarations/src/internals/observable.d.ts.map +0 -1
- package/dist/declarations/src/internals/transformRPCResponse.d.ts +0 -7
- package/dist/declarations/src/internals/transformRPCResponse.d.ts.map +0 -1
- package/dist/declarations/src/links/core.d.ts +0 -45
- package/dist/declarations/src/links/core.d.ts.map +0 -1
- package/dist/httpRequest-672ff1f0.esm.js +0 -128
- package/dist/httpRequest-7bfaf300.cjs.dev.js +0 -132
- package/dist/httpRequest-8c422e7a.cjs.prod.js +0 -132
- package/src/internals/TRPCAbortError.ts +0 -7
- package/src/internals/executeChain.ts +0 -67
- package/src/internals/observable.ts +0 -89
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var _classCallCheck = require('@babel/runtime/helpers/classCallCheck');
|
|
4
|
-
var _assertThisInitialized = require('@babel/runtime/helpers/assertThisInitialized');
|
|
5
|
-
var _inherits = require('@babel/runtime/helpers/inherits');
|
|
6
|
-
var _createSuper = require('@babel/runtime/helpers/createSuper');
|
|
7
|
-
var _wrapNativeSuper = require('@babel/runtime/helpers/wrapNativeSuper');
|
|
8
|
-
var _objectSpread = require('@babel/runtime/helpers/objectSpread2');
|
|
9
|
-
var TRPCClientError = require('./TRPCClientError-bfee2bf5.cjs.dev.js');
|
|
10
|
-
|
|
11
|
-
var TRPCAbortError = /*#__PURE__*/function (_Error) {
|
|
12
|
-
_inherits(TRPCAbortError, _Error);
|
|
13
|
-
|
|
14
|
-
var _super = /*#__PURE__*/_createSuper(TRPCAbortError);
|
|
15
|
-
|
|
16
|
-
function TRPCAbortError() {
|
|
17
|
-
var _this;
|
|
18
|
-
|
|
19
|
-
_classCallCheck(this, TRPCAbortError);
|
|
20
|
-
|
|
21
|
-
_this = _super.call(this, 'The operation was aborted.');
|
|
22
|
-
_this.name = 'TRPCAbortError';
|
|
23
|
-
Object.setPrototypeOf(_assertThisInitialized(_this), TRPCAbortError.prototype);
|
|
24
|
-
return _this;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
return TRPCAbortError;
|
|
28
|
-
}( /*#__PURE__*/_wrapNativeSuper(Error));
|
|
29
|
-
|
|
30
|
-
function transformRPCResponse(_ref) {
|
|
31
|
-
var envelope = _ref.envelope,
|
|
32
|
-
runtime = _ref.runtime;
|
|
33
|
-
|
|
34
|
-
if ('error' in envelope) {
|
|
35
|
-
return TRPCClientError.TRPCClientError.from(_objectSpread(_objectSpread({}, envelope), {}, {
|
|
36
|
-
error: runtime.transformer.deserialize(envelope.error)
|
|
37
|
-
}));
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
if (envelope.result.type === 'data') {
|
|
41
|
-
return _objectSpread(_objectSpread({}, envelope.result), {}, {
|
|
42
|
-
data: runtime.transformer.deserialize(envelope.result.data)
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
return envelope.result;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
// https://github.com/trpc/trpc/pull/669
|
|
50
|
-
function arrayToDict(array) {
|
|
51
|
-
var dict = {};
|
|
52
|
-
|
|
53
|
-
for (var index = 0; index < array.length; index++) {
|
|
54
|
-
var element = array[index];
|
|
55
|
-
dict[index] = element;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
return dict;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function httpRequest(props) {
|
|
62
|
-
var type = props.type,
|
|
63
|
-
rt = props.runtime,
|
|
64
|
-
path = props.path;
|
|
65
|
-
var ac = rt.AbortController ? new rt.AbortController() : null;
|
|
66
|
-
var method = {
|
|
67
|
-
query: 'GET',
|
|
68
|
-
mutation: 'POST',
|
|
69
|
-
subscription: 'PATCH'
|
|
70
|
-
};
|
|
71
|
-
var input = 'input' in props ? rt.transformer.serialize(props.input) : arrayToDict(props.inputs.map(function (_input) {
|
|
72
|
-
return rt.transformer.serialize(_input);
|
|
73
|
-
}));
|
|
74
|
-
|
|
75
|
-
function getUrl() {
|
|
76
|
-
var url = props.url + '/' + path;
|
|
77
|
-
var queryParts = [];
|
|
78
|
-
|
|
79
|
-
if ('inputs' in props) {
|
|
80
|
-
queryParts.push('batch=1');
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
if (type === 'query' && input !== undefined) {
|
|
84
|
-
queryParts.push("input=".concat(encodeURIComponent(JSON.stringify(input))));
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
if (queryParts.length) {
|
|
88
|
-
url += '?' + queryParts.join('&');
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
return url;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
function getBody() {
|
|
95
|
-
if (type === 'query') {
|
|
96
|
-
return undefined;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
return input !== undefined ? JSON.stringify(input) : undefined;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
var promise = new Promise(function (resolve, reject) {
|
|
103
|
-
var url = getUrl();
|
|
104
|
-
Promise.resolve(rt.headers()).then(function (headers) {
|
|
105
|
-
return rt.fetch(url, {
|
|
106
|
-
method: method[type],
|
|
107
|
-
signal: ac === null || ac === void 0 ? void 0 : ac.signal,
|
|
108
|
-
body: getBody(),
|
|
109
|
-
headers: _objectSpread({
|
|
110
|
-
'content-type': 'application/json'
|
|
111
|
-
}, headers)
|
|
112
|
-
});
|
|
113
|
-
}).then(function (res) {
|
|
114
|
-
return res.json();
|
|
115
|
-
}).then(function (json) {
|
|
116
|
-
resolve(json);
|
|
117
|
-
}).catch(reject);
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
var cancel = function cancel() {
|
|
121
|
-
ac === null || ac === void 0 ? void 0 : ac.abort();
|
|
122
|
-
};
|
|
123
|
-
|
|
124
|
-
return {
|
|
125
|
-
promise: promise,
|
|
126
|
-
cancel: cancel
|
|
127
|
-
};
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
exports.TRPCAbortError = TRPCAbortError;
|
|
131
|
-
exports.httpRequest = httpRequest;
|
|
132
|
-
exports.transformRPCResponse = transformRPCResponse;
|
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var _classCallCheck = require('@babel/runtime/helpers/classCallCheck');
|
|
4
|
-
var _assertThisInitialized = require('@babel/runtime/helpers/assertThisInitialized');
|
|
5
|
-
var _inherits = require('@babel/runtime/helpers/inherits');
|
|
6
|
-
var _createSuper = require('@babel/runtime/helpers/createSuper');
|
|
7
|
-
var _wrapNativeSuper = require('@babel/runtime/helpers/wrapNativeSuper');
|
|
8
|
-
var _objectSpread = require('@babel/runtime/helpers/objectSpread2');
|
|
9
|
-
var TRPCClientError = require('./TRPCClientError-e9bf96e9.cjs.prod.js');
|
|
10
|
-
|
|
11
|
-
var TRPCAbortError = /*#__PURE__*/function (_Error) {
|
|
12
|
-
_inherits(TRPCAbortError, _Error);
|
|
13
|
-
|
|
14
|
-
var _super = /*#__PURE__*/_createSuper(TRPCAbortError);
|
|
15
|
-
|
|
16
|
-
function TRPCAbortError() {
|
|
17
|
-
var _this;
|
|
18
|
-
|
|
19
|
-
_classCallCheck(this, TRPCAbortError);
|
|
20
|
-
|
|
21
|
-
_this = _super.call(this, 'The operation was aborted.');
|
|
22
|
-
_this.name = 'TRPCAbortError';
|
|
23
|
-
Object.setPrototypeOf(_assertThisInitialized(_this), TRPCAbortError.prototype);
|
|
24
|
-
return _this;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
return TRPCAbortError;
|
|
28
|
-
}( /*#__PURE__*/_wrapNativeSuper(Error));
|
|
29
|
-
|
|
30
|
-
function transformRPCResponse(_ref) {
|
|
31
|
-
var envelope = _ref.envelope,
|
|
32
|
-
runtime = _ref.runtime;
|
|
33
|
-
|
|
34
|
-
if ('error' in envelope) {
|
|
35
|
-
return TRPCClientError.TRPCClientError.from(_objectSpread(_objectSpread({}, envelope), {}, {
|
|
36
|
-
error: runtime.transformer.deserialize(envelope.error)
|
|
37
|
-
}));
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
if (envelope.result.type === 'data') {
|
|
41
|
-
return _objectSpread(_objectSpread({}, envelope.result), {}, {
|
|
42
|
-
data: runtime.transformer.deserialize(envelope.result.data)
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
return envelope.result;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
// https://github.com/trpc/trpc/pull/669
|
|
50
|
-
function arrayToDict(array) {
|
|
51
|
-
var dict = {};
|
|
52
|
-
|
|
53
|
-
for (var index = 0; index < array.length; index++) {
|
|
54
|
-
var element = array[index];
|
|
55
|
-
dict[index] = element;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
return dict;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function httpRequest(props) {
|
|
62
|
-
var type = props.type,
|
|
63
|
-
rt = props.runtime,
|
|
64
|
-
path = props.path;
|
|
65
|
-
var ac = rt.AbortController ? new rt.AbortController() : null;
|
|
66
|
-
var method = {
|
|
67
|
-
query: 'GET',
|
|
68
|
-
mutation: 'POST',
|
|
69
|
-
subscription: 'PATCH'
|
|
70
|
-
};
|
|
71
|
-
var input = 'input' in props ? rt.transformer.serialize(props.input) : arrayToDict(props.inputs.map(function (_input) {
|
|
72
|
-
return rt.transformer.serialize(_input);
|
|
73
|
-
}));
|
|
74
|
-
|
|
75
|
-
function getUrl() {
|
|
76
|
-
var url = props.url + '/' + path;
|
|
77
|
-
var queryParts = [];
|
|
78
|
-
|
|
79
|
-
if ('inputs' in props) {
|
|
80
|
-
queryParts.push('batch=1');
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
if (type === 'query' && input !== undefined) {
|
|
84
|
-
queryParts.push("input=".concat(encodeURIComponent(JSON.stringify(input))));
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
if (queryParts.length) {
|
|
88
|
-
url += '?' + queryParts.join('&');
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
return url;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
function getBody() {
|
|
95
|
-
if (type === 'query') {
|
|
96
|
-
return undefined;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
return input !== undefined ? JSON.stringify(input) : undefined;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
var promise = new Promise(function (resolve, reject) {
|
|
103
|
-
var url = getUrl();
|
|
104
|
-
Promise.resolve(rt.headers()).then(function (headers) {
|
|
105
|
-
return rt.fetch(url, {
|
|
106
|
-
method: method[type],
|
|
107
|
-
signal: ac === null || ac === void 0 ? void 0 : ac.signal,
|
|
108
|
-
body: getBody(),
|
|
109
|
-
headers: _objectSpread({
|
|
110
|
-
'content-type': 'application/json'
|
|
111
|
-
}, headers)
|
|
112
|
-
});
|
|
113
|
-
}).then(function (res) {
|
|
114
|
-
return res.json();
|
|
115
|
-
}).then(function (json) {
|
|
116
|
-
resolve(json);
|
|
117
|
-
}).catch(reject);
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
var cancel = function cancel() {
|
|
121
|
-
ac === null || ac === void 0 ? void 0 : ac.abort();
|
|
122
|
-
};
|
|
123
|
-
|
|
124
|
-
return {
|
|
125
|
-
promise: promise,
|
|
126
|
-
cancel: cancel
|
|
127
|
-
};
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
exports.TRPCAbortError = TRPCAbortError;
|
|
131
|
-
exports.httpRequest = httpRequest;
|
|
132
|
-
exports.transformRPCResponse = transformRPCResponse;
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import { AnyRouter } from '@trpc/server';
|
|
2
|
-
import { TRPCResult } from '@trpc/server/rpc';
|
|
3
|
-
import { TRPCClientError } from '../TRPCClientError';
|
|
4
|
-
import {
|
|
5
|
-
Operation,
|
|
6
|
-
OperationLink,
|
|
7
|
-
OperationResponse,
|
|
8
|
-
PrevCallback,
|
|
9
|
-
} from '../links/core';
|
|
10
|
-
import { observableSubject } from './observable';
|
|
11
|
-
|
|
12
|
-
export function executeChain<
|
|
13
|
-
TRouter extends AnyRouter,
|
|
14
|
-
TInput = unknown,
|
|
15
|
-
TOutput = unknown,
|
|
16
|
-
>(opts: {
|
|
17
|
-
links: OperationLink<TRouter, TInput, TOutput>[];
|
|
18
|
-
op: Operation<TInput>;
|
|
19
|
-
}) {
|
|
20
|
-
const $result = observableSubject<
|
|
21
|
-
null | TRPCResult<TOutput>,
|
|
22
|
-
TRPCClientError<TRouter>
|
|
23
|
-
>(null);
|
|
24
|
-
|
|
25
|
-
const updateResult = (result: OperationResponse<TRouter, TOutput>) => {
|
|
26
|
-
if (result instanceof Error) {
|
|
27
|
-
$result.error(result);
|
|
28
|
-
if (result.isDone) {
|
|
29
|
-
$result.done();
|
|
30
|
-
}
|
|
31
|
-
} else {
|
|
32
|
-
$result.next(result);
|
|
33
|
-
}
|
|
34
|
-
};
|
|
35
|
-
function walk({
|
|
36
|
-
index,
|
|
37
|
-
op,
|
|
38
|
-
stack,
|
|
39
|
-
}: {
|
|
40
|
-
index: number;
|
|
41
|
-
op: Operation<TInput>;
|
|
42
|
-
stack: PrevCallback<TRouter, TOutput>[];
|
|
43
|
-
}) {
|
|
44
|
-
const link = opts.links[index];
|
|
45
|
-
const prev: PrevCallback<TRouter, TOutput> =
|
|
46
|
-
index === 0 ? (value) => updateResult(value) : stack[index - 1];
|
|
47
|
-
|
|
48
|
-
link({
|
|
49
|
-
op,
|
|
50
|
-
prev,
|
|
51
|
-
next: (op, prevOp) => {
|
|
52
|
-
const prevStack = stack.slice();
|
|
53
|
-
prevStack[index] = prevOp;
|
|
54
|
-
walk({ index: index + 1, op, stack: prevStack });
|
|
55
|
-
},
|
|
56
|
-
onDestroy: (callback) => {
|
|
57
|
-
$result.subscribe({
|
|
58
|
-
onDone() {
|
|
59
|
-
callback();
|
|
60
|
-
},
|
|
61
|
-
});
|
|
62
|
-
},
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
walk({ index: 0, op: opts.op, stack: [] });
|
|
66
|
-
return $result;
|
|
67
|
-
}
|
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
export interface ObservableCallbacks<TValue, TError> {
|
|
2
|
-
onNext?: (opts: TValue) => void;
|
|
3
|
-
onError?: (opts: TError) => void;
|
|
4
|
-
onDone?: () => void;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export interface ObservableLike<TValue, TError = unknown> {
|
|
8
|
-
subscribe(callbacks: ObservableCallbacks<TValue, TError>): UnsubscribeFn;
|
|
9
|
-
next(value: TValue): void;
|
|
10
|
-
done(): void;
|
|
11
|
-
error(error: TError): void;
|
|
12
|
-
}
|
|
13
|
-
export interface ObservableSubject<TValue, TError = unknown>
|
|
14
|
-
extends ObservableLike<TValue, TError> {
|
|
15
|
-
get(): TValue;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export type UnsubscribeFn = () => void;
|
|
19
|
-
|
|
20
|
-
export function observable<TValue, TError = unknown>(): ObservableLike<
|
|
21
|
-
TValue,
|
|
22
|
-
TError
|
|
23
|
-
> {
|
|
24
|
-
type Listener = {
|
|
25
|
-
callbacks: ObservableCallbacks<TValue, TError>;
|
|
26
|
-
unsubscribe: () => void;
|
|
27
|
-
};
|
|
28
|
-
const listeners: Listener[] = [];
|
|
29
|
-
|
|
30
|
-
let value: TValue | null = null;
|
|
31
|
-
return {
|
|
32
|
-
subscribe(callbacks) {
|
|
33
|
-
const listener: Listener = {
|
|
34
|
-
callbacks,
|
|
35
|
-
unsubscribe() {
|
|
36
|
-
const index = listeners.indexOf(listener);
|
|
37
|
-
if (index !== -1) {
|
|
38
|
-
listeners.splice(index, 1);
|
|
39
|
-
listener.callbacks.onDone?.();
|
|
40
|
-
}
|
|
41
|
-
},
|
|
42
|
-
};
|
|
43
|
-
listeners.push(listener);
|
|
44
|
-
return () => {
|
|
45
|
-
listener.unsubscribe();
|
|
46
|
-
};
|
|
47
|
-
},
|
|
48
|
-
next(newValue) {
|
|
49
|
-
const oldValue = value;
|
|
50
|
-
value = newValue;
|
|
51
|
-
if (oldValue !== newValue) {
|
|
52
|
-
for (const listener of listeners) {
|
|
53
|
-
listener.callbacks.onNext?.(newValue);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
},
|
|
57
|
-
done() {
|
|
58
|
-
while (listeners.length) {
|
|
59
|
-
const listener = listeners.pop();
|
|
60
|
-
listener?.callbacks.onDone?.();
|
|
61
|
-
listener?.unsubscribe();
|
|
62
|
-
}
|
|
63
|
-
},
|
|
64
|
-
error(error) {
|
|
65
|
-
for (const listener of listeners) {
|
|
66
|
-
listener.callbacks.onError?.(error);
|
|
67
|
-
}
|
|
68
|
-
},
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
export function observableSubject<TValue, TError = unknown>(
|
|
73
|
-
initialValue: TValue,
|
|
74
|
-
): ObservableSubject<TValue, TError> {
|
|
75
|
-
const $obs = observable<TValue>();
|
|
76
|
-
let value = initialValue;
|
|
77
|
-
$obs.next(initialValue);
|
|
78
|
-
|
|
79
|
-
return {
|
|
80
|
-
...$obs,
|
|
81
|
-
next(v) {
|
|
82
|
-
value = v;
|
|
83
|
-
$obs.next(v);
|
|
84
|
-
},
|
|
85
|
-
get() {
|
|
86
|
-
return value;
|
|
87
|
-
},
|
|
88
|
-
};
|
|
89
|
-
}
|