1.0.5 • Published 3 years ago

fusion-helper-http-lib v1.0.5

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

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

  1. FusionHttpReqHandler
  2. 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)
  1. handler - The main method that handles the request.
  2. req - This is the HTTP request from express.
  3. res - This is the HTTP response from express.
  4. 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:

  1. Create the handler function
  2. Create the FusionHttpReqHandler
  3. 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

  1. data: any - this is the data returned by the action handler
  2. error: FusionHttpError - If there is an error, this is constructed. If not, it's null.
  3. 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

  1. stack: string - the stack trace from the original error
  2. message: string - the main messages from the original error
  3. name: string - OPTIONAL. This is the name of the error.
1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago