1.0.1 • Published 4 years ago
fetch_helper_vt v1.0.1
What is this package for?
It is build to handle api calls GET/POST/Multiple GET requests and etc.
How to install it?
Just run
npm i fetch_helper_vt --save
Then...
import {makeGetRequest} from 'fetch_helper;
import {makePostRequest} from 'fetch_helper;
import {resolveAllGetRequests} from 'fetch_helper;
#Example of usage
If your application is using csrf token like authentication like in django made app you need to set the token before using the library, otherwise the requests won't be authorized
import {setCsrfToken} from 'fetch_helper;
let token = 'your_token'
setCsrfToken(token);
Making simple GET request:
url - required, params - optional
const url = 'https://cat-fact.herokuapp.com/facts/random';
const url_params = {
name: 'Jenny',
age: 4,
}
const success = (response) => {
console.log('success', response);
};
const error = (error) => {
console.log('error', error);
};
makeGetRequest({
url,
params: url_params,
successCallback: success,
errorCallback: error
});
Making simple POST request:
url - required, data - optional
const url = 'https://cat-fact.herokuapp.com/facts/random';
const postData = {
name: 'Jenny',
age: 4,
};
const successCallback = (response) => {
console.log('success', response);
};
const errorCallback = (error) => {
console.log('error', error);
};
makePostRequest({
url,
data: postData,
successCallback,
errorCallback
});
Resolve multiple GET calls:
requestsOptions - required and must be in a form of an array of objects with url parameter in them
const randomCatRequestOptions = [
{
url: 'https://cat-fact.herokuapp.com/facts/random'
},
{
url: 'https://cat-fact.herokuapp.com/facts/random'
},
{
url: 'https://cat-fact.herokuapp.com/facts/random'
},
];
const successCallback = (response) => {
console.log('success', response);
};
const errorCallback = (error) => {
console.log('error', error);
};
resolveAllGetRequests({
requestsOptions: randomCatRequestOptions,
successCallback,
errorCallback
});