Skip to content

Following

Dillon Redding edited this page Dec 18, 2023 · 8 revisions

The simplest way for a client to interact with hypertext is by following some sort of link. We can accomplish this with the follow function, which is a thin wrapper around the standard fetch function. The first argument is our target and it can take several forms.

The target can be a URL string.

import { follow } from '@siren-js/client';

const entryPoint = 'http://api.example.io';
let response = await follow(entryPoint);

It can also be a URL object.

const url = new URL(entryPoint);
response = await follow(url);

And probably the most useful is when the target is a Link or EmbeddedLink object.

response = await follow(customerLink); // customerLink is from the Modeling page 👉

We can easily combine this with the parse function (see Parsing).

const customerEntity = await follow(customerLink).then(parse);

The follow function also supports a couple options in the second argument. We can provide a baseUrl in case the target URI is relative.

const baseUrl = 'http://api.example.io';
response = await follow('/home', { baseUrl });

The requestInit option allows us to customize the internal call to fetch.

response = await follow(baseUrl, {
  requestInit: {
    headers: {
      'Api-Key': 'abcdefghijklmnopqrstuvwxyz'
    }
  }
});

Clone this wiki locally