) => any
14 | ) {
15 | return function createProxy(name?: string): P {
16 | const tasks = [];
17 | const handler = function (payload?) {
18 | const debugInfo = []
19 | const nextPayload = tasks.reduce((currentPayload, task, index) => {
20 | const populatedTask = {
21 | ...task, context: this, debug(details) {
22 | debugInfo[index].details = details
23 | }
24 | }
25 | debugInfo.push({
26 | type: populatedTask.type,
27 | details: null
28 | })
29 | if (currentPayload instanceof Promise) {
30 | return currentPayload.then(
31 | (promisedPayload) => (reducer ? reducer(promisedPayload, populatedTask) : promisedPayload)
32 | );
33 | }
34 |
35 | return reducer ? reducer(currentPayload, populatedTask) : currentPayload;
36 | }, payload);
37 |
38 | return nextPayload instanceof Promise ? nextPayload : Promise.resolve(nextPayload)
39 | .then((returnValue) => {
40 | if (!name) {
41 | return returnValue
42 | }
43 | console.groupCollapsed('### ' + name + ' ###')
44 | debugInfo.forEach(info => {
45 | console.log(info.type, info.details)
46 | })
47 | console.groupEnd()
48 | return returnValue
49 | })
50 | };
51 | const proxy = new Proxy(handler, {
52 | get(_, property) {
53 | return (...args) => {
54 | tasks.push({
55 | type: property,
56 | args
57 | });
58 | return proxy;
59 | };
60 | }
61 | });
62 |
63 | return proxy as P;
64 | };
65 | }
66 |
--------------------------------------------------------------------------------
/tests/index.ts:
--------------------------------------------------------------------------------
1 | import { ProxyChain, IProxyChain } from '../src/index';
2 | import { expect } from 'chai';
3 | import 'mocha';
4 |
5 | describe('ProxyChain', () => {
6 | it('should run', () => {
7 | expect(() => ProxyChain(() => { })).to.not.throw();
8 | });
9 | it('should return promise when run', () => {
10 | const chain = ProxyChain(() => { });
11 | const doThis = chain();
12 | expect(doThis()).to.be.instanceof(Promise);
13 | });
14 | it('should proxy any method', () => {
15 | interface AppChain extends IProxyChain {
16 | set: () => AppChain;
17 | foo: () => AppChain;
18 | bar: () => AppChain;
19 | }
20 | const chain = ProxyChain(() => { });
21 |
22 | expect(() => chain().set()).to.not.throw();
23 | expect(() => chain().foo()).to.not.throw();
24 | expect(() => chain().bar()).to.not.throw();
25 | });
26 | it('should return passed payload', () => {
27 | interface AppChain extends IProxyChain
{
28 | set: () => AppChain;
29 | }
30 | const chain = ProxyChain();
31 | const doThis = chain>().set();
32 |
33 | return doThis('foo').then((value) => expect(value).to.be.equal('foo'));
34 | });
35 | it('should allow overriding payload', () => {
36 | interface ProxyChain extends IProxyChain {
37 | map: (cb: (p: O) => P) => ProxyChain;
38 | }
39 | const chain = ProxyChain>((payload, operator) => {
40 | switch (operator.type) {
41 | case 'map':
42 | return operator.args[0](payload);
43 | }
44 |
45 | return payload;
46 | });
47 | const doThis = chain>().map((p) => p.toUpperCase());
48 |
49 | return doThis('foo').then((value) => expect(value).to.be.equal('FOO'));
50 | });
51 | it('should handle promise results', () => {
52 | interface ProxyChain extends IProxyChain {
53 | map: