1.2.2 • Published 8 years ago

api-common v1.2.2

Weekly downloads
114
License
ISC
Repository
-
Last release
8 years ago

API Common

Features

  • Logger class that logs to console in dev mode and to File in non development mode
  • Middlewares for
    • Request response logging
    • Error handler for uncaught exceptions, 400 bad request and explicitly set 500.
    • Correlation ID setting middleware
    • JWT unpacking
  • Error Formatters
    const Fmt =  require('api-common').Formatter;
    const err = new Error('Any error you want to log');
    const reporter = 'Place from which this log message is emitted';
    const appName = 'sample-app';
    Fmt.buildLogMessage(req, res, err, appName, reporter);
  • Ping and app info endpoints

Steps

  1. Install the package

    npm i api-common --save
  2. Using the logger

    'use strict';
    
    // Get the logger instance configured with provided config
    const logger = require('api-common').getLogger('fullPathToLogFile');
    
    // export it so that your rest of the application can simply consume
    // this preconfigured logger.
    module.exports = logger;
  3. Using the middleware

    'use strict';
    
    const SwaggerExpressMiddleware = require('swagger-express-mw');
    const SwaggerUIMiddleware = require('swagger-ui-middleware');
    
    // A very good middleware that works out of the box, please use it.
    const ResponseTimer = require('response-time');
    
    // STEP 1: Load the appconfig.json that has the logging config in it.
    const appConfig = require('./../config/appconfig.json');
    
    // STEP 2: Initialize the middleware
    const CustomMiddleware = require('api-common').Middleware.init(appConfig.appName);
    /*
      The app name can be provided any way you can.
    */
    
    const port = process.env.PORT || appConfig.port;
    const config = {
        appRoot: "./src" // required config
    };
    let app = require('express')();
    
    // STEP: 3 Start wiring up the middleware [Order is important]
    app.use(CustomMiddleware.CorrelationIdMiddleware);
    app.use(CustomMiddleware.RequestResponseMiddleware);
    app.use(ResponseTimer());
    
    SwaggerUIMiddleware.hostUI(app,{
        path: appConfig.swaggerUiPath,
        overrides: "./src/swagger"
    });
    
    SwaggerExpressMiddleware.create(config, function (err, swaggerExpress) {
        if (err) {
            throw err;
        }
    
        // install middleware
        swaggerExpress.register(app);
    
        // Wire error handlers after this line
    
        //STEP:4 ALWAYS put this middleware in the end. It ends request pipe.
        app.use(CustomMiddleware.ErrorLoggingMiddleware);
    });
module.exports = app; // for testing

app.listen(port);
```
  1. Using the logger from within Sequelize model initializers

    'use strict';
    const path = require('path');
    const dbconf = require('../../../config/dbconfig');
    // here we use the logger wrapper from #2
    const logger = require('../helpers/Logger');
    
    let Sequelize = require('sequelize');
    dbconf.CONNECTION_CONFIG.logging = function(sqlInfo){
        logger.debug({ sql: sqlInfo ,from: path.basename(__filename) });
    };
  2. Using the ping and app info endpoints

    Pass in the express app and the configuration you want to show in the app info endpoint.

    require('api-common').StatusInformation.initializeStatusEndpoints(app, { example: 'configuration' });
  3. Important note when using within Swagger projects.

    • in ./config/default.yaml if it looks like

      # pipe for all swagger-node controllers
          swagger_controllers:
            - onError: json_error_handler
            - cors
            - swagger_security
            - _swagger_validate
            - express_compatibility
            - _router
    • Make sure you remove the onError key. It interferes with our error handler.

      # pipe for all swagger-node controllers
          swagger_controllers:
            - cors
            - swagger_security
            - _swagger_validate
            - express_compatibility
            - _router
1.2.2

8 years ago

1.2.0

8 years ago

1.1.6

8 years ago

1.1.4

8 years ago

1.1.3

8 years ago

1.1.2

8 years ago

1.1.1

8 years ago

1.1.0

8 years ago

1.0.16

8 years ago

1.0.15

8 years ago

1.0.13

8 years ago

1.0.12

8 years ago

1.0.11

8 years ago

1.0.10

8 years ago

1.0.9

8 years ago

1.0.8

8 years ago

1.0.7

8 years ago

1.0.6

8 years ago

1.0.5

8 years ago

1.0.4

8 years ago

1.0.3

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago