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

director-reflector

v0.1.0

Published

Reflects [request](https://github.com/mikeal/request) based HTTP client API wrappers from [Director](http://github.com/flatiron/director) routers. Requires **no** additional configuration and is compatible with any existing `Director.Router` instance.

Downloads

7

Readme

Director-Reflector

Reflects request based HTTP client API wrappers from Director routers. Requires no additional configuration and is compatible with any existing Director.Router instance.

Explanation

Director-Reflector removes the process of writing boilerplate client code for communicating with HTTP Director routers. Director-Reflector uses reflection to reflect an API client which maps all remote routes to a local object.

Through the removal of writing boilerplate HTTP API client REST mappings for remote Director routers, Director-Reflector creates a robust, standardized, and re-usable HTTP API client.

Note: Director-Reflector makes the least amount of assumptions possible about your application. The API client wrapper it reflects is designed to be as minimalistic as possible. The default client will cover all routes in any Director routing map out of the box, but you are encouraged to extend the reflected base client with your own personal sugar-syntax.

Default API Client Mappings

If only one verb is bound to the route, the route name becomes the method name.

router.get('/foo')                          =>  client.foo()
router.post('/bar')                         =>  client.bar()

If multiple verbs are bound to the route, each verb becomes a method with a restful name.

router.get('/moo')                          =>  client.moo.get()
router.post('/moo')                         =>  client.moo.create()
router.delete('/moo')                       =>  client.moo.destroy()
router.put('/moo')                          =>  client.moo.save()

If route parameters are used ( such as an id ), they become the last method's first argument.

router.post('/albums/:albumid')             =>  client.albums.create('ill-communication', { artist: "beastie boys" })
router.get('/albums/:albumid')              =>  client.albums.get('ill-communication')

If multiple route parameters are used, the parameters curry as arguments in the last method from left-to-right.

router.post('/albums/:albumid/songs/:id')   =>  client.albums.songs.create('ill-communication', 'root-down', data)
router.get('/albums/:albumid/songs/:id')    =>  client.albums.songs.get('ill-communication', 'root-down')

Nested routing scopes follow the same rules.

router.path('/users', function(){             
 this.path('/:id', function(){               
   this.post(n);                           => client.users.create('bob', data)
   this.get(n);                            => client.users.get('bob')
   this.delete(n);                         => client.users.destroy('bob')
   this.put(n);                            => client.users.save('bob', data)
   this.path('/dongles/:id', function(){
     this.post(n);                         => client.users.dongles.create('bob', 'the-dongle', data)
     this.get(n);                          => client.users.dongles('bob', 'the-dongle');
 })
});

Installation

 npm install director-reflector

Usage

Creating a Director router


var director = require('director'),
    router   = new director.http.Router();

router.get('/foo', function(){
  this.res.end('hello');
});

Creating a new API client from a Director router


var dr = require('director-reflector'),

var client = dr.createClient(router);

client.foo(function(err, res, body){
  console.log(body);
})

Making your Director router portable

In most cases, it's not going to be feasible to expose the entire Router instance to the client.

Have no fear! Director routing maps can safety be serialized without exposing any protected logic.

Run the following code to export a portable ( and safe ) routing map as JSON:

var str    = dr.toJSON(router);

// Somewhere else, where `router` instance is not available
dr.createClient(JSON.parse(str));

Here is an example of an exported routing map.

TODO

  • Add ability for built-in authorization strategies.
  • Add nconf based config management
  • Add more tests