@siren-js/client
Version:
Siren API client library
127 lines (94 loc) • 3.71 kB
Markdown
[](https://npmjs.org/@siren-js/client)
[](https://github.com/siren-js/client/actions/workflows/build.yaml)
[](https://github.com/RichardLitt/standard-readme)
[](LICENSE)
[](https://github.com/siren-js/.github/blob/main/profile/CONTRIBUTING.md)
[](https://github.com/kevinswiber/siren) API client library for JavaScript
This library handles much of the boilerplate, protocol-level details around interacting with a Siren API. Here are the things it can do:
- [x] Parse and validate Siren representations
- [x] Follow a `Link` (or any URL)
- [x] Submit an `Action`
- [x] Customize `Field` validation and serialization
- [x] Resolve a `SubEntity`
- [x] Traverse an `Entity` via the [Visitor pattern](https://en.wikipedia.org/wiki/Visitor_pattern)
- [x] Crawl a Siren API
- [Install](
- [Usage](
- [Development](
- [API](
- [Maintainer](
- [Contributing](
- [License](
```bash
npm install @siren-js/client
```
```js
import { follow, parse, resolve, submit } from '@siren-js/client';
// follow API entry point
let response = await follow('https://api.example.com/entry-point');
// parse the response as Siren
let entity = await parse(response);
// find the first 'item' sub-entity
const itemSubEntity = entity.entities.find((subEntity) => subEntity.rel.includes('item'));
if (itemSubEntity != null) {
// resolve the sub-entity to a full entity
entity = await resolve(itemSubEntity);
}
// find the first 'next' link
const nextLink = entity.links.find((link) => link.rel.includes('next'));
if (nextLink != null) {
// follow the 'next' link, if present, and parse as a Siren entity
entity = await follow(nextLink).then(parse);
}
// find the 'edit' action
const editAction = entity.getAction('edit');
if (editAction != null) {
// find the 'quantity' field in the 'edit' action
const quantityField = editAction.getField('quantity');
if (quantityField != null) {
// set the 'quantity' field value
quantityField.value = 69;
}
// submit the action and parse the response as Siren
response = await submit(editAction).then(parse);
}
```
```sh
$ nvm use
$ npm test
$ npm run test:watch
$ npm run test:cov
$ npm run compile
$ npm run lint
$ npm run lint:fix
$ npm run format
$ npm run format:check
$ npm run build:lib
$ npm run build:docs
```
See our [docs](https://siren-js.github.io/client).
[@dillonredding](https://github.com/dillonredding)
See our [contribution guidelines](https://github.com/siren-js/.github/blob/main/profile/CONTRIBUTING.md).
PRs accepted.
If editing the README, please conform to the [standard-readme](https://github.com/RichardLitt/standard-readme) specification.
[](./LICENSE) © 2021 Dillon Redding