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:
You'll be fine extending JsonAPIClient
in most cases. Though, for finer control we recommend using GenericAPIClient
.
Usage example
A typical usage example is as follows (using GenericAPIClient
):
/** * You can override this method to pipe all your responses with it. * @override to recieve json instead of a fetch response (like in JsonAPIClient) * * @param resp */ async { const resp = super; // Let's say we want to throw errors for 400+ statuses too if respstatus >= 400 throw MyApiClient respstatus resp; return await resp; } { super // Provide a base endpoint for your client 'https://my-api-server.com/api' // Provide generic request options used in most of your requests // For example - common headers (here we want to send basic token with each request) headers: 'Authorization': 'Basic kjhkowgurgybihfjqwuoe968tgyib3jqipwfe08s79d==' ; // Set a custom variable to the instace thismyVeryImportantSetting = myVeryImportantSetting; } // In class' body we can write custom method handlers for our API calls async { try // Send a GET request to 'https://my-api-server.com/api/important-things?importance=high&amount=5&type={value-of-myVeryImportantSetting}' return await this; catch e // e instanceof ResponseError === true // Here you can handle method-specific errors if estatus === 401 console; return ; else throw e; }
And then just
const myApi = ; myApi;