This is a simple wrapper over superagent library.
Any pull request for optimizations and new additions is more than welcome.
npm install superagent-wrapper
import api from 'superagent-wrapper';
Takes apiUrl and headers as parameters.
Initialisation should be called before any get/post/put/delete request.
const API_BASE_URL = 'http://domain/api';
const defaultHeaders = {
Accept: 'application/json',
AppVersion: '1.0.0'
};
api.init(API_BASE_URL, defaultHeaders);
Takes endpoint and headers as parameters.
Headers sent to the server will extend the default headers set into the initialization call.
Returns a promise.
get('/fetch-endpoint', { Authorization: `Token 123` })
.then((data) => {
// do something with data
})
.catch((error) => {
// check for error.data and error.status and use these values
});
Takes endpoint and headers as parameters.
Headers sent to the server will extend the default headers set into the initialization call.
Returns a promise.
post('/post-endpoint', { Authorization: `Token 123` })
.then((data) => {
// do something with data
})
.catch((error) => {
// check for error.data and error.status and use these values
});
Takes endpoint and headers as parameters.
Headers sent to the server will extend the default headers set into the initialization call.
Returns a promise.
put('/put-endpoint', { Authorization: `Token 123` })
.then((data) => {
// do something with data
})
.catch((error) => {
// check for error.data and error.status and use these values
});
Takes endpoint and headers as parameters.
Headers sent to the server will extend the default headers set into the initialization call.
Returns a promise.
del('/delete-endpoint', { Authorization: `Token 123` })
.then((data) => {
// do something with data
})
.catch((error) => {
// check for error.data and error.status and use these values
});