alcortech-common-javascript-util v2.2.5
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 servicejwtService
: 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:
- Ensure you are added to the project you are working on as an active contributor.
- 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. - 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}
- 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 isutils.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 useawait
. 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 useawait
. 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.
6 months ago