Nuclear Powered Marshmallows

    @financial-times/n-express-enhancer

    1.8.1 • Public • Published

    n-express-enhancer

    toolsets to create javascript function enhancers (decorators but more powerful and robust)

    npm version npm download node version

    CircleCI Coverage Status Known Vulnerabilities Scrutinizer Code Quality Dependencies devDependencies



    Install

    npm install @financial-times/n-express-enhancer -D

    Create Enhancers

    upgrade a decorator to an enhancer with createEnhancer

    import { createEnhancer } from '@financial-times/n-express-enhancer';
    
    // create a decorator with currying
    const decorator = targetFunction => (...args) => {
      // ...enhancement
      return targetFunction(...args);
    }
    
    export default createEnhancer(decorator);

    Decorators created by currying with arrow functions in ES6 would return a decorated function with undefined name, as arrow functions are anonymous.

    Enhancers upgraded from decorators by createEnhancer would return an enhanced function inheriting the original name of the target function, which is useful if you need to access the function name and make it available in the next chained enhancer. Furthermore, enhancers can also enhance a function bundle (functions wrapped as methods in an object), handy for reducing repeated lines to enhance a set of functions.

    Use Enhancers

    enhance a set of functions

    export default enhancer({
      someFunctionA: () => {},
      someFunctionB: () => {},
    });

    chain enhancers together

    import { errorToHandler, compose } from '@financial-times/n-express-enhancer';
    
    const someMiddleware = (req, res) => {};
    export default compose(errorToHandler, enhancerA, enhancerB)(someMiddleware);

    order of how enhancers would be executed

    Available Enhancers

    auto next for express

    There's an enhancer included in the package that can be used to enhance middleware/controller function in express to next() any error caught to error handler or calling next() on successful middleware execution.

    From:

    const someMiddleware = (req, res, next) => {
     try {
       //...throw error if needed
       next();
     } catch(e) {
       next(e);
     }
    };
    
    export default someMiddleware;

    To:

    import { errorToHandler } from '@financial-times/n-express-enhancer';
    
    const someMiddleware = (req, res) => {
      //...throw error if needed
    };
    
    export default errorToHandler(someMiddleware);

    If you need to use res.render in controller, please use enhancedRender together. This is due to an restriction in express@4.

    import { enhancedRender } from '@financial-times/n-express-enhancer';
    
    app.use('/route', enhancedRender, enhancedMiddleware);

    auto log for express

    n-auto-logger - auto log every operation and action in express

    auto metrics for express

    n-auto-metrics - complementary metrics to refelect operations and actions

    auto next/log/metrics for express

    n-express-monitor

    Licence

    MIT

    Keywords

    none

    Install

    npm i @financial-times/n-express-enhancer

    DownloadsWeekly Downloads

    48

    Version

    1.8.1

    License

    MIT

    Unpacked Size

    14.5 kB

    Total Files

    11

    Last publish

    Collaborators

    • seraph2000
    • robgodfrey
    • hamza.samih
    • notlee
    • efinlay24
    • emmalewis
    • aendra
    • the-ft
    • rowanmanning
    • chee
    • alexwilson