-
Notifications
You must be signed in to change notification settings - Fork 2
Following
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 fetch
function. It accepts a target and a set of options. Because the response to following the target may not contain Siren content, follow
returns a Response
object.
The target is polymorphic. It can be a string representing a URL:
import { follow } from '@siren-js/client';
const entryPoint = 'http://api.example.io';
let response = await follow(entryPoint);
It can be a URL
object:
response = await follow(new URL(entryPoint));
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 👉
If you can safely assume the server only responds with Siren content, you can easily combine follow
with parse
(see Parsing).
const customerEntity = await follow(customerLink).then(parse);
You can provide a baseUrl
in case the target URI is relative.
response = await follow('/orders', {
baseUrl: 'http://api.example.io'
});
Use the requestInit
option to specify the options for the call to fetch
.
response = await follow('http://api.example.io', {
requestInit: {
headers: {
'Api-Key': 'abcdefghijklmnopqrstuvwxyz'
}
}
});