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

halfpenny

v10.0.1

Published

Official JavaScript client for steelpenny.

Downloads

18

Readme

Halfpenny

Codeship Status for MRN-Code/halfpenny Coverage Status

Official JavaScript client for steelpenny.

Installation

Ensure Node.js (version 6+) and npm are set up on your machine. To install Halfpenny in your project, simply run:

npm install halfpenny --save

This persists Haflpenny to your package.json. You can now use it in your application code:

const Halfpenny = require('halfpenny');

Basic Use

const Halfpenny = require('halfpenny');

const myHalfpenny = Halfpenny.factory({
  baseUrl: 'http://localhost:8800',
  authCookieName: 'CAS_Auth_User'
});

The options are:

  • authCookieName <String>: Authentication cookie’s name. [REQUIRED]
  • baseUrl <String>: Base URL for the API server. [REQUIRED]
  • storage <Object>: Storage instance for persisting authentication credentials. This defaults to localStorage in browsers, or dom-storage in Node.js.

Philosophy

Halfpenny is a minimal, lightweight API client: it exposes the essential functionality required for COINS ecosystem applications. This includes:

  • Account creation
  • Logging in and logging out
  • Persisted authentication via headers
  • Password reset

All other required functionality for an application should be placed in application code. Halfpenny exposes methods to assist in API requests.

Methods

Halfpenny#get(endpoint[, authenticate])

  • endpoint <String>: API route’s endpoint
  • authenticate <Boolean>: Send authentication headers with the request. Default = false.

Make a HTTP GET request with the specified endpoint to the API. Returns a Promise.

hp.get('/scans')
  .then((response) => {
    const scans = response.data.data;
    console.log('Received scans!', scans);
  })
  .catch((response) => {
    console.error('Request error!', response.error);
  });

Halfpenny#post(endpoint[, data][, authenticate])

  • endpoint <String>: API route’s endpoint
  • data <String> | <Object>: Payload to send with the request
  • authenticate <Boolean>: Send authentication headers with the request. Default = false.

Make a HTTP POST request with the specified endpoint to the API. Optionally, send data in the request body. Returns a Promise.

hp.post('/scans', {
  label: 'My great scan!',
  segmentInterval: 1,
  studyId: 123,
  scannerId: 5,
  operatorId: 9,
  ursi: 'M123456789',
  // ...
})
  .then((response) => {
    const newScan = response.data.data[0];
    console.log('New scan created!', newScan);
  })
  .catch((response) => {
    console.error('Request error!', response.error);
  });

Halfpenny#put(endpoint[, data][, authenticate])

  • endpoint <String>: API route’s endpoint
  • data <String> | <Object>: Payload to send with the request
  • authenticate <Boolean>: Send authentication headers with the request. Default = false.

Make a HTTP PUT request with the specified endpoint to the API. Optionally, send data in the request body. Returns a Promise.

hp.put('/scans/123', {
  subjectMass: 120,
  subjectMassUnits: 'LBS',
  subjectAge: 18,
  notes: 'The subject ate a bagel prior to scan',
})
  .then((response) => {
    const updatedScan = response.data.data[0];
    console.log('Scan updated!', updatedScan);
  })
  .catch((response) => {
    console.error('Request error!', response.error);
  });

Halfpenny#delete(endpoint[, data][, authenticate])

  • endpoint <String>: API route’s endpoint
  • data <String> | <Object>: Payload to send with the request
  • authenticate <Boolean>: Send authentication headers with the request. Default = false.

Make a HTTP DELETE request with the specified endpoint to the API. Optionally, send data in the request body. Returns a Promise.

hp.delete('/scans/123')
  .then((response) => {
    const removedScan = respones.data.data[0];
    console.log('Scan deleted!', removedScan)
  })
  .catch((response) => {
    console.error('Request error!', response.error);
  });

Custom Request Engine

Halfpenny allows you to override the default request engine (axios) with a custom one. Pass it as a parameter on config.requestEngine to the constructor:

const coinsDepositBox = require('coins-deposit-box');
const Halfpenny = require('halfpenny');
const jQuery = require('jquery');

const hp = new Halfpenny({
  authCookieName: coinsDepositBox.cookieName,
  baseUrl: coinsDepositBox.apiURL,
  storage: localStorage,
  requestEngine: jQuery.ajax,
});

Halfpenny uses the private Halfpenny#mapRequestOptions method on every request to transform the request options. Override this method to map arguments to the new request engine:

/**
 * Map request options.
 *
 * @param {Object} requestOptions
 * @param {Object} requestOptions.headers
 * @param {string} requestOptions.method
 * @param {string} requestOptions.url
 * @param {booleam} requestOptions.withCredentials
 * @param {(string|Object)} [requestOptions.data]
 * @returns {Object}
 */
hp.mapRequestOptions = (requestOptions) => {
  const data = requestOptions.data;
  const mappedOptions = {
    method: requestOptions.method.toUpperCase(),
    url: requestOptions.url,
  };

  if (requestOptions.headers) {
    mappedOptions.headers = requestOptions.headers;
  }

  if (requestOptions.withCredentials) {
    mappedOptions.xhrFields = {
      withCredentials: true,
    };
  }

  if (data) {
    if (typeof data === 'object') {
      mappedOptions.dataType = 'json';
    }

    mappedOptions.data = data;
  }

  return mappedOptions;
};

Documentation

Run npm run docs to generate API documentation, which is output in the docs directory as webpages.

Development

  • Linting: This project adheres to Airbnb’s JavaScript style guide. Run npm run lint to lint the project’s source and test files.
  • Testing: This project uses the minimal test framework tape and Sinon.js for spying and stubbing. Run npm test to run the project’s tests.

License

MIT. See LICENSE for details.