0.1.2 • Published 2 years ago

api-req-builder v0.1.2

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

Api Request Builder

Package Info:

This is an utility of api request builder base on axios package. It make more easily to create new a http request and handling response.

Installing Package:

npm install api-req-builder

Table methods:

Method nameDescriptionsNote
addConfigAdding any config for a http requestRead axios config
addParamAdding single query param or path param specify key and value stringContent
addParamsAdding object quey param or path paramInclude 1 or more properties
addHeaderAdding single header with key-value pair stringContent
addHeadersAdding object header with more propertiesContent
sendRequestCreate a http request and send it with all config addedContent

Usage:

  1. import module Api Request Builder
import { ApiRequestBuilder }  from 'api-req-builder';
  1. Create object Api Request Builder:
const builder = new ApiRequestBuilder();
  1. Create a http request
const response = builder
            .addConfig('baseURL', 'http://localhost:9001')
            .addConfig('method', 'get')
            .addConfig('url', '/:version/customers/customer-core/{cgid}')
            .addHeader('x-channel', 'mobile')
            .addHeader('token', '123-abc')
            .addHeaders({ 'x-metadata': '123123123', 'x-version': 1.9 })
            .addParam(PARAM_TYPE.PATH, 'version', 'v1')
            .addParam(PARAM_TYPE.PATH, 'cgid', '200101HYPHEN020235000CM3')
            .addParam(PARAM_TYPE.QUERY, 'partyType', 'ORG')
            .addParam(PARAM_TYPE.QUERY, 'cgid', '123123123')
            .addParam(PARAM_TYPE.QUERY, 'transRef', 'LMBULK321')
            .sendRequest();

Note: You can declare a placeholder for path param in url endpoint like this

addConfig('url', '/:version/customers/customer-core/:cgid')

or

addConfig('url', '/{version}/customers/customer-core/{cgid}')

then assign value addParam method:

.addParam(PARAM_TYPE.PATH, 'version', 'v1')
            .addParam(PARAM_TYPE.PATH, 'cgid', '200101HYPHEN020235000CM3')

Publish package:

- npm run build
- npm publish