Columbo
RIMMER: You can scoff, Lister. That's nothing new. They laughed at Galileo. They laughed at Edison. They laughed at Columbo.
LISTER: Who's Columbo?
RIMMER: The man with the dirty mac who discovered America.Red Dwarf Season 1, Episode 4
Discovers REST resources in js files named by convention. Is it a good idea? I don't know. Either way, it makes setting up a REST interface pretty easy.
Usage
URLs
Put your resource classes in a folder (the default is name is resources
). Follow the convention:
Library.js
LibraryBook.js
or
LibraryResource.js
LibraryBookResource.js
This will generate resources for the following URLs:
/libraries
/libraries/{libraryId}/books
/libraries/{libraryId}/books/{bookId}
Methods
HTTP Methods are determined by the methods exposed on your resource classes. An OPTIONS resource is generated dynamically.
Your methods should accept whatever arguments your web framework requires.
{ }; LibraryBookprototype { // generates GET with a URL parameter named 'id'}; LibraryBookprototype { // generates GET with no URL parameter}; LibraryBookprototype { // generates GET with no URL parameter (intended for singletons, do no use with retrieve and retrieveAll)}; LibraryBookprototype { // generates POST}; LibraryBookprototype { // generates PUT}; LibraryBookprototype { // generates PATCH}; LibraryBookprototype { // generates DELETE};
API
var Columbo = ; // all options are, well, optionalvar columbo = // The path to the resources directory resourceDirectory: "./resources" // All files in `resourceDirectory` will be `require`d - pass a function here // to override how the resources are created. `resource` will be whatever is // returned from `require` and `name` is the camelised name of the file { return } // This will be called for every resource found - if you need to edit the resource // definition, do it here then pass it to the callback { } // In order to support :id or {id} form url arguments, pass a function here // that will return arguments in the format you desire. { return "{" + id + "}"; } // Responder for OPTIONS requests - the first argument will be an array of // strings corresponding to HTTP verbs, subsequent arguments are supplied by // your web framework of choice. { request; } // Optionally pass in a logger (e.g. Winston) - defaults to console. logger: {}; // Discover resourcesvar resources = columbo;
Above resources
will be an array:
method: String // e.g. GET path: String // e.g. /foo handler: Function // a bound function - the method on the resource class ...
You can then use it with Hapi
var columbo = ; var server = ;server; columbo
Or with Express:
var app = ;...var columbo = { return ":" + id; } { response; };columbo
Etc, etc.
Todo
- Framework agnostic argument validation
- OPTIONS is a little half baked