@trpc/client 10.0.0-alpha.5 → 10.0.0-alpha.52

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 (178) hide show
  1. package/README.md +58 -1
  2. package/dist/TRPCClientError.d.ts +16 -20
  3. package/dist/TRPCClientError.d.ts.map +1 -1
  4. package/dist/createTRPCClient.d.ts +5 -3
  5. package/dist/createTRPCClient.d.ts.map +1 -1
  6. package/dist/createTRPCClientProxy.d.ts +37 -0
  7. package/dist/createTRPCClientProxy.d.ts.map +1 -0
  8. package/dist/httpUtils-44781613.mjs +76 -0
  9. package/dist/httpUtils-801112a6.js +79 -0
  10. package/dist/index.d.ts +2 -1
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +256 -280
  13. package/dist/index.mjs +249 -271
  14. package/dist/internals/TRPCClient.d.ts +38 -17
  15. package/dist/internals/TRPCClient.d.ts.map +1 -1
  16. package/dist/internals/dataLoader.d.ts +7 -4
  17. package/dist/internals/dataLoader.d.ts.map +1 -1
  18. package/dist/internals/fetchHelpers.d.ts.map +1 -1
  19. package/dist/internals/retryDelay.d.ts.map +1 -1
  20. package/dist/links/dedupeLink.d.ts.map +1 -1
  21. package/dist/links/httpBatchLink.d.ts +5 -2
  22. package/dist/links/httpBatchLink.d.ts.map +1 -1
  23. package/dist/links/httpBatchLink.js +189 -166
  24. package/dist/links/httpBatchLink.mjs +189 -166
  25. package/dist/links/httpLink.d.ts +1 -1
  26. package/dist/links/httpLink.d.ts.map +1 -1
  27. package/dist/links/httpLink.js +31 -35
  28. package/dist/links/httpLink.mjs +31 -35
  29. package/dist/links/index.d.ts +9 -0
  30. package/dist/links/index.d.ts.map +1 -0
  31. package/dist/links/internals/createChain.d.ts.map +1 -1
  32. package/dist/links/internals/httpUtils.d.ts +31 -0
  33. package/dist/links/internals/httpUtils.d.ts.map +1 -0
  34. package/dist/links/internals/transformResult.d.ts +28 -0
  35. package/dist/links/internals/transformResult.d.ts.map +1 -0
  36. package/dist/links/loggerLink.d.ts +3 -3
  37. package/dist/links/loggerLink.d.ts.map +1 -1
  38. package/dist/links/loggerLink.js +90 -96
  39. package/dist/links/loggerLink.mjs +90 -91
  40. package/dist/links/retryLink.d.ts.map +1 -1
  41. package/dist/links/splitLink.d.ts +1 -1
  42. package/dist/links/splitLink.d.ts.map +1 -1
  43. package/dist/links/splitLink.js +3 -25
  44. package/dist/links/splitLink.mjs +2 -28
  45. package/dist/links/types.d.ts +13 -12
  46. package/dist/links/types.d.ts.map +1 -1
  47. package/dist/links/wsLink.d.ts +12 -5
  48. package/dist/links/wsLink.d.ts.map +1 -1
  49. package/dist/links/wsLink.js +250 -357
  50. package/dist/links/wsLink.mjs +250 -348
  51. package/dist/splitLink-695ae288.js +48 -0
  52. package/dist/splitLink-ad44a55d.mjs +45 -0
  53. package/dist/transformResult-106791d7.mjs +62 -0
  54. package/dist/transformResult-e15ccdf6.js +65 -0
  55. package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.cjs.dev.js +15 -13
  56. package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.cjs.prod.js +15 -13
  57. package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.esm.js +12 -10
  58. package/links/httpLink/dist/trpc-client-links-httpLink.cjs.dev.js +4 -4
  59. package/links/httpLink/dist/trpc-client-links-httpLink.cjs.prod.js +4 -4
  60. package/links/httpLink/dist/trpc-client-links-httpLink.esm.js +1 -1
  61. package/links/wsLink/dist/trpc-client-links-wsLink.cjs.dev.js +3 -1
  62. package/links/wsLink/dist/trpc-client-links-wsLink.cjs.prod.js +3 -1
  63. package/links/wsLink/dist/trpc-client-links-wsLink.esm.js +3 -1
  64. package/package.json +23 -34
  65. package/src/TRPCClientError.ts +38 -28
  66. package/src/createTRPCClient.ts +11 -3
  67. package/src/createTRPCClientProxy.ts +113 -0
  68. package/src/index.ts +2 -1
  69. package/src/internals/TRPCClient.ts +96 -59
  70. package/src/internals/dataLoader.ts +120 -45
  71. package/src/links/dedupeLink.test.ts +9 -15
  72. package/src/links/dedupeLink.ts +6 -7
  73. package/src/links/httpBatchLink.ts +77 -34
  74. package/src/links/httpLink.ts +18 -6
  75. package/src/links/index.ts +8 -0
  76. package/src/links/internals/createChain.test.ts +23 -38
  77. package/src/links/internals/createChain.ts +8 -3
  78. package/src/links/internals/httpUtils.ts +117 -0
  79. package/src/links/internals/transformResult.ts +40 -0
  80. package/src/links/loggerLink.ts +6 -7
  81. package/src/links/retryLink.ts +2 -3
  82. package/src/links/splitLink.test.ts +3 -3
  83. package/src/links/splitLink.ts +5 -5
  84. package/src/links/types.ts +15 -12
  85. package/src/links/wsLink.ts +57 -23
  86. package/dist/TRPCClientError-17b117e1.mjs +0 -81
  87. package/dist/TRPCClientError-ec6bd2ca.js +0 -94
  88. package/dist/createChain-389cc116.mjs +0 -25
  89. package/dist/createChain-864dbb30.js +0 -27
  90. package/dist/httpUtils-1f9e4e3c.js +0 -97
  91. package/dist/httpUtils-c09d7b79.mjs +0 -91
  92. package/dist/index.ts.js +0 -1
  93. package/dist/internals/transformRPCResponse.d.ts +0 -15
  94. package/dist/internals/transformRPCResponse.d.ts.map +0 -1
  95. package/dist/links/httpBatchLink.ts.js +0 -1
  96. package/dist/links/httpLink.ts.js +0 -1
  97. package/dist/links/httpUtils.d.ts +0 -22
  98. package/dist/links/httpUtils.d.ts.map +0 -1
  99. package/dist/links/internals/transformOperationResult.d.ts +0 -16
  100. package/dist/links/internals/transformOperationResult.d.ts.map +0 -1
  101. package/dist/links/loggerLink.ts.js +0 -1
  102. package/dist/links/splitLink.ts.js +0 -1
  103. package/dist/links/transformerLink.d.ts +0 -4
  104. package/dist/links/transformerLink.d.ts.map +0 -1
  105. package/dist/links/transformerLink.js +0 -75
  106. package/dist/links/transformerLink.mjs +0 -67
  107. package/dist/links/transformerLink.ts.js +0 -1
  108. package/dist/links/wsLink.ts.js +0 -1
  109. package/dist/observable-7e66cb56.mjs +0 -102
  110. package/dist/observable-85669100.js +0 -104
  111. package/dist/rx/index.d.ts +0 -3
  112. package/dist/rx/index.d.ts.map +0 -1
  113. package/dist/rx/index.js +0 -9
  114. package/dist/rx/index.mjs +0 -1
  115. package/dist/rx/index.ts.js +0 -1
  116. package/dist/rx/observable.d.ts +0 -4
  117. package/dist/rx/observable.d.ts.map +0 -1
  118. package/dist/rx/operators/index.d.ts +0 -4
  119. package/dist/rx/operators/index.d.ts.map +0 -1
  120. package/dist/rx/operators/index.js +0 -38
  121. package/dist/rx/operators/index.mjs +0 -32
  122. package/dist/rx/operators/index.ts.js +0 -1
  123. package/dist/rx/operators/map.d.ts +0 -3
  124. package/dist/rx/operators/map.d.ts.map +0 -1
  125. package/dist/rx/operators/share.d.ts +0 -6
  126. package/dist/rx/operators/share.d.ts.map +0 -1
  127. package/dist/rx/operators/tap.d.ts +0 -3
  128. package/dist/rx/operators/tap.d.ts.map +0 -1
  129. package/dist/rx/types.d.ts +0 -29
  130. package/dist/rx/types.d.ts.map +0 -1
  131. package/dist/rx/util/identity.d.ts +0 -2
  132. package/dist/rx/util/identity.d.ts.map +0 -1
  133. package/dist/rx/util/observableToPromise.d.ts +0 -10
  134. package/dist/rx/util/observableToPromise.d.ts.map +0 -1
  135. package/dist/rx/util/pipe.d.ts +0 -4
  136. package/dist/rx/util/pipe.d.ts.map +0 -1
  137. package/dist/share-4d79dc49.js +0 -110
  138. package/dist/share-671b63fa.mjs +0 -108
  139. package/dist/tap-538fa07f.mjs +0 -30
  140. package/dist/tap-799a2d33.js +0 -32
  141. package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.d.ts +0 -1
  142. package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.dev.js +0 -67
  143. package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.js +0 -7
  144. package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.prod.js +0 -67
  145. package/links/transformerLink/dist/trpc-client-links-transformerLink.esm.js +0 -63
  146. package/links/transformerLink/index.d.ts +0 -1
  147. package/links/transformerLink/index.js +0 -1
  148. package/rx/dist/trpc-client-rx.cjs.d.ts +0 -1
  149. package/rx/dist/trpc-client-rx.cjs.dev.js +0 -9
  150. package/rx/dist/trpc-client-rx.cjs.js +0 -7
  151. package/rx/dist/trpc-client-rx.cjs.prod.js +0 -9
  152. package/rx/dist/trpc-client-rx.esm.js +0 -1
  153. package/rx/index.d.ts +0 -1
  154. package/rx/index.js +0 -1
  155. package/rx/operators/dist/trpc-client-rx-operators.cjs.d.ts +0 -1
  156. package/rx/operators/dist/trpc-client-rx-operators.cjs.dev.js +0 -39
  157. package/rx/operators/dist/trpc-client-rx-operators.cjs.js +0 -7
  158. package/rx/operators/dist/trpc-client-rx-operators.cjs.prod.js +0 -39
  159. package/rx/operators/dist/trpc-client-rx-operators.esm.js +0 -33
  160. package/rx/operators/index.d.ts +0 -1
  161. package/rx/operators/index.js +0 -1
  162. package/src/internals/transformRPCResponse.ts +0 -25
  163. package/src/links/httpUtils.ts +0 -94
  164. package/src/links/internals/transformOperationResult.ts +0 -25
  165. package/src/links/transformerLink.ts +0 -70
  166. package/src/rx/index.ts +0 -2
  167. package/src/rx/observable.test.ts +0 -83
  168. package/src/rx/observable.ts +0 -70
  169. package/src/rx/operators/index.ts +0 -3
  170. package/src/rx/operators/map.test.ts +0 -92
  171. package/src/rx/operators/map.ts +0 -25
  172. package/src/rx/operators/share.test.ts +0 -60
  173. package/src/rx/operators/share.ts +0 -67
  174. package/src/rx/operators/tap.ts +0 -26
  175. package/src/rx/types.ts +0 -69
  176. package/src/rx/util/identity.ts +0 -3
  177. package/src/rx/util/observableToPromise.ts +0 -49
  178. package/src/rx/util/pipe.ts +0 -22
@@ -1,185 +1,208 @@
1
- import { o as observable } from '../observable-7e66cb56.mjs';
2
- import { h as httpRequest } from '../httpUtils-c09d7b79.mjs';
3
- import '@babel/runtime/helpers/defineProperty';
4
-
5
- function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
6
-
7
- function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
8
-
9
- function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
10
-
1
+ import { observable } from '@trpc/server/observable';
2
+ import { t as transformResult, T as TRPCClientError } from '../transformResult-106791d7.mjs';
3
+ import { g as getUrl, h as httpRequest } from '../httpUtils-44781613.mjs';
4
+
5
+ /* eslint-disable @typescript-eslint/no-non-null-assertion */ /**
6
+ * A function that should never be called unless we messed something up.
7
+ */ const throwFatalError = ()=>{
8
+ throw new Error('Something went wrong. Please submit an issue at https://github.com/trpc/trpc/issues/new');
9
+ };
11
10
  /**
12
11
  * Dataloader that's very inspired by https://github.com/graphql/dataloader
13
12
  * Less configuration, no caching, and allows you to cancel requests
14
13
  * When cancelling a single fetch the whole batch will be cancelled only when _all_ items are cancelled
15
- */
16
- function dataLoader(fetchMany) {
17
- var batch = null;
18
- var dispatchTimer = null;
19
-
20
- var destroyTimerAndBatch = function destroyTimerAndBatch() {
21
- clearTimeout(dispatchTimer);
22
- dispatchTimer = null;
23
- batch = null;
24
- };
25
-
26
- function dispatch() {
27
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
28
- var batchCopy = batch;
29
- destroyTimerAndBatch();
30
-
31
- var _fetchMany = fetchMany(batchCopy.items.map(function (v) {
32
- return v.key;
33
- })),
34
- promise = _fetchMany.promise,
35
- cancel = _fetchMany.cancel;
36
-
37
- batchCopy.cancel = cancel;
38
- promise.then(function (result) {
39
- for (var i = 0; i < result.length; i++) {
40
- var _value = result[i];
41
- batchCopy.items[i].resolve(_value);
42
- }
43
- }).catch(function (cause) {
44
- var _iterator = _createForOfIteratorHelper(batchCopy.items),
45
- _step;
46
-
47
- try {
48
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
49
- var item = _step.value;
50
- item.reject(cause);
51
- }
52
- } catch (err) {
53
- _iterator.e(err);
54
- } finally {
55
- _iterator.f();
56
- }
57
- });
58
- }
59
-
60
- function load(key) {
61
- var batchItem = {
62
- cancelled: false,
63
- key: key
14
+ */ function dataLoader(batchLoader) {
15
+ let pendingItems = null;
16
+ let dispatchTimer = null;
17
+ const destroyTimerAndPendingItems = ()=>{
18
+ clearTimeout(dispatchTimer);
19
+ dispatchTimer = null;
20
+ pendingItems = null;
64
21
  };
65
-
66
- if (!batch) {
67
- batch = {
68
- items: [],
69
- cancel: function cancel() {
70
- destroyTimerAndBatch();
22
+ /**
23
+ * Iterate through the items and split them into groups based on the `batchLoader`'s validate function
24
+ */ function groupItems(items) {
25
+ const groupedItems = [
26
+ []
27
+ ];
28
+ let index = 0;
29
+ while(true){
30
+ const item = items[index];
31
+ if (!item) {
32
+ break;
33
+ }
34
+ const lastGroup = groupedItems[groupedItems.length - 1];
35
+ if (item.aborted) {
36
+ // Item was aborted before it was dispatched
37
+ item.reject(new Error('Aborted'));
38
+ index++;
39
+ continue;
40
+ }
41
+ const isValid = batchLoader.validate(lastGroup.concat(item).map((it)=>it.key));
42
+ if (isValid) {
43
+ lastGroup.push(item);
44
+ index++;
45
+ continue;
46
+ }
47
+ if (lastGroup.length === 0) {
48
+ item.reject(new Error('Input is too big for a single dispatch'));
49
+ index++;
50
+ continue;
51
+ }
52
+ // Create new group, next iteration will try to add the item to that
53
+ groupedItems.push([]);
71
54
  }
72
- };
55
+ return groupedItems;
73
56
  }
74
-
75
- var thisBatch = batch;
76
- var promise = new Promise(function (resolve, reject) {
77
- var item = batchItem;
78
- item.reject = reject;
79
- item.resolve = resolve;
80
- thisBatch.items.push(item);
81
- });
82
-
83
- if (!dispatchTimer) {
84
- dispatchTimer = setTimeout(dispatch);
57
+ function dispatch() {
58
+ const groupedItems = groupItems(pendingItems);
59
+ destroyTimerAndPendingItems();
60
+ // Create batches for each group of items
61
+ for (const items of groupedItems){
62
+ if (!items.length) {
63
+ continue;
64
+ }
65
+ const batch = {
66
+ items,
67
+ cancel: throwFatalError
68
+ };
69
+ for (const item of items){
70
+ item.batch = batch;
71
+ }
72
+ const { promise , cancel } = batchLoader.fetch(batch.items.map((_item)=>_item.key));
73
+ batch.cancel = cancel;
74
+ promise.then((result)=>{
75
+ for(let i = 0; i < result.length; i++){
76
+ const value = result[i];
77
+ const item = batch.items[i];
78
+ item.resolve(value);
79
+ item.batch = null;
80
+ }
81
+ }).catch((cause)=>{
82
+ for (const item of batch.items){
83
+ item.reject(cause);
84
+ item.batch = null;
85
+ }
86
+ });
87
+ }
88
+ }
89
+ function load(key) {
90
+ const item = {
91
+ aborted: false,
92
+ key,
93
+ batch: null,
94
+ resolve: throwFatalError,
95
+ reject: throwFatalError
96
+ };
97
+ const promise = new Promise((resolve, reject)=>{
98
+ item.reject = reject;
99
+ item.resolve = resolve;
100
+ if (!pendingItems) {
101
+ pendingItems = [];
102
+ }
103
+ pendingItems.push(item);
104
+ });
105
+ if (!dispatchTimer) {
106
+ dispatchTimer = setTimeout(dispatch);
107
+ }
108
+ const cancel = ()=>{
109
+ item.aborted = true;
110
+ if (item.batch?.items.every((item)=>item.aborted)) {
111
+ // All items in the batch have been cancelled
112
+ item.batch.cancel();
113
+ item.batch = null;
114
+ }
115
+ };
116
+ return {
117
+ promise,
118
+ cancel
119
+ };
85
120
  }
86
-
87
- var cancel = function cancel() {
88
- batchItem.cancelled = true;
89
-
90
- if (thisBatch.items.some(function (item) {
91
- return !item.cancelled;
92
- })) {
93
- // there are still things that can be resolved
94
- return;
95
- }
96
-
97
- thisBatch.cancel();
98
- };
99
-
100
121
  return {
101
- promise: promise,
102
- cancel: cancel
122
+ load
103
123
  };
104
- }
105
-
106
- return {
107
- load: load
108
- };
109
124
  }
110
125
 
111
126
  function httpBatchLink(opts) {
112
- var url = opts.url;
113
- return function (runtime) {
114
- var fetcher = function fetcher(type) {
115
- return function (keyInputPairs) {
116
- var path = keyInputPairs.map(function (op) {
117
- return op.path;
118
- }).join(',');
119
- var inputs = keyInputPairs.map(function (op) {
120
- return op.input;
121
- });
122
-
123
- var _httpRequest = httpRequest({
124
- url: url,
125
- inputs: inputs,
126
- path: path,
127
- runtime: runtime,
128
- type: type
129
- }),
130
- promise = _httpRequest.promise,
131
- cancel = _httpRequest.cancel;
132
-
133
- return {
134
- promise: promise.then(function (res) {
135
- var resJSON = Array.isArray(res.json) ? res.json : keyInputPairs.map(function () {
136
- return res.json;
137
- });
138
- var result = resJSON.map(function (item) {
139
- return {
140
- meta: res.meta,
141
- json: item
142
- };
143
- });
144
- return result;
145
- }),
146
- cancel: cancel
127
+ // initialized config
128
+ return (runtime)=>{
129
+ const maxURLLength = opts.maxURLLength || Infinity;
130
+ const batchLoader = (type)=>{
131
+ const validate = (batchOps)=>{
132
+ if (maxURLLength === Infinity) {
133
+ // escape hatch for quick calcs
134
+ return true;
135
+ }
136
+ const path = batchOps.map((op)=>op.path).join(',');
137
+ const inputs = batchOps.map((op)=>op.input);
138
+ const url = getUrl({
139
+ url: opts.url,
140
+ runtime,
141
+ type,
142
+ path,
143
+ inputs
144
+ });
145
+ return url.length <= maxURLLength;
146
+ };
147
+ const fetch = (batchOps)=>{
148
+ const path = batchOps.map((op)=>op.path).join(',');
149
+ const inputs = batchOps.map((op)=>op.input);
150
+ const { promise , cancel } = httpRequest({
151
+ url: opts.url,
152
+ runtime,
153
+ type,
154
+ path,
155
+ inputs
156
+ });
157
+ return {
158
+ promise: promise.then((res)=>{
159
+ const resJSON = Array.isArray(res.json) ? res.json : batchOps.map(()=>res.json);
160
+ const result = resJSON.map((item)=>({
161
+ meta: res.meta,
162
+ json: item
163
+ }));
164
+ return result;
165
+ }),
166
+ cancel
167
+ };
168
+ };
169
+ return {
170
+ validate,
171
+ fetch
172
+ };
147
173
  };
148
- };
149
- };
150
-
151
- var query = dataLoader(fetcher('query'));
152
- var mutation = dataLoader(fetcher('mutation'));
153
- var subscription = dataLoader(fetcher('subscription'));
154
- var loaders = {
155
- query: query,
156
- subscription: subscription,
157
- mutation: mutation
158
- };
159
- return function (_ref) {
160
- var op = _ref.op;
161
- return observable(function (observer) {
162
- var loader = loaders[op.type];
163
-
164
- var _loader$load = loader.load(op),
165
- promise = _loader$load.promise,
166
- cancel = _loader$load.cancel;
167
-
168
- promise.then(function (res) {
169
- observer.next({
170
- context: res.meta,
171
- data: res.json
172
- });
173
- observer.complete();
174
- }).catch(function (err) {
175
- return observer.error(err);
176
- });
177
- return function () {
178
- cancel();
174
+ const query = dataLoader(batchLoader('query'));
175
+ const mutation = dataLoader(batchLoader('mutation'));
176
+ const subscription = dataLoader(batchLoader('subscription'));
177
+ const loaders = {
178
+ query,
179
+ subscription,
180
+ mutation
181
+ };
182
+ return ({ op })=>{
183
+ return observable((observer)=>{
184
+ const loader = loaders[op.type];
185
+ const { promise , cancel } = loader.load(op);
186
+ promise.then((res)=>{
187
+ const transformed = transformResult(res.json, runtime);
188
+ if (!transformed.ok) {
189
+ observer.error(TRPCClientError.from(transformed.error, {
190
+ meta: res.meta
191
+ }));
192
+ return;
193
+ }
194
+ observer.next({
195
+ context: res.meta,
196
+ result: transformed.result
197
+ });
198
+ observer.complete();
199
+ }).catch((err)=>observer.error(TRPCClientError.from(err)));
200
+ return ()=>{
201
+ cancel();
202
+ };
203
+ });
179
204
  };
180
- });
181
205
  };
182
- };
183
206
  }
184
207
 
185
208
  export { httpBatchLink };
@@ -1,5 +1,5 @@
1
1
  import { AnyRouter } from '@trpc/server';
2
+ import { HTTPLinkOptions } from './internals/httpUtils';
2
3
  import { TRPCLink } from './types';
3
- import { HTTPLinkOptions } from './httpUtils';
4
4
  export declare function httpLink<TRouter extends AnyRouter>(opts: HTTPLinkOptions): TRPCLink<TRouter>;
5
5
  //# sourceMappingURL=httpLink.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"httpLink.d.ts","sourceRoot":"","sources":["../../src/links/httpLink.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEzC,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AACnC,OAAO,EAAE,eAAe,EAAe,MAAM,aAAa,CAAC;AAE3D,wBAAgB,QAAQ,CAAC,OAAO,SAAS,SAAS,EAChD,IAAI,EAAE,eAAe,GACpB,QAAQ,CAAC,OAAO,CAAC,CA2BnB"}
1
+ {"version":3,"file":"httpLink.d.ts","sourceRoot":"","sources":["../../../../packages/client/src/links/httpLink.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAGzC,OAAO,EAAE,eAAe,EAAe,MAAM,uBAAuB,CAAC;AAErE,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAEnC,wBAAgB,QAAQ,CAAC,OAAO,SAAS,SAAS,EAChD,IAAI,EAAE,eAAe,GACpB,QAAQ,CAAC,OAAO,CAAC,CAqCnB"}
@@ -2,43 +2,39 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var observable = require('../observable-85669100.js');
6
- var httpUtils = require('../httpUtils-1f9e4e3c.js');
7
- require('@babel/runtime/helpers/defineProperty');
5
+ var observable = require('@trpc/server/observable');
6
+ var transformResult = require('../transformResult-e15ccdf6.js');
7
+ var httpUtils = require('../httpUtils-801112a6.js');
8
8
 
9
9
  function httpLink(opts) {
10
- var url = opts.url;
11
- return function (runtime) {
12
- return function (_ref) {
13
- var op = _ref.op;
14
- return observable.observable(function (observer) {
15
- var path = op.path,
16
- input = op.input,
17
- type = op.type;
18
-
19
- var _httpRequest = httpUtils.httpRequest({
20
- runtime: runtime,
21
- path: path,
22
- input: input,
23
- type: type,
24
- url: url
25
- }),
26
- promise = _httpRequest.promise,
27
- cancel = _httpRequest.cancel;
28
-
29
- promise.then(function (res) {
30
- observer.next({
31
- context: res.meta,
32
- data: res.json
33
- });
34
- observer.complete();
35
- }).catch(observer.error);
36
- return function () {
37
- cancel();
38
- };
39
- });
40
- };
41
- };
10
+ const { url } = opts;
11
+ return (runtime)=>({ op })=>observable.observable((observer)=>{
12
+ const { path , input , type } = op;
13
+ const { promise , cancel } = httpUtils.httpRequest({
14
+ url,
15
+ runtime,
16
+ type,
17
+ path,
18
+ input
19
+ });
20
+ promise.then((res)=>{
21
+ const transformed = transformResult.transformResult(res.json, runtime);
22
+ if (!transformed.ok) {
23
+ observer.error(transformResult.TRPCClientError.from(transformed.error, {
24
+ meta: res.meta
25
+ }));
26
+ return;
27
+ }
28
+ observer.next({
29
+ context: res.meta,
30
+ result: transformed.result
31
+ });
32
+ observer.complete();
33
+ }).catch((cause)=>observer.error(transformResult.TRPCClientError.from(cause)));
34
+ return ()=>{
35
+ cancel();
36
+ };
37
+ });
42
38
  }
43
39
 
44
40
  exports.httpLink = httpLink;
@@ -1,40 +1,36 @@
1
- import { o as observable } from '../observable-7e66cb56.mjs';
2
- import { h as httpRequest } from '../httpUtils-c09d7b79.mjs';
3
- import '@babel/runtime/helpers/defineProperty';
1
+ import { observable } from '@trpc/server/observable';
2
+ import { t as transformResult, T as TRPCClientError } from '../transformResult-106791d7.mjs';
3
+ import { h as httpRequest } from '../httpUtils-44781613.mjs';
4
4
 
5
5
  function httpLink(opts) {
6
- var url = opts.url;
7
- return function (runtime) {
8
- return function (_ref) {
9
- var op = _ref.op;
10
- return observable(function (observer) {
11
- var path = op.path,
12
- input = op.input,
13
- type = op.type;
14
-
15
- var _httpRequest = httpRequest({
16
- runtime: runtime,
17
- path: path,
18
- input: input,
19
- type: type,
20
- url: url
21
- }),
22
- promise = _httpRequest.promise,
23
- cancel = _httpRequest.cancel;
24
-
25
- promise.then(function (res) {
26
- observer.next({
27
- context: res.meta,
28
- data: res.json
29
- });
30
- observer.complete();
31
- }).catch(observer.error);
32
- return function () {
33
- cancel();
34
- };
35
- });
36
- };
37
- };
6
+ const { url } = opts;
7
+ return (runtime)=>({ op })=>observable((observer)=>{
8
+ const { path , input , type } = op;
9
+ const { promise , cancel } = httpRequest({
10
+ url,
11
+ runtime,
12
+ type,
13
+ path,
14
+ input
15
+ });
16
+ promise.then((res)=>{
17
+ const transformed = transformResult(res.json, runtime);
18
+ if (!transformed.ok) {
19
+ observer.error(TRPCClientError.from(transformed.error, {
20
+ meta: res.meta
21
+ }));
22
+ return;
23
+ }
24
+ observer.next({
25
+ context: res.meta,
26
+ result: transformed.result
27
+ });
28
+ observer.complete();
29
+ }).catch((cause)=>observer.error(TRPCClientError.from(cause)));
30
+ return ()=>{
31
+ cancel();
32
+ };
33
+ });
38
34
  }
39
35
 
40
36
  export { httpLink };
@@ -0,0 +1,9 @@
1
+ export * from './dedupeLink';
2
+ export * from './httpBatchLink';
3
+ export * from './httpLink';
4
+ export * from './loggerLink';
5
+ export * from './retryLink';
6
+ export * from './splitLink';
7
+ export * from './wsLink';
8
+ export * from './types';
9
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../packages/client/src/links/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,iBAAiB,CAAC;AAChC,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,UAAU,CAAC;AACzB,cAAc,SAAS,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"createChain.d.ts","sourceRoot":"","sources":["../../../src/links/internals/createChain.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEzC,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,yBAAyB,EAAE,MAAM,UAAU,CAAC;AAE/E,gBAAgB;AAChB,wBAAgB,WAAW,CACzB,OAAO,SAAS,SAAS,EACzB,MAAM,GAAG,OAAO,EAChB,OAAO,GAAG,OAAO,EACjB,IAAI,EAAE;IACN,KAAK,EAAE,aAAa,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,CAAC;IACjD,EAAE,EAAE,SAAS,CAAC,MAAM,CAAC,CAAC;CACvB,GAAG,yBAAyB,CAAC,OAAO,EAAE,OAAO,CAAC,CAkB9C"}
1
+ {"version":3,"file":"createChain.d.ts","sourceRoot":"","sources":["../../../../../packages/client/src/links/internals/createChain.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEzC,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,yBAAyB,EAAE,MAAM,UAAU,CAAC;AAE/E,gBAAgB;AAChB,wBAAgB,WAAW,CACzB,OAAO,SAAS,SAAS,EACzB,MAAM,GAAG,OAAO,EAChB,OAAO,GAAG,OAAO,EACjB,IAAI,EAAE;IACN,KAAK,EAAE,aAAa,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,CAAC;IACjD,EAAE,EAAE,SAAS,CAAC,MAAM,CAAC,CAAC;CACvB,GAAG,yBAAyB,CAAC,OAAO,EAAE,OAAO,CAAC,CAuB9C"}
@@ -0,0 +1,31 @@
1
+ import { ProcedureType } from '@trpc/server';
2
+ import { TRPCResponse } from '@trpc/server/rpc';
3
+ import { PromiseAndCancel, TRPCClientRuntime } from '../types';
4
+ export interface HTTPLinkOptions {
5
+ url: string;
6
+ }
7
+ export interface HTTPResult {
8
+ json: TRPCResponse;
9
+ meta: {
10
+ response: Response;
11
+ };
12
+ }
13
+ declare type GetInputOptions = {
14
+ runtime: TRPCClientRuntime;
15
+ } & ({
16
+ inputs: unknown[];
17
+ } | {
18
+ input: unknown;
19
+ });
20
+ export declare type HTTPRequestOptions = HTTPLinkOptions & GetInputOptions & {
21
+ type: ProcedureType;
22
+ path: string;
23
+ };
24
+ export declare function getUrl(opts: HTTPRequestOptions): string;
25
+ declare type GetBodyOptions = {
26
+ type: ProcedureType;
27
+ } & GetInputOptions;
28
+ export declare function getBody(opts: GetBodyOptions): string | undefined;
29
+ export declare function httpRequest(opts: HTTPRequestOptions): PromiseAndCancel<HTTPResult>;
30
+ export {};
31
+ //# sourceMappingURL=httpUtils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"httpUtils.d.ts","sourceRoot":"","sources":["../../../../../packages/client/src/links/internals/httpUtils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAC7C,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAChD,OAAO,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC;AAE/D,MAAM,WAAW,eAAe;IAC9B,GAAG,EAAE,MAAM,CAAC;CACb;AAkBD,MAAM,WAAW,UAAU;IACzB,IAAI,EAAE,YAAY,CAAC;IACnB,IAAI,EAAE;QACJ,QAAQ,EAAE,QAAQ,CAAC;KACpB,CAAC;CACH;AAED,aAAK,eAAe,GAAG;IACrB,OAAO,EAAE,iBAAiB,CAAC;CAC5B,GAAG,CAAC;IAAE,MAAM,EAAE,OAAO,EAAE,CAAA;CAAE,GAAG;IAAE,KAAK,EAAE,OAAO,CAAA;CAAE,CAAC,CAAC;AAUjD,oBAAY,kBAAkB,GAAG,eAAe,GAC9C,eAAe,GAAG;IAChB,IAAI,EAAE,aAAa,CAAC;IACpB,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEJ,wBAAgB,MAAM,CAAC,IAAI,EAAE,kBAAkB,UAgB9C;AAED,aAAK,cAAc,GAAG;IAAE,IAAI,EAAE,aAAa,CAAA;CAAE,GAAG,eAAe,CAAC;AAEhE,wBAAgB,OAAO,CAAC,IAAI,EAAE,cAAc,sBAM3C;AAED,wBAAgB,WAAW,CACzB,IAAI,EAAE,kBAAkB,GACvB,gBAAgB,CAAC,UAAU,CAAC,CAqC9B"}
@@ -0,0 +1,28 @@
1
+ import { AnyRouter, inferRouterError } from '@trpc/server';
2
+ import { TRPCResponse, TRPCResponseMessage } from '@trpc/server/rpc';
3
+ import { TRPCClientRuntime } from '..';
4
+ /** @internal */
5
+ export declare function transformResult<TRouter extends AnyRouter, TOutput>(response: TRPCResponseMessage<TOutput, inferRouterError<TRouter>> | TRPCResponse<TOutput, inferRouterError<TRouter>>, runtime: TRPCClientRuntime): {
6
+ readonly ok: false;
7
+ readonly error: {
8
+ readonly error: inferRouterError<TRouter>;
9
+ readonly id?: import("@trpc/server/rpc").JSONRPC2.RequestId | undefined;
10
+ readonly jsonrpc?: "2.0" | undefined;
11
+ } | {
12
+ readonly error: inferRouterError<TRouter>;
13
+ readonly id: string | number | null;
14
+ readonly jsonrpc?: "2.0" | undefined;
15
+ };
16
+ readonly result?: undefined;
17
+ } | {
18
+ readonly ok: true;
19
+ readonly result: {
20
+ type: "started";
21
+ } | {
22
+ type: "stopped";
23
+ } | (import("@trpc/server/rpc").TRPCResult<TOutput> & {
24
+ type: "data";
25
+ });
26
+ readonly error?: undefined;
27
+ };
28
+ //# sourceMappingURL=transformResult.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transformResult.d.ts","sourceRoot":"","sources":["../../../../../packages/client/src/links/internals/transformResult.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAC3D,OAAO,EACL,YAAY,EACZ,mBAAmB,EAEpB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,iBAAiB,EAAE,MAAM,IAAI,CAAC;AAKvC,gBAAgB;AAChB,wBAAgB,eAAe,CAAC,OAAO,SAAS,SAAS,EAAE,OAAO,EAChE,QAAQ,EACJ,mBAAmB,CAAC,OAAO,EAAE,gBAAgB,CAAC,OAAO,CAAC,CAAC,GACvD,YAAY,CAAC,OAAO,EAAE,gBAAgB,CAAC,OAAO,CAAC,CAAC,EACpD,OAAO,EAAE,iBAAiB;;;;;;;;;;;;;;;;;;;;;;EAuB3B"}
@@ -1,6 +1,6 @@
1
1
  import { AnyRouter } from '@trpc/server';
2
2
  import { TRPCClientError } from '..';
3
- import { Operation, OperationResult, TRPCLink } from './types';
3
+ import { Operation, OperationResultEnvelope, TRPCLink } from './types';
4
4
  declare type ConsoleEsque = {
5
5
  log: (...args: any[]) => void;
6
6
  error: (...args: any[]) => void;
@@ -9,7 +9,7 @@ declare type EnableFnOptions<TRouter extends AnyRouter> = (Operation & {
9
9
  direction: 'up';
10
10
  }) | {
11
11
  direction: 'down';
12
- result: OperationResult<TRouter, unknown> | TRPCClientError<TRouter>;
12
+ result: OperationResultEnvelope<unknown> | TRPCClientError<TRouter>;
13
13
  };
14
14
  declare type EnabledFn<TRouter extends AnyRouter> = (opts: EnableFnOptions<TRouter>) => boolean;
15
15
  declare type LogFnOptions<TRouter extends AnyRouter> = Operation & ({
@@ -22,7 +22,7 @@ declare type LogFnOptions<TRouter extends AnyRouter> = Operation & ({
22
22
  * Request result
23
23
  */
24
24
  direction: 'down';
25
- result: OperationResult<TRouter, unknown> | TRPCClientError<TRouter>;
25
+ result: OperationResultEnvelope<unknown> | TRPCClientError<TRouter>;
26
26
  elapsedMs: number;
27
27
  });
28
28
  declare type LogFn<TRouter extends AnyRouter> = (opts: LogFnOptions<TRouter>) => void;