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

halapi

v1.0.0

Published

My node module

Downloads

5

Readme

halapi Build Status Coverage Status

My node module

Install

In Browser:

$ bower install --save halapi

In Node:

$ npm install --save halapi

Usage

Given the API response:

{
  id: 1,
  firstname: 'John',
  lastname: 'Bar',
  links: {
    self: {
      href: '/person/1'
    },
    house: {
      href: '/person/1/house'
    },
    children: {
      href: '/person/1/children'
    }
  }
}

You must create the wrapper using the halapi, as below:

var api = new halapi({
  endpoint: 'http://foo.tld/bar'
});

halapi does not make the request by itself. Should you want to use $.ajax, superagent for the browser or request, got in node, it's up to you.

You must provide your own function that must follow this API:

var request = function (url) {
  return new Promise(function (resolve, reject) {
    // do what you want here !
  });
});

api.request(request);

You can now request any resource using the api.fetch method

api.fetch('/person/1');
api.fetch('/person/1/house');

The api.fetch return a promise which will return a halapi.Resource when resolved.

api.fetch('/person/1').then(function (john) {
  // john is an instance of halapi.Resource
})

To retrieve a linked resource from an existing resource, it's as simple as:

john.link('house').then(function (house) {
  // house is an instance of halapi.Resource
})

The halapi.Resource is not exported as it is easily accessible using api.fetch.

The halapi.Resource has a simple API:

  • data(): return only the data part of the response, without the links property
  • get(name): return the data property name
  • links(): return the links property
  • link(name): return a promise to fetch the resource for the given name link
  • save(data): replace the content of the resource. Normally you should not need to use this.
  • linkAttr: if you need to override the default value (links) for the link property

A small example below:

Given both the reponses for:

the resource /person/1:

{
  "id": 1,
  "firstname": "John",
  "lastname": "Foo",
  "links": {
    "self": {
      "href": "/person/1"
    },
    "house": {
      "href": "/person/1/house"
    }
  }
}

And the resource /person/1/house:

{
  "hid": 123,
  "name": "The little house",
  "links": {
    "self": {
      "href": "/person/1/house"
    },
    "address": {
      "href": "/house/1/address"
    },
    "badLink": {}
  }
}
api.fetch('/person/1').then(function (person) {
  person.get('firstname'); // John

  person.links();
  /*
  {
    "self": {
      "href": "/person/1"
    },
    "house": {
      "href": "/person/1/house"
    }
  }
  */

  person.link('house').then(function (house) {
    house.data();
    /*
    {
      "hid": 123,
      "name": "The little house"
    }
    */

    // this should not be needed.
    // Beware that it's the whole server response and as such you should provide
    // the links property
    house.save({
      hid: 124,
      name: "My little house"
    });

    house.data(); // will return the same data which where saved
    house.links(); // undefined because no links where provided.
  })
});

License

MIT © Stéphane Bachelier