Skip to content
This repository has been archived by the owner on Jan 11, 2019. It is now read-only.
/ bff-utils Public archive

🔩 Fyndiq utility toolbelt to build perfect BFFs

License

Notifications You must be signed in to change notification settings

fyndiq/bff-utils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

BFF utils

npm Travis Codecov code style: prettier

This repository includes some utilities to share accross BFFs

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 } = require('fyndiq-bff-utils')

// Make a GET request and parse the json
const getData = async () => {
  const response = await HttpClient.get('http://my-api-remote/')
  const data = await response.json()
  return data
}

// Make a POST request with some body data
HttpClient.post('http://my-api-remote/post', {
  headers: {
    'Content-type': 'application/json',
  },
  body: JSON.stringify({
    id: 123,
    name: 'My name',
  }),
})

// Handle server errors
try {
  await HttpClient.get('http://my-api-remote/some-api-that-might-fail')
} catch (error) {
  if (error.output && error.output.statusCode === 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 = require('koa')
const { KoaBoom } = require('fyndiq-bff-utils')

app = new Koa()
app.use(KoaBoom())

// rest of your middlewares

Doing so will enable you to forward the HTTP error to the client.

About

🔩 Fyndiq utility toolbelt to build perfect BFFs

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published