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 🙏

© 2025 – Pkg Stats / Ryan Hefner

j-resource

v1.2.0

Published

http resource

Downloads

37

Readme

j-resource

It is a library like ngResource form angular.js

Use

var Resources = require('j-resource');

var config = {

    // it is a config for request lib
    query: {
        uri: {
            hostname: 'my.domain.com',
            path: '/user',
            port: '80',
            protocol: 'http:'
        },
        method: 'GET'
    }
};

var user = new Resources(config);

user
    .query()
    .then((result) => {
        console.log(result);
    });

Extend behavior

var Resources = require('j-resource');

Resources.addInterceptor({
    request: function(config) {
        config = Object.assign({}, config);
        var url = config.url;
        delete config.url;

        config.uri = {
            hostname: 'my.domain.com',
            path: url,
            port: '80',
            protocol: 'http:'
        };

        return config;
    }
});

var config = {

    // it is a config for request lib
    query: {
        url: '/user',
        method: 'GET'
    }
};

var user = new Resources(config);

user
    .query()
    .then((result) => {
        console.log(result);
    })
    .catch((e) => {
        console.log(e);
    });

Check response status

var Resources = require('j-resource');

Resources.addInterceptor({
    response: function(result) {
            switch (result.response.statusCode) {
                case (200):
                case (201):
                case (202):
                case (203):
                case (204):
                    return result;
                    break;
                default:
                    throw result;
            }
        }
});

var config = {

    // it is a config for request lib
    query: {
        uri: {
            hostname: 'my.domain.com',
            path: '/user',
            port: '80',
            protocol: 'http:'
        },
        method: 'GET'
    }
};

var user = new Resources(config);

user
    .query()
    .then((result) => {
        console.log(result);
    })
    .catch((e) => {
        console.log(e);
    });

Errors

var Resources = require('j-resource');

Resources.addInterceptor({
    responseError: function(error) {
        console.log(error);

        var defaultData = {
            string: 'foo'
        };

        return defaultData;
    }
});

var config = {

    // it is a config for request lib
    query: {
        uri: {
            hostname: 'bad.domain.com',
            path: '/user',
            port: '80',
            protocol: 'http:'
        },
        method: 'GET'
    }
};

var user = new Resources(config);

user
    .query()
    .then((result) => {
        console.log(result);
    })
    .catch((e) => {
        console.log(e);
    });

Sending data

var Resources = require('j-resource');

var config = {

    // it is a config for request lib
    add: {
        uri: {
            hostname: 'my.domain.com',
            path: '/user',
            port: '80',
            protocol: 'http:'
        },
        method: 'PUT'
    }
};

var user = new Resources(config);

user
    .add({name: 'Matvei'})
    .then((result) => {
        console.log(result)
    })

Uri params

var Resources = require('j-resource');

Resources.addInterceptor('paramsToUri');

var config = {

    // it is a config for request lib
    getItem: {
        uri: {
            hostname: 'localhost',
            path: '/user/:name',
            port: '80',
            protocol: 'http:'
        },
        method: 'get'
    }
};

var user = new Resources(config);

user
    .getItem({name: 'Matvei'})
    .then((result) => {
        console.log(result)
    })

Interceptors for one resource

var Resources = require('j-resource');

Resources.addInterceptor('paramsToUri');


var right = new Resources({
    check: {
        uri: {
            hostname: 'right.domain.com',
            path: '/:resource/:action',
            port: '80',
            protocol: 'http:'
        }
    }
});

var config = {

    // it is a config for request lib
    add: {
        uri: {
            hostname: 'my.domain.com',
            path: '/user/:name',
            port: '80',
            protocol: 'http:'
        },
        method: 'get',
        interceptors: [{
            request: function(config) {
                return right
                    .check({
                        resource: 'user',
                        action: 'get'
                    })
                    .then(() => {
                        return config;
                    });
            }
        }]
    }
};

var user = new Resources(config);

user
    .add({name: 'Matvei'})
    .then((result) => {
        console.log(result)
    })
    .catch(() => {
        console.log(e);
    });

Change transport

By default j-resourse uses npm module request but you can change it.

var Resources = require('j-resource');
var myHttpLib = require('my-http-lib');

setTransport.setTransport(function(config) {
    return new Promise(function(resolve, reject) {
        if (config.type == 'save') {
            myHttpLib.post('http://mydomian.com', config.params, cb);
        } else if (config.type == 'find') {
            myHttpLib.get('http://mydomian.com', config.params, cb);
        } else {
            cb('There is no type');
        }
        
        function cb(err, result) {
            if (err) {
                reject(err);
            } else {
                resolve(result);
            }
        }
    });
});

var config = {
    addMyModel: {
        type: 'save'
    },
    getMyModel: {
        type: 'faind'
    }
};

var user = new Resources(config);

user
    .addMyModel()
    .then((result) => {
        console.log(result);
    });