JSPM

fluent-siren-client

1.4.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 2038
  • Score
    100M100P100Q121993F
  • License MIT

Fluent client for Siren Hyper Media APIs

Package Exports

  • fluent-siren-client
  • fluent-siren-client/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (fluent-siren-client) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

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 };
}