fluent-siren-client

Fluent client for Siren Hyper Media APIs

Usage no npm install needed!

<script type="module">
  import fluentSirenClient from 'https://cdn.skypack.dev/fluent-siren-client';
</script>

README

fluent-siren-client

Fluent client for Siren Hyper Media APIs

Fetches a Siren object using the provided href and request function, and parses it into a node-siren-parser Entity. Configures the Entity in such a way that uri links can be followed, and actions can be performed. Result of following a link is another fluent Entity.

Installation

Install from NPM:

npm install fluent-siren-client

Usage

const client = new SirenClient(requestFn); // async requestFn(uri, method, fieldValues)
const entity = await client.start('http://api.example.com');
const linkedEntity = await entity.getLinkByRel('external').follow();
const subEntity = await entity.getSubEntity('item').follow();
const actionResult = await entity.getActionByName('action').perform();
const actionResultStatusCode = actionResult.getResponse().statusCode;

Setup

Sample requestFn using request-promise library

const rp = require('request-promise');

async function requestFn(uri, method, fieldValues) {
  const params = {
    defaults: {
      headers: { Authorization: `Bearer ${authorizationToken}` }, // inject authorization token
      json: true,
      resolveWithFullResponse: true,
    },
    event: true,
  };

  const options = { uri, method };

  if (method.toUpperCase() === 'GET') {
    options.qs = fieldValues;
  } else {
    options.body = fieldValues;
  }

  const request = rp(params);
  const response = await request(options);
  const { body, headers } = response;
  return { body, contentType: headers['content-type'], response };
}