0.1.1 • Published 5 years ago

ianes v0.1.1

Weekly downloads
3
License
ISC
Repository
github
Last release
5 years ago

Ianes

Ianes - A request handler based on axios

npm version Statements coverage

IANES is a wrapper for axios, a promise based HTTP client for the browser and node.js. Built with :heart:.

Features

  • Easy configuration for global Authorization Header
  • Easy connfiguration for global apiUrl
  • Supports the Promise API
  • Intercept request and response
  • Transform request and response data

Installing

Using yarn:

$ yarn add ianes

Using npm:

$ npm install ianes

Getting Started

If your'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 DEFAULT_CONTENT_TYPE = "application/json";
const DEFAULT_TIMEOUT = 240000;

const configurations = {
  apiUrl: API_URL,
  defaultContentType: DEFAULT_CONTENT_TYPE,
  defaultTimeout: DEFAULT_TIMEOUT,
};

configureIanes(configurations);
//more code...

Configurations

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 method 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)
}

Examples

Set Authorization Header

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);

Reset Authorization Header

Resetting token through resetAuthorizationHeader makes that Authorization header stops being sent.

import { resetAuthorizationHeader } from "ianes/modules";

resetAuthorizationHeader();

Get

import { get } from "ianes/modules";

get("/teachers")
  .then((response) => {
    console.log(response)
  })
  .catch((error) => {
    console.log(error)
  })

Post

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)
  })

Put

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)
  })

Patch

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)
  })

Remove (delete)

import { patch } from "ianes/modules";

const student = { id: 234432, name: "Marrie Mayer", age: 21 };

patch(`/students/${student.id}`)
  .then((response) => {
    console.log(response)
  })
  .catch((error) => {
    console.log(error)
  })

Credits

Ianes is heavily based on axios.