@trpc/client 9.20.1 → 10.0.0-alpha.10

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 (154) hide show
  1. package/dist/TRPCClientError-17b117e1.mjs +81 -0
  2. package/dist/TRPCClientError-ec6bd2ca.js +94 -0
  3. package/dist/{declarations/src/TRPCClientError.d.ts → TRPCClientError.d.ts} +29 -33
  4. package/dist/TRPCClientError.d.ts.map +1 -0
  5. package/dist/createChain-3b458753.js +27 -0
  6. package/dist/createChain-b7cb1f5a.mjs +25 -0
  7. package/dist/{declarations/src/createTRPCClient.d.ts → createTRPCClient.d.ts} +5 -6
  8. package/dist/createTRPCClient.d.ts.map +1 -0
  9. package/dist/{declarations/src/getFetch.d.ts → getFetch.d.ts} +1 -1
  10. package/dist/getFetch.d.ts.map +1 -0
  11. package/dist/httpUtils-1f9e4e3c.js +97 -0
  12. package/dist/httpUtils-c09d7b79.mjs +91 -0
  13. package/dist/{declarations/src/index.d.ts → index.d.ts} +4 -4
  14. package/dist/index.d.ts.map +1 -0
  15. package/dist/index.js +243 -0
  16. package/dist/index.mjs +233 -0
  17. package/dist/index.ts.js +1 -0
  18. package/dist/{declarations/src/internals → internals}/TRPCClient.d.ts +62 -61
  19. package/dist/internals/TRPCClient.d.ts.map +1 -0
  20. package/dist/{declarations/src/internals → internals}/dataLoader.d.ts +16 -16
  21. package/dist/internals/dataLoader.d.ts.map +1 -0
  22. package/dist/{declarations/src/internals → internals}/fetchHelpers.d.ts +2 -2
  23. package/dist/internals/fetchHelpers.d.ts.map +1 -0
  24. package/dist/{declarations/src/internals → internals}/retryDelay.d.ts +1 -1
  25. package/dist/internals/retryDelay.d.ts.map +1 -0
  26. package/dist/links/dedupeLink.d.ts +4 -0
  27. package/dist/links/dedupeLink.d.ts.map +1 -0
  28. package/dist/{declarations/src/links → links}/httpBatchLink.d.ts +7 -6
  29. package/dist/links/httpBatchLink.d.ts.map +1 -0
  30. package/dist/links/httpBatchLink.js +195 -0
  31. package/dist/links/httpBatchLink.mjs +191 -0
  32. package/dist/links/httpBatchLink.ts.js +1 -0
  33. package/dist/{declarations/src/links → links}/httpLink.d.ts +4 -3
  34. package/dist/links/httpLink.d.ts.map +1 -0
  35. package/dist/links/httpLink.js +44 -0
  36. package/dist/links/httpLink.mjs +40 -0
  37. package/dist/links/httpLink.ts.js +1 -0
  38. package/dist/links/httpUtils.d.ts +22 -0
  39. package/dist/links/httpUtils.d.ts.map +1 -0
  40. package/dist/links/internals/createChain.d.ts +8 -0
  41. package/dist/links/internals/createChain.d.ts.map +1 -0
  42. package/dist/links/internals/transformOperationResult.d.ts +16 -0
  43. package/dist/links/internals/transformOperationResult.d.ts.map +1 -0
  44. package/dist/{declarations/src/links → links}/loggerLink.d.ts +38 -37
  45. package/dist/links/loggerLink.d.ts.map +1 -0
  46. package/dist/links/loggerLink.js +107 -0
  47. package/dist/links/loggerLink.mjs +98 -0
  48. package/dist/links/loggerLink.ts.js +1 -0
  49. package/dist/links/retryLink.d.ts +6 -0
  50. package/dist/links/retryLink.d.ts.map +1 -0
  51. package/dist/links/splitLink.d.ts +14 -0
  52. package/dist/links/splitLink.d.ts.map +1 -0
  53. package/dist/links/splitLink.js +32 -0
  54. package/dist/links/splitLink.mjs +28 -0
  55. package/dist/links/splitLink.ts.js +1 -0
  56. package/dist/links/transformerLink.d.ts +4 -0
  57. package/dist/links/transformerLink.d.ts.map +1 -0
  58. package/dist/links/transformerLink.js +79 -0
  59. package/dist/links/transformerLink.mjs +71 -0
  60. package/dist/links/transformerLink.ts.js +1 -0
  61. package/dist/links/types.d.ts +39 -0
  62. package/dist/links/types.d.ts.map +1 -0
  63. package/dist/links/wsLink.d.ts +21 -0
  64. package/dist/links/wsLink.d.ts.map +1 -0
  65. package/dist/links/wsLink.js +385 -0
  66. package/dist/links/wsLink.mjs +370 -0
  67. package/dist/links/wsLink.ts.js +1 -0
  68. package/dist/rx.d.ts +11 -0
  69. package/dist/rx.d.ts.map +1 -0
  70. package/dist/rx.js +88 -0
  71. package/dist/rx.mjs +73 -0
  72. package/dist/rx.ts.js +1 -0
  73. package/links/httpBatchLink/index.d.ts +1 -0
  74. package/links/httpBatchLink/index.js +1 -0
  75. package/links/httpLink/index.d.ts +1 -0
  76. package/links/httpLink/index.js +1 -0
  77. package/links/loggerLink/index.d.ts +1 -0
  78. package/links/loggerLink/index.js +1 -0
  79. package/links/splitLink/index.d.ts +1 -0
  80. package/links/splitLink/index.js +1 -0
  81. package/links/transformerLink/index.d.ts +1 -0
  82. package/links/transformerLink/index.js +1 -0
  83. package/links/wsLink/index.d.ts +1 -0
  84. package/links/wsLink/index.js +1 -0
  85. package/package.json +53 -18
  86. package/src/TRPCClientError.ts +17 -21
  87. package/src/createTRPCClient.ts +4 -3
  88. package/src/index.ts +1 -1
  89. package/src/internals/TRPCClient.ts +78 -92
  90. package/src/internals/dataLoader.ts +1 -1
  91. package/src/links/dedupeLink.test.ts +134 -0
  92. package/src/links/dedupeLink.ts +54 -0
  93. package/src/links/httpBatchLink.ts +36 -46
  94. package/src/links/httpLink.ts +26 -37
  95. package/src/{internals/httpRequest.ts → links/httpUtils.ts} +30 -21
  96. package/src/links/internals/createChain.test.ts +132 -0
  97. package/src/links/internals/createChain.ts +31 -0
  98. package/src/links/internals/transformOperationResult.ts +25 -0
  99. package/src/links/loggerLink.ts +39 -22
  100. package/src/links/retryLink.ts +44 -14
  101. package/src/links/splitLink.test.ts +56 -0
  102. package/src/links/splitLink.ts +23 -38
  103. package/src/links/transformerLink.ts +74 -0
  104. package/src/links/types.ts +64 -0
  105. package/src/links/wsLink.ts +57 -77
  106. package/src/rx.ts +50 -0
  107. package/dist/TRPCClientError-09b8a26b.esm.js +0 -77
  108. package/dist/TRPCClientError-bfee2bf5.cjs.dev.js +0 -79
  109. package/dist/TRPCClientError-e9bf96e9.cjs.prod.js +0 -79
  110. package/dist/declarations/src/TRPCClientError.d.ts.map +0 -1
  111. package/dist/declarations/src/createTRPCClient.d.ts.map +0 -1
  112. package/dist/declarations/src/getFetch.d.ts.map +0 -1
  113. package/dist/declarations/src/index.d.ts.map +0 -1
  114. package/dist/declarations/src/internals/TRPCAbortError.d.ts +0 -4
  115. package/dist/declarations/src/internals/TRPCAbortError.d.ts.map +0 -1
  116. package/dist/declarations/src/internals/TRPCClient.d.ts.map +0 -1
  117. package/dist/declarations/src/internals/dataLoader.d.ts.map +0 -1
  118. package/dist/declarations/src/internals/executeChain.d.ts +0 -9
  119. package/dist/declarations/src/internals/executeChain.d.ts.map +0 -1
  120. package/dist/declarations/src/internals/fetchHelpers.d.ts.map +0 -1
  121. package/dist/declarations/src/internals/httpRequest.d.ts +0 -14
  122. package/dist/declarations/src/internals/httpRequest.d.ts.map +0 -1
  123. package/dist/declarations/src/internals/observable.d.ts +0 -18
  124. package/dist/declarations/src/internals/observable.d.ts.map +0 -1
  125. package/dist/declarations/src/internals/retryDelay.d.ts.map +0 -1
  126. package/dist/declarations/src/internals/transformRPCResponse.d.ts +0 -15
  127. package/dist/declarations/src/internals/transformRPCResponse.d.ts.map +0 -1
  128. package/dist/declarations/src/links/core.d.ts +0 -45
  129. package/dist/declarations/src/links/core.d.ts.map +0 -1
  130. package/dist/declarations/src/links/httpBatchLink.d.ts.map +0 -1
  131. package/dist/declarations/src/links/httpLink.d.ts.map +0 -1
  132. package/dist/declarations/src/links/loggerLink.d.ts.map +0 -1
  133. package/dist/declarations/src/links/splitLink.d.ts +0 -24
  134. package/dist/declarations/src/links/splitLink.d.ts.map +0 -1
  135. package/dist/declarations/src/links/wsLink.d.ts +0 -30
  136. package/dist/declarations/src/links/wsLink.d.ts.map +0 -1
  137. package/dist/transformRPCResponse-57e80136.cjs.dev.js +0 -133
  138. package/dist/transformRPCResponse-739c13d0.cjs.prod.js +0 -133
  139. package/dist/transformRPCResponse-f7f8b0e1.esm.js +0 -129
  140. package/dist/trpc-client.cjs.d.ts +0 -1
  141. package/dist/trpc-client.cjs.dev.js +0 -379
  142. package/dist/trpc-client.cjs.js +0 -7
  143. package/dist/trpc-client.cjs.prod.js +0 -379
  144. package/dist/trpc-client.esm.js +0 -374
  145. package/links/httpBatchLink/package.json +0 -4
  146. package/links/httpLink/package.json +0 -4
  147. package/links/loggerLink/package.json +0 -4
  148. package/links/splitLink/package.json +0 -4
  149. package/links/wsLink/package.json +0 -4
  150. package/src/internals/TRPCAbortError.ts +0 -7
  151. package/src/internals/executeChain.ts +0 -67
  152. package/src/internals/observable.ts +0 -89
  153. package/src/internals/transformRPCResponse.ts +0 -25
  154. package/src/links/core.ts +0 -71
@@ -1,374 +0,0 @@
1
- import _classCallCheck from '@babel/runtime/helpers/esm/classCallCheck';
2
- import _createClass from '@babel/runtime/helpers/esm/createClass';
3
- import _objectSpread from '@babel/runtime/helpers/esm/objectSpread2';
4
- import _createForOfIteratorHelper from '@babel/runtime/helpers/esm/createForOfIteratorHelper';
5
- import { T as TRPCAbortError } from './transformRPCResponse-f7f8b0e1.esm.js';
6
- import { httpBatchLink } from '../links/httpBatchLink/dist/trpc-client-links-httpBatchLink.esm.js';
7
- import { T as TRPCClientError } from './TRPCClientError-09b8a26b.esm.js';
8
- export { T as TRPCClientError } from './TRPCClientError-09b8a26b.esm.js';
9
- import '@babel/runtime/helpers/assertThisInitialized';
10
- import '@babel/runtime/helpers/inherits';
11
- import '@babel/runtime/helpers/createSuper';
12
- import '@babel/runtime/helpers/wrapNativeSuper';
13
-
14
- function observable() {
15
- var listeners = [];
16
- var value = null;
17
- return {
18
- subscribe: function subscribe(callbacks) {
19
- var listener = {
20
- callbacks: callbacks,
21
- unsubscribe: function unsubscribe() {
22
- var index = listeners.indexOf(listener);
23
-
24
- if (index !== -1) {
25
- var _listener$callbacks$o, _listener$callbacks;
26
-
27
- listeners.splice(index, 1);
28
- (_listener$callbacks$o = (_listener$callbacks = listener.callbacks).onDone) === null || _listener$callbacks$o === void 0 ? void 0 : _listener$callbacks$o.call(_listener$callbacks);
29
- }
30
- }
31
- };
32
- listeners.push(listener);
33
- return function () {
34
- listener.unsubscribe();
35
- };
36
- },
37
- next: function next(newValue) {
38
- var oldValue = value;
39
- value = newValue;
40
-
41
- if (oldValue !== newValue) {
42
- var _iterator = _createForOfIteratorHelper(listeners),
43
- _step;
44
-
45
- try {
46
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
47
- var _listener$callbacks$o2, _listener$callbacks2;
48
-
49
- var listener = _step.value;
50
- (_listener$callbacks$o2 = (_listener$callbacks2 = listener.callbacks).onNext) === null || _listener$callbacks$o2 === void 0 ? void 0 : _listener$callbacks$o2.call(_listener$callbacks2, newValue);
51
- }
52
- } catch (err) {
53
- _iterator.e(err);
54
- } finally {
55
- _iterator.f();
56
- }
57
- }
58
- },
59
- done: function done() {
60
- while (listeners.length) {
61
- var _listener$callbacks$o3, _listener$callbacks3;
62
-
63
- var listener = listeners.pop();
64
- listener === null || listener === void 0 ? void 0 : (_listener$callbacks$o3 = (_listener$callbacks3 = listener.callbacks).onDone) === null || _listener$callbacks$o3 === void 0 ? void 0 : _listener$callbacks$o3.call(_listener$callbacks3);
65
- listener === null || listener === void 0 ? void 0 : listener.unsubscribe();
66
- }
67
- },
68
- error: function (_error) {
69
- function error(_x) {
70
- return _error.apply(this, arguments);
71
- }
72
-
73
- error.toString = function () {
74
- return _error.toString();
75
- };
76
-
77
- return error;
78
- }(function (error) {
79
- var _iterator2 = _createForOfIteratorHelper(listeners),
80
- _step2;
81
-
82
- try {
83
- for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
84
- var _listener$callbacks$o4, _listener$callbacks4;
85
-
86
- var listener = _step2.value;
87
- (_listener$callbacks$o4 = (_listener$callbacks4 = listener.callbacks).onError) === null || _listener$callbacks$o4 === void 0 ? void 0 : _listener$callbacks$o4.call(_listener$callbacks4, error);
88
- }
89
- } catch (err) {
90
- _iterator2.e(err);
91
- } finally {
92
- _iterator2.f();
93
- }
94
- })
95
- };
96
- }
97
- function observableSubject(initialValue) {
98
- var $obs = observable();
99
- var value = initialValue;
100
- $obs.next(initialValue);
101
- return _objectSpread(_objectSpread({}, $obs), {}, {
102
- next: function next(v) {
103
- value = v;
104
- $obs.next(v);
105
- },
106
- get: function get() {
107
- return value;
108
- }
109
- });
110
- }
111
-
112
- function executeChain(opts) {
113
- var $result = observableSubject(null);
114
-
115
- var updateResult = function updateResult(result) {
116
- if (result instanceof Error) {
117
- $result.error(result);
118
-
119
- if (result.isDone) {
120
- $result.done();
121
- }
122
- } else {
123
- $result.next(result);
124
- }
125
- };
126
-
127
- function walk(_ref) {
128
- var index = _ref.index,
129
- op = _ref.op,
130
- stack = _ref.stack;
131
- var link = opts.links[index];
132
- var prev = index === 0 ? function (value) {
133
- return updateResult(value);
134
- } : stack[index - 1];
135
- link({
136
- op: op,
137
- prev: prev,
138
- next: function next(op, prevOp) {
139
- var prevStack = stack.slice();
140
- prevStack[index] = prevOp;
141
- walk({
142
- index: index + 1,
143
- op: op,
144
- stack: prevStack
145
- });
146
- },
147
- onDestroy: function onDestroy(callback) {
148
- $result.subscribe({
149
- onDone: function onDone() {
150
- callback();
151
- }
152
- });
153
- }
154
- });
155
- }
156
-
157
- walk({
158
- index: 0,
159
- op: opts.op,
160
- stack: []
161
- });
162
- return $result;
163
- }
164
-
165
- function getWindow() {
166
- if (typeof window !== 'undefined') {
167
- return window;
168
- }
169
-
170
- return global;
171
- }
172
- function getAbortController(ac) {
173
- var win = getWindow();
174
- return ac || win.AbortController || null;
175
- }
176
-
177
- function getFetch(f) {
178
- if (f) {
179
- return f;
180
- }
181
-
182
- var win = getWindow();
183
-
184
- if (win.fetch) {
185
- return typeof win.fetch.bind === 'function' ? win.fetch.bind(win) : win.fetch;
186
- }
187
-
188
- throw new Error('No fetch implementation found');
189
- }
190
-
191
- var idCounter = 0;
192
-
193
- function getRequestId() {
194
- return ++idCounter;
195
- }
196
-
197
- var TRPCClient = /*#__PURE__*/function () {
198
- function TRPCClient(opts) {
199
- var _this = this;
200
-
201
- _classCallCheck(this, TRPCClient);
202
-
203
- this.links = void 0;
204
- this.runtime = void 0;
205
- var transformer = opts.transformer ? 'input' in opts.transformer ? {
206
- serialize: opts.transformer.input.serialize,
207
- deserialize: opts.transformer.output.deserialize
208
- } : opts.transformer : {
209
- serialize: function serialize(data) {
210
- return data;
211
- },
212
- deserialize: function deserialize(data) {
213
- return data;
214
- }
215
- };
216
-
217
- var _fetch = getFetch(opts === null || opts === void 0 ? void 0 : opts.fetch);
218
-
219
- var AC = getAbortController(opts === null || opts === void 0 ? void 0 : opts.AbortController);
220
-
221
- function getHeadersFn() {
222
- if (opts.headers) {
223
- var headers = opts.headers;
224
- return typeof headers === 'function' ? headers : function () {
225
- return headers;
226
- };
227
- }
228
-
229
- return function () {
230
- return {};
231
- };
232
- }
233
-
234
- this.runtime = {
235
- transformer: transformer,
236
- AbortController: AC,
237
- fetch: _fetch,
238
- headers: getHeadersFn()
239
- };
240
-
241
- if ('links' in opts) {
242
- this.links = opts.links.map(function (link) {
243
- return link(_this.runtime);
244
- });
245
- } else {
246
- this.links = [httpBatchLink({
247
- url: opts.url
248
- })(this.runtime)];
249
- }
250
- }
251
-
252
- _createClass(TRPCClient, [{
253
- key: "$request",
254
- value: function $request(_ref) {
255
- var type = _ref.type,
256
- input = _ref.input,
257
- path = _ref.path,
258
- _ref$context = _ref.context,
259
- context = _ref$context === void 0 ? {} : _ref$context;
260
- var $result = executeChain({
261
- links: this.links,
262
- op: {
263
- id: getRequestId(),
264
- type: type,
265
- path: path,
266
- input: input,
267
- context: context
268
- }
269
- });
270
- return $result;
271
- }
272
- }, {
273
- key: "requestAsPromise",
274
- value: function requestAsPromise(opts) {
275
- var $result = this.$request(opts);
276
- var promise = new Promise(function (resolve, reject) {
277
- var res = $result.get();
278
-
279
- if ((res === null || res === void 0 ? void 0 : res.type) === 'data') {
280
- resolve(res.data);
281
- $result.done();
282
- return;
283
- }
284
-
285
- $result.subscribe({
286
- onNext: function onNext(result) {
287
- if ((result === null || result === void 0 ? void 0 : result.type) !== 'data') {
288
- return;
289
- }
290
-
291
- resolve(result.data);
292
- $result.done();
293
- },
294
- onError: function onError(err) {
295
- reject(err);
296
- $result.done();
297
- },
298
- onDone: function onDone() {
299
- reject(TRPCClientError.from(new TRPCAbortError()));
300
- }
301
- });
302
- });
303
-
304
- promise.cancel = function () {
305
- $result.done();
306
- };
307
-
308
- return promise;
309
- }
310
- }, {
311
- key: "query",
312
- value: function query(path) {
313
- var _ref2;
314
-
315
- var context = (_ref2 = arguments.length <= 2 ? undefined : arguments[2]) === null || _ref2 === void 0 ? void 0 : _ref2.context;
316
- return this.requestAsPromise({
317
- type: 'query',
318
- path: path,
319
- input: arguments.length <= 1 ? undefined : arguments[1],
320
- context: context
321
- });
322
- }
323
- }, {
324
- key: "mutation",
325
- value: function mutation(path) {
326
- var _ref3;
327
-
328
- var context = (_ref3 = arguments.length <= 2 ? undefined : arguments[2]) === null || _ref3 === void 0 ? void 0 : _ref3.context;
329
- return this.requestAsPromise({
330
- type: 'mutation',
331
- path: path,
332
- input: arguments.length <= 1 ? undefined : arguments[1],
333
- context: context
334
- });
335
- }
336
- }, {
337
- key: "subscription",
338
- value: function subscription(path, input, opts) {
339
- var $res = this.$request({
340
- type: 'subscription',
341
- path: path,
342
- input: input,
343
- context: opts.context
344
- });
345
- $res.subscribe({
346
- onNext: function onNext(output) {
347
- if (output) {
348
- var _opts$onNext;
349
-
350
- (_opts$onNext = opts.onNext) === null || _opts$onNext === void 0 ? void 0 : _opts$onNext.call(opts, output);
351
- }
352
- },
353
- onError: function onError(err) {
354
- var _opts$onError;
355
-
356
- (_opts$onError = opts.onError) === null || _opts$onError === void 0 ? void 0 : _opts$onError.call(opts, err);
357
- },
358
- onDone: opts.onDone
359
- });
360
- return function () {
361
- $res.done();
362
- };
363
- }
364
- }]);
365
-
366
- return TRPCClient;
367
- }();
368
-
369
- /* eslint-disable @typescript-eslint/no-explicit-any */
370
- function createTRPCClient(opts) {
371
- return new TRPCClient(opts);
372
- }
373
-
374
- export { createTRPCClient, getFetch };
@@ -1,4 +0,0 @@
1
- {
2
- "main": "dist/trpc-client-links-httpBatchLink.cjs.js",
3
- "module": "dist/trpc-client-links-httpBatchLink.esm.js"
4
- }
@@ -1,4 +0,0 @@
1
- {
2
- "main": "dist/trpc-client-links-httpLink.cjs.js",
3
- "module": "dist/trpc-client-links-httpLink.esm.js"
4
- }
@@ -1,4 +0,0 @@
1
- {
2
- "main": "dist/trpc-client-links-loggerLink.cjs.js",
3
- "module": "dist/trpc-client-links-loggerLink.esm.js"
4
- }
@@ -1,4 +0,0 @@
1
- {
2
- "main": "dist/trpc-client-links-splitLink.cjs.js",
3
- "module": "dist/trpc-client-links-splitLink.esm.js"
4
- }
@@ -1,4 +0,0 @@
1
- {
2
- "main": "dist/trpc-client-links-wsLink.cjs.js",
3
- "module": "dist/trpc-client-links-wsLink.esm.js"
4
- }
@@ -1,7 +0,0 @@
1
- export class TRPCAbortError extends Error {
2
- constructor() {
3
- super('The operation was aborted.');
4
- this.name = 'TRPCAbortError';
5
- Object.setPrototypeOf(this, TRPCAbortError.prototype);
6
- }
7
- }
@@ -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
- }
@@ -1,25 +0,0 @@
1
- import { TRPCResponse } from '@trpc/server/rpc';
2
- import { TRPCClientError } from '../TRPCClientError';
3
- import { LinkRuntimeOptions } from '../links/core';
4
-
5
- export function transformRPCResponse({
6
- envelope,
7
- runtime,
8
- }: {
9
- envelope: TRPCResponse;
10
- runtime: LinkRuntimeOptions;
11
- }) {
12
- if ('error' in envelope) {
13
- return TRPCClientError.from({
14
- ...envelope,
15
- error: runtime.transformer.deserialize(envelope.error),
16
- });
17
- }
18
- if (envelope.result.type === 'data') {
19
- return {
20
- ...envelope.result,
21
- data: runtime.transformer.deserialize(envelope.result.data),
22
- };
23
- }
24
- return envelope.result;
25
- }
package/src/links/core.ts DELETED
@@ -1,71 +0,0 @@
1
- import { AnyRouter, DataTransformer } from '@trpc/server';
2
- import { TRPCResult } from '@trpc/server/rpc';
3
- import { TRPCClientError } from '../TRPCClientError';
4
-
5
- export type OperationContext = Record<string, unknown>;
6
- export type Operation<TInput = unknown> = {
7
- id: number;
8
- type: 'query' | 'mutation' | 'subscription';
9
- input: TInput;
10
- path: string;
11
- context: OperationContext;
12
- };
13
-
14
- export type OperationResponse<TRouter extends AnyRouter, TOutput = unknown> =
15
- | TRPCResult<TOutput>
16
- | TRPCClientError<TRouter>;
17
-
18
- export type PrevCallback<TRouter extends AnyRouter, TOutput = unknown> = (
19
- result: OperationResponse<TRouter, TOutput>,
20
- ) => void;
21
- export type OperationLink<
22
- TRouter extends AnyRouter,
23
- TInput = unknown,
24
- TOutput = unknown,
25
- > = (opts: {
26
- op: Operation;
27
- prev: PrevCallback<TRouter, TOutput>;
28
- next: (
29
- op: Operation<TInput>,
30
- callback: PrevCallback<TRouter, TOutput>,
31
- ) => void;
32
- onDestroy: (callback: () => void) => void;
33
- }) => void;
34
-
35
- export type TRPCLink<TRouter extends AnyRouter> = (
36
- opts: LinkRuntimeOptions,
37
- ) => OperationLink<TRouter>;
38
-
39
- export interface HTTPLinkOptions {
40
- url: string;
41
- }
42
-
43
- /**
44
- * @deprecated use `HTTPLinkOptions`
45
- */
46
- export type HttpLinkOptions = HTTPLinkOptions;
47
-
48
- export type HTTPHeaders = Record<string, string | string[] | undefined>;
49
-
50
- /**
51
- * The default `fetch` implementation has an overloaded signature. By convention this library
52
- * only uses the overload taking a string and options object.
53
- */
54
- export type TRPCFetch = (
55
- url: string,
56
- options?: RequestInit,
57
- ) => Promise<Response>;
58
-
59
- export type LinkRuntimeOptions = Readonly<{
60
- transformer: DataTransformer;
61
- headers: () => HTTPHeaders | Promise<HTTPHeaders>;
62
- fetch: TRPCFetch;
63
- AbortController?: typeof AbortController;
64
- }>;
65
-
66
- export type CancelFn = () => void;
67
-
68
- export type PromiseAndCancel<TValue> = {
69
- promise: Promise<TValue>;
70
- cancel: CancelFn;
71
- };