@trpc/client 10.0.0-alpha.43 → 10.0.0-alpha.46

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 (62) hide show
  1. package/dist/TRPCClientError.d.ts +14 -10
  2. package/dist/TRPCClientError.d.ts.map +1 -1
  3. package/dist/httpUtils-44781613.mjs +76 -0
  4. package/dist/httpUtils-801112a6.js +79 -0
  5. package/dist/index.js +226 -337
  6. package/dist/index.mjs +228 -333
  7. package/dist/internals/TRPCClient.d.ts +3 -1
  8. package/dist/internals/TRPCClient.d.ts.map +1 -1
  9. package/dist/internals/dataLoader.d.ts.map +1 -1
  10. package/dist/internals/fetchHelpers.d.ts.map +1 -1
  11. package/dist/internals/retryDelay.d.ts.map +1 -1
  12. package/dist/links/dedupeLink.d.ts.map +1 -1
  13. package/dist/links/httpBatchLink.d.ts.map +1 -1
  14. package/dist/links/httpBatchLink.js +182 -304
  15. package/dist/links/httpBatchLink.mjs +182 -304
  16. package/dist/links/httpLink.d.ts.map +1 -1
  17. package/dist/links/httpLink.js +30 -53
  18. package/dist/links/httpLink.mjs +30 -53
  19. package/dist/links/index.d.ts.map +1 -1
  20. package/dist/links/internals/createChain.d.ts.map +1 -1
  21. package/dist/links/internals/httpUtils.d.ts.map +1 -1
  22. package/dist/links/internals/transformResult.d.ts.map +1 -1
  23. package/dist/links/loggerLink.d.ts.map +1 -1
  24. package/dist/links/loggerLink.js +89 -94
  25. package/dist/links/loggerLink.mjs +89 -89
  26. package/dist/links/retryLink.d.ts.map +1 -1
  27. package/dist/links/splitLink.d.ts.map +1 -1
  28. package/dist/links/splitLink.js +1 -1
  29. package/dist/links/splitLink.mjs +1 -1
  30. package/dist/links/types.d.ts.map +1 -1
  31. package/dist/links/wsLink.d.ts.map +1 -1
  32. package/dist/links/wsLink.js +248 -379
  33. package/dist/links/wsLink.mjs +248 -368
  34. package/dist/splitLink-695ae288.js +48 -0
  35. package/dist/splitLink-ad44a55d.mjs +45 -0
  36. package/dist/transformResult-106791d7.mjs +62 -0
  37. package/dist/transformResult-e15ccdf6.js +65 -0
  38. package/links/httpBatchLink/index.d.ts +1 -1
  39. package/links/httpBatchLink/index.js +1 -1
  40. package/links/httpLink/index.d.ts +1 -1
  41. package/links/httpLink/index.js +1 -1
  42. package/links/loggerLink/index.d.ts +1 -1
  43. package/links/loggerLink/index.js +1 -1
  44. package/links/splitLink/index.d.ts +1 -1
  45. package/links/splitLink/index.js +1 -1
  46. package/links/wsLink/index.d.ts +1 -1
  47. package/links/wsLink/index.js +1 -1
  48. package/package.json +7 -11
  49. package/src/TRPCClientError.ts +35 -17
  50. package/src/internals/TRPCClient.ts +10 -9
  51. package/dist/httpUtils-089853f6.mjs +0 -99
  52. package/dist/httpUtils-a0169eef.js +0 -106
  53. package/dist/index.ts.js +0 -1
  54. package/dist/links/httpBatchLink.ts.js +0 -1
  55. package/dist/links/httpLink.ts.js +0 -1
  56. package/dist/links/loggerLink.ts.js +0 -1
  57. package/dist/links/splitLink.ts.js +0 -1
  58. package/dist/links/wsLink.ts.js +0 -1
  59. package/dist/splitLink-0202cd7b.mjs +0 -54
  60. package/dist/splitLink-23c5ce5c.js +0 -57
  61. package/dist/transformResult-06bec648.mjs +0 -108
  62. package/dist/transformResult-3ccc74ea.js +0 -122
package/dist/index.js CHANGED
@@ -2,386 +2,275 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var _classCallCheck = require('@babel/runtime/helpers/classCallCheck');
6
- var _createClass = require('@babel/runtime/helpers/createClass');
7
5
  var observable = require('@trpc/server/observable');
8
- var transformResult = require('./transformResult-3ccc74ea.js');
6
+ var transformResult = require('./transformResult-e15ccdf6.js');
9
7
  var links_httpBatchLink = require('./links/httpBatchLink.js');
10
- require('@babel/runtime/helpers/defineProperty');
11
- require('@babel/runtime/helpers/slicedToArray');
12
- var links_splitLink = require('./splitLink-23c5ce5c.js');
13
- require('@babel/runtime/helpers/assertThisInitialized');
14
- require('@babel/runtime/helpers/inherits');
15
- require('@babel/runtime/helpers/possibleConstructorReturn');
16
- require('@babel/runtime/helpers/getPrototypeOf');
17
- require('@babel/runtime/helpers/wrapNativeSuper');
18
- var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
8
+ var links_splitLink = require('./splitLink-695ae288.js');
19
9
  var shared = require('@trpc/server/shared');
20
10
  var links_httpLink = require('./links/httpLink.js');
21
11
  var links_loggerLink = require('./links/loggerLink.js');
22
12
  var links_wsLink = require('./links/wsLink.js');
23
- require('./httpUtils-a0169eef.js');
24
-
25
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
26
-
27
- var _classCallCheck__default = /*#__PURE__*/_interopDefaultLegacy(_classCallCheck);
28
- var _createClass__default = /*#__PURE__*/_interopDefaultLegacy(_createClass);
29
- var _toConsumableArray__default = /*#__PURE__*/_interopDefaultLegacy(_toConsumableArray);
13
+ require('./httpUtils-801112a6.js');
30
14
 
31
15
  function getWindow() {
32
- if (typeof window !== 'undefined') {
33
- return window;
34
- }
35
-
36
- return global;
16
+ if (typeof window !== 'undefined') {
17
+ return window;
18
+ }
19
+ return global;
37
20
  }
38
21
  function getAbortController(ac) {
39
- var win = getWindow();
40
- return ac || win.AbortController || null;
22
+ const win = getWindow();
23
+ return ac || win.AbortController || null;
41
24
  }
42
25
 
43
26
  function getFetch(f) {
44
- if (f) {
45
- return f;
46
- }
47
-
48
- var win = getWindow();
49
-
50
- if (win.fetch) {
51
- return typeof win.fetch.bind === 'function' ? win.fetch.bind(win) : win.fetch;
52
- }
53
-
54
- throw new Error('No fetch implementation found');
27
+ if (f) {
28
+ return f;
29
+ }
30
+ const win = getWindow();
31
+ if (win.fetch) {
32
+ return typeof win.fetch.bind === 'function' ? win.fetch.bind(win) : win.fetch;
33
+ }
34
+ throw new Error('No fetch implementation found');
55
35
  }
56
36
 
57
37
  function dedupeLink() {
58
- // initialized config
59
- return function () {
60
- // initialized in app
61
- var pending = {};
62
- return function (_ref) {
63
- var op = _ref.op,
64
- next = _ref.next;
65
-
66
- // initialized for request
67
- if (op.type !== 'query') {
68
- // pass through
69
- return next(op);
70
- }
71
-
72
- var key = JSON.stringify([op.path, op.input]);
73
- var obs$ = pending[key];
74
-
75
- if (obs$) {
76
- // console.log('hooking into pending', { op });
77
- return observable.observable(function (observer) {
78
- return obs$.subscribe(observer);
79
- });
80
- }
81
-
82
- var shared$ = observable.observable(function (observer) {
83
- function reset() {
84
- delete pending[key];
85
- }
86
-
87
- var subscription = next(op).subscribe({
88
- next: function next(v) {
89
- observer.next(v);
90
- },
91
- error: function error(e) {
92
- reset();
93
- observer.error(e);
94
- },
95
- complete: function complete() {
96
- reset();
97
- observer.complete();
98
- }
99
- });
100
- return function () {
101
- reset();
102
- subscription.unsubscribe();
38
+ // initialized config
39
+ return ()=>{
40
+ // initialized in app
41
+ const pending = {};
42
+ return ({ op , next })=>{
43
+ // initialized for request
44
+ if (op.type !== 'query') {
45
+ // pass through
46
+ return next(op);
47
+ }
48
+ const key = JSON.stringify([
49
+ op.path,
50
+ op.input
51
+ ]);
52
+ const obs$ = pending[key];
53
+ if (obs$) {
54
+ // console.log('hooking into pending', { op });
55
+ return observable.observable((observer)=>obs$.subscribe(observer));
56
+ }
57
+ const shared$ = observable.observable((observer)=>{
58
+ function reset() {
59
+ delete pending[key];
60
+ }
61
+ const subscription = next(op).subscribe({
62
+ next (v) {
63
+ observer.next(v);
64
+ },
65
+ error (e) {
66
+ reset();
67
+ observer.error(e);
68
+ },
69
+ complete () {
70
+ reset();
71
+ observer.complete();
72
+ }
73
+ });
74
+ return ()=>{
75
+ reset();
76
+ subscription.unsubscribe();
77
+ };
78
+ }).pipe(observable.share());
79
+ pending[key] = shared$;
80
+ return shared$;
103
81
  };
104
- }).pipe(observable.share());
105
- pending[key] = shared$;
106
- return shared$;
107
82
  };
108
- };
109
83
  }
110
84
 
111
85
  function retryLink(opts) {
112
- // initialized config
113
- return function () {
114
- // initialized in app
115
- return function (_ref) {
116
- var op = _ref.op,
117
- next = _ref.next;
118
- // initialized for request
119
- return observable.observable(function (observer) {
120
- var next$ = null;
121
- var attempts = 0;
122
- var isDone = false;
123
-
124
- function attempt() {
125
- var _next$;
126
-
127
- attempts++;
128
- (_next$ = next$) === null || _next$ === void 0 ? void 0 : _next$.unsubscribe();
129
- next$ = next(op).subscribe({
130
- error: function error(_error) {
131
- if (attempts >= opts.attempts) {
132
- observer.error(_error);
133
- return;
134
- }
135
-
136
- attempt();
137
- },
138
- next: function next(result) {
139
- if ('result' in result.result) {
140
- isDone = true;
141
- observer.next(result);
142
- return;
143
- }
144
-
145
- if (attempts >= opts.attempts) {
146
- isDone = true;
147
- observer.next(result);
148
- return;
149
- }
150
-
151
- attempt();
152
- },
153
- complete: function complete() {
154
- if (isDone) {
155
- observer.complete();
156
- }
157
- }
158
- });
159
- }
160
-
161
- attempt();
162
- return function () {
163
- var _next$2;
164
-
165
- isDone = true;
166
- (_next$2 = next$) === null || _next$2 === void 0 ? void 0 : _next$2.unsubscribe();
86
+ // initialized config
87
+ return ()=>{
88
+ // initialized in app
89
+ return ({ op , next })=>{
90
+ // initialized for request
91
+ return observable.observable((observer)=>{
92
+ let next$ = null;
93
+ let attempts = 0;
94
+ let isDone = false;
95
+ function attempt() {
96
+ attempts++;
97
+ next$?.unsubscribe();
98
+ next$ = next(op).subscribe({
99
+ error (error) {
100
+ if (attempts >= opts.attempts) {
101
+ observer.error(error);
102
+ return;
103
+ }
104
+ attempt();
105
+ },
106
+ next (result) {
107
+ if ('result' in result.result) {
108
+ isDone = true;
109
+ observer.next(result);
110
+ return;
111
+ }
112
+ if (attempts >= opts.attempts) {
113
+ isDone = true;
114
+ observer.next(result);
115
+ return;
116
+ }
117
+ attempt();
118
+ },
119
+ complete () {
120
+ if (isDone) {
121
+ observer.complete();
122
+ }
123
+ }
124
+ });
125
+ }
126
+ attempt();
127
+ return ()=>{
128
+ isDone = true;
129
+ next$?.unsubscribe();
130
+ };
131
+ });
167
132
  };
168
- });
169
133
  };
170
- };
171
134
  }
172
135
 
173
- var idCounter = 0;
174
-
175
- function getRequestId() {
176
- return ++idCounter;
177
- }
178
-
179
- var TRPCClient = /*#__PURE__*/function () {
180
- function TRPCClient(opts) {
181
- var _this = this;
182
-
183
- _classCallCheck__default["default"](this, TRPCClient);
184
-
185
- this.links = void 0;
186
- this.runtime = void 0;
187
-
188
- var _fetch = getFetch(opts === null || opts === void 0 ? void 0 : opts.fetch);
189
-
190
- var AC = getAbortController(opts === null || opts === void 0 ? void 0 : opts.AbortController);
191
-
192
- function getHeadersFn() {
193
- if (opts.headers) {
194
- var headers = opts.headers;
195
- return typeof headers === 'function' ? headers : function () {
196
- return headers;
197
- };
198
- }
199
-
200
- return function () {
201
- return {};
202
- };
136
+ class TRPCClient {
137
+ getRequestId() {
138
+ return ++this.requestId;
203
139
  }
204
-
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
- this.runtime = {
217
- AbortController: AC,
218
- fetch: _fetch,
219
- headers: getHeadersFn(),
220
- transformer: transformer
221
- };
222
-
223
- if ('links' in opts) {
224
- this.links = opts.links.map(function (link) {
225
- return link(_this.runtime);
226
- });
227
- } else {
228
- this.links = [links_httpBatchLink.httpBatchLink({
229
- url: opts.url
230
- })(this.runtime)];
140
+ $request({ type , input , path , context ={} }) {
141
+ const chain$ = links_splitLink.createChain({
142
+ links: this.links,
143
+ op: {
144
+ id: this.getRequestId(),
145
+ type,
146
+ path,
147
+ input,
148
+ context
149
+ }
150
+ });
151
+ return chain$.pipe(observable.share());
231
152
  }
232
- }
233
-
234
- _createClass__default["default"](TRPCClient, [{
235
- key: "$request",
236
- value: function $request(_ref) {
237
- var type = _ref.type,
238
- input = _ref.input,
239
- path = _ref.path,
240
- _ref$context = _ref.context,
241
- context = _ref$context === void 0 ? {} : _ref$context;
242
- var chain$ = links_splitLink.createChain({
243
- links: this.links,
244
- op: {
245
- id: getRequestId(),
246
- type: type,
247
- path: path,
248
- input: input,
249
- context: context
250
- }
251
- });
252
- return chain$.pipe(observable.share());
153
+ requestAsPromise(opts) {
154
+ const req$ = this.$request(opts);
155
+ const { promise , abort } = observable.observableToPromise(req$);
156
+ const cancellablePromise = new Promise((resolve, reject)=>{
157
+ promise.then((envelope)=>{
158
+ resolve(envelope.result.data);
159
+ }).catch((err)=>{
160
+ reject(transformResult.TRPCClientError.from(err));
161
+ });
162
+ });
163
+ cancellablePromise.cancel = abort;
164
+ return cancellablePromise;
253
165
  }
254
- }, {
255
- key: "requestAsPromise",
256
- value: function requestAsPromise(opts) {
257
- var req$ = this.$request(opts);
258
-
259
- var _observableToPromise = observable.observableToPromise(req$),
260
- promise = _observableToPromise.promise,
261
- abort = _observableToPromise.abort;
262
-
263
- var cancellablePromise = new Promise(function (resolve, reject) {
264
- promise.then(function (envelope) {
265
- resolve(envelope.result.data);
266
- }).catch(function (err) {
267
- reject(transformResult.TRPCClientError.from(err));
166
+ query(path, ...args) {
167
+ const context = args[1]?.context;
168
+ return this.requestAsPromise({
169
+ type: 'query',
170
+ path,
171
+ input: args[0],
172
+ context
268
173
  });
269
- });
270
- cancellablePromise.cancel = abort;
271
- return cancellablePromise;
272
174
  }
273
- }, {
274
- key: "query",
275
- value: function query(path) {
276
- var _args$;
277
-
278
- for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
279
- args[_key - 1] = arguments[_key];
280
- }
281
-
282
- var context = (_args$ = args[1]) === null || _args$ === void 0 ? void 0 : _args$.requestContext;
283
- return this.requestAsPromise({
284
- type: 'query',
285
- path: path,
286
- input: args[0],
287
- context: context
288
- });
175
+ mutation(path, ...args) {
176
+ const context = args[1]?.context;
177
+ return this.requestAsPromise({
178
+ type: 'mutation',
179
+ path,
180
+ input: args[0],
181
+ context
182
+ });
289
183
  }
290
- }, {
291
- key: "mutation",
292
- value: function mutation(path) {
293
- var _args$2;
294
-
295
- for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
296
- args[_key2 - 1] = arguments[_key2];
297
- }
298
-
299
- var context = (_args$2 = args[1]) === null || _args$2 === void 0 ? void 0 : _args$2.requestContext;
300
- return this.requestAsPromise({
301
- type: 'mutation',
302
- path: path,
303
- input: args[0],
304
- context: context
305
- });
184
+ subscription(path, input, opts) {
185
+ const observable$ = this.$request({
186
+ type: 'subscription',
187
+ path,
188
+ input,
189
+ context: opts?.context
190
+ });
191
+ return observable$.subscribe({
192
+ next (envelope) {
193
+ if (envelope.result.type === 'started') {
194
+ opts.onStarted?.();
195
+ } else if (envelope.result.type === 'stopped') {
196
+ opts.onStopped?.();
197
+ } else {
198
+ opts.onData?.(envelope.result.data);
199
+ }
200
+ },
201
+ error (err) {
202
+ opts.onError?.(err);
203
+ },
204
+ complete () {
205
+ opts.onComplete?.();
206
+ }
207
+ });
306
208
  }
307
- }, {
308
- key: "subscription",
309
- value: function subscription(path, input, opts) {
310
- var observable$ = this.$request({
311
- type: 'subscription',
312
- path: path,
313
- input: input,
314
- context: opts.requestContext
315
- });
316
- return observable$.subscribe({
317
- next: function next(envelope) {
318
- if (envelope.result.type === 'started') {
319
- var _opts$onStarted;
320
-
321
- (_opts$onStarted = opts.onStarted) === null || _opts$onStarted === void 0 ? void 0 : _opts$onStarted.call(opts);
322
- } else if (envelope.result.type === 'stopped') {
323
- var _opts$onStopped;
324
-
325
- (_opts$onStopped = opts.onStopped) === null || _opts$onStopped === void 0 ? void 0 : _opts$onStopped.call(opts);
326
- } else {
327
- var _opts$onData;
328
-
329
- (_opts$onData = opts.onData) === null || _opts$onData === void 0 ? void 0 : _opts$onData.call(opts, envelope.result.data);
330
- }
331
- },
332
- error: function error(err) {
333
- var _opts$onError;
334
-
335
- (_opts$onError = opts.onError) === null || _opts$onError === void 0 ? void 0 : _opts$onError.call(opts, err);
336
- },
337
- complete: function complete() {
338
- var _opts$onComplete;
339
-
340
- (_opts$onComplete = opts.onComplete) === null || _opts$onComplete === void 0 ? void 0 : _opts$onComplete.call(opts);
209
+ constructor(opts){
210
+ const _fetch = getFetch(opts?.fetch);
211
+ const AC = getAbortController(opts?.AbortController);
212
+ this.requestId = 0;
213
+ function getHeadersFn() {
214
+ if (opts.headers) {
215
+ const headers = opts.headers;
216
+ return typeof headers === 'function' ? headers : ()=>headers;
217
+ }
218
+ return ()=>({});
219
+ }
220
+ const transformer = opts.transformer ? 'input' in opts.transformer ? {
221
+ serialize: opts.transformer.input.serialize,
222
+ deserialize: opts.transformer.output.deserialize
223
+ } : opts.transformer : {
224
+ serialize: (data)=>data,
225
+ deserialize: (data)=>data
226
+ };
227
+ this.runtime = {
228
+ AbortController: AC,
229
+ fetch: _fetch,
230
+ headers: getHeadersFn(),
231
+ transformer
232
+ };
233
+ if ('links' in opts) {
234
+ this.links = opts.links.map((link)=>link(this.runtime));
235
+ } else {
236
+ this.links = [
237
+ links_httpBatchLink.httpBatchLink({
238
+ url: opts.url
239
+ })(this.runtime),
240
+ ];
341
241
  }
342
- });
343
242
  }
344
- }]);
345
-
346
- return TRPCClient;
347
- }();
243
+ }
348
244
 
349
- /* eslint-disable @typescript-eslint/no-non-null-assertion */
350
245
  function createTRPCClient(opts) {
351
- var client = new TRPCClient(opts);
352
- return client;
353
- } // Also the client created above needs to somehow be like `TRPCClient<Router> & Omit<Router, 'createCaller' | 'createProcedure' | '_def' | 'transformer' | 'errorFormatter' | 'getErrorShape>`
246
+ const client = new TRPCClient(opts);
247
+ return client;
248
+ }
354
249
 
355
- var clientCallTypeMap = {
356
- query: 'query',
357
- mutate: 'mutation',
358
- subscribe: 'subscription'
250
+ const clientCallTypeMap = {
251
+ query: 'query',
252
+ mutate: 'mutation',
253
+ subscribe: 'subscription'
359
254
  };
360
255
  /**
361
256
  * @deprecated use createTRPCProxyClient instead
362
257
  * @internal
363
- */
364
-
365
- function createTRPCClientProxy(client) {
366
- var proxy = shared.createProxy(function (_ref) {
367
- var _ref2;
368
-
369
- var path = _ref.path,
370
- args = _ref.args;
371
-
372
- var pathCopy = _toConsumableArray__default["default"](path);
373
-
374
- var clientCallType = pathCopy.pop();
375
- var procedureType = clientCallTypeMap[clientCallType];
376
- var fullPath = pathCopy.join('.');
377
- return (_ref2 = client)[procedureType].apply(_ref2, [fullPath].concat(_toConsumableArray__default["default"](args)));
378
- });
379
- return proxy;
258
+ */ function createTRPCClientProxy(client) {
259
+ const proxy = shared.createProxy(({ path , args })=>{
260
+ const pathCopy = [
261
+ ...path
262
+ ];
263
+ const clientCallType = pathCopy.pop();
264
+ const procedureType = clientCallTypeMap[clientCallType];
265
+ const fullPath = pathCopy.join('.');
266
+ return client[procedureType](fullPath, ...args);
267
+ });
268
+ return proxy;
380
269
  }
381
270
  function createTRPCProxyClient(opts) {
382
- var client = createTRPCClient(opts);
383
- var proxy = createTRPCClientProxy(client);
384
- return proxy;
271
+ const client = createTRPCClient(opts);
272
+ const proxy = createTRPCClientProxy(client);
273
+ return proxy;
385
274
  }
386
275
 
387
276
  exports.TRPCClientError = transformResult.TRPCClientError;