@trpc/client 10.0.0-alpha.15 → 10.0.0-alpha.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (155) hide show
  1. package/dist/index.d.ts +1 -1
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +1081 -18
  4. package/dist/index.mjs +1064 -13
  5. package/dist/internals/TRPCClient.d.ts +3 -3
  6. package/dist/internals/TRPCClient.d.ts.map +1 -1
  7. package/dist/links/httpBatchLink.d.ts +1 -1
  8. package/dist/links/httpBatchLink.d.ts.map +1 -1
  9. package/dist/links/httpLink.d.ts +1 -1
  10. package/dist/links/httpLink.d.ts.map +1 -1
  11. package/dist/links/index.d.ts +11 -0
  12. package/dist/links/index.d.ts.map +1 -0
  13. package/dist/links/{httpUtils.d.ts → internals/httpUtils.d.ts} +1 -1
  14. package/dist/links/internals/httpUtils.d.ts.map +1 -0
  15. package/dist/links/transformerLink.d.ts.map +1 -1
  16. package/dist/links/types.d.ts +1 -1
  17. package/dist/links/types.d.ts.map +1 -1
  18. package/dist/links/wsLink.d.ts +1 -1
  19. package/dist/links/wsLink.d.ts.map +1 -1
  20. package/dist/observable/index.d.ts +4 -0
  21. package/dist/observable/index.d.ts.map +1 -0
  22. package/dist/{rx/operators → observable}/index.js +3 -3
  23. package/dist/{rx/operators → observable}/index.mjs +1 -2
  24. package/dist/{rx → observable}/index.ts.js +0 -0
  25. package/dist/{rx/util → observable/internals}/identity.d.ts +0 -0
  26. package/dist/observable/internals/identity.d.ts.map +1 -0
  27. package/dist/{rx/util → observable/internals}/observableToPromise.d.ts +0 -0
  28. package/dist/observable/internals/observableToPromise.d.ts.map +1 -0
  29. package/dist/{rx/util → observable/internals}/pipe.d.ts +0 -0
  30. package/dist/observable/internals/pipe.d.ts.map +1 -0
  31. package/dist/{rx → observable}/observable.d.ts +0 -0
  32. package/dist/observable/observable.d.ts.map +1 -0
  33. package/dist/{rx → observable}/operators/index.d.ts +0 -0
  34. package/dist/observable/operators/index.d.ts.map +1 -0
  35. package/dist/{rx → observable}/operators/map.d.ts +0 -0
  36. package/dist/observable/operators/map.d.ts.map +1 -0
  37. package/dist/{rx → observable}/operators/share.d.ts +0 -0
  38. package/dist/observable/operators/share.d.ts.map +1 -0
  39. package/dist/{rx → observable}/operators/tap.d.ts +0 -0
  40. package/dist/observable/operators/tap.d.ts.map +1 -0
  41. package/dist/{rx → observable}/types.d.ts +0 -0
  42. package/dist/observable/types.d.ts.map +1 -0
  43. package/dist/{share-671b63fa.mjs → tap-4bc60fb0.mjs} +131 -1
  44. package/dist/{share-4d79dc49.js → tap-fcd41ae6.js} +132 -0
  45. package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.cjs.d.ts +1 -11
  46. package/{dist/links/httpBatchLink.js → links/httpBatchLink/dist/trpc-client-links-httpBatchLink.cjs.dev.js} +63 -44
  47. package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.cjs.js +6 -15
  48. package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.cjs.prod.js +214 -0
  49. package/{dist/links/httpBatchLink.mjs → links/httpBatchLink/dist/trpc-client-links-httpBatchLink.esm.js} +62 -43
  50. package/links/httpLink/dist/trpc-client-links-httpLink.cjs.d.ts +1 -11
  51. package/links/httpLink/dist/trpc-client-links-httpLink.cjs.dev.js +67 -0
  52. package/links/httpLink/dist/trpc-client-links-httpLink.cjs.js +6 -15
  53. package/links/httpLink/dist/trpc-client-links-httpLink.cjs.prod.js +67 -0
  54. package/links/httpLink/dist/trpc-client-links-httpLink.esm.js +63 -0
  55. package/links/loggerLink/dist/trpc-client-links-loggerLink.cjs.d.ts +1 -11
  56. package/links/loggerLink/dist/trpc-client-links-loggerLink.cjs.dev.js +89 -0
  57. package/links/loggerLink/dist/trpc-client-links-loggerLink.cjs.js +6 -15
  58. package/links/loggerLink/dist/trpc-client-links-loggerLink.cjs.prod.js +89 -0
  59. package/links/loggerLink/dist/trpc-client-links-loggerLink.esm.js +85 -0
  60. package/links/splitLink/dist/trpc-client-links-splitLink.cjs.d.ts +1 -11
  61. package/links/splitLink/dist/trpc-client-links-splitLink.cjs.dev.js +19 -0
  62. package/links/splitLink/dist/trpc-client-links-splitLink.cjs.js +6 -15
  63. package/links/splitLink/dist/trpc-client-links-splitLink.cjs.prod.js +19 -0
  64. package/links/splitLink/dist/trpc-client-links-splitLink.esm.js +15 -0
  65. package/links/wsLink/dist/trpc-client-links-wsLink.cjs.d.ts +1 -11
  66. package/{dist/links/wsLink.js → links/wsLink/dist/trpc-client-links-wsLink.cjs.dev.js} +92 -82
  67. package/links/wsLink/dist/trpc-client-links-wsLink.cjs.js +6 -15
  68. package/links/wsLink/dist/trpc-client-links-wsLink.cjs.prod.js +395 -0
  69. package/{dist/links/wsLink.mjs → links/wsLink/dist/trpc-client-links-wsLink.esm.js} +87 -67
  70. package/package.json +8 -43
  71. package/src/index.ts +1 -1
  72. package/src/internals/TRPCClient.ts +8 -11
  73. package/src/links/dedupeLink.test.ts +1 -1
  74. package/src/links/dedupeLink.ts +5 -5
  75. package/src/links/httpBatchLink.ts +6 -2
  76. package/src/links/httpLink.ts +2 -2
  77. package/src/links/index.ts +10 -0
  78. package/src/links/internals/createChain.test.ts +5 -5
  79. package/src/links/internals/createChain.ts +3 -3
  80. package/src/links/{httpUtils.ts → internals/httpUtils.ts} +1 -1
  81. package/src/links/loggerLink.ts +2 -2
  82. package/src/links/retryLink.ts +2 -2
  83. package/src/links/splitLink.test.ts +3 -3
  84. package/src/links/splitLink.ts +1 -1
  85. package/src/links/transformerLink.ts +9 -5
  86. package/src/links/types.ts +1 -1
  87. package/src/links/wsLink.ts +1 -2
  88. package/src/{rx → observable}/index.ts +1 -0
  89. package/src/{rx/util → observable/internals}/identity.ts +0 -0
  90. package/src/{rx/util → observable/internals}/observableToPromise.ts +0 -0
  91. package/src/{rx/util → observable/internals}/pipe.ts +0 -0
  92. package/src/{rx → observable}/observable.test.ts +21 -1
  93. package/src/{rx → observable}/observable.ts +1 -1
  94. package/src/{rx → observable}/operators/index.ts +0 -0
  95. package/src/{rx → observable}/operators/map.test.ts +0 -0
  96. package/src/{rx → observable}/operators/map.ts +0 -0
  97. package/src/{rx → observable}/operators/share.test.ts +0 -0
  98. package/src/{rx → observable}/operators/share.ts +0 -0
  99. package/src/{rx → observable}/operators/tap.ts +0 -0
  100. package/src/{rx → observable}/types.ts +0 -0
  101. package/dist/TRPCClientError-17b117e1.mjs +0 -81
  102. package/dist/TRPCClientError-ec6bd2ca.js +0 -94
  103. package/dist/createChain-389cc116.mjs +0 -25
  104. package/dist/createChain-864dbb30.js +0 -27
  105. package/dist/httpUtils-1f9e4e3c.js +0 -97
  106. package/dist/httpUtils-c09d7b79.mjs +0 -91
  107. package/dist/links/httpBatchLink.ts.js +0 -1
  108. package/dist/links/httpLink.js +0 -44
  109. package/dist/links/httpLink.mjs +0 -40
  110. package/dist/links/httpLink.ts.js +0 -1
  111. package/dist/links/httpUtils.d.ts.map +0 -1
  112. package/dist/links/loggerLink.js +0 -108
  113. package/dist/links/loggerLink.mjs +0 -99
  114. package/dist/links/loggerLink.ts.js +0 -1
  115. package/dist/links/splitLink.js +0 -32
  116. package/dist/links/splitLink.mjs +0 -28
  117. package/dist/links/splitLink.ts.js +0 -1
  118. package/dist/links/transformerLink.js +0 -75
  119. package/dist/links/transformerLink.mjs +0 -67
  120. package/dist/links/transformerLink.ts.js +0 -1
  121. package/dist/links/wsLink.ts.js +0 -1
  122. package/dist/observable-7e66cb56.mjs +0 -102
  123. package/dist/observable-85669100.js +0 -104
  124. package/dist/rx/index.d.ts +0 -3
  125. package/dist/rx/index.d.ts.map +0 -1
  126. package/dist/rx/index.js +0 -9
  127. package/dist/rx/index.mjs +0 -1
  128. package/dist/rx/observable.d.ts.map +0 -1
  129. package/dist/rx/operators/index.d.ts.map +0 -1
  130. package/dist/rx/operators/index.ts.js +0 -1
  131. package/dist/rx/operators/map.d.ts.map +0 -1
  132. package/dist/rx/operators/share.d.ts.map +0 -1
  133. package/dist/rx/operators/tap.d.ts.map +0 -1
  134. package/dist/rx/types.d.ts.map +0 -1
  135. package/dist/rx/util/identity.d.ts.map +0 -1
  136. package/dist/rx/util/observableToPromise.d.ts.map +0 -1
  137. package/dist/rx/util/pipe.d.ts.map +0 -1
  138. package/dist/tap-538fa07f.mjs +0 -30
  139. package/dist/tap-799a2d33.js +0 -32
  140. package/links/httpBatchLink/index.d.ts +0 -1
  141. package/links/httpBatchLink/index.js +0 -1
  142. package/links/httpLink/index.d.ts +0 -1
  143. package/links/httpLink/index.js +0 -1
  144. package/links/loggerLink/index.d.ts +0 -1
  145. package/links/loggerLink/index.js +0 -1
  146. package/links/splitLink/index.d.ts +0 -1
  147. package/links/splitLink/index.js +0 -1
  148. package/links/transformerLink/index.d.ts +0 -1
  149. package/links/transformerLink/index.js +0 -1
  150. package/links/wsLink/index.d.ts +0 -1
  151. package/links/wsLink/index.js +0 -1
  152. package/rx/index.d.ts +0 -1
  153. package/rx/index.js +0 -1
  154. package/rx/operators/index.d.ts +0 -1
  155. package/rx/operators/index.js +0 -1
@@ -0,0 +1,395 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var _objectSpread = require('@babel/runtime/helpers/objectSpread2');
6
+ var _createClass = require('@babel/runtime/helpers/createClass');
7
+ var _classCallCheck = require('@babel/runtime/helpers/classCallCheck');
8
+ var _assertThisInitialized = require('@babel/runtime/helpers/assertThisInitialized');
9
+ var _inherits = require('@babel/runtime/helpers/inherits');
10
+ var _createSuper = require('@babel/runtime/helpers/createSuper');
11
+ var _wrapNativeSuper = require('@babel/runtime/helpers/wrapNativeSuper');
12
+ var TRPCClientError = require('../../../dist/TRPCClientError-e9bf96e9.cjs.prod.js');
13
+
14
+ /* istanbul ignore next */
15
+ var retryDelay = function retryDelay(attemptIndex) {
16
+ return attemptIndex === 0 ? 0 : Math.min(1000 * Math.pow(2, attemptIndex), 30000);
17
+ };
18
+
19
+ function createWSClient(opts) {
20
+ var url = opts.url,
21
+ _opts$WebSocket = opts.WebSocket,
22
+ WebSocketImpl = _opts$WebSocket === void 0 ? WebSocket : _opts$WebSocket,
23
+ _opts$retryDelayMs = opts.retryDelayMs,
24
+ retryDelayFn = _opts$retryDelayMs === void 0 ? retryDelay : _opts$retryDelayMs;
25
+ /* istanbul ignore next */
26
+
27
+ if (!WebSocketImpl) {
28
+ 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");
29
+ }
30
+ /**
31
+ * outgoing messages buffer whilst not open
32
+ */
33
+
34
+
35
+ var outgoing = [];
36
+ /**
37
+ * pending outgoing requests that are awaiting callback
38
+ */
39
+
40
+ var pendingRequests = Object.create(null);
41
+ var connectAttempt = 0;
42
+ var dispatchTimer = null;
43
+ var connectTimer = null;
44
+ var activeConnection = createWS();
45
+ var state = 'connecting';
46
+ /**
47
+ * tries to send the list of messages
48
+ */
49
+
50
+ function dispatch() {
51
+ if (state !== 'open' || dispatchTimer) {
52
+ return;
53
+ }
54
+
55
+ dispatchTimer = setTimeout(function () {
56
+ dispatchTimer = null;
57
+
58
+ if (outgoing.length === 1) {
59
+ // single send
60
+ activeConnection.send(JSON.stringify(outgoing.pop()));
61
+ } else {
62
+ // batch send
63
+ activeConnection.send(JSON.stringify(outgoing));
64
+ } // clear
65
+
66
+
67
+ outgoing = [];
68
+ });
69
+ }
70
+
71
+ function tryReconnect() {
72
+ if (connectTimer || state === 'closed') {
73
+ return;
74
+ }
75
+
76
+ var timeout = retryDelayFn(connectAttempt++);
77
+ reconnectInMs(timeout);
78
+ }
79
+
80
+ function reconnect() {
81
+ state = 'connecting';
82
+ var oldConnection = activeConnection;
83
+ activeConnection = createWS();
84
+ closeIfNoPending(oldConnection);
85
+ }
86
+
87
+ function reconnectInMs(ms) {
88
+ if (connectTimer) {
89
+ return;
90
+ }
91
+
92
+ state = 'connecting';
93
+ connectTimer = setTimeout(reconnect, ms);
94
+ }
95
+
96
+ function closeIfNoPending(conn) {
97
+ // disconnect as soon as there are are no pending result
98
+ var hasPendingRequests = Object.values(pendingRequests).some(function (p) {
99
+ return p.ws === conn;
100
+ });
101
+
102
+ if (!hasPendingRequests) {
103
+ conn.close();
104
+ }
105
+ }
106
+
107
+ function resumeSubscriptionOnReconnect(req) {
108
+ if (outgoing.some(function (r) {
109
+ return r.id === req.op.id;
110
+ })) {
111
+ return;
112
+ }
113
+
114
+ request(req.op, req.callbacks);
115
+ }
116
+
117
+ function createWS() {
118
+ var conn = new WebSocket(url);
119
+ clearTimeout(connectTimer);
120
+ connectTimer = null;
121
+ conn.addEventListener('open', function () {
122
+ /* istanbul ignore next */
123
+ if (conn !== activeConnection) {
124
+ return;
125
+ }
126
+
127
+ connectAttempt = 0;
128
+ state = 'open';
129
+ dispatch();
130
+ });
131
+ conn.addEventListener('error', function () {
132
+ if (conn === activeConnection) {
133
+ tryReconnect();
134
+ }
135
+ });
136
+
137
+ var handleIncomingRequest = function handleIncomingRequest(req) {
138
+ if (req.method === 'reconnect' && conn === activeConnection) {
139
+ reconnect(); // notify subscribers
140
+
141
+ for (var _i = 0, _Object$values = Object.values(pendingRequests); _i < _Object$values.length; _i++) {
142
+ var pendingReq = _Object$values[_i];
143
+
144
+ if (pendingReq.type === 'subscription') {
145
+ resumeSubscriptionOnReconnect(pendingReq);
146
+ }
147
+ }
148
+ }
149
+ };
150
+
151
+ var handleIncomingResponse = function handleIncomingResponse(res) {
152
+ var _req$callbacks$onNext, _req$callbacks2;
153
+
154
+ var req = res.id !== null && pendingRequests[res.id];
155
+
156
+ if (!req) {
157
+ // do something?
158
+ return;
159
+ }
160
+
161
+ if ('error' in res) {
162
+ var _req$callbacks$onErro, _req$callbacks;
163
+
164
+ (_req$callbacks$onErro = (_req$callbacks = req.callbacks).onError) === null || _req$callbacks$onErro === void 0 ? void 0 : _req$callbacks$onErro.call(_req$callbacks, res);
165
+ return;
166
+ }
167
+
168
+ (_req$callbacks$onNext = (_req$callbacks2 = req.callbacks).onNext) === null || _req$callbacks$onNext === void 0 ? void 0 : _req$callbacks$onNext.call(_req$callbacks2, res.result);
169
+
170
+ if (req.ws !== activeConnection && conn === activeConnection) {
171
+ var oldWs = req.ws; // gracefully replace old connection with this
172
+
173
+ req.ws = activeConnection;
174
+ closeIfNoPending(oldWs);
175
+ }
176
+
177
+ if (res.result.type === 'stopped' && conn === activeConnection) {
178
+ var _req$callbacks$onDone, _req$callbacks3;
179
+
180
+ (_req$callbacks$onDone = (_req$callbacks3 = req.callbacks).onDone) === null || _req$callbacks$onDone === void 0 ? void 0 : _req$callbacks$onDone.call(_req$callbacks3);
181
+ }
182
+ };
183
+
184
+ conn.addEventListener('message', function (_ref) {
185
+ var data = _ref.data;
186
+ var msg = JSON.parse(data);
187
+
188
+ if ('method' in msg) {
189
+ handleIncomingRequest(msg);
190
+ } else {
191
+ handleIncomingResponse(msg);
192
+ }
193
+
194
+ if (conn !== activeConnection || state === 'closed') {
195
+ // when receiving a message, we close old connection that has no pending requests
196
+ closeIfNoPending(conn);
197
+ }
198
+ });
199
+ conn.addEventListener('close', function () {
200
+ if (activeConnection === conn) {
201
+ // connection might have been replaced already
202
+ tryReconnect();
203
+ }
204
+
205
+ for (var key in pendingRequests) {
206
+ var _req$callbacks$onErro2, _req$callbacks4;
207
+
208
+ var req = pendingRequests[key];
209
+
210
+ if (req.ws !== conn) {
211
+ continue;
212
+ }
213
+
214
+ (_req$callbacks$onErro2 = (_req$callbacks4 = req.callbacks).onError) === null || _req$callbacks$onErro2 === void 0 ? void 0 : _req$callbacks$onErro2.call(_req$callbacks4, TRPCClientError.TRPCClientError.from(new TRPCWebSocketClosedError('WebSocket closed prematurely')));
215
+
216
+ if (req.type !== 'subscription') {
217
+ var _req$callbacks$onDone2, _req$callbacks5;
218
+
219
+ delete pendingRequests[key];
220
+ (_req$callbacks$onDone2 = (_req$callbacks5 = req.callbacks).onDone) === null || _req$callbacks$onDone2 === void 0 ? void 0 : _req$callbacks$onDone2.call(_req$callbacks5);
221
+ } else if (state !== 'closed') {
222
+ // request restart of sub with next connection
223
+ resumeSubscriptionOnReconnect(req);
224
+ }
225
+ }
226
+ });
227
+ return conn;
228
+ }
229
+
230
+ function request(op, callbacks) {
231
+ var type = op.type,
232
+ input = op.input,
233
+ path = op.path,
234
+ id = op.id;
235
+ var envelope = {
236
+ id: id,
237
+ jsonrpc: '2.0',
238
+ method: type,
239
+ params: {
240
+ input: input,
241
+ path: path
242
+ }
243
+ };
244
+ pendingRequests[id] = {
245
+ ws: activeConnection,
246
+ type: type,
247
+ callbacks: callbacks,
248
+ op: op
249
+ }; // enqueue message
250
+
251
+ outgoing.push(envelope);
252
+ dispatch();
253
+ return function () {
254
+ var _pendingRequests$id, _callbacks$onDone;
255
+
256
+ var callbacks = (_pendingRequests$id = pendingRequests[id]) === null || _pendingRequests$id === void 0 ? void 0 : _pendingRequests$id.callbacks;
257
+ delete pendingRequests[id];
258
+ outgoing = outgoing.filter(function (msg) {
259
+ return msg.id !== id;
260
+ });
261
+ callbacks === null || callbacks === void 0 ? void 0 : (_callbacks$onDone = callbacks.onDone) === null || _callbacks$onDone === void 0 ? void 0 : _callbacks$onDone.call(callbacks);
262
+
263
+ if (op.type === 'subscription') {
264
+ outgoing.push({
265
+ id: id,
266
+ method: 'subscription.stop',
267
+ params: undefined
268
+ });
269
+ dispatch();
270
+ }
271
+ };
272
+ }
273
+
274
+ return {
275
+ close: function close() {
276
+ state = 'closed';
277
+ closeIfNoPending(activeConnection);
278
+ },
279
+ request: request,
280
+ getConnection: function getConnection() {
281
+ return activeConnection;
282
+ }
283
+ };
284
+ }
285
+
286
+ var TRPCWebSocketClosedError = /*#__PURE__*/function (_Error) {
287
+ _inherits(TRPCWebSocketClosedError, _Error);
288
+
289
+ var _super = /*#__PURE__*/_createSuper(TRPCWebSocketClosedError);
290
+
291
+ function TRPCWebSocketClosedError(message) {
292
+ var _this;
293
+
294
+ _classCallCheck(this, TRPCWebSocketClosedError);
295
+
296
+ _this = _super.call(this, message);
297
+ _this.name = 'TRPCWebSocketClosedError';
298
+ Object.setPrototypeOf(_assertThisInitialized(_this), TRPCWebSocketClosedError.prototype);
299
+ return _this;
300
+ }
301
+
302
+ return _createClass(TRPCWebSocketClosedError);
303
+ }( /*#__PURE__*/_wrapNativeSuper(Error));
304
+
305
+ var TRPCSubscriptionEndedError = /*#__PURE__*/function (_Error2) {
306
+ _inherits(TRPCSubscriptionEndedError, _Error2);
307
+
308
+ var _super2 = /*#__PURE__*/_createSuper(TRPCSubscriptionEndedError);
309
+
310
+ function TRPCSubscriptionEndedError(message) {
311
+ var _this2;
312
+
313
+ _classCallCheck(this, TRPCSubscriptionEndedError);
314
+
315
+ _this2 = _super2.call(this, message);
316
+ _this2.name = 'TRPCSubscriptionEndedError';
317
+ Object.setPrototypeOf(_assertThisInitialized(_this2), TRPCSubscriptionEndedError.prototype);
318
+ return _this2;
319
+ }
320
+
321
+ return _createClass(TRPCSubscriptionEndedError);
322
+ }( /*#__PURE__*/_wrapNativeSuper(Error));
323
+
324
+ function wsLink(opts) {
325
+ // initialized config
326
+ return function (rt) {
327
+ var client = opts.client;
328
+ return function (_ref2) {
329
+ var op = _ref2.op,
330
+ prev = _ref2.prev,
331
+ onDestroy = _ref2.onDestroy;
332
+ var type = op.type,
333
+ rawInput = op.input,
334
+ path = op.path,
335
+ id = op.id;
336
+ var input = rt.transformer.serialize(rawInput);
337
+ var isDone = false;
338
+ var unsub = client.request({
339
+ type: type,
340
+ path: path,
341
+ input: input,
342
+ id: id
343
+ }, {
344
+ onNext: function onNext(result) {
345
+ if (isDone) {
346
+ return;
347
+ }
348
+
349
+ if ('data' in result) {
350
+ var data = rt.transformer.deserialize(result.data);
351
+ prev({
352
+ type: 'data',
353
+ data: data
354
+ });
355
+ } else {
356
+ prev(result);
357
+ }
358
+
359
+ if (op.type !== 'subscription') {
360
+ // if it isn't a subscription we don't care about next response
361
+ isDone = true;
362
+ unsub();
363
+ }
364
+ },
365
+ onError: function onError(err) {
366
+ if (isDone) {
367
+ return;
368
+ }
369
+
370
+ prev(err instanceof Error ? err : TRPCClientError.TRPCClientError.from(_objectSpread(_objectSpread({}, err), {}, {
371
+ error: rt.transformer.deserialize(err.error)
372
+ })));
373
+ },
374
+ onDone: function onDone() {
375
+ if (isDone) {
376
+ return;
377
+ }
378
+
379
+ var result = new TRPCSubscriptionEndedError('Operation ended prematurely');
380
+ prev(TRPCClientError.TRPCClientError.from(result, {
381
+ isDone: true
382
+ }));
383
+ isDone = true;
384
+ }
385
+ });
386
+ onDestroy(function () {
387
+ isDone = true;
388
+ unsub();
389
+ });
390
+ };
391
+ };
392
+ }
393
+
394
+ exports.createWSClient = createWSClient;
395
+ exports.wsLink = wsLink;
@@ -1,22 +1,17 @@
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 { o as observable } from '../observable-7e66cb56.mjs';
9
- import { T as TRPCClientError } from '../TRPCClientError-17b117e1.mjs';
10
- import '@babel/runtime/helpers/defineProperty';
1
+ import _objectSpread from '@babel/runtime/helpers/esm/objectSpread2';
2
+ import _createClass from '@babel/runtime/helpers/esm/createClass';
3
+ import _classCallCheck from '@babel/runtime/helpers/esm/classCallCheck';
4
+ import _assertThisInitialized from '@babel/runtime/helpers/esm/assertThisInitialized';
5
+ import _inherits from '@babel/runtime/helpers/esm/inherits';
6
+ import _createSuper from '@babel/runtime/helpers/esm/createSuper';
7
+ import _wrapNativeSuper from '@babel/runtime/helpers/esm/wrapNativeSuper';
8
+ import { T as TRPCClientError } from '../../../dist/TRPCClientError-09b8a26b.esm.js';
11
9
 
12
10
  /* istanbul ignore next */
13
11
  var retryDelay = function retryDelay(attemptIndex) {
14
12
  return attemptIndex === 0 ? 0 : Math.min(1000 * Math.pow(2, attemptIndex), 30000);
15
13
  };
16
14
 
17
- 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); }; }
18
-
19
- 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; } }
20
15
  function createWSClient(opts) {
21
16
  var url = opts.url,
22
17
  _opts$WebSocket = opts.WebSocket,
@@ -149,19 +144,24 @@ function createWSClient(opts) {
149
144
  }
150
145
  };
151
146
 
152
- var handleIncomingResponse = function handleIncomingResponse(data) {
153
- var _req$callbacks$next, _req$callbacks;
147
+ var handleIncomingResponse = function handleIncomingResponse(res) {
148
+ var _req$callbacks$onNext, _req$callbacks2;
154
149
 
155
- var req = data.id !== null && pendingRequests[data.id];
150
+ var req = res.id !== null && pendingRequests[res.id];
156
151
 
157
152
  if (!req) {
158
153
  // do something?
159
154
  return;
160
155
  }
161
156
 
162
- (_req$callbacks$next = (_req$callbacks = req.callbacks).next) === null || _req$callbacks$next === void 0 ? void 0 : _req$callbacks$next.call(_req$callbacks, {
163
- data: data
164
- });
157
+ if ('error' in res) {
158
+ var _req$callbacks$onErro, _req$callbacks;
159
+
160
+ (_req$callbacks$onErro = (_req$callbacks = req.callbacks).onError) === null || _req$callbacks$onErro === void 0 ? void 0 : _req$callbacks$onErro.call(_req$callbacks, res);
161
+ return;
162
+ }
163
+
164
+ (_req$callbacks$onNext = (_req$callbacks2 = req.callbacks).onNext) === null || _req$callbacks$onNext === void 0 ? void 0 : _req$callbacks$onNext.call(_req$callbacks2, res.result);
165
165
 
166
166
  if (req.ws !== activeConnection && conn === activeConnection) {
167
167
  var oldWs = req.ws; // gracefully replace old connection with this
@@ -170,8 +170,10 @@ function createWSClient(opts) {
170
170
  closeIfNoPending(oldWs);
171
171
  }
172
172
 
173
- if ('result' in data && data.result.type === 'stopped' && conn === activeConnection) {
174
- req.callbacks.complete();
173
+ if (res.result.type === 'stopped' && conn === activeConnection) {
174
+ var _req$callbacks$onDone, _req$callbacks3;
175
+
176
+ (_req$callbacks$onDone = (_req$callbacks3 = req.callbacks).onDone) === null || _req$callbacks$onDone === void 0 ? void 0 : _req$callbacks$onDone.call(_req$callbacks3);
175
177
  }
176
178
  };
177
179
 
@@ -197,7 +199,7 @@ function createWSClient(opts) {
197
199
  }
198
200
 
199
201
  for (var key in pendingRequests) {
200
- var _req$callbacks$error, _req$callbacks2;
202
+ var _req$callbacks$onErro2, _req$callbacks4;
201
203
 
202
204
  var req = pendingRequests[key];
203
205
 
@@ -205,13 +207,13 @@ function createWSClient(opts) {
205
207
  continue;
206
208
  }
207
209
 
208
- (_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')));
210
+ (_req$callbacks$onErro2 = (_req$callbacks4 = req.callbacks).onError) === null || _req$callbacks$onErro2 === void 0 ? void 0 : _req$callbacks$onErro2.call(_req$callbacks4, TRPCClientError.from(new TRPCWebSocketClosedError('WebSocket closed prematurely')));
209
211
 
210
212
  if (req.type !== 'subscription') {
211
- var _req$callbacks$comple, _req$callbacks3;
213
+ var _req$callbacks$onDone2, _req$callbacks5;
212
214
 
213
215
  delete pendingRequests[key];
214
- (_req$callbacks$comple = (_req$callbacks3 = req.callbacks).complete) === null || _req$callbacks$comple === void 0 ? void 0 : _req$callbacks$comple.call(_req$callbacks3);
216
+ (_req$callbacks$onDone2 = (_req$callbacks5 = req.callbacks).onDone) === null || _req$callbacks$onDone2 === void 0 ? void 0 : _req$callbacks$onDone2.call(_req$callbacks5);
215
217
  } else if (state !== 'closed') {
216
218
  // request restart of sub with next connection
217
219
  resumeSubscriptionOnReconnect(req);
@@ -245,14 +247,14 @@ function createWSClient(opts) {
245
247
  outgoing.push(envelope);
246
248
  dispatch();
247
249
  return function () {
248
- var _pendingRequests$id, _callbacks$complete;
250
+ var _pendingRequests$id, _callbacks$onDone;
249
251
 
250
252
  var callbacks = (_pendingRequests$id = pendingRequests[id]) === null || _pendingRequests$id === void 0 ? void 0 : _pendingRequests$id.callbacks;
251
253
  delete pendingRequests[id];
252
254
  outgoing = outgoing.filter(function (msg) {
253
255
  return msg.id !== id;
254
256
  });
255
- callbacks === null || callbacks === void 0 ? void 0 : (_callbacks$complete = callbacks.complete) === null || _callbacks$complete === void 0 ? void 0 : _callbacks$complete.call(callbacks);
257
+ callbacks === null || callbacks === void 0 ? void 0 : (_callbacks$onDone = callbacks.onDone) === null || _callbacks$onDone === void 0 ? void 0 : _callbacks$onDone.call(callbacks);
256
258
 
257
259
  if (op.type === 'subscription') {
258
260
  outgoing.push({
@@ -316,52 +318,70 @@ var TRPCSubscriptionEndedError = /*#__PURE__*/function (_Error2) {
316
318
  }( /*#__PURE__*/_wrapNativeSuper(Error));
317
319
 
318
320
  function wsLink(opts) {
319
- return function () {
321
+ // initialized config
322
+ return function (rt) {
320
323
  var client = opts.client;
321
324
  return function (_ref2) {
322
- var op = _ref2.op;
323
- return observable(function (observer) {
324
- var type = op.type,
325
- input = op.input,
326
- path = op.path,
327
- id = op.id,
328
- context = op.context;
329
- var isDone = false;
330
- var unsub = client.request({
331
- type: type,
332
- path: path,
333
- input: input,
334
- id: id,
335
- context: context
336
- }, {
337
- error: function error(err) {
325
+ var op = _ref2.op,
326
+ prev = _ref2.prev,
327
+ onDestroy = _ref2.onDestroy;
328
+ var type = op.type,
329
+ rawInput = op.input,
330
+ path = op.path,
331
+ id = op.id;
332
+ var input = rt.transformer.serialize(rawInput);
333
+ var isDone = false;
334
+ var unsub = client.request({
335
+ type: type,
336
+ path: path,
337
+ input: input,
338
+ id: id
339
+ }, {
340
+ onNext: function onNext(result) {
341
+ if (isDone) {
342
+ return;
343
+ }
344
+
345
+ if ('data' in result) {
346
+ var data = rt.transformer.deserialize(result.data);
347
+ prev({
348
+ type: 'data',
349
+ data: data
350
+ });
351
+ } else {
352
+ prev(result);
353
+ }
354
+
355
+ if (op.type !== 'subscription') {
356
+ // if it isn't a subscription we don't care about next response
338
357
  isDone = true;
339
- observer.error(err);
340
358
  unsub();
341
- },
342
- complete: function complete() {
343
- if (!isDone) {
344
- isDone = true;
345
- observer.error(TRPCClientError.from(new TRPCSubscriptionEndedError('Operation ended prematurely')));
346
- } else {
347
- observer.complete();
348
- }
349
- },
350
- next: function next(result) {
351
- observer.next(result);
352
-
353
- if (op.type !== 'subscription') {
354
- // if it isn't a subscription we don't care about next response
355
- isDone = true;
356
- unsub();
357
- observer.complete();
358
- }
359
359
  }
360
- });
361
- return function () {
360
+ },
361
+ onError: function onError(err) {
362
+ if (isDone) {
363
+ return;
364
+ }
365
+
366
+ prev(err instanceof Error ? err : TRPCClientError.from(_objectSpread(_objectSpread({}, err), {}, {
367
+ error: rt.transformer.deserialize(err.error)
368
+ })));
369
+ },
370
+ onDone: function onDone() {
371
+ if (isDone) {
372
+ return;
373
+ }
374
+
375
+ var result = new TRPCSubscriptionEndedError('Operation ended prematurely');
376
+ prev(TRPCClientError.from(result, {
377
+ isDone: true
378
+ }));
362
379
  isDone = true;
363
- unsub();
364
- };
380
+ }
381
+ });
382
+ onDestroy(function () {
383
+ isDone = true;
384
+ unsub();
365
385
  });
366
386
  };
367
387
  };