1.0.3 • Published 4 years ago

api-request-builder v1.0.3

Weekly downloads
3
License
MIT
Repository
github
Last release
4 years ago

API Request Builder

Simple request builder on top of Axios that allows pre-configured options, query params parser and errors to text with a Lang option

Todo list

  • ⏳ Full docs
  • ⏳ Tests and examples
  • ⏳ Cancel/Abort request

Installation

You can install using npm:

npm install api-request-builder

Usage

// Load the module
const apiRequestBuilder = require('api-request-builder')

// Default params for all requests
const DEFAULT_PARAMS = {
  server: "/api",   // Self domain requests to '/api' path
  timeout: 5000     // 5s timeout
}

// Initialize
const api = apiRequestBuilder( DEFAULT_PARMS )

/**
 * Your code ♥
 */

Params

This library allows you to set a default set of params for all the request.

Config params

ParamTypeDefaultDescription
validMethodsArray \["GET","POST","PUT","DELETE"]Allowed HTTP Methods
langObjectLANGLang configuration
tokenAsParamBooleanfalseAppend token (if exist) in the query params
translateErrorsBooleantrueParse request errors objects and return strings based in the lang param

Request params

ParamTypeDefaultDescription
serverString"http://localhost"API Server
endpointStringRequest Enpoint path (relative to server)
pathStringRequest URI path (relative to server/endpoint)
varsObject{}URI path variables values
paramsObject{}Query params
mimeString"application/json"Request Content-Type header
headersObject{}Request headers
tokenStringRequest Auth token
timeoutNumber50000Request timeout in miliseconds