Skip to content

Latest commit

 

History

History
191 lines (151 loc) · 4.78 KB

File metadata and controls

191 lines (151 loc) · 4.78 KB

🚀 A common utility library for NodeJS


In your package.json > dependencies:

  "common-api": "git+https://github.com/grupo-a/common-nodejs-tools.git#<TAG_VERSION>"

You need to change <TAG_VERSION>. See all tags clicking here.


  # Express
  PORT=3000

  # Logs
  LOG_INFO_ENABLE=true
  LOG_WARN_ENABLE=true
  LOG_ERROR_ENABLE=true
  LOG_AUDIT_ENABLE=true

  # Database
  DB_READ_HOST=localhost
  DB_WRITE_HOST=localhost
  DB_PORT=5432
  DB_DATABASE=database-example
  DB_USER=postgres
  DB_PASSWORD=postgres

In your file.js, import the dependency and extract the common function that you want to use.

  const { database } = require('common-api');

It's a function to use PostgreSQL and you need to set the environment variables shown on the previous section. PG is the dependency that we use to connecting host.

Note: We are used to using two types of instances: read and write. By the way, when you will use this function, remember to inform which of the options you wanna use.

Example:

  const { postgres } = require('common-api').database;
  const query   = "SELECT * FROM example WHERE id = $1";
  const values  = [1];
  await postgres.read.queryFirstOrNull(query, values);

There are two models: HttpError and DbError. They are responsible for mount the default structure when an error is occurred.

Example:

  const { error } = require('common-api');
  throw new error.HttpError(message, httpStatusCode, businessStatusCode);

To use express in your API, we recommend our common function. It's responsible to set default configs like: enable cors, format body, handle the error page (route not found), etc.

Note: The default port is 3000.

Example:

  const { express } = require('common-api');
  express.instance.get('/path', 'middlewareIfNecessary', controller.get);
  express.init();
  module.exports = express.instance;

There are some functions inside this module which we use either success or failure logs. They are important for debugging our API and register what's happening inside it.

Example:

  const { logger } = require('common-api');
  logger.info('Example console');
  logger.warn('Error inside function');

When you need to do some request, we recommend to use this function. You just have to pass the options from your request like the example.

Example:

  const { request } = require('common-api');
  const options     = {
    uri     : `localhost`,
    method  : 'GET',
    headers : {}
  };
  const response = await request.handler(options);

Each response from your API, you can use this standard function that receives the parameter res (from your express), body (body response) and HttpCode, in addition to setting up the structure to serve your client.

Example:

  const { response } = require('common-api');
  return response.success(res, body, 200);

It's a helper function that uses jsonschema and validade your received body.

Example:

  const { validator } = require('common-api');
  const changeSchema = {
    'id'  : '/MethodName',
    'type': 'object',
    'properties': {
      'variable' : {'type' : 'string'}
    },
    'required': ['variable'] // if is required
  };
  validator.validate(changeSchema, body);

🚧 Open for contribuitions... 🚧