@trpc/client 10.0.0-alpha.3 → 10.0.0-alpha.30
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.d.ts +3 -11
- package/dist/TRPCClientError.d.ts.map +1 -1
- package/dist/createTRPCClient.d.ts +7 -4
- package/dist/createTRPCClient.d.ts.map +1 -1
- package/dist/{httpUtils-c09d7b79.mjs → httpUtils-089853f6.mjs} +38 -30
- package/dist/{httpUtils-1f9e4e3c.js → httpUtils-a0169eef.js} +38 -29
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +257 -103
- package/dist/index.mjs +246 -95
- package/dist/internals/TRPCClient.d.ts +14 -17
- package/dist/internals/TRPCClient.d.ts.map +1 -1
- package/dist/internals/dataLoader.d.ts +7 -4
- package/dist/internals/dataLoader.d.ts.map +1 -1
- package/dist/links/dedupeLink.d.ts.map +1 -1
- package/dist/links/httpBatchLink.d.ts +5 -2
- package/dist/links/httpBatchLink.d.ts.map +1 -1
- package/dist/links/httpBatchLink.js +197 -69
- package/dist/links/httpBatchLink.mjs +197 -69
- package/dist/links/httpLink.d.ts +1 -1
- package/dist/links/httpLink.d.ts.map +1 -1
- package/dist/links/httpLink.js +5 -5
- package/dist/links/httpLink.mjs +5 -5
- package/dist/links/index.d.ts +9 -0
- package/dist/links/index.d.ts.map +1 -0
- package/dist/links/internals/httpUtils.d.ts +30 -0
- package/dist/links/internals/httpUtils.d.ts.map +1 -0
- package/dist/links/internals/transformOperationResult.d.ts +17 -3
- package/dist/links/internals/transformOperationResult.d.ts.map +1 -1
- package/dist/links/loggerLink.d.ts.map +1 -1
- package/dist/links/loggerLink.js +2 -3
- package/dist/links/loggerLink.mjs +1 -2
- package/dist/links/retryLink.d.ts.map +1 -1
- package/dist/links/splitLink.d.ts +1 -1
- package/dist/links/splitLink.d.ts.map +1 -1
- package/dist/links/splitLink.js +3 -25
- package/dist/links/splitLink.mjs +2 -28
- package/dist/links/types.d.ts +12 -9
- package/dist/links/types.d.ts.map +1 -1
- package/dist/links/wsLink.d.ts +3 -3
- package/dist/links/wsLink.d.ts.map +1 -1
- package/dist/links/wsLink.js +11 -376
- package/dist/links/wsLink.mjs +9 -369
- package/dist/splitLink-62ff9db0.mjs +49 -0
- package/dist/splitLink-8202386e.js +52 -0
- package/dist/wsLink-69f5adb8.js +451 -0
- package/dist/wsLink-9468a563.mjs +436 -0
- package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.cjs.dev.js +81 -15
- package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.cjs.prod.js +81 -15
- package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.esm.js +77 -15
- package/links/httpBatchLink/index.d.ts +1 -1
- package/links/httpBatchLink/index.js +1 -1
- package/links/httpLink/dist/trpc-client-links-httpLink.cjs.dev.js +76 -9
- package/links/httpLink/dist/trpc-client-links-httpLink.cjs.prod.js +76 -9
- package/links/httpLink/dist/trpc-client-links-httpLink.esm.js +72 -9
- package/links/httpLink/index.d.ts +1 -1
- package/links/httpLink/index.js +1 -1
- package/links/loggerLink/index.d.ts +1 -1
- package/links/loggerLink/index.js +1 -1
- package/links/splitLink/index.d.ts +1 -1
- package/links/splitLink/index.js +1 -1
- package/links/wsLink/dist/trpc-client-links-wsLink.cjs.dev.js +3 -1
- package/links/wsLink/dist/trpc-client-links-wsLink.cjs.prod.js +3 -1
- package/links/wsLink/dist/trpc-client-links-wsLink.esm.js +3 -1
- package/links/wsLink/index.d.ts +1 -1
- package/links/wsLink/index.js +1 -1
- package/package.json +23 -30
- package/src/TRPCClientError.ts +5 -13
- package/src/createTRPCClient.ts +57 -3
- package/src/index.ts +1 -1
- package/src/internals/TRPCClient.ts +52 -36
- package/src/internals/dataLoader.ts +120 -45
- package/src/links/dedupeLink.test.ts +1 -1
- package/src/links/dedupeLink.ts +3 -5
- package/src/links/httpBatchLink.ts +64 -31
- package/src/links/httpLink.ts +4 -4
- package/src/links/index.ts +8 -0
- package/src/links/internals/createChain.test.ts +5 -5
- package/src/links/internals/createChain.ts +3 -3
- package/src/links/internals/httpUtils.ts +116 -0
- package/src/links/internals/transformOperationResult.ts +40 -12
- package/src/links/loggerLink.ts +1 -2
- package/src/links/retryLink.ts +1 -2
- package/src/links/splitLink.test.ts +3 -3
- package/src/links/splitLink.ts +5 -5
- package/src/links/types.ts +14 -9
- package/src/links/wsLink.ts +16 -14
- package/dist/TRPCClientError-17b117e1.mjs +0 -81
- package/dist/TRPCClientError-ec6bd2ca.js +0 -94
- package/dist/createChain-389cc116.mjs +0 -25
- package/dist/createChain-864dbb30.js +0 -27
- package/dist/internals/transformRPCResponse.d.ts +0 -15
- package/dist/internals/transformRPCResponse.d.ts.map +0 -1
- package/dist/links/httpUtils.d.ts +0 -22
- package/dist/links/httpUtils.d.ts.map +0 -1
- package/dist/links/transformerLink.d.ts +0 -4
- package/dist/links/transformerLink.d.ts.map +0 -1
- package/dist/links/transformerLink.js +0 -75
- package/dist/links/transformerLink.mjs +0 -67
- package/dist/links/transformerLink.ts.js +0 -1
- package/dist/observable-7e66cb56.mjs +0 -102
- package/dist/observable-85669100.js +0 -104
- package/dist/rx/index.d.ts +0 -3
- package/dist/rx/index.d.ts.map +0 -1
- package/dist/rx/index.js +0 -9
- package/dist/rx/index.mjs +0 -1
- package/dist/rx/index.ts.js +0 -1
- package/dist/rx/observable.d.ts +0 -4
- package/dist/rx/observable.d.ts.map +0 -1
- package/dist/rx/operators/index.d.ts +0 -4
- package/dist/rx/operators/index.d.ts.map +0 -1
- package/dist/rx/operators/index.js +0 -38
- package/dist/rx/operators/index.mjs +0 -32
- package/dist/rx/operators/index.ts.js +0 -1
- package/dist/rx/operators/map.d.ts +0 -3
- package/dist/rx/operators/map.d.ts.map +0 -1
- package/dist/rx/operators/share.d.ts +0 -6
- package/dist/rx/operators/share.d.ts.map +0 -1
- package/dist/rx/operators/tap.d.ts +0 -3
- package/dist/rx/operators/tap.d.ts.map +0 -1
- package/dist/rx/types.d.ts +0 -29
- package/dist/rx/types.d.ts.map +0 -1
- package/dist/rx/util/identity.d.ts +0 -2
- package/dist/rx/util/identity.d.ts.map +0 -1
- package/dist/rx/util/observableToPromise.d.ts +0 -10
- package/dist/rx/util/observableToPromise.d.ts.map +0 -1
- package/dist/rx/util/pipe.d.ts +0 -4
- package/dist/rx/util/pipe.d.ts.map +0 -1
- package/dist/share-4d79dc49.js +0 -110
- package/dist/share-671b63fa.mjs +0 -108
- package/dist/tap-538fa07f.mjs +0 -30
- package/dist/tap-799a2d33.js +0 -32
- package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.d.ts +0 -1
- package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.dev.js +0 -67
- package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.js +0 -7
- package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.prod.js +0 -67
- package/links/transformerLink/dist/trpc-client-links-transformerLink.esm.js +0 -63
- package/links/transformerLink/index.d.ts +0 -1
- package/links/transformerLink/index.js +0 -1
- package/rx/dist/trpc-client-rx.cjs.d.ts +0 -1
- package/rx/dist/trpc-client-rx.cjs.dev.js +0 -9
- package/rx/dist/trpc-client-rx.cjs.js +0 -7
- package/rx/dist/trpc-client-rx.cjs.prod.js +0 -9
- package/rx/dist/trpc-client-rx.esm.js +0 -1
- package/rx/index.d.ts +0 -1
- package/rx/index.js +0 -1
- package/rx/operators/dist/trpc-client-rx-operators.cjs.d.ts +0 -1
- package/rx/operators/dist/trpc-client-rx-operators.cjs.dev.js +0 -39
- package/rx/operators/dist/trpc-client-rx-operators.cjs.js +0 -7
- package/rx/operators/dist/trpc-client-rx-operators.cjs.prod.js +0 -39
- package/rx/operators/dist/trpc-client-rx-operators.esm.js +0 -33
- package/rx/operators/index.d.ts +0 -1
- package/rx/operators/index.js +0 -1
- package/src/internals/transformRPCResponse.ts +0 -25
- package/src/links/httpUtils.ts +0 -94
- package/src/links/transformerLink.ts +0 -70
- package/src/rx/index.ts +0 -2
- package/src/rx/observable.test.ts +0 -83
- package/src/rx/observable.ts +0 -70
- package/src/rx/operators/index.ts +0 -3
- package/src/rx/operators/map.test.ts +0 -92
- package/src/rx/operators/map.ts +0 -25
- package/src/rx/operators/share.test.ts +0 -60
- package/src/rx/operators/share.ts +0 -67
- package/src/rx/operators/tap.ts +0 -26
- package/src/rx/types.ts +0 -69
- package/src/rx/util/identity.ts +0 -3
- package/src/rx/util/observableToPromise.ts +0 -49
- package/src/rx/util/pipe.ts +0 -22
|
@@ -0,0 +1,436 @@
|
|
|
1
|
+
import _createClass from '@babel/runtime/helpers/createClass';
|
|
2
|
+
import _classCallCheck from '@babel/runtime/helpers/classCallCheck';
|
|
3
|
+
import _assertThisInitialized from '@babel/runtime/helpers/assertThisInitialized';
|
|
4
|
+
import _inherits from '@babel/runtime/helpers/inherits';
|
|
5
|
+
import _possibleConstructorReturn from '@babel/runtime/helpers/possibleConstructorReturn';
|
|
6
|
+
import _getPrototypeOf from '@babel/runtime/helpers/getPrototypeOf';
|
|
7
|
+
import _wrapNativeSuper from '@babel/runtime/helpers/wrapNativeSuper';
|
|
8
|
+
import { observable } from '@trpc/server/observable';
|
|
9
|
+
import _defineProperty from '@babel/runtime/helpers/defineProperty';
|
|
10
|
+
|
|
11
|
+
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; }
|
|
12
|
+
|
|
13
|
+
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; }
|
|
14
|
+
|
|
15
|
+
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); }; }
|
|
16
|
+
|
|
17
|
+
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; } }
|
|
18
|
+
|
|
19
|
+
var TRPCClientError = /*#__PURE__*/function (_Error) {
|
|
20
|
+
_inherits(TRPCClientError, _Error);
|
|
21
|
+
|
|
22
|
+
var _super = /*#__PURE__*/_createSuper$1(TRPCClientError);
|
|
23
|
+
|
|
24
|
+
function TRPCClientError(message, opts) {
|
|
25
|
+
var _opts$result, _opts$result2;
|
|
26
|
+
|
|
27
|
+
var _this;
|
|
28
|
+
|
|
29
|
+
_classCallCheck(this, TRPCClientError);
|
|
30
|
+
|
|
31
|
+
var cause = opts === null || opts === void 0 ? void 0 : opts.cause; // eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
32
|
+
// @ts-ignore https://github.com/tc39/proposal-error-cause
|
|
33
|
+
|
|
34
|
+
_this = _super.call(this, message, {
|
|
35
|
+
cause: cause
|
|
36
|
+
});
|
|
37
|
+
_this.cause = void 0;
|
|
38
|
+
_this.shape = void 0;
|
|
39
|
+
_this.data = void 0;
|
|
40
|
+
_this.meta = void 0;
|
|
41
|
+
_this.meta = opts === null || opts === void 0 ? void 0 : opts.meta;
|
|
42
|
+
_this.cause = cause;
|
|
43
|
+
_this.shape = opts === null || opts === void 0 ? void 0 : (_opts$result = opts.result) === null || _opts$result === void 0 ? void 0 : _opts$result.error;
|
|
44
|
+
_this.data = opts === null || opts === void 0 ? void 0 : (_opts$result2 = opts.result) === null || _opts$result2 === void 0 ? void 0 : _opts$result2.error.data;
|
|
45
|
+
_this.name = 'TRPCClientError';
|
|
46
|
+
Object.setPrototypeOf(_assertThisInitialized(_this), TRPCClientError.prototype);
|
|
47
|
+
return _this;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
_createClass(TRPCClientError, null, [{
|
|
51
|
+
key: "from",
|
|
52
|
+
value: function from(cause) {
|
|
53
|
+
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
54
|
+
|
|
55
|
+
if (!(cause instanceof Error)) {
|
|
56
|
+
var _message;
|
|
57
|
+
|
|
58
|
+
return new TRPCClientError((_message = cause.error.message) !== null && _message !== void 0 ? _message : '', _objectSpread(_objectSpread({}, opts), {}, {
|
|
59
|
+
cause: undefined,
|
|
60
|
+
result: cause
|
|
61
|
+
}));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (cause.name === 'TRPCClientError') {
|
|
65
|
+
return cause;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return new TRPCClientError(cause.message, _objectSpread(_objectSpread({}, opts), {}, {
|
|
69
|
+
cause: cause,
|
|
70
|
+
result: null
|
|
71
|
+
}));
|
|
72
|
+
}
|
|
73
|
+
}]);
|
|
74
|
+
|
|
75
|
+
return TRPCClientError;
|
|
76
|
+
}( /*#__PURE__*/_wrapNativeSuper(Error));
|
|
77
|
+
|
|
78
|
+
/* istanbul ignore next */
|
|
79
|
+
var retryDelay = function retryDelay(attemptIndex) {
|
|
80
|
+
return attemptIndex === 0 ? 0 : Math.min(1000 * Math.pow(2, attemptIndex), 30000);
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
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); }; }
|
|
84
|
+
|
|
85
|
+
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; } }
|
|
86
|
+
function createWSClient(opts) {
|
|
87
|
+
var url = opts.url,
|
|
88
|
+
_opts$WebSocket = opts.WebSocket,
|
|
89
|
+
WebSocketImpl = _opts$WebSocket === void 0 ? WebSocket : _opts$WebSocket,
|
|
90
|
+
_opts$retryDelayMs = opts.retryDelayMs,
|
|
91
|
+
retryDelayFn = _opts$retryDelayMs === void 0 ? retryDelay : _opts$retryDelayMs;
|
|
92
|
+
/* istanbul ignore next */
|
|
93
|
+
|
|
94
|
+
if (!WebSocketImpl) {
|
|
95
|
+
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");
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* outgoing messages buffer whilst not open
|
|
99
|
+
*/
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
var outgoing = [];
|
|
103
|
+
/**
|
|
104
|
+
* pending outgoing requests that are awaiting callback
|
|
105
|
+
*/
|
|
106
|
+
|
|
107
|
+
var pendingRequests = Object.create(null);
|
|
108
|
+
var connectAttempt = 0;
|
|
109
|
+
var dispatchTimer = null;
|
|
110
|
+
var connectTimer = null;
|
|
111
|
+
var activeConnection = createWS();
|
|
112
|
+
var state = 'connecting';
|
|
113
|
+
/**
|
|
114
|
+
* tries to send the list of messages
|
|
115
|
+
*/
|
|
116
|
+
|
|
117
|
+
function dispatch() {
|
|
118
|
+
if (state !== 'open' || dispatchTimer) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
dispatchTimer = setTimeout(function () {
|
|
123
|
+
dispatchTimer = null;
|
|
124
|
+
|
|
125
|
+
if (outgoing.length === 1) {
|
|
126
|
+
// single send
|
|
127
|
+
activeConnection.send(JSON.stringify(outgoing.pop()));
|
|
128
|
+
} else {
|
|
129
|
+
// batch send
|
|
130
|
+
activeConnection.send(JSON.stringify(outgoing));
|
|
131
|
+
} // clear
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
outgoing = [];
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function tryReconnect() {
|
|
139
|
+
if (connectTimer || state === 'closed') {
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
var timeout = retryDelayFn(connectAttempt++);
|
|
144
|
+
reconnectInMs(timeout);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function reconnect() {
|
|
148
|
+
state = 'connecting';
|
|
149
|
+
var oldConnection = activeConnection;
|
|
150
|
+
activeConnection = createWS();
|
|
151
|
+
closeIfNoPending(oldConnection);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function reconnectInMs(ms) {
|
|
155
|
+
if (connectTimer) {
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
state = 'connecting';
|
|
160
|
+
connectTimer = setTimeout(reconnect, ms);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function closeIfNoPending(conn) {
|
|
164
|
+
// disconnect as soon as there are are no pending result
|
|
165
|
+
var hasPendingRequests = Object.values(pendingRequests).some(function (p) {
|
|
166
|
+
return p.ws === conn;
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
if (!hasPendingRequests) {
|
|
170
|
+
conn.close();
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function resumeSubscriptionOnReconnect(req) {
|
|
175
|
+
if (outgoing.some(function (r) {
|
|
176
|
+
return r.id === req.op.id;
|
|
177
|
+
})) {
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
request(req.op, req.callbacks);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function createWS() {
|
|
185
|
+
var conn = new WebSocketImpl(url);
|
|
186
|
+
clearTimeout(connectTimer);
|
|
187
|
+
connectTimer = null;
|
|
188
|
+
conn.addEventListener('open', function () {
|
|
189
|
+
/* istanbul ignore next */
|
|
190
|
+
if (conn !== activeConnection) {
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
connectAttempt = 0;
|
|
195
|
+
state = 'open';
|
|
196
|
+
dispatch();
|
|
197
|
+
});
|
|
198
|
+
conn.addEventListener('error', function () {
|
|
199
|
+
if (conn === activeConnection) {
|
|
200
|
+
tryReconnect();
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
var handleIncomingRequest = function handleIncomingRequest(req) {
|
|
205
|
+
if (req.method === 'reconnect' && conn === activeConnection) {
|
|
206
|
+
reconnect(); // notify subscribers
|
|
207
|
+
|
|
208
|
+
for (var _i = 0, _Object$values = Object.values(pendingRequests); _i < _Object$values.length; _i++) {
|
|
209
|
+
var pendingReq = _Object$values[_i];
|
|
210
|
+
|
|
211
|
+
if (pendingReq.type === 'subscription') {
|
|
212
|
+
resumeSubscriptionOnReconnect(pendingReq);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
var handleIncomingResponse = function handleIncomingResponse(data) {
|
|
219
|
+
var _req$callbacks$next, _req$callbacks;
|
|
220
|
+
|
|
221
|
+
var req = data.id !== null && pendingRequests[data.id];
|
|
222
|
+
|
|
223
|
+
if (!req) {
|
|
224
|
+
// do something?
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
(_req$callbacks$next = (_req$callbacks = req.callbacks).next) === null || _req$callbacks$next === void 0 ? void 0 : _req$callbacks$next.call(_req$callbacks, {
|
|
229
|
+
data: data
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
if (req.ws !== activeConnection && conn === activeConnection) {
|
|
233
|
+
var oldWs = req.ws; // gracefully replace old connection with this
|
|
234
|
+
|
|
235
|
+
req.ws = activeConnection;
|
|
236
|
+
closeIfNoPending(oldWs);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
if ('result' in data && data.result.type === 'stopped' && conn === activeConnection) {
|
|
240
|
+
req.callbacks.complete();
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
|
|
244
|
+
conn.addEventListener('message', function (_ref) {
|
|
245
|
+
var data = _ref.data;
|
|
246
|
+
var msg = JSON.parse(data);
|
|
247
|
+
|
|
248
|
+
if ('method' in msg) {
|
|
249
|
+
handleIncomingRequest(msg);
|
|
250
|
+
} else {
|
|
251
|
+
handleIncomingResponse(msg);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
if (conn !== activeConnection || state === 'closed') {
|
|
255
|
+
// when receiving a message, we close old connection that has no pending requests
|
|
256
|
+
closeIfNoPending(conn);
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
conn.addEventListener('close', function () {
|
|
260
|
+
if (activeConnection === conn) {
|
|
261
|
+
// connection might have been replaced already
|
|
262
|
+
tryReconnect();
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
for (var key in pendingRequests) {
|
|
266
|
+
var _req$callbacks$error, _req$callbacks2;
|
|
267
|
+
|
|
268
|
+
var req = pendingRequests[key];
|
|
269
|
+
|
|
270
|
+
if (req.ws !== conn) {
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
(_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')));
|
|
275
|
+
|
|
276
|
+
if (req.type !== 'subscription') {
|
|
277
|
+
var _req$callbacks$comple, _req$callbacks3;
|
|
278
|
+
|
|
279
|
+
delete pendingRequests[key];
|
|
280
|
+
(_req$callbacks$comple = (_req$callbacks3 = req.callbacks).complete) === null || _req$callbacks$comple === void 0 ? void 0 : _req$callbacks$comple.call(_req$callbacks3);
|
|
281
|
+
} else if (state !== 'closed') {
|
|
282
|
+
// request restart of sub with next connection
|
|
283
|
+
resumeSubscriptionOnReconnect(req);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
});
|
|
287
|
+
return conn;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
function request(op, callbacks) {
|
|
291
|
+
var type = op.type,
|
|
292
|
+
input = op.input,
|
|
293
|
+
path = op.path,
|
|
294
|
+
id = op.id;
|
|
295
|
+
var envelope = {
|
|
296
|
+
id: id,
|
|
297
|
+
method: type,
|
|
298
|
+
params: {
|
|
299
|
+
input: input,
|
|
300
|
+
path: path
|
|
301
|
+
}
|
|
302
|
+
};
|
|
303
|
+
pendingRequests[id] = {
|
|
304
|
+
ws: activeConnection,
|
|
305
|
+
type: type,
|
|
306
|
+
callbacks: callbacks,
|
|
307
|
+
op: op
|
|
308
|
+
}; // enqueue message
|
|
309
|
+
|
|
310
|
+
outgoing.push(envelope);
|
|
311
|
+
dispatch();
|
|
312
|
+
return function () {
|
|
313
|
+
var _pendingRequests$id, _callbacks$complete;
|
|
314
|
+
|
|
315
|
+
var callbacks = (_pendingRequests$id = pendingRequests[id]) === null || _pendingRequests$id === void 0 ? void 0 : _pendingRequests$id.callbacks;
|
|
316
|
+
delete pendingRequests[id];
|
|
317
|
+
outgoing = outgoing.filter(function (msg) {
|
|
318
|
+
return msg.id !== id;
|
|
319
|
+
});
|
|
320
|
+
callbacks === null || callbacks === void 0 ? void 0 : (_callbacks$complete = callbacks.complete) === null || _callbacks$complete === void 0 ? void 0 : _callbacks$complete.call(callbacks);
|
|
321
|
+
|
|
322
|
+
if (op.type === 'subscription') {
|
|
323
|
+
outgoing.push({
|
|
324
|
+
id: id,
|
|
325
|
+
method: 'subscription.stop'
|
|
326
|
+
});
|
|
327
|
+
dispatch();
|
|
328
|
+
}
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
return {
|
|
333
|
+
close: function close() {
|
|
334
|
+
state = 'closed';
|
|
335
|
+
closeIfNoPending(activeConnection);
|
|
336
|
+
clearTimeout(connectTimer);
|
|
337
|
+
connectTimer = null;
|
|
338
|
+
},
|
|
339
|
+
request: request,
|
|
340
|
+
getConnection: function getConnection() {
|
|
341
|
+
return activeConnection;
|
|
342
|
+
}
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
var TRPCWebSocketClosedError = /*#__PURE__*/function (_Error) {
|
|
347
|
+
_inherits(TRPCWebSocketClosedError, _Error);
|
|
348
|
+
|
|
349
|
+
var _super = /*#__PURE__*/_createSuper(TRPCWebSocketClosedError);
|
|
350
|
+
|
|
351
|
+
function TRPCWebSocketClosedError(message) {
|
|
352
|
+
var _this;
|
|
353
|
+
|
|
354
|
+
_classCallCheck(this, TRPCWebSocketClosedError);
|
|
355
|
+
|
|
356
|
+
_this = _super.call(this, message);
|
|
357
|
+
_this.name = 'TRPCWebSocketClosedError';
|
|
358
|
+
Object.setPrototypeOf(_assertThisInitialized(_this), TRPCWebSocketClosedError.prototype);
|
|
359
|
+
return _this;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
return _createClass(TRPCWebSocketClosedError);
|
|
363
|
+
}( /*#__PURE__*/_wrapNativeSuper(Error));
|
|
364
|
+
|
|
365
|
+
var TRPCSubscriptionEndedError = /*#__PURE__*/function (_Error2) {
|
|
366
|
+
_inherits(TRPCSubscriptionEndedError, _Error2);
|
|
367
|
+
|
|
368
|
+
var _super2 = /*#__PURE__*/_createSuper(TRPCSubscriptionEndedError);
|
|
369
|
+
|
|
370
|
+
function TRPCSubscriptionEndedError(message) {
|
|
371
|
+
var _this2;
|
|
372
|
+
|
|
373
|
+
_classCallCheck(this, TRPCSubscriptionEndedError);
|
|
374
|
+
|
|
375
|
+
_this2 = _super2.call(this, message);
|
|
376
|
+
_this2.name = 'TRPCSubscriptionEndedError';
|
|
377
|
+
Object.setPrototypeOf(_assertThisInitialized(_this2), TRPCSubscriptionEndedError.prototype);
|
|
378
|
+
return _this2;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
return _createClass(TRPCSubscriptionEndedError);
|
|
382
|
+
}( /*#__PURE__*/_wrapNativeSuper(Error));
|
|
383
|
+
|
|
384
|
+
function wsLink(opts) {
|
|
385
|
+
return function (runtime) {
|
|
386
|
+
var client = opts.client;
|
|
387
|
+
return function (_ref2) {
|
|
388
|
+
var op = _ref2.op;
|
|
389
|
+
return observable(function (observer) {
|
|
390
|
+
var type = op.type,
|
|
391
|
+
path = op.path,
|
|
392
|
+
id = op.id,
|
|
393
|
+
context = op.context;
|
|
394
|
+
var input = runtime.transformer.serialize(op.input);
|
|
395
|
+
var isDone = false;
|
|
396
|
+
var unsub = client.request({
|
|
397
|
+
type: type,
|
|
398
|
+
path: path,
|
|
399
|
+
input: input,
|
|
400
|
+
id: id,
|
|
401
|
+
context: context
|
|
402
|
+
}, {
|
|
403
|
+
error: function error(err) {
|
|
404
|
+
isDone = true;
|
|
405
|
+
observer.error(err);
|
|
406
|
+
unsub();
|
|
407
|
+
},
|
|
408
|
+
complete: function complete() {
|
|
409
|
+
if (!isDone) {
|
|
410
|
+
isDone = true;
|
|
411
|
+
observer.error(TRPCClientError.from(new TRPCSubscriptionEndedError('Operation ended prematurely')));
|
|
412
|
+
} else {
|
|
413
|
+
observer.complete();
|
|
414
|
+
}
|
|
415
|
+
},
|
|
416
|
+
next: function next(result) {
|
|
417
|
+
observer.next(result);
|
|
418
|
+
|
|
419
|
+
if (op.type !== 'subscription') {
|
|
420
|
+
// if it isn't a subscription we don't care about next response
|
|
421
|
+
isDone = true;
|
|
422
|
+
unsub();
|
|
423
|
+
observer.complete();
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
});
|
|
427
|
+
return function () {
|
|
428
|
+
isDone = true;
|
|
429
|
+
unsub();
|
|
430
|
+
};
|
|
431
|
+
});
|
|
432
|
+
};
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
export { TRPCClientError as T, createWSClient as c, wsLink as w };
|
|
@@ -2,16 +2,22 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var
|
|
6
|
-
var
|
|
7
|
-
var
|
|
8
|
-
require('@babel/runtime/helpers/createClass');
|
|
5
|
+
var _objectSpread = require('@babel/runtime/helpers/objectSpread2');
|
|
6
|
+
var _asyncToGenerator = require('@babel/runtime/helpers/asyncToGenerator');
|
|
7
|
+
var _regeneratorRuntime = require('@babel/runtime/regenerator');
|
|
9
8
|
require('@babel/runtime/helpers/classCallCheck');
|
|
9
|
+
require('@babel/runtime/helpers/createClass');
|
|
10
|
+
var TRPCClientError = require('../../../dist/TRPCClientError-bfee2bf5.cjs.dev.js');
|
|
11
|
+
var transformRPCResponse = require('../../../dist/transformRPCResponse-c6cc4677.cjs.dev.js');
|
|
12
|
+
var _createForOfIteratorHelper = require('@babel/runtime/helpers/createForOfIteratorHelper');
|
|
10
13
|
require('@babel/runtime/helpers/assertThisInitialized');
|
|
11
14
|
require('@babel/runtime/helpers/inherits');
|
|
12
15
|
require('@babel/runtime/helpers/createSuper');
|
|
13
16
|
require('@babel/runtime/helpers/wrapNativeSuper');
|
|
14
|
-
|
|
17
|
+
|
|
18
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
|
|
19
|
+
|
|
20
|
+
var _regeneratorRuntime__default = /*#__PURE__*/_interopDefault(_regeneratorRuntime);
|
|
15
21
|
|
|
16
22
|
/**
|
|
17
23
|
* Dataloader that's very inspired by https://github.com/graphql/dataloader
|
|
@@ -124,12 +130,65 @@ function httpBatchLink(opts) {
|
|
|
124
130
|
|
|
125
131
|
return function (runtime) {
|
|
126
132
|
// initialized in app
|
|
133
|
+
var getHeaders = /*#__PURE__*/function () {
|
|
134
|
+
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime__default['default'].mark(function _callee(arg1) {
|
|
135
|
+
var val;
|
|
136
|
+
return _regeneratorRuntime__default['default'].wrap(function _callee$(_context) {
|
|
137
|
+
while (1) {
|
|
138
|
+
switch (_context.prev = _context.next) {
|
|
139
|
+
case 0:
|
|
140
|
+
val = opts.headers;
|
|
141
|
+
_context.t0 = _objectSpread;
|
|
142
|
+
_context.t1 = _objectSpread;
|
|
143
|
+
_context.t2 = {};
|
|
144
|
+
_context.next = 6;
|
|
145
|
+
return runtime.headers();
|
|
146
|
+
|
|
147
|
+
case 6:
|
|
148
|
+
_context.t3 = _context.sent;
|
|
149
|
+
_context.t4 = (0, _context.t1)(_context.t2, _context.t3);
|
|
150
|
+
|
|
151
|
+
if (!(typeof val === 'function')) {
|
|
152
|
+
_context.next = 14;
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
_context.next = 11;
|
|
157
|
+
return val(arg1);
|
|
158
|
+
|
|
159
|
+
case 11:
|
|
160
|
+
_context.t5 = _context.sent;
|
|
161
|
+
_context.next = 15;
|
|
162
|
+
break;
|
|
163
|
+
|
|
164
|
+
case 14:
|
|
165
|
+
_context.t5 = val;
|
|
166
|
+
|
|
167
|
+
case 15:
|
|
168
|
+
_context.t6 = _context.t5;
|
|
169
|
+
return _context.abrupt("return", (0, _context.t0)(_context.t4, _context.t6));
|
|
170
|
+
|
|
171
|
+
case 17:
|
|
172
|
+
case "end":
|
|
173
|
+
return _context.stop();
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}, _callee);
|
|
177
|
+
}));
|
|
178
|
+
|
|
179
|
+
return function getHeaders(_x) {
|
|
180
|
+
return _ref.apply(this, arguments);
|
|
181
|
+
};
|
|
182
|
+
}();
|
|
183
|
+
|
|
127
184
|
var fetcher = function fetcher(type) {
|
|
128
|
-
return function (
|
|
129
|
-
var
|
|
185
|
+
return function (pairs) {
|
|
186
|
+
var _opts$fetch, _opts$AbortController;
|
|
187
|
+
|
|
188
|
+
var path = pairs.map(function (op) {
|
|
130
189
|
return op.path;
|
|
131
190
|
}).join(',');
|
|
132
|
-
var inputs =
|
|
191
|
+
var inputs = pairs.map(function (op) {
|
|
133
192
|
return op.input;
|
|
134
193
|
});
|
|
135
194
|
|
|
@@ -137,8 +196,15 @@ function httpBatchLink(opts) {
|
|
|
137
196
|
url: url,
|
|
138
197
|
inputs: inputs,
|
|
139
198
|
path: path,
|
|
140
|
-
|
|
141
|
-
|
|
199
|
+
type: type,
|
|
200
|
+
fetch: transformRPCResponse.getFetch((_opts$fetch = opts.fetch) !== null && _opts$fetch !== void 0 ? _opts$fetch : runtime.fetch),
|
|
201
|
+
AbortController: transformRPCResponse.getAbortController((_opts$AbortController = opts.AbortController) !== null && _opts$AbortController !== void 0 ? _opts$AbortController : runtime.AbortController),
|
|
202
|
+
headers: function headers() {
|
|
203
|
+
return getHeaders({
|
|
204
|
+
operations: pairs
|
|
205
|
+
});
|
|
206
|
+
},
|
|
207
|
+
transformer: runtime.transformer
|
|
142
208
|
}),
|
|
143
209
|
promise = _httpRequest.promise,
|
|
144
210
|
cancel = _httpRequest.cancel;
|
|
@@ -146,7 +212,7 @@ function httpBatchLink(opts) {
|
|
|
146
212
|
return {
|
|
147
213
|
promise: promise.then(function (res) {
|
|
148
214
|
if (!Array.isArray(res)) {
|
|
149
|
-
return
|
|
215
|
+
return pairs.map(function () {
|
|
150
216
|
return res;
|
|
151
217
|
});
|
|
152
218
|
}
|
|
@@ -172,10 +238,10 @@ function httpBatchLink(opts) {
|
|
|
172
238
|
subscription: subscription,
|
|
173
239
|
mutation: mutation
|
|
174
240
|
};
|
|
175
|
-
return function (
|
|
176
|
-
var op =
|
|
177
|
-
prev =
|
|
178
|
-
onDestroy =
|
|
241
|
+
return function (_ref2) {
|
|
242
|
+
var op = _ref2.op,
|
|
243
|
+
prev = _ref2.prev,
|
|
244
|
+
onDestroy = _ref2.onDestroy;
|
|
179
245
|
var loader = loaders[op.type];
|
|
180
246
|
|
|
181
247
|
var _loader$load = loader.load(op),
|