@trpc/client 10.0.0-alpha.4 → 10.0.0-alpha.40

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 (174) hide show
  1. package/README.md +58 -1
  2. package/dist/TRPCClientError.d.ts +3 -11
  3. package/dist/TRPCClientError.d.ts.map +1 -1
  4. package/dist/createTRPCClient.d.ts +2 -3
  5. package/dist/createTRPCClient.d.ts.map +1 -1
  6. package/dist/createTRPCClientProxy.d.ts +24 -0
  7. package/dist/createTRPCClientProxy.d.ts.map +1 -0
  8. package/dist/{httpUtils-c09d7b79.mjs → httpUtils-089853f6.mjs} +38 -30
  9. package/dist/{httpUtils-1f9e4e3c.js → httpUtils-a0169eef.js} +38 -29
  10. package/dist/index.d.ts +2 -1
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +240 -102
  13. package/dist/index.mjs +228 -94
  14. package/dist/internals/TRPCClient.d.ts +14 -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/links/dedupeLink.d.ts.map +1 -1
  19. package/dist/links/httpBatchLink.d.ts +5 -2
  20. package/dist/links/httpBatchLink.d.ts.map +1 -1
  21. package/dist/links/httpBatchLink.js +197 -69
  22. package/dist/links/httpBatchLink.mjs +197 -69
  23. package/dist/links/httpLink.d.ts +1 -1
  24. package/dist/links/httpLink.d.ts.map +1 -1
  25. package/dist/links/httpLink.js +5 -5
  26. package/dist/links/httpLink.mjs +5 -5
  27. package/dist/links/index.d.ts +9 -0
  28. package/dist/links/index.d.ts.map +1 -0
  29. package/dist/links/internals/createChain.d.ts.map +1 -1
  30. package/dist/links/internals/httpUtils.d.ts +30 -0
  31. package/dist/links/internals/httpUtils.d.ts.map +1 -0
  32. package/dist/links/internals/transformOperationResult.d.ts +17 -3
  33. package/dist/links/internals/transformOperationResult.d.ts.map +1 -1
  34. package/dist/links/loggerLink.d.ts.map +1 -1
  35. package/dist/links/loggerLink.js +2 -3
  36. package/dist/links/loggerLink.mjs +1 -2
  37. package/dist/links/retryLink.d.ts.map +1 -1
  38. package/dist/links/splitLink.d.ts +1 -1
  39. package/dist/links/splitLink.d.ts.map +1 -1
  40. package/dist/links/splitLink.js +3 -25
  41. package/dist/links/splitLink.mjs +2 -28
  42. package/dist/links/types.d.ts +12 -9
  43. package/dist/links/types.d.ts.map +1 -1
  44. package/dist/links/wsLink.d.ts +3 -3
  45. package/dist/links/wsLink.d.ts.map +1 -1
  46. package/dist/links/wsLink.js +12 -376
  47. package/dist/links/wsLink.mjs +10 -369
  48. package/dist/splitLink-0202cd7b.mjs +54 -0
  49. package/dist/splitLink-23c5ce5c.js +57 -0
  50. package/dist/wsLink-11ee4922.mjs +439 -0
  51. package/dist/wsLink-807f4ad9.js +455 -0
  52. package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.cjs.dev.js +4 -4
  53. package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.cjs.prod.js +4 -4
  54. package/links/httpBatchLink/dist/trpc-client-links-httpBatchLink.esm.js +4 -4
  55. package/links/httpBatchLink/index.d.ts +1 -1
  56. package/links/httpBatchLink/index.js +1 -1
  57. package/links/httpLink/dist/trpc-client-links-httpLink.cjs.dev.js +1 -1
  58. package/links/httpLink/dist/trpc-client-links-httpLink.cjs.prod.js +1 -1
  59. package/links/httpLink/dist/trpc-client-links-httpLink.esm.js +1 -1
  60. package/links/httpLink/index.d.ts +1 -1
  61. package/links/httpLink/index.js +1 -1
  62. package/links/loggerLink/index.d.ts +1 -1
  63. package/links/loggerLink/index.js +1 -1
  64. package/links/splitLink/index.d.ts +1 -1
  65. package/links/splitLink/index.js +1 -1
  66. package/links/wsLink/dist/trpc-client-links-wsLink.cjs.dev.js +3 -1
  67. package/links/wsLink/dist/trpc-client-links-wsLink.cjs.prod.js +3 -1
  68. package/links/wsLink/dist/trpc-client-links-wsLink.esm.js +3 -1
  69. package/links/wsLink/index.d.ts +1 -1
  70. package/links/wsLink/index.js +1 -1
  71. package/package.json +23 -30
  72. package/src/TRPCClientError.ts +5 -13
  73. package/src/createTRPCClient.ts +7 -3
  74. package/src/createTRPCClientProxy.ts +59 -0
  75. package/src/index.ts +2 -1
  76. package/src/internals/TRPCClient.ts +52 -36
  77. package/src/internals/dataLoader.ts +120 -45
  78. package/src/links/dedupeLink.test.ts +1 -1
  79. package/src/links/dedupeLink.ts +6 -7
  80. package/src/links/httpBatchLink.ts +64 -31
  81. package/src/links/httpLink.ts +4 -4
  82. package/src/links/index.ts +8 -0
  83. package/src/links/internals/createChain.test.ts +5 -5
  84. package/src/links/internals/createChain.ts +8 -3
  85. package/src/links/internals/httpUtils.ts +116 -0
  86. package/src/links/internals/transformOperationResult.ts +40 -12
  87. package/src/links/loggerLink.ts +1 -2
  88. package/src/links/retryLink.ts +1 -2
  89. package/src/links/splitLink.test.ts +3 -3
  90. package/src/links/splitLink.ts +5 -5
  91. package/src/links/types.ts +14 -9
  92. package/src/links/wsLink.ts +17 -16
  93. package/dist/TRPCClientError-17b117e1.mjs +0 -81
  94. package/dist/TRPCClientError-ec6bd2ca.js +0 -94
  95. package/dist/createChain-389cc116.mjs +0 -25
  96. package/dist/createChain-864dbb30.js +0 -27
  97. package/dist/internals/transformRPCResponse.d.ts +0 -15
  98. package/dist/internals/transformRPCResponse.d.ts.map +0 -1
  99. package/dist/links/httpUtils.d.ts +0 -22
  100. package/dist/links/httpUtils.d.ts.map +0 -1
  101. package/dist/links/transformerLink.d.ts +0 -4
  102. package/dist/links/transformerLink.d.ts.map +0 -1
  103. package/dist/links/transformerLink.js +0 -75
  104. package/dist/links/transformerLink.mjs +0 -67
  105. package/dist/links/transformerLink.ts.js +0 -1
  106. package/dist/observable-7e66cb56.mjs +0 -102
  107. package/dist/observable-85669100.js +0 -104
  108. package/dist/rx/index.d.ts +0 -3
  109. package/dist/rx/index.d.ts.map +0 -1
  110. package/dist/rx/index.js +0 -9
  111. package/dist/rx/index.mjs +0 -1
  112. package/dist/rx/index.ts.js +0 -1
  113. package/dist/rx/observable.d.ts +0 -4
  114. package/dist/rx/observable.d.ts.map +0 -1
  115. package/dist/rx/operators/index.d.ts +0 -4
  116. package/dist/rx/operators/index.d.ts.map +0 -1
  117. package/dist/rx/operators/index.js +0 -38
  118. package/dist/rx/operators/index.mjs +0 -32
  119. package/dist/rx/operators/index.ts.js +0 -1
  120. package/dist/rx/operators/map.d.ts +0 -3
  121. package/dist/rx/operators/map.d.ts.map +0 -1
  122. package/dist/rx/operators/share.d.ts +0 -6
  123. package/dist/rx/operators/share.d.ts.map +0 -1
  124. package/dist/rx/operators/tap.d.ts +0 -3
  125. package/dist/rx/operators/tap.d.ts.map +0 -1
  126. package/dist/rx/types.d.ts +0 -29
  127. package/dist/rx/types.d.ts.map +0 -1
  128. package/dist/rx/util/identity.d.ts +0 -2
  129. package/dist/rx/util/identity.d.ts.map +0 -1
  130. package/dist/rx/util/observableToPromise.d.ts +0 -10
  131. package/dist/rx/util/observableToPromise.d.ts.map +0 -1
  132. package/dist/rx/util/pipe.d.ts +0 -4
  133. package/dist/rx/util/pipe.d.ts.map +0 -1
  134. package/dist/share-4d79dc49.js +0 -110
  135. package/dist/share-671b63fa.mjs +0 -108
  136. package/dist/tap-538fa07f.mjs +0 -30
  137. package/dist/tap-799a2d33.js +0 -32
  138. package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.d.ts +0 -1
  139. package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.dev.js +0 -67
  140. package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.js +0 -7
  141. package/links/transformerLink/dist/trpc-client-links-transformerLink.cjs.prod.js +0 -67
  142. package/links/transformerLink/dist/trpc-client-links-transformerLink.esm.js +0 -63
  143. package/links/transformerLink/index.d.ts +0 -1
  144. package/links/transformerLink/index.js +0 -1
  145. package/rx/dist/trpc-client-rx.cjs.d.ts +0 -1
  146. package/rx/dist/trpc-client-rx.cjs.dev.js +0 -9
  147. package/rx/dist/trpc-client-rx.cjs.js +0 -7
  148. package/rx/dist/trpc-client-rx.cjs.prod.js +0 -9
  149. package/rx/dist/trpc-client-rx.esm.js +0 -1
  150. package/rx/index.d.ts +0 -1
  151. package/rx/index.js +0 -1
  152. package/rx/operators/dist/trpc-client-rx-operators.cjs.d.ts +0 -1
  153. package/rx/operators/dist/trpc-client-rx-operators.cjs.dev.js +0 -39
  154. package/rx/operators/dist/trpc-client-rx-operators.cjs.js +0 -7
  155. package/rx/operators/dist/trpc-client-rx-operators.cjs.prod.js +0 -39
  156. package/rx/operators/dist/trpc-client-rx-operators.esm.js +0 -33
  157. package/rx/operators/index.d.ts +0 -1
  158. package/rx/operators/index.js +0 -1
  159. package/src/internals/transformRPCResponse.ts +0 -25
  160. package/src/links/httpUtils.ts +0 -94
  161. package/src/links/transformerLink.ts +0 -70
  162. package/src/rx/index.ts +0 -2
  163. package/src/rx/observable.test.ts +0 -83
  164. package/src/rx/observable.ts +0 -70
  165. package/src/rx/operators/index.ts +0 -3
  166. package/src/rx/operators/map.test.ts +0 -92
  167. package/src/rx/operators/map.ts +0 -25
  168. package/src/rx/operators/share.test.ts +0 -60
  169. package/src/rx/operators/share.ts +0 -67
  170. package/src/rx/operators/tap.ts +0 -26
  171. package/src/rx/types.ts +0 -69
  172. package/src/rx/util/identity.ts +0 -3
  173. package/src/rx/util/observableToPromise.ts +0 -49
  174. package/src/rx/util/pipe.ts +0 -22
@@ -1,63 +0,0 @@
1
- import _objectSpread from '@babel/runtime/helpers/esm/objectSpread2';
2
- import { o as observable } from '../../../dist/observable-01534ea8.esm.js';
3
-
4
- /**
5
- * @internal
6
- */
7
- function transformOperationResult(result, transformer) {
8
- if ('error' in result.data) {
9
- return _objectSpread(_objectSpread({}, result), {}, {
10
- data: _objectSpread(_objectSpread({}, result.data), {}, {
11
- error: transformer.deserialize(result.data.error)
12
- })
13
- });
14
- }
15
-
16
- if (result.data.result.type !== 'data') {
17
- return result;
18
- }
19
-
20
- return _objectSpread(_objectSpread({}, result), {}, {
21
- data: _objectSpread(_objectSpread({}, result.data), {}, {
22
- result: _objectSpread(_objectSpread({}, result.data.result), {}, {
23
- data: transformer.deserialize(result.data.result.data)
24
- })
25
- })
26
- });
27
- }
28
-
29
- function transformerLink(transformer) {
30
- var _transformer = transformer ? 'input' in transformer ? {
31
- serialize: transformer.input.serialize,
32
- deserialize: transformer.output.deserialize
33
- } : transformer : {
34
- serialize: function serialize(data) {
35
- return data;
36
- },
37
- deserialize: function deserialize(data) {
38
- return data;
39
- }
40
- };
41
-
42
- return function () {
43
- return function (props) {
44
- var input = _transformer.serialize(props.op.input);
45
-
46
- return observable(function (observer) {
47
- var next$ = props.next(_objectSpread(_objectSpread({}, props.op), {}, {
48
- input: input
49
- })).subscribe({
50
- next: function next(value) {
51
- var transformed = transformOperationResult(value, _transformer);
52
- observer.next(transformed);
53
- },
54
- error: observer.error,
55
- complete: observer.complete
56
- });
57
- return next$;
58
- });
59
- };
60
- };
61
- }
62
-
63
- export { transformerLink };
@@ -1 +0,0 @@
1
- export * from '../../dist/links/transformerLink';
@@ -1 +0,0 @@
1
- module.exports = require('../../dist/links/transformerLink');
@@ -1 +0,0 @@
1
- export * from "../../dist/declarations/src/rx/index";
@@ -1,9 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var observable = require('../../dist/observable-9aad8c3e.cjs.dev.js');
6
-
7
-
8
-
9
- exports.observable = observable.observable;
@@ -1,7 +0,0 @@
1
- 'use strict';
2
-
3
- if (process.env.NODE_ENV === "production") {
4
- module.exports = require("./trpc-client-rx.cjs.prod.js");
5
- } else {
6
- module.exports = require("./trpc-client-rx.cjs.dev.js");
7
- }
@@ -1,9 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var observable = require('../../dist/observable-ccb54234.cjs.prod.js');
6
-
7
-
8
-
9
- exports.observable = observable.observable;
@@ -1 +0,0 @@
1
- export { o as observable } from '../../dist/observable-01534ea8.esm.js';
package/rx/index.d.ts DELETED
@@ -1 +0,0 @@
1
- export * from '../dist/rx';
package/rx/index.js DELETED
@@ -1 +0,0 @@
1
- module.exports = require('../dist/rx/index');
@@ -1 +0,0 @@
1
- export * from "../../../dist/declarations/src/rx/operators/index";
@@ -1,39 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var share = require('../../../dist/share-f2ac9332.cjs.dev.js');
6
- var tap = require('../../../dist/tap-2d565250.cjs.dev.js');
7
- require('@babel/runtime/helpers/createForOfIteratorHelper');
8
-
9
- function map(project) {
10
- return function (originalObserver) {
11
- return {
12
- subscribe: function subscribe(observer) {
13
- var index = 0;
14
- var subscription = originalObserver.subscribe({
15
- next: function next(value) {
16
- var _observer$next;
17
-
18
- (_observer$next = observer.next) === null || _observer$next === void 0 ? void 0 : _observer$next.call(observer, project(value, index++));
19
- },
20
- error: function error(_error) {
21
- var _observer$error;
22
-
23
- (_observer$error = observer.error) === null || _observer$error === void 0 ? void 0 : _observer$error.call(observer, _error);
24
- },
25
- complete: function complete() {
26
- var _observer$complete;
27
-
28
- (_observer$complete = observer.complete) === null || _observer$complete === void 0 ? void 0 : _observer$complete.call(observer);
29
- }
30
- });
31
- return subscription;
32
- }
33
- };
34
- };
35
- }
36
-
37
- exports.share = share.share;
38
- exports.tap = tap.tap;
39
- exports.map = map;
@@ -1,7 +0,0 @@
1
- 'use strict';
2
-
3
- if (process.env.NODE_ENV === "production") {
4
- module.exports = require("./trpc-client-rx-operators.cjs.prod.js");
5
- } else {
6
- module.exports = require("./trpc-client-rx-operators.cjs.dev.js");
7
- }
@@ -1,39 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var share = require('../../../dist/share-5a8c2543.cjs.prod.js');
6
- var tap = require('../../../dist/tap-d907998d.cjs.prod.js');
7
- require('@babel/runtime/helpers/createForOfIteratorHelper');
8
-
9
- function map(project) {
10
- return function (originalObserver) {
11
- return {
12
- subscribe: function subscribe(observer) {
13
- var index = 0;
14
- var subscription = originalObserver.subscribe({
15
- next: function next(value) {
16
- var _observer$next;
17
-
18
- (_observer$next = observer.next) === null || _observer$next === void 0 ? void 0 : _observer$next.call(observer, project(value, index++));
19
- },
20
- error: function error(_error) {
21
- var _observer$error;
22
-
23
- (_observer$error = observer.error) === null || _observer$error === void 0 ? void 0 : _observer$error.call(observer, _error);
24
- },
25
- complete: function complete() {
26
- var _observer$complete;
27
-
28
- (_observer$complete = observer.complete) === null || _observer$complete === void 0 ? void 0 : _observer$complete.call(observer);
29
- }
30
- });
31
- return subscription;
32
- }
33
- };
34
- };
35
- }
36
-
37
- exports.share = share.share;
38
- exports.tap = tap.tap;
39
- exports.map = map;
@@ -1,33 +0,0 @@
1
- export { s as share } from '../../../dist/share-ca585761.esm.js';
2
- export { t as tap } from '../../../dist/tap-606957cb.esm.js';
3
- import '@babel/runtime/helpers/createForOfIteratorHelper';
4
-
5
- function map(project) {
6
- return function (originalObserver) {
7
- return {
8
- subscribe: function subscribe(observer) {
9
- var index = 0;
10
- var subscription = originalObserver.subscribe({
11
- next: function next(value) {
12
- var _observer$next;
13
-
14
- (_observer$next = observer.next) === null || _observer$next === void 0 ? void 0 : _observer$next.call(observer, project(value, index++));
15
- },
16
- error: function error(_error) {
17
- var _observer$error;
18
-
19
- (_observer$error = observer.error) === null || _observer$error === void 0 ? void 0 : _observer$error.call(observer, _error);
20
- },
21
- complete: function complete() {
22
- var _observer$complete;
23
-
24
- (_observer$complete = observer.complete) === null || _observer$complete === void 0 ? void 0 : _observer$complete.call(observer);
25
- }
26
- });
27
- return subscription;
28
- }
29
- };
30
- };
31
- }
32
-
33
- export { map };
@@ -1 +0,0 @@
1
- export * from '../../dist/rx/operators';
@@ -1 +0,0 @@
1
- module.exports = require('../../dist/rx/operators/index');
@@ -1,25 +0,0 @@
1
- import { TRPCResponse } from '@trpc/server/rpc';
2
- import { TRPCClientError } from '../TRPCClientError';
3
- import { LinkRuntime } from '../links/types';
4
-
5
- export function transformRPCResponse({
6
- envelope,
7
- runtime,
8
- }: {
9
- envelope: TRPCResponse;
10
- runtime: LinkRuntime;
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
- }
@@ -1,94 +0,0 @@
1
- import { ProcedureType } from '@trpc/server';
2
- import { LinkRuntime, PromiseAndCancel } from './types';
3
- export interface HTTPLinkOptions {
4
- url: string;
5
- }
6
-
7
- // https://github.com/trpc/trpc/pull/669
8
- function arrayToDict(array: unknown[]) {
9
- const dict: Record<number, unknown> = {};
10
- for (let index = 0; index < array.length; index++) {
11
- const element = array[index];
12
- dict[index] = element;
13
- }
14
- return dict;
15
- }
16
- const METHOD = {
17
- query: 'GET',
18
- mutation: 'POST',
19
- subscription: 'PATCH',
20
- } as const;
21
-
22
- export interface ResponseShape {
23
- json: unknown;
24
- meta: {
25
- response: Response;
26
- };
27
- }
28
- export function httpRequest(
29
- props: {
30
- runtime: LinkRuntime;
31
- type: ProcedureType;
32
- path: string;
33
- url: string;
34
- } & ({ inputs: unknown[] } | { input: unknown }),
35
- ): PromiseAndCancel<ResponseShape> {
36
- const { type, runtime: rt, path } = props;
37
- const ac = rt.AbortController ? new rt.AbortController() : null;
38
-
39
- const input = 'input' in props ? props.input : arrayToDict(props.inputs);
40
-
41
- function getUrl() {
42
- let url = props.url + '/' + path;
43
- const queryParts: string[] = [];
44
- if ('inputs' in props) {
45
- queryParts.push('batch=1');
46
- }
47
- if (type === 'query' && input !== undefined) {
48
- queryParts.push(`input=${encodeURIComponent(JSON.stringify(input))}`);
49
- }
50
- if (queryParts.length) {
51
- url += '?' + queryParts.join('&');
52
- }
53
- return url;
54
- }
55
- function getBody() {
56
- if (type === 'query') {
57
- return undefined;
58
- }
59
- return input !== undefined ? JSON.stringify(input) : undefined;
60
- }
61
-
62
- const promise = new Promise<ResponseShape>((resolve, reject) => {
63
- const url = getUrl();
64
-
65
- const meta = {} as any as ResponseShape['meta'];
66
- Promise.resolve(rt.headers())
67
- .then((headers) =>
68
- rt.fetch(url, {
69
- method: METHOD[type],
70
- signal: ac?.signal,
71
- body: getBody(),
72
- headers: {
73
- 'content-type': 'application/json',
74
- ...headers,
75
- },
76
- }),
77
- )
78
- .then((_res) => {
79
- meta.response = _res;
80
- return _res.json();
81
- })
82
- .then((json) => {
83
- resolve({
84
- json,
85
- meta,
86
- });
87
- })
88
- .catch(reject);
89
- });
90
- const cancel = () => {
91
- ac?.abort();
92
- };
93
- return { promise, cancel };
94
- }
@@ -1,70 +0,0 @@
1
- import {
2
- AnyRouter,
3
- ClientDataTransformerOptions,
4
- DataTransformer,
5
- } from '@trpc/server';
6
- import { observable } from '../rx/observable';
7
- import { OperationResult, TRPCLink } from './types';
8
-
9
- /**
10
- * @internal
11
- */
12
- function transformOperationResult<TResult extends OperationResult<any, any>>(
13
- result: TResult,
14
- transformer: DataTransformer,
15
- ) {
16
- if ('error' in result.data) {
17
- return {
18
- ...result,
19
- data: {
20
- ...result.data,
21
- error: transformer.deserialize(result.data.error),
22
- },
23
- };
24
- }
25
- if (result.data.result.type !== 'data') {
26
- return result;
27
- }
28
- return {
29
- ...result,
30
- data: {
31
- ...result.data,
32
- result: {
33
- ...result.data.result,
34
- data: transformer.deserialize(result.data.result.data),
35
- },
36
- },
37
- };
38
- }
39
- export function transformerLink<TRouter extends AnyRouter = AnyRouter>(
40
- transformer: ClientDataTransformerOptions,
41
- ): TRPCLink<TRouter> {
42
- const _transformer: DataTransformer = transformer
43
- ? 'input' in transformer
44
- ? {
45
- serialize: transformer.input.serialize,
46
- deserialize: transformer.output.deserialize,
47
- }
48
- : transformer
49
- : {
50
- serialize: (data) => data,
51
- deserialize: (data) => data,
52
- };
53
-
54
- return () => {
55
- return (props) => {
56
- const input = _transformer.serialize(props.op.input);
57
- return observable((observer) => {
58
- const next$ = props.next({ ...props.op, input }).subscribe({
59
- next(value) {
60
- const transformed = transformOperationResult(value, _transformer);
61
- observer.next(transformed);
62
- },
63
- error: observer.error,
64
- complete: observer.complete,
65
- });
66
- return next$;
67
- });
68
- };
69
- };
70
- }
package/src/rx/index.ts DELETED
@@ -1,2 +0,0 @@
1
- export * from './observable';
2
- export * from './types';
@@ -1,83 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-empty-function */
2
- import { observable } from './observable';
3
- import { tap } from './operators';
4
- import { share } from './operators/share';
5
-
6
- test('vanilla observable', () => {
7
- const obs = observable<number, Error>((observer) => {
8
- observer.next(1);
9
- observer.complete();
10
- });
11
-
12
- const next = jest.fn();
13
- const error = jest.fn();
14
- const complete = jest.fn();
15
- obs.subscribe({
16
- next,
17
- error,
18
- complete,
19
- });
20
- expect(next.mock.calls).toHaveLength(1);
21
- expect(complete.mock.calls).toHaveLength(1);
22
- expect(error.mock.calls).toHaveLength(0);
23
- expect(next.mock.calls[0][0]).toBe(1);
24
- });
25
-
26
- test('pipe - combine operators', () => {
27
- const taps = {
28
- next: jest.fn(),
29
- complete: jest.fn(),
30
- error: jest.fn(),
31
- };
32
- const obs = observable<number, Error>((observer) => {
33
- observer.next(1);
34
- }).pipe(
35
- // operators:
36
- share(),
37
- tap(taps),
38
- );
39
- {
40
- const next = jest.fn();
41
- const error = jest.fn();
42
- const complete = jest.fn();
43
- obs.subscribe({
44
- next,
45
- error,
46
- complete,
47
- });
48
- expect(next.mock.calls).toHaveLength(1);
49
- expect(complete.mock.calls).toHaveLength(0);
50
- expect(error.mock.calls).toHaveLength(0);
51
- expect(next.mock.calls[0][0]).toBe(1);
52
- }
53
-
54
- {
55
- const next = jest.fn();
56
- const error = jest.fn();
57
- const complete = jest.fn();
58
- obs.subscribe({
59
- next,
60
- error,
61
- complete,
62
- });
63
- expect(next.mock.calls).toHaveLength(0);
64
- expect(complete.mock.calls).toHaveLength(0);
65
- expect(error.mock.calls).toHaveLength(0);
66
- }
67
-
68
- expect({
69
- next: taps.next.mock.calls,
70
- error: taps.error.mock.calls,
71
- complete: taps.complete.mock.calls,
72
- }).toMatchInlineSnapshot(`
73
- Object {
74
- "complete": Array [],
75
- "error": Array [],
76
- "next": Array [
77
- Array [
78
- 1,
79
- ],
80
- ],
81
- }
82
- `);
83
- });
@@ -1,70 +0,0 @@
1
- import { pipeFromArray } from './util/pipe';
2
- import { Observable, Observer, OperatorFunction, TeardownLogic } from './types';
3
-
4
- export type inferObservableValue<TObservable extends Observable<any, any>> =
5
- TObservable extends Observable<infer TValue, any> ? TValue : never;
6
-
7
- export function observable<TValue, TError = unknown>(
8
- subscribe: (observer: Observer<TValue, TError>) => TeardownLogic,
9
- ): Observable<TValue, TError> {
10
- const self: Observable<TValue, TError> = {
11
- subscribe(observer) {
12
- let teardownRef: TeardownLogic | null = null;
13
- let isDone = false;
14
- let unsubscribed = false;
15
- let teardownImmediately = false;
16
- function unsubscribe() {
17
- if (teardownRef === null) {
18
- teardownImmediately = true;
19
- return;
20
- }
21
- if (unsubscribed) {
22
- return;
23
- }
24
- unsubscribed = true;
25
-
26
- if (typeof teardownRef === 'function') {
27
- teardownRef();
28
- } else if (teardownRef) {
29
- teardownRef.unsubscribe();
30
- }
31
- }
32
- teardownRef = subscribe({
33
- next(value) {
34
- if (isDone) {
35
- return;
36
- }
37
- observer.next?.(value);
38
- },
39
- error(err) {
40
- if (isDone) {
41
- return;
42
- }
43
- isDone = true;
44
- observer.error?.(err);
45
- unsubscribe();
46
- },
47
- complete() {
48
- if (isDone) {
49
- return;
50
- }
51
- isDone = true;
52
- observer.complete?.();
53
- unsubscribe();
54
- },
55
- });
56
- if (teardownImmediately) {
57
- unsubscribe();
58
- }
59
- return {
60
- unsubscribe,
61
- };
62
- },
63
- pipe(
64
- ...operations: OperatorFunction<any, any, any, any>[]
65
- ): Observable<any, any> {
66
- return pipeFromArray(operations)(self) as any;
67
- },
68
- };
69
- return self;
70
- }
@@ -1,3 +0,0 @@
1
- export * from './share';
2
- export * from './map';
3
- export * from './tap';
@@ -1,92 +0,0 @@
1
- import { expectTypeOf } from 'expect-type';
2
- import { observable } from '../observable';
3
- import { map } from '.';
4
- import { EventEmitter } from 'events';
5
-
6
- interface SubscriptionEvents<TOutput> {
7
- data: (data: TOutput) => void;
8
- }
9
-
10
- declare interface CustomEventEmitter<TOutput> {
11
- on<U extends keyof SubscriptionEvents<TOutput>>(
12
- event: U,
13
- listener: SubscriptionEvents<TOutput>[U],
14
- ): this;
15
-
16
- once<U extends keyof SubscriptionEvents<TOutput>>(
17
- event: U,
18
- listener: SubscriptionEvents<TOutput>[U],
19
- ): this;
20
-
21
- emit<U extends keyof SubscriptionEvents<TOutput>>(
22
- event: U,
23
- ...args: Parameters<SubscriptionEvents<TOutput>[U]>
24
- ): boolean;
25
- }
26
- class CustomEventEmitter<TOutput>
27
- extends EventEmitter
28
- implements CustomEventEmitter<TOutput> {}
29
- test('map', () => {
30
- type EventShape = { num: number };
31
- const ee = new CustomEventEmitter<EventShape>();
32
- const eventObservable = observable<EventShape, unknown>((observer) => {
33
- const callback = (data: EventShape) => {
34
- observer.next(data);
35
- };
36
- ee.on('data', callback);
37
-
38
- return () => {
39
- ee.off('data', callback);
40
- };
41
- });
42
- const pipeCalls = jest.fn();
43
- const piped = eventObservable.pipe(
44
- map((...args) => {
45
- pipeCalls(...args);
46
- const [value] = args;
47
- return value.num;
48
- }),
49
- );
50
-
51
- const next = jest.fn();
52
- const subscription = piped.subscribe({
53
- next(value) {
54
- expectTypeOf<number>(value);
55
- next(value);
56
- },
57
- });
58
- expect(next).not.toHaveBeenCalled();
59
- ee.emit('data', { num: 1 });
60
- ee.emit('data', { num: 2 });
61
- expect(next).toHaveBeenCalledTimes(2);
62
- expect(next.mock.calls).toMatchInlineSnapshot(`
63
- Array [
64
- Array [
65
- 1,
66
- ],
67
- Array [
68
- 2,
69
- ],
70
- ]
71
- `);
72
- expect(pipeCalls.mock.calls).toMatchInlineSnapshot(`
73
- Array [
74
- Array [
75
- Object {
76
- "num": 1,
77
- },
78
- 0,
79
- ],
80
- Array [
81
- Object {
82
- "num": 2,
83
- },
84
- 1,
85
- ],
86
- ]
87
- `);
88
-
89
- expect(ee.listeners('data')).toHaveLength(1);
90
- subscription.unsubscribe();
91
- expect(ee.listeners('data')).toHaveLength(0);
92
- });