1.0.4 • Published 6 years ago
fetchios v1.0.4
Fetchios
Axios-inspired Fetch client.
Installing
NPM:
npm install fetchios --save
Yarn
yarn add fetchios
Basic usage
Perform a GET request:
import fetchios from 'fetchios';
fetchios.get('https://swapi.co/api/starships')
.then(function (response) {
// handle success
console.log(response.data);
})
.catch(function (error) {
// handle error
console.log(error);
})
.finally(function () {
// always executed
});
Add query params:
// ...
fetchios.get('https://swapi.co/api/starships', { page: 2 })
// ...
Perform a POST request:
import fetchios from 'fetchios';
const authData = {
username: 'myUsername',
password: 'Top$ecre7'
};
fetchios.post('https://example.com/api/login', authData)
.then(function (response) {
// handle success
console.log(response.data);
})
.catch(function (error) {
// handle error
console.log(error);
})
.finally(function () {
// always executed
});
Create an instance
import { Fetchios } from 'fetchios';
const fetchios = new Fetchios('https://swapi.co/api');
fetchios.get('/starships')
// ...
Attach an interceptor
class AttachAwesomeHeader implements HttpRequestInterceptor {
request(url: string, config: RequestInit) {
return [
url,
{
...config,
headers: {
...config.headers,
{ 'X-Custom-Header': 'awesomepossum' }
}
},
];
}
}
fetchios.attachRequestInterceptor(new AttachAwesomeHeader());
TODOs
- Improve README
- Write basic tests
- Add lint and prettier
- Add more interceptors