KeFetchUp!
Simple fetch client API to spice up your application
npm i -S kefetchup
What is it?
It's just a small and very extendable fetch client made for our company's purposes.
Kefetchup aims to help you move your API calls into a higher generic abstraction by providing necessary tools for it.
Basic operation principles (API TLDR)
kefetchup
provides several classes and utilities to help you with building your custom API client:
import {
GenericAPIClient,
JsonAPIClient,
TextAPIClient,
ResponseException,
ResponseErrors,
withQuery
} from 'kefetchup'
You'll be fine extending JsonAPIClient
in most cases. Though, for finer control we recommend using GenericAPIClient
.
A typical usage example is as follows (using GenericAPIClient
, for example):
import { GenericAPIClient, ResponseException, ResponseErrors, withQuery } from 'kefetchup'
class MyApiClient extends GenericAPIClient {
async responseHandler(response) {
const resp = super.responseHandler(response);
if (resp.status >= 400) {
throw new ResponseException(ResponseErrors[resp.status], resp.status, resp);
}
return await resp.json();
}
constructor(myVeryImportantSetting) {
super(
'https://my-api-server.com/api',
{
headers: {
'Authorization': 'Basic kjhkowgurgybihfjqwuoe968tgyib3jqipwfe08s79d=='
}
}
);
this.myVeryImportantSetting = myVeryImportantSetting;
}
async getImportantThingsList() {
try {
return await this.get(withQuery('/important-things', {
importance: 'high',
amount: 5
}));
} catch (e) {
if (e.status === 401) {
console.error('Token is incorrect for', e.data);
return [];
} else {
throw e;
}
}
}
}
And then just
const myApi = new MyApiClient();
myApi.getImportantThingsList().then(things => {
}).catch(e => {
});