Installation
Install with NPM
npm install -S fyndiq-bff-utils
HttpClient
Usage
This HttpClient for Node is based on node-fetch and includes a built-in logger.
const HttpClient =// Make a GET request and parse the jsonconst getData = async {const response = await HttpClientconst data = await responsereturn data}// Make a POST request with some body dataHttpClient// Handle server errorstryawait HttpClientcatch errorif erroroutput && erroroutputstatusCode === 500// Do something to handle the server error gracefully...
API
HttpClient.get(url, options)
HttpClient.post(url, options)
HttpClient.put(url, options)
HttpClient.delete(url, options)
The methods GET, POST, PUT and DELETE have the same API as fetch. For more informations about the options
field, check the fetch reference
Contrary to fetch
, HttpClient will throw an error if the response status is 4xx or 5xx. The error object thrown is a Boom Error. If you are using a Koa server, you can use KoaBoom
exported by fyndiq-bff-utils
as a middleware:
const Koa =const KoaBoom =app =app// rest of your middlewares
Doing so will enable you to forward the HTTP error to the client.