2.2.5 • Published 6 months ago

alcortech-common-javascript-util v2.2.5

Weekly downloads
-
License
-
Repository
github
Last release
6 months ago

common-javascript-util

An NPM package with common components for Alcor Javascript/Typescript development projects.

What is this?

This is a collection of common functions. This includes, but is not limited to, the following:

  • logger: a common logging formatting and output service
  • jwtService: common JWT generation and validation functionality

Setup

For local development setup and software requirements see the CONTRIBUTING documentation.

Installing this package

For use in an active project follow these steps:

  1. Ensure you are added to the project you are working on as an active contributor.
  2. Ensure you add an NPM_TOKEN value to the project's .env file or to your local environment. This value should be a token generated within GitHub. To generate a token for use, please follow this guide.
  3. If your project already has a commited .npmrc file in place then skip this step. Otherwise add one and make it look like this:
@AlcorTech:registry=https://npm.pkg.github.com
//npm.pkg.github.com/:_authToken=${NPM_TOKEN}
  1. Install the module by running npm i @AlcorTech/common-javascript-util

Using this package

There are a number of things this package can do for you. It can offer common configuration for different project types. It can also offer some awesome common functions which is its primary function.

To import configs and the primary functions, add something like this to your code after installing the package:

import utils from '@AlcorTech/common-javascript-util';

If you need to use ES5 syntax for whatever reason (not recommended) you'd import like this:

const utils = require('@AlcorTech/common-javascript-util');

Each provided config and function is defined in the following sections.

functionality

The package includes several useful functions specific to your project. Here is how you can use them:

  • dateFormat - This function formats a date object into a string using the en-US locale and a standard for your project. Usage: const now = utils.dateFormat(new Date());
  • logger - This function creates logs using the Winston logging library in a unified format. Example usage is utils.logger.info("Information message").
  • propertiesLookup - This is a configuration import from the './config' file.
  • HttpStatusCodes - This is also a configuration import from the './config' file.
  • getUserName - This function attempts to retrieve a username from the 'authorization' or 'Authorization' headers in a request object. It returns a promise, so remember to use await. Example: const userName = await utils.getUserName(req).
  • getPath - This function retrieves the root path from a given full path string. Example: const rootPath = utils.getPath(fullPath).
  • withStatusCode - This function produces a function that can respond with a certain status code, optionally transforming the response body with a provided format function. Example: const successResponse = utils.withStatusCode(200)(data, origin).
  • setEnvFromStage - This function sets process.env variables with given stage variables. It returns a promise, so remember to use await. Example: await utils.setEnvFromStage(stageVariables).

Each of these functions is exported by default from the package and can be imported using import utils from 'your-package'.

Testing

There is no external testing required for this package. For development testing see the CONTRIBUTING documentation.