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

api-resource

v1.0.12

Published

Simple HTTP resource wrapper

Downloads

33

Readme

API Resource

Simple wrapper for http resources for client-side JavaScript or Node.js

Setup

npm install api-resource

Simple usage

var Resource = require('api-resource');

var listUsers = new Resource({
        method: 'get',
        route: 'http://localhost/users/:group',
    }),
    createUser: new Resource({
        method: 'post',
        route: 'http://localhost/users',
    });

// Fires GET request to http://localhost/users/followers?q=foo
listUsers.query({ // Request payload + route variables
    group: 'followers'
}, { // Query params
    q: 'foo'
})
.then(function (res) {
    // Response code was successful (<= 200)
    var results = res.body;
}, function (res) {
    // Response code was not successful (> 200)
    throw new Error('Users list responded ' + res.status + ', ' + res.body);
})

// Fires GET request to http://localhost/users/followers?q=foo
createUser.query({ // Request payload + route variables
    username: 'johndoe'
})
.then(function (res) {
    var response = res;
});

Middleware example

var Resource = require('api-resource'),
    token = 'arbitrary-auth-token',
    session;

var sessionEndpoint = new Resource({
    method: 'get',
    route: 'http://localhost/session'
});

sessionEndpoint.use(function (req, next) {
    // Always add authorisation header to this resource
    req.setRequestHeader('Authorization', token);

    // Bind a callback to an event on this resource
    this.once('success', function (res) {
        console.log('Session received correctly');
    });
});

sessionEndpoint.query()
.then(function (res) {
    session = res.body;
});

Resource

Resource instances are also event-emitters. After querying them you can listen you can handle responses by using the promised form or by binding callbacks to their events.

Options

  • method (String) Case insensitive HTTP method (E.g. 'post')
  • route (String) Dynamic resource route (E.g. 'http://www.foo.bar/users/:username')

Events

  • response Fired when a response is received, with a response object passed as argument
  • success Fired when a successful response (Status <= 200) is received, with a response object passed as argument
  • failure Fired when a unsuccessful response (Status > 200) is received, with a response object passed as argument
  • error Fired when an error occurs sending the XMLHttpRequest, with the error passed as argument

Methods

.query([ data ], [ query_params ]) Query endpoint. The values in the data object will also be used to build the URL if the resource route is dynamic. Query params will be added to the final URL as a query string. .use(middleware) Add a middleware function to the resource. The function will be called on the Resource instance and receive as arguments the XMLHttpRequest request instance and the next function to proceed.

Test

Tests are currently written on server-side only. Run npm install and npm test to test.

Contribution

Contributions are welcome as long as documented and tested.

License

Copyright (c) 2014 Kano Computing Ltd. - Released under the MIT license