1.13.1 • Published 5 years ago

webiny-compose v1.13.1

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

webiny-compose

This library is used to compose multiple functions into a connect-style middleware with one difference: it is ONLY intended to be used with arbitrary data.

Often we need to create a queue of functions to process an input, and we need to be able to control the flow. Usually we would use a publish/subscribe pattern, but it is not often straightforward and not easily controllable due to the nature of events, propagation, and order of events.

Middleware lets us specify the exact order of execution which is especially useful in configurations where you DO have control over your code.

How to use

import compose from 'webiny-compose';

const middleware = compose([
    /**
    * @param {mixed} params Arbitrary data sent to the middleware 
    * @param {Function} next Execute next function in middleware queue 
    * @param {Function} finish Finish middleware execution and return the data passed as parameter from the middleware 
    */
    (params, next, finish) => {
        // Do something with the params
        params.key = 10;
        // Execute next function
        next();
    },
    (params, next, finish) => {
        if (params.key > 0) {
            // Finish execution and return data
            finish({success: true});
            return;
        }
        next();
    },
    (params, next) => {
        // This function will only be executed if `next` was called in the previous function
        params.optional = 'value';
        next();
    }
]);

// Now execute the middleware
const params = {};
middleware(params).then(result => {
    if (!result) {
        // `finish` was not called
        // `params.optional` will equal to `value` since `params` is modified by reference
    } else {
        // `finish` was called
        // `result.success` will equal to `true`     
    }
});
1.13.1

5 years ago

1.13.0

5 years ago

1.12.7

5 years ago

1.12.6

5 years ago

1.12.5

5 years ago

1.12.4

5 years ago

1.12.3

5 years ago

1.12.2

5 years ago

1.12.1

5 years ago

1.12.0

5 years ago

1.11.0

5 years ago

1.10.5

5 years ago

1.10.4

5 years ago

1.10.3

5 years ago

1.10.2

5 years ago

1.10.1

5 years ago

1.10.0

5 years ago

1.9.0

5 years ago

1.8.1

5 years ago

1.8.0

5 years ago

1.7.0

5 years ago

1.6.0

5 years ago

1.5.1

5 years ago

1.5.0

5 years ago

1.4.0

5 years ago

1.3.0

6 years ago

1.2.0

6 years ago

1.1.0

6 years ago

1.0.0

6 years ago