Ianes - A request handler based on axios
IANES is a wrapper for axios, a promise based HTTP client for the browser and node.js. Built with ❤️.
- Easy configuration for global Authorization Header
- Easy connfiguration for global apiUrl
- Supports the Promise API
- Intercept request and response
- Transform request and response data
Using yarn:
$ yarn add ianes
Using npm:
$ npm install ianes
If you're using React, we recommend you to configure IANES in some of the entry point of your application. In our example, it was made in the file that contains all the possible routes.
//others imports
import { configureIanes } from "ianes/modules";
const API_URL = "http://localhost:3000/api";
const configurations = {
apiUrl: API_URL,
};
configureIanes(configurations);
//more code...
By default,handleResponseDataSuccess
and handleResponseError
are initialized with default handlers functions. Also, defaultContentType
and defaultTimeout
are initialized with application/json
and 240000
, respectively.
The configureIanes
functions accept the following properties:
{
//default base url used in requests
apiUrl: "https://myapiurl.com",
//It will be used in the Authorization header in all requests sent
apiToken: "Bearer MYWEBTOKENAPIANDANOTHERSWORDS",
//Content type to be used in requests sent
defaultContentType: "application/json",
//defaultTimeout should be a number
defaultTimeout: 240000,
//handleBeforeTheRequestIsSent expects a function that should return the config object
handleBeforeTheRequestIsSent: config => config,
//handleRequestError expects a function that must return a promise
handleRequestError: error => Promise.reject(error),
//handleResponseDataSuccess expects a function that should return response
handleResponseDataSuccess: response => (response),
//handleRequestError expects a function that must return a promise with the error object
handleResponseError: error => Promise.reject(error)
}
Setting token through setAuthorizationHeader
makes that Authorization
header sent in requests uses the value passed by it.
import { setAuthorizationHeader } from "ianes/modules";
const token = "Bearer MYWEBTOKENAPIANDANOTHERSWORDS"
setAuthorizationHeader(token);
Resetting token through resetAuthorizationHeader
makes that Authorization
header stops being sent.
import { resetAuthorizationHeader } from "ianes/modules";
resetAuthorizationHeader();
import { get } from "ianes/modules";
get("/teachers")
.then((response) => {
console.log(response)
})
.catch((error) => {
console.log(error)
})
import { post } from "ianes/modules";
const student = { name: "John Mayer", age: 21 };
post("/students", student)
.then((response) => {
console.log(response)
})
.catch((error) => {
console.log(error)
})
import { put } from "ianes/modules";
const student = { id: 234432, name: "Marrie Mayer", age: 21 };
put(`/students/${student.id}`, student)
.then((response) => {
console.log(response)
})
.catch((error) => {
console.log(error)
})
import { patch } from "ianes/modules";
const student = { id: 234432, name: "Marrie Mayer", age: 21 };
patch(`/students/${student.id}`, { name: "Larry" })
.then((response) => {
console.log(response)
})
.catch((error) => {
console.log(error)
})
import { remove } from "ianes/modules";
const student = { id: 234432, name: "Marrie Mayer", age: 21 };
remove(`/students/${student.id}`)
.then((response) => {
console.log(response)
})
.catch((error) => {
console.log(error)
})
Ianes is heavily based on axios.