1.0.0 • Published 3 years ago

@rove-team/requester v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

Requester

The requester project is a wrapper for handling the client side requests. The latest version of this package, is using axios as its request manager.

Installation

To install this library using npm, you can use the following command:

npm i @rove-team/requester

Documentation

To use the requester package, you need to create an instance of Requester class:

const requester: Requester = new Requester();

Then you can use get() and post() for sending a get or a post request.

Get Request

The get method is getting one parameter with some properties which are required for any get request. Also you should specify the type of you response body, by passing a generic type to this method:

export interface IExampleResponseType {
  readonly name: string;
  readonly family: string;
}

const response: IResponse<IExampleResponseType> = await requester.get<IExampleResponseType>({
  // this is required
  url: 'your request url',
  // this is optional
  options: {
    headers: {
      'Content-Type': 'application/json'
    }
  }
});

Post Request

The post method is getting one parameter with some properties which are required for any post request. Also you should specify the type of your request and response body, by passing two generic type for this method:

export interface IExampleRequestBodyType {
  readonly username: string;
  readonly password: string;
}

export interface IExampleResponseBodyType {
  readonly name: string;
  readonly family: string;
}

const response: IResponse<IExampleResponseBodyType> = await requester.post<IExampleRequestBodyType, IExampleResponseBodyType>({
  // this is required
  url: 'your request url',
  // this is required
  body: {
    username: 'username',
    password: 'a password'
  },
  // this is optional
  options: {
    headers: {
      'Content-Type': 'application/json'
    }
  }
});

Response Handler

Also theres is a ResponseHandler class which is getting your response and organize it for you. If you like to use the @rove-team/requester package, it's highly recommended to use this class for your responses. Because if we decide to change our request manager from axios to anything else, in our later versions, you don't need to change anything of your previously written codes.

const responseHandler: ResponseHandler = new ResponseHandler({
  response: // your response goes here
});

const status: IResponseStatus = responseHandler.getStatus();

const body: YourResponseBodyType = responseHandler.getBody<YourResponseBodyType>();

const headers: IHeaderType = responseHandler.getHeaders();