backpack-fetch
v0.4.2
Published
A Fetch wrapper utility for use with the Backpack API.
Downloads
12
Readme
backpack-fetch
A Fetch wrapper utility for use with Backpack API.
Basic usage:
import api from 'backpack-fetch';
Example GET request:
const res = await api.get('/books/1234');
if (res.error) return console.log(res.error);
const books = res.data;
The res object returned in the above example is like this:
{
status: 200,
data: [ array of book objects ],
error: null
}
Example POST request:
const payload = { username: 'billgates', password: 'win95roolz' };
const res = await api.post('/getTokenWithPassword', payload);
if (res.error) return console.log(res.error);
const loggedInUser = res.data;
This time, the res object returned also contains a token (assuming this API route was written to include one).
{
status: 200,
data: { user object },
error: null,
token: 'abcdefg123456.abcdefg123456.abcdefg123456'
}
Whenever a token is returned, it is automatically saved to the client's local storage as 'token'.
...and whenever a token exists in localstorage, it will always be sent in the HTTP headers along
with every request. For example, the original example api.get('/books/1234')
would have included
the JSON webtoken if one existed in localstorage.