call-api-factory
Simple utils library for API calls.

Instalation
To install call-api-factory - you can use npm or yarn package manager.
npm install call-api-factory
yarn add call-api-factory
Documentation
The call-api-factory library includes two functions: callAPI and callAPIFactory.
Below you can find the documentation for both functions.
The callAPI function
The simple method for API calls. Currently is a build on native fetch API.
The callAPI function accept next arguments:
| Name | Type |
|---|
| url | string |
| options | object |
| events | object |
The options parameter may includes the following fields:
| Name | Type | Default value |
|---|
| method | string | GET |
| mode | string | same-origin |
| credentials | string | omit |
| redirect | string | following |
| cache | string | default |
| headers | object | Headers |
| query | object | {} |
| body | object | {} |
The events parameter may includes the following fields:
| Name | Type | Descriptions |
|---|
| onRequest | function | Called before the request. Gets options as the first argument to modify it. |
| onResponse | function | Called after the request. Gets response as the first argument. |
The callAPIFactory function
Factory for custom callAPI function. The difference betwee factory function and custom function is compatibilities with versions. You don't need to implement different API calls methods for different API versions. You may use only one callAPI for different API versions.
The callAPIFactory function accept next arguments:
| Name | Type |
|---|
| options | object / aray of objects |
The options parameter may includes the following fields:
| Name | Type | Default value |
|---|
| baseURL | string | |
| version | string / number | none |
| method | string | GET |
| mode | string | same-origin |
| credentials | string | omit |
| redirect | string | following |
| cache | string | default |
| headers | object | Headers |
| query | object | {} |
| body | object | {} |
| evens | object | {} |
The events parameter may includes the following fields:
| Name | Type | Descriptions |
|---|
| onRequest | function | Called before the request. Gets options as the first argument to modify it. |
| onResponse | function | Called after the request. Gets response as the first argument. |
Examples
import { callAPI } from 'call-api-factory';
function someContext() {
// Promise style
// Will make request to https://example.com/api/1/public/users?limit=10 | GET
callAPI('https://example.com/api/1/public/users', {
method: 'GET',
query: { limit: 10 }
})
.then((response) => console.log(response))
.catch((error) => console.error(error));
}
async function someContext() {
// Asyncfunction style
// Will make request to https://example.com/api/1/public/users?limit=10 | GET
const response = await callAPI('https://example.com/api/1/public/users', {
method: 'GET',
query: { limit: 10 }
});
}
// Generator style
// Will make request to https://example.com/api/1/public/users | POST
function* someContext() {
const response = yield callAPI('https://example.com/api/1/public/users', {
method: 'POST',
body: { userName: 'Bob' }
});
}
import { callAPI } from 'call-api-factory';
// Events usage
async function someContext() {
const url = 'https://example.com/api/1/secure/users';
const options = { query: { limit: 10 } };
const events = {
onRequest: (options) => {
const token = getSomeToken();
options.headers.append('Authorization', `Bearer ${token}`);
}
}
// Will make request to https://example.com/api/1/public/users?limit=10 | GET
const response = await callAPI(url, options, events);
}
import { callAPIFactory } from 'call-api-factory';
async function someContext() {
const callAPI = callAPIFactory({
baseURL: 'https://example.com/api/1/public',
version: 'none' // You don't need to provide a version for only one option.
});
// Will make request to https://example.com/api/1/public/users?page=1 | GET
const response = await callAPI('/users', { query: { page: 1 } });
}
async function someContext() {
const callAPI = callAPIFactory([
{
baseURL: 'https://example.com/api/1/public',
version: 1
},
{
baseURL: 'https://example.com/api/1.1/public',
version: 1.1
},
{
baseURL: 'https://example.com/api/2/public',
version: 2
},
{
baseURL: 'https://example.com/api/3/secure',
version: 'secure',
events: {
onRequest: (options) => {
const token = getSomeToken();
options.headers.append('Authorization', `Bearer ${token}`);
}
}
}
]);
// Will make request to https://example.com/api/1/public/users?page=1 | GET
const response1 = await callAPI(1, '/users', { query: { page: 1 } });
// Will make request to https://example.com/api/1.1/public/users?page=2 | GET
const response11 = await callAPI(1.1, '/users', { query: { page: 2 } });
// Will make request to https://example.com/api/2/public/users?page=3 | GET
const response2 = await callAPI(2, '/users', { query: { page: 3 } });
// Will make request to https://example.com/api/3/secure/users?page=4 | GET
const response3 = await callAPI('secure', '/users', { query: { page: 4 } });
}