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-res

v3.1.1

Published

RESTish wrapper for superagent, based on angular-resource

Downloads

15

Readme

super-res

Travis build status Test Coverage Dependency Status devDependency Status

This is patterned off of Angular's $resource service, except that it does not depend on Angular and uses superagent instead of $http. Route parsing is done with the route-parser module.

var superRes = require('super-res');

var myResource = superRes.resource('/my-endpoint/:id')

myResource.get({id: 1})
    .then(function (responseData) {
        console.log(responseData);
    });

myResource.save({id: 1}, {content: 'some sort of content'})
    .then(function (responseData) {
        console.log(responseData);
    });

The options and interface defined in the $resource doc is accurate (aside from the missing features and differences mentioned later)

Caching

Caching is handled by the cache-manager node module. If you create an action with the cache option set to true, it will use cache-manager's default in-memory cache with a max size of 100 items and a max age of 20 minutes. Or, you can provide your own cache object, so long as it follows the cache-manager interface.

Angular

There is also a function called promiseWrapper, which will wrap the promises returned by each action with an instance of $q passed to it. This is helpful if you want to use it with Angular:

angular.module('test', []).factory('myResource', function ($q) {
    var superRes = require('super-res');
    
    return superRes.promiseWrapper($q.when)(superRes.resource('/my-endpoint/:id'));
});

//somewhere else
myResource.get({id: 1}; // returns a promise wrapped in $q.when() to hook into digest cycle

Future

Here's what's on the list to add:

  • batching

Differences from angular-resource

  • Entirely promise based. No $promise or $resolved properties.
  • The data returned is just an object, it does not have any resource functions attached.
  • isArray doesn't do anything. I haven't seen a use case for it (arrays and objects work as you'd expect).
  • Includes a default PUT action (called put).
  • Transforms are passed a headers object as the second argument, rather than a getter.
  • Superagent will automatically parse certain response types. This is not suppressed by passing a responseTransform of [].
  • Excess default parameters are not automatically added to the query string if they're not in the route template (PRs welcome for this).

Superagent Plugins

Additionally to the angular resource options, superagent plugins can be configured for each resource action.

var nocache = require('superagent-no-cache');
var prefix = require('superagent-prefix');

var myResource = superRes.resource('/my-endpoint/:id', {
  action: {
    method: 'GET',
    plugins: [nocache, prefix('/static')]
  }
})