-
Notifications
You must be signed in to change notification settings - Fork 3
Following
Dillon Redding edited this page Dec 20, 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.
response = await follow('/orders', {
baseUrl: 'http://api.example.io'
});The requestInit option includes options for the call to fetch.
response = await follow('http://api.example.io', {
requestInit: {
headers: {
'Api-Key': 'abcdefghijklmnopqrstuvwxyz'
}
}
});