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

hypergard

v6.0.0

Published

[![Build Status](https://travis-ci.org/Comcast/hypergard.svg?branch=master)](https://travis-ci.org/Comcast/hypergard)

Downloads

17

Readme

HyperGard

Build Status

Javascript client for HAL APIs, with support for Hypermedia Forms

Installation

When using npm

npm install --save hypergard

Usage

Initialize HyperGard instance

const HOMEPAGE_ENDPOINT = 'https://hypermedia-endpoint.com/'
const HalApi = new HyperGard(HOMEPAGE_ENDPOINT, {});

Fetch Homepage

const homepageResource = await HalApi.fetchHomepage();

Options

cacheHomepage

Default value: false

Determines whether to keep locally closed over reference to homepage response, since the Homepage resource should be highly cache-able by Hypermedia standards.

preloadHomepage

Default: true

Auto-fetch homepage endpoint on initialization of HyperGard object.

xhr

Network request that will be passed along to fetch.

Additionally hypergard implements a timeout around any network fetch, which will default to 60000 milliseconds

{
  // Headers to be added to each network fetch
  headers: {
    ['X-Custom-Header']: "value",
  }
  // Timeout period in milliseconds
  timeout: 2000,
}

applyMiddlewareStack

Allows you to pass an array of middleware function to wrap around each fetch.

Each middleware function should:

  • Have a method signature with arguments url, options, and next
  • Return the remaining stack return next(url, options) so the promise chain isn't broken

Example of Middleware for accessing calls before fetch. (Replaces beforeFetch and uniqueFetchHeaders functionality)

function setCustomHeader(url, options, next) {
  var newOptions = Object.assign({}, options, {
    headers: {
      'X-MoneyTrace': 'hey nowwwww',
    }
  });

  // Call next piece of middleware
  return next(url, newOptions);
}

Example of Middleware for accessing calls after fetch (Replaces afterFetchSuccess and afterFetchFailure functionality)

function loggerMiddleware(url, options, next) {
  // Call next piece of middleware
  var promiseChain = next(url, options);

  // Log any 401
  promiseChain.catch(function(error) {
      if (error.status === '401') {
        mockLogger.log('Unauthorized', {error: error});
      }
    })

  // Return un-caught promise chain
  return promiseChain;
}

Example of Applying middleware stack

Middleware can be applied to an initialized HyperGard object, and will be executed based on order of array.

HalApi.applyMiddlewareStack([
  setCustomHeader,
  loggerMiddleware,
]);

Running Tests

Install Dependencies

$ npm run setup

Running tests to manually validate a patchset

$ npm test

Running tests during development

$ gulp test

That gulp test command will load up Chrome. Click the "Debug" button and then open the JavaScript Console to see the test results. You can also use console methods to be able to debug your tests.

Note that if you make a code change, you cannot simply reload http://localhost:8080/debug.html in Chrome. You have to stop the gulp test process with Control+C and then rerun the command (there's probably a better way to handle that, but it does the job for now).