A reusable rest header object which can be used in your JavaScript code to create headers using builder pattern.
To generate the following output,
{
'Cache-Control': 'no-store no-cache',
'api-version': 1,
'Content-Type': 'application/json',
Accept: 'application/json',
Authorization: 'Bearer 0b195ce6-09a1-4dc7-a4eb-256535065846'
}
Use the below code:
new RESTHeader(1).addBearerAuth(uuidv4()).build();
Install package to your project using the following command:
npm i reusable-rest-header
Once installed you can import the RESTHeader
const RESTHeader = require('reusable-rest-header/RESTHeader');
Use this defined const
as:
new RESTHeader(1).addBearerAuth(uuidv4()).build();
To speed up development, use available static methods:
Using:
RESTHeader.bearerAuthOnly(random_token);
{
'Cache-Control': 'no-store no-cache',
'Content-Type': 'application/json',
Accept: 'application/json',
Authorization: 'Bearer 187addda-b57b-4642-87dc-24a6a155f172'
}
Using:
RESTHeader.bearerAuthWithApiVersion(1, random_token);
{
'Cache-Control': 'no-store no-cache',
'api-version': 1,
'Content-Type': 'application/json',
Accept: 'application/json',
Authorization: 'Bearer 1305da60-434e-4edd-a9c5-a5794d97a870'
}
Using:
RESTHeader.defaultHeaders();
{
'Cache-Control': 'no-store no-cache',
'Content-Type': 'application/json',
Accept: 'application/json',
}