npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

super-siren

v2.0.2

Published

Siren client based built on top of superagent

Downloads

142

Readme

Super-Siren

Description

SIREN Hypermedia client library which process SIREN HTTP responses into immutable SIREN representations utilizing superagent for the underlying HTTP requests. All superagent requests are wrapped with superagent-promise which exposes Bluebird promises for processing http requests.

SIREN models within this library are created using Immutable.js Records with properties constructed using Immutable maps, sets and lists.

Links

API docs

Siren-Crawler - Siren Crawler is a generic Siren browser that utilizes super-siren for requesting and parsing all SIREN messages. Siren Crawler is implemented as a single page javascript application utilizing React and flux.

Getting Started

Install super-siren using npm.

npm install super-siren

Then require super-siren from any JavaScript module:

let Siren = require('super-siren');

To get started provide the SIREN billboard URL to the static get method.

let Siren = require('super-siren');

Siren.get('http://example-siren-url/api')
	.then(res => {
		/* process http result here */
		/* Siren instance lives on res.body */
	});

super-siren automatically registers a SIREN parser for all http requests made through super-siren. Any HTTP response which specifies a content type of application/vnd.siren+json. All other content-types will fall back to the default superagent parsers.

Setting Global Headers

super-siren allows you to set global headers for all requests made through super-siren. Super-siren will automatically add an Accepts header requesting application/vnd.siren+json, so you don't have to worry about adding that one. To add a custom header simply do the following:

let Siren = require('super-siren');

Siren.Client.addHeader('Authentication', 'Bearer ' + bearerToken);

To then later remove the header you can simply:

Siren.Client.removeHeader('Authentication');

Helpers

super-siren provides helper functions to assist with navigating through SIREN responses. This allows for using a fluent syntax to navigate from a base SIREN URL through it's available links, entities and actions.

For example if you had a base SIREN entity at http://example-siren/api and wanted to follow through a link at rel favorites and wanted to perform an action named add-favorite in order to add a favorite you could write the following:

let Siren = require('super-siren');
let follow = Siren.Helper.follow;
let performAction = Siren.Helper.performAction;

Siren.get('http://example-siren/api')
	.then(follow('favorites'))
	.then(performAction('add-favorite', { name: 'Siren' })) // the data opts are optional
	.then(res => {
		/* Process Siren action HTTP response here */
	});

SIREN structure

let Siren = require('super-siren');
Siren.get('http://example-siren/api')
	.then(res => {
		let siren = res.body;

		/* prints all classes as separate log entires*/

		siren.classes
			.forEach(class => console.log(class));

		/* prints all hrefs that have the rel 'example' */

		siren.links
			.filter(link => link.rels.contains('example'))
			.map(link => link.href)
			.forEach(href => console.log(href));


		/* prints the first rel of all sub-entities which have the
		'example' rel*/

		siren.entities
			.filter(entity => entity.rels.contains('example'))
			.forEach(entity => console.log(entity.rels.first()));


		/* prints the JS array of all classes for all embedded sub entities
		which have a rel of 'example' */

		siren.embeddedEntities
			.filter(entity => entity.rels.contains('example'))
			.map(entity => entity.entity)
			.forEach(entity => console.log(entity.classes.toJS()));

		/* prints all href locations for all linked sub-entities that have
		the rel of 'example' */

		siren.linkedEntities
			.filter(entity => entity.rels.contains('example'))
			.map(entity => entity.href)
			.forEach(href => console.log(href));

		/* performs the siren action named 'add-favorite' */

		siren.actions
			.first(action => action.name === 'add-favorite')
			.perform({
				name: 'Siren'
			})
			.then(res => {
				console.log('Submitted siren as my favorite!');
			})
			.catch(err => {
				console.log('Submitting siren as my favorite would never fail!');
			})
	});

SIREN navigation helpers

let Siren = require('super-siren');

Siren.get('http://example-siren/api')
	.then(res => {
		let siren = res.body;

		/* find the first action with the requested name. null when not found */

		siren.findActionByName('add-favorite')
			.perform({
				name: 'Siren'
			})
			.then(res => {

			});

		/* find the first link by the requested rel. null when not found */

		let href = siren.findLinkByRel('favorites').href;

		/* return all entities with the provided rel */

		siren.findEntitiesByRel('cool-stuff');

		/* same thing but only for embedded entities */

		siren.embeddedEntitiesByRel('cool-stuff');

		/* and only for linked sub-entities */

		siren.linkedEntitiesByRel('cool-stuff');
	});