1.0.0 • Published 9 days ago

@koibanx/http v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
9 days ago

HTTP

Koibanx's Axios based generic HTTP module

Installation

Add the dependency to your package.json file by running

npm install @koibanx/http

NOTE: you must have the npm token in your .npmrc file

Usage

Initialize the client by setting the optional default parameters:

  • baseURL: string
  • timeout: number
  • baseHeaders: object
  • requestInterceptors: Array<(request: any) => Promise>
  • responseInterceptors: Array<(response: any) => Promise>

Full example

import http from "@koibanx/http";

const baseURL = "anyurl.com/api"
const timeout = 5000
const baseHeaders = {
    "Authorization": "JWT some-token",
    "Content-Type": "application/json"
}

const logRequestInterceptor = (request) => console.log("Request:", request)
const logResponseInterceptor = (response) => console.log("Response:", response)

const requestInterceptors = [logRequestInterceptor]
const responseInterceptors = [logResponseInterceptor]

// initialize client
const client = http({ baseURL, timeout, baseHeaders, requestInterceptors, responseInterceptors })

const extraHeaders = { "Custom-Header": "SOME_VALUE" }
const foos = await httpGet({url: "/foo", headers: extraHeaders})
console.log("Received foos", foos)

const bar = {
    bar: "bar"
}

const createdBar = await httpPost({url: "/bar", headers: extraHeaders, body: bar})
console.log("Received response", createdBar)