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

resourcedjs

v0.0.7

Published

a simple restful resource url mapper

Downloads

5

Readme

Resourced

Resourced is a nice nifty library that provides a basic standard api for creating a combination of restful urls to function calls,it generates the basic standard sets and allows the developer to push beyond by mapping.

Installation

npm install resourcedjs

Examples

Lets require and setup our resourced object for restful mapping:

  var resd = require('resourced');

  var res = resd.make('Users',{});

  res.use({
	find: function(){
		/* handles all get request for /users on http 'get' method */
	},
	findOne: function(){
		/* handles all get request for /users/:id on http 'get' method */
	},
	update: function(){
		/* handles all get request for /users/:id on http 'put' method */
	},
	create: function(){
		/* handles all get request for /users on http 'post' method */
	},
	destroy: function(){
		/* handles all get request for /users/:id on http 'delete' method */
	},
	patch: function(){
		/* handles all get request for /users/:id on http 'patch' method */
	},
	track: function(){
    		/* handles all get request for /users and /users/:id on http 'track' method */
	},
	trackAll: function(){
   		/* handles all get request for /users on http 'track' method */
	},
  proxyComments: function(req){
    /* handles all routes for the /users/comments request regardless of http methods */
    /* you get to decide how you want to deal with these,maybe send to another resourced object
      or do all the delegation work here and return the result as part of the result for the request
    */

},
  });

Lets include a embedded model also:

res.has('comments');

This creates a '/users/comments' route that is called on all http request regardless of the method, this allows the user of the api to forward the request to another resourced object or to their own internal api or library. Now we can make a series of requests and see how it behave nicely.

res.request('/users','get');
res.request('/users','post');
res.request('/users','track');
res.request('/users/1','get');
res.request('/users/1','put');
res.request('/users/1','delete');
res.request('/users/1','track');
res.request('/users/comments','get');
res.request('/users/comments/1','put');

res.on('badRequest:Provider',function(c){
  /* returns a map({}) containing the url parsed from the internal resourced router */
});

Lets add a custom route with a custom mapping: (Routd: http://github.com/influx6/routd - resourced internal router)

/*

 Resourced.prototype.add
 Arguments: method, route, mapping, routerConfig
 	method: any http method or custom method to watch for
 	route: the route to be used
 	mapping: the function name to use as the mapping provider
 	routerConfig: a map({}) containing configuration for the internal router as to this route,look to
 	Routd github page for more information
*/

res.add('get','/users/:name','findName',{
	   exactMatch: false,
	   params:{
	        name: 'string'
	   },
   validators: {
   	name: function(f){ return typeof f === 'string'; }
   }
});

res.use({
 	findName: function(){
		// deal with all /users/:name route calls on the http 'get' method
 	}
});

Now all request of the type '/users/:string', will be directed to the function 'findName'.

We can eqauly stop listening for specific requests type. Lets stop listening for 'get' request for our comments embedded route:

/*
 Resourced.prototype.remove
 Arguments: route, method, shouldRemoveInRouter?
 	route: the route used
 	method: the specific method to stop watching
 	shouldRemoveInRouter: should just stop listening for the route in the router,makes the method of no effect as it overrides all methods
*/

res.remove('/users/comments','get',/* true | false */);

res.request('/users/comments/1','put');

The last request is not dealt with but is transfered to badRequest:Provider listener, unless the 'default' provider mapping is changed to perform a different action.