@geniucode/axios
TypeScript icon, indicating that this package has built-in type declarations

0.0.1 • Public • Published

Stuff Axios

The purpose of this package is to make sure we have one way to create an axios instance so that any app can work on it directly.

Install

npm i --save @geniucode/axios

API Documentation

Build an axios instance

const _axios = axiosInstance(baseUrl, headers).get();

Example: const todoAxios = axiosInstance('https://jsonplaceholder.typicode.com', {
  'Content-Type': 'application/json',
}).get();

Usage for GET method

const todos = await todoAxios.get('/todos/1');

Usage for POST method

const newTodo = await todoAxios.post('/todos', {
  completed: false,
  id: 123456789,
  title: 'delectus aut autem',
  userId: 1,
});

Ability to transform response

To avoid using .data.data again and again

const _axios = axiosInstance(baseUrl, headers).get();

Example: const todoAxios = axiosInstance('https://jsonplaceholder.typicode.com', {
  'Content-Type': 'application/json',
  'isTransformResponse' : true
}).get();

Using multiple instances in Axios

apparently each instance will be discriminated by its url, having 2 instances with the same url will cause the headers of the 2 instances to override each others.

const { axiosInstance } = require('@geniucode/axios');


export const bubbleAxiosInstance = () => {
    const axios = axiosInstance(`${process.env.COMMON_CLUSTER_BASE_URL}/api/bubble`, {}).get()
    return axios
}

export const conflakeAxiosInstance = () => {
    const axios = axiosInstance(process.env.COMMON_CLUSTER_BASE_URL, {
        'Content-Type': 'application/json',
        'api-key': process.env.CONFLAKE_API_KEY,
    }).get();
    return axios
}

export const legacyAPIAxiosInstance = () => {
    const axios = axiosInstance(process.env.LEGACY_API_SERVER_URL, {
        'Content-Type': 'application/json',
    }).get();
    return axios
}

export const ticketingAPIAxiosInstance = () => {
    const axios = axiosInstance(process.env.TICKETING_SYSTEM_API_URL, {
        'ticketing-api-key': process.env.TICKETING_SYSTEM_API_KEY,
        'Content-Type': 'application/json',
    }).get();
    return axios
}

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. Please make sure to update tests as appropriate.

License

Please see License File for more information.

Readme

Keywords

none

Package Sidebar

Install

npm i @geniucode/axios

Weekly Downloads

0

Version

0.0.1

License

ISC

Unpacked Size

11.2 kB

Total Files

7

Last publish

Collaborators

  • geniucode-dev