1.0.5 • Published 3 years ago
fusion-helper-http-lib v1.0.5
Fusion Helper Http Library
This is a library that the fusion helper application uses to standardize its HTTPS calls/responses.
Installation
Using npm:
npm i --save fusion-helper-http-lib
Usage
The package contains two objects
- FusionHttpReqHandler
- FusionHttpResponse
FusionHttpReqHandler
This is the main service used to run http requests and respond with a common structure
Creating an Instance
import { Fusion } from "fusion-helper-http-lib";
const httpHandler = new FusionHttpReqHandler(handler: Function, req: express.Request, res: express.Response, next: express.NextFunction)
- handler - The main method that handles the request.
- req - This is the HTTP request from express.
- res - This is the HTTP response from express.
- next - This is the next method from express.
Using The Instance - run a request
this.router.post(
"/api/upload/emp",
this.upload.any(),
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
const action = (a: number, b: number) => {return a+b};
const actionHandler = new HttpActionHandler();
await actionHandler.runAction(handler, req, res, next);
}
);
Description Within an express method, you use the following process to handle a request and return a common response no matter what the handler does:
- Create the handler function
- Create the FusionHttpReqHandler
- Await FusionHttpReqHandler.runAction (it will write the response to the response (res) and handle errors)
FusionHttpResponse
This is the response when using the FusionHttpReqHandler.
Properties
- data: any - this is the data returned by the action handler
- error: FusionHttpError - If there is an error, this is constructed. If not, it's null.
- statusCode: statusCodes - This is a type that allows the following values
export type statusCodes = 200 | 401 | 403 | 404 | 500;
FusionHttpError
This is part of the process because the JS Error object is not serializable. We take the properties from the thrown error and push them on to the instance of the FusionHttpError
Properties
- stack: string - the stack trace from the original error
- message: string - the main messages from the original error
- name: string - OPTIONAL. This is the name of the error.