@trpc/client 10.0.0-alpha.45 → 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 (60) hide show
  1. package/dist/httpUtils-44781613.mjs +76 -0
  2. package/dist/httpUtils-801112a6.js +79 -0
  3. package/dist/index.js +226 -337
  4. package/dist/index.mjs +228 -333
  5. package/dist/internals/TRPCClient.d.ts +3 -1
  6. package/dist/internals/TRPCClient.d.ts.map +1 -1
  7. package/dist/internals/dataLoader.d.ts.map +1 -1
  8. package/dist/internals/fetchHelpers.d.ts.map +1 -1
  9. package/dist/internals/retryDelay.d.ts.map +1 -1
  10. package/dist/links/dedupeLink.d.ts.map +1 -1
  11. package/dist/links/httpBatchLink.d.ts.map +1 -1
  12. package/dist/links/httpBatchLink.js +182 -304
  13. package/dist/links/httpBatchLink.mjs +182 -304
  14. package/dist/links/httpLink.d.ts.map +1 -1
  15. package/dist/links/httpLink.js +30 -53
  16. package/dist/links/httpLink.mjs +30 -53
  17. package/dist/links/index.d.ts.map +1 -1
  18. package/dist/links/internals/createChain.d.ts.map +1 -1
  19. package/dist/links/internals/httpUtils.d.ts.map +1 -1
  20. package/dist/links/internals/transformResult.d.ts.map +1 -1
  21. package/dist/links/loggerLink.d.ts.map +1 -1
  22. package/dist/links/loggerLink.js +89 -94
  23. package/dist/links/loggerLink.mjs +89 -89
  24. package/dist/links/retryLink.d.ts.map +1 -1
  25. package/dist/links/splitLink.d.ts.map +1 -1
  26. package/dist/links/splitLink.js +1 -1
  27. package/dist/links/splitLink.mjs +1 -1
  28. package/dist/links/types.d.ts.map +1 -1
  29. package/dist/links/wsLink.d.ts.map +1 -1
  30. package/dist/links/wsLink.js +248 -379
  31. package/dist/links/wsLink.mjs +248 -368
  32. package/dist/splitLink-695ae288.js +48 -0
  33. package/dist/splitLink-ad44a55d.mjs +45 -0
  34. package/dist/transformResult-106791d7.mjs +62 -0
  35. package/dist/transformResult-e15ccdf6.js +65 -0
  36. package/links/httpBatchLink/index.d.ts +1 -1
  37. package/links/httpBatchLink/index.js +1 -1
  38. package/links/httpLink/index.d.ts +1 -1
  39. package/links/httpLink/index.js +1 -1
  40. package/links/loggerLink/index.d.ts +1 -1
  41. package/links/loggerLink/index.js +1 -1
  42. package/links/splitLink/index.d.ts +1 -1
  43. package/links/splitLink/index.js +1 -1
  44. package/links/wsLink/index.d.ts +1 -1
  45. package/links/wsLink/index.js +1 -1
  46. package/package.json +7 -11
  47. package/src/internals/TRPCClient.ts +10 -9
  48. package/src/links/wsLink.ts +1 -1
  49. package/dist/httpUtils-089853f6.mjs +0 -99
  50. package/dist/httpUtils-a0169eef.js +0 -106
  51. package/dist/index.ts.js +0 -1
  52. package/dist/links/httpBatchLink.ts.js +0 -1
  53. package/dist/links/httpLink.ts.js +0 -1
  54. package/dist/links/loggerLink.ts.js +0 -1
  55. package/dist/links/splitLink.ts.js +0 -1
  56. package/dist/links/wsLink.ts.js +0 -1
  57. package/dist/splitLink-0202cd7b.mjs +0 -54
  58. package/dist/splitLink-23c5ce5c.js +0 -57
  59. package/dist/transformResult-06bec648.mjs +0 -108
  60. package/dist/transformResult-3ccc74ea.js +0 -122
@@ -0,0 +1,76 @@
1
+ // https://github.com/trpc/trpc/pull/669
2
+ function arrayToDict(array) {
3
+ const dict = {};
4
+ for(let index = 0; index < array.length; index++){
5
+ const element = array[index];
6
+ dict[index] = element;
7
+ }
8
+ return dict;
9
+ }
10
+ const METHOD = {
11
+ query: 'GET',
12
+ mutation: 'POST',
13
+ subscription: 'PATCH'
14
+ };
15
+ function getInput(opts) {
16
+ return 'input' in opts ? opts.runtime.transformer.serialize(opts.input) : arrayToDict(opts.inputs.map((_input)=>opts.runtime.transformer.serialize(_input)));
17
+ }
18
+ function getUrl(opts) {
19
+ let url = opts.url + '/' + opts.path;
20
+ const queryParts = [];
21
+ if ('inputs' in opts) {
22
+ queryParts.push('batch=1');
23
+ }
24
+ if (opts.type === 'query') {
25
+ const input = getInput(opts);
26
+ if (input !== undefined) {
27
+ queryParts.push(`input=${encodeURIComponent(JSON.stringify(input))}`);
28
+ }
29
+ }
30
+ if (queryParts.length) {
31
+ url += '?' + queryParts.join('&');
32
+ }
33
+ return url;
34
+ }
35
+ function getBody(opts) {
36
+ if (opts.type === 'query') {
37
+ return undefined;
38
+ }
39
+ const input = getInput(opts);
40
+ return input !== undefined ? JSON.stringify(input) : undefined;
41
+ }
42
+ function httpRequest(opts) {
43
+ const { type , runtime } = opts;
44
+ const ac = runtime.AbortController ? new runtime.AbortController() : null;
45
+ const promise = new Promise((resolve, reject)=>{
46
+ const url = getUrl(opts);
47
+ const body = getBody(opts);
48
+ const meta = {};
49
+ Promise.resolve(runtime.headers()).then((headers)=>runtime.fetch(url, {
50
+ method: METHOD[type],
51
+ signal: ac?.signal,
52
+ body: body,
53
+ headers: {
54
+ 'content-type': 'application/json',
55
+ ...headers
56
+ }
57
+ })).then((_res)=>{
58
+ meta.response = _res;
59
+ return _res.json();
60
+ }).then((json)=>{
61
+ resolve({
62
+ json,
63
+ meta
64
+ });
65
+ }).catch(reject);
66
+ });
67
+ const cancel = ()=>{
68
+ ac?.abort();
69
+ };
70
+ return {
71
+ promise,
72
+ cancel
73
+ };
74
+ }
75
+
76
+ export { getUrl as g, httpRequest as h };
@@ -0,0 +1,79 @@
1
+ 'use strict';
2
+
3
+ // https://github.com/trpc/trpc/pull/669
4
+ function arrayToDict(array) {
5
+ const dict = {};
6
+ for(let index = 0; index < array.length; index++){
7
+ const element = array[index];
8
+ dict[index] = element;
9
+ }
10
+ return dict;
11
+ }
12
+ const METHOD = {
13
+ query: 'GET',
14
+ mutation: 'POST',
15
+ subscription: 'PATCH'
16
+ };
17
+ function getInput(opts) {
18
+ return 'input' in opts ? opts.runtime.transformer.serialize(opts.input) : arrayToDict(opts.inputs.map((_input)=>opts.runtime.transformer.serialize(_input)));
19
+ }
20
+ function getUrl(opts) {
21
+ let url = opts.url + '/' + opts.path;
22
+ const queryParts = [];
23
+ if ('inputs' in opts) {
24
+ queryParts.push('batch=1');
25
+ }
26
+ if (opts.type === 'query') {
27
+ const input = getInput(opts);
28
+ if (input !== undefined) {
29
+ queryParts.push(`input=${encodeURIComponent(JSON.stringify(input))}`);
30
+ }
31
+ }
32
+ if (queryParts.length) {
33
+ url += '?' + queryParts.join('&');
34
+ }
35
+ return url;
36
+ }
37
+ function getBody(opts) {
38
+ if (opts.type === 'query') {
39
+ return undefined;
40
+ }
41
+ const input = getInput(opts);
42
+ return input !== undefined ? JSON.stringify(input) : undefined;
43
+ }
44
+ function httpRequest(opts) {
45
+ const { type , runtime } = opts;
46
+ const ac = runtime.AbortController ? new runtime.AbortController() : null;
47
+ const promise = new Promise((resolve, reject)=>{
48
+ const url = getUrl(opts);
49
+ const body = getBody(opts);
50
+ const meta = {};
51
+ Promise.resolve(runtime.headers()).then((headers)=>runtime.fetch(url, {
52
+ method: METHOD[type],
53
+ signal: ac?.signal,
54
+ body: body,
55
+ headers: {
56
+ 'content-type': 'application/json',
57
+ ...headers
58
+ }
59
+ })).then((_res)=>{
60
+ meta.response = _res;
61
+ return _res.json();
62
+ }).then((json)=>{
63
+ resolve({
64
+ json,
65
+ meta
66
+ });
67
+ }).catch(reject);
68
+ });
69
+ const cancel = ()=>{
70
+ ac?.abort();
71
+ };
72
+ return {
73
+ promise,
74
+ cancel
75
+ };
76
+ }
77
+
78
+ exports.getUrl = getUrl;
79
+ exports.httpRequest = httpRequest;