Skip to content

Add a library for interpreting application/linkset+json responses #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 34 commits into
base: main
Choose a base branch
from

Conversation

gabesullice
Copy link
Contributor

@gabesullice gabesullice commented Mar 4, 2021

The proposed menu endpoint returns JSON formatted using the linkset media type. This library provides an easy way to extract links from such a linkset response.

This does not have any customizations for dealing with menus specifically, as I hope that we can share this library with the broader ecosystem.

The library only exports two concrete functions—denormalize and parse—and a number of interfaces. denormalize takes an plain object and "upcasts" it into an object that adheres to the new LinksetInterface. parse is a simple wrapper around denormalize that takes a JSON string instead of an object. LinksetInterface provides a few helpful utilities for understanding the set of links returned in a linkset response. This can help clients program RESTful behaviors into their UIs.

For example, the following pseudo-code shows how a linkset could be used to automatically prompt a user for login credentials and then start a session:

import { parse } from '@jsdrupal/linkset`;

const linkset = parse(menuJSON);

if (linkset.hasLinkTo('authenticate')) {
  // ... get user credentials ...
  fetch(linkset.linkTo('authenticate').href, {
    method: 'POST',
    body: JSON.stringify(credentials),
    credentials: 'cross-origin',
  });
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant