isomorphic-unfetch-api-service

isomorphic-unfetch decorator

Usage no npm install needed!

<script type="module">
  import isomorphicUnfetchApiService from 'https://cdn.skypack.dev/isomorphic-unfetch-api-service';
</script>

README

Why To be able to make server/client side requests with the same service, with common headers preset, and a standard output data shape.

Installation

npm install isomorphic-unfetch-api-service

Usage

const ApiService = require(`isomorphic-unfetch-api-service`); 
const apiServiceInstance = new ApiService()
apiServiceInstance.get('/api/thing')
.then(res => {
  // res is a JSON object/array
})
.catch(error => {
  // error is a message 
})
;

If the url needs a token or you are calling the same host, you can set it by extending the ApiService extendPath method:


const ApiService = require(`isomorphic-unfetch-api-service`);

class PathwaysApiService extends ApiService {

  extendPath = path => {
    const delimiter = path.indexOf(`?`) === -1 ? `?` : `&`;
    return `${process.env.API_URI}${path}${delimiter}token=${process.env.API_TOKEN}`;
  }

}
module.exports = new PathwaysApiService()
;