• Stars
    star
    156
  • Rank 239,589 (Top 5 %)
  • Language
    JavaScript
  • Created over 13 years ago
  • Updated almost 13 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Resource oriented abstraction layer for JSON-REST

Alt text

porter is a lightweight, resourced oriented, abstraction layer for JSON-REST. It will generate methods needed to access resources based on a JSON configuration. It will balance your code's signal to noise ratio by simplifying the communication interfaces.

var porter = Porter({

  users: {
    list: ['get', '/api/users/:partialname'],
    update: ['post', '/api/apps/:username']
  },

  apps: {
    list: ['get', '/api/apps/:username'],
    create: ['post', '/api/apps/:username/:appname']
  }

});

Produces the following methods.

  porter.users.list(/* ... */);
  porter.users.update(/* ... */);
  porter.apps.list(/* ... */);
  porter.apps.create(/* ... */);

The Porter constructor takes a single object literal containing members grouped by resource. Resources are then expressed as arrays. In the case of defining a REST call, there is a verb and a path, where each path can have tokens in it that will get supplanted when used. Here is the above definition put in use...

Payload and Parameters

porter.users.list(

  { partialname: 'bill' }, // replaces the ':partialname' token in the 'list' resource's URI.
  { foo: 10, bar: 20 }, // appends '?foobar=10&bar=20' to the URL when the method is a GET, adds as a message body for a POST.
  function(error, response) {
    // do something...
  }

);

The list function was generated from its definition in the users group. We pass it 1) an object literal that supplants the token in the request url and 2) a callback function that will process when the request is done.

Adding inbound and outbound data validation, and more complex resource organization.

function hasData(data) { // a simple data validator.
  if(typeof data !== 'undefined') {
    return true;
  }
}

var porter = Porter({

  admin: {
    users: {
      list: ['get', '/api/users/:partialname', { outbound: hasData, inbound: hasData }],
      update: ['post', '/api/apps/:username']
    },

    apps: {
      list: ['get', '/api/apps/:username'],
      create: ['post', '/api/apps/:username/:appname']
    }
  }
});

Any arbitrary function can be applied to assert the inbound and outbound data of a request, as seen above. If a validating function returns anything other than true, it is considered invalid and the callback for the resource will will have its 'error' parameter populated with either the exception or the return value of the validator.

Specifying settings that apply to all calls that get made.

var porter = Porter({

  users: {
    list: ['get', '/api/users/:partialname', { outbound: hasData, inbound: hasData }],
    update: ['post', '/api/apps/:username', { inbound: hasData }]
  },

  apps: {
    list: ['get', '/api/apps/:username', { inbound: hasData }],
    create: ['post', '/api/apps/:username/:appname', { inbound: hasData }]
  }

}).use({
  port: 8080,
  inbound: hasData,
  outbound: hasData,
  headers: { 'Accept': 'application/json' }
});

The use function sets the defaults for all calls that get made. It accepts an object literal containing the following members...

port Number - The port of the server that will accept the requests.
inbound Object - A JSONSchema object that will validate against every incoming request.
outbound Object - A JSONSchema object that will validate against every outgoing request.
host String - An IP address of the host server that will accept the requests.
headers Object - An object literal of HTTP request headers that will be attached to each request.
protocol String - The protocol to be used for all requests, ie 'http', 'https'.
lib Object - If you want to use a more full featured, cross-browser friendly ajax library add this back!.

And here is the above code in use...

porter.headers['Authorization'] = 'Basic ' + encodeBase64('username:password');

porter.users.update(
  
  { partialname: 'bill' },
  { address: '555 Mockingbird Ln' },
  
  function(error, response) {
    // do something...
  }
);

The update function was generated from its definition in the users group. We pass it a payload object, some data to replace the url tokens with and a callback function for when the request has finished processing. The app object will also expose the headers collection, this is simply an object literal that contains the headers to be used for the request.

Specifying what to do with the response.

var porter = Porter({

  users: {
    list: ['get', '/api/users/:partialname']
  }

}).use({
  port: 8080,
  host: 'google.com'
}).on({
  '500': function(err, response) {
    // do something...
  },
  '404': function(err, response) {
    // do something...
  }
});

In a lot of cases you'll want to handle http responses based on their response code. using the on method will allow you to associate methods with these response codes. In some cases you'll want to explicitly override these http response code handlers. you can do this by replacing the regular callback method with an object literal containing the items to overwrite.

porter.users.update(
  
  { partialname: 'bill' },
  { address: '555 Mockingbird Ln' },
  
  {
    '404': function(err, response) {
      // do something...
    },
    '500': function(err, response) {
      // do something...
    }
  }
);

Testing and debugging.

Porter provides a simple Node.js server to complement it's test suite. You may find this a useful starting point for your own test suite. Running npm install && npm test from root folder of this project will start development server that will be used for serving tests. Alt text

Credits

Author: @hij1nx

Contributors: @indexzero, @marak, @indutny

Licence

(The MIT License)

Copyright (c) 2011 hij1nx http://www.twitter.com/hij1nx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

levelui

A GUI for LevelDB management based on atom-shell.
CSS
723
star
2

lev

The complete REPL & CLI for managing LevelDB instances.
JavaScript
296
star
3

node-chrome

deprecated
JavaScript
281
star
4

cdir

An interactive console.dir() for node.js similar to console.dir() in webkit.
JavaScript
269
star
5

prompt-sync

a synchronous prompt for node.js
JavaScript
217
star
6

ldb

A C++ REPL / CLI for LevelDB
C++
201
star
7

cxx-http

A simple http library in c++ backed by libuv and http-parser.
C++
183
star
8

complete

Does your Node.js command line program have lots of arguments? Add custom tab completion!
JavaScript
165
star
9

codesurgeon

[DEPRECATED] Build a subset or superset of a javascript codebase via reading the AST.
JavaScript
163
star
10

liveswap

zero downtime re-deploys with Node.js
JavaScript
162
star
11

EventVat

An evented, in-process key/value store for small volatile working sets in Node.js or the Browser
JavaScript
111
star
12

prohub

A Project Perspective for Github
CSS
90
star
13

paramify

Parameterized routes without a big bloated router, e.g. "showtimes/:start/:end" and "files/*"
JavaScript
84
star
14

gmm

Git Module Manager - http://gitmodules.org
Shell
78
star
15

blog

personal blog
HTML
73
star
16

level-replicator

WIP; Eventually consistent log-based multi-master replication for levelDB (@Level)
JavaScript
70
star
17

JUP

A fast JSON to Markup Engine
JavaScript
62
star
18

jsgit-stream

JavaScript
62
star
19

txl

All your html templating needs in under 10 lines of JS.
JavaScript
62
star
20

logmap

Filter json streams/logging output using JSONSelect and format the output
JavaScript
59
star
21

tsd

spins up a server to receive time-series data via tcp streams and then graphs it in the browser via web-sockets
JavaScript
52
star
22

asde

A low level module for destructuring asynchronous values.
JavaScript
50
star
23

pipe

Unix style pipes for nodejs streams `streamA | streamB | streamA`
JavaScript
46
star
24

pxx

a cli tool to store encrypted values in a json file and easily get them out again
JavaScript
40
star
25

net-log

a very fast network-based application logger
JavaScript
39
star
26

bale

A transpiler/module system polyfill for C++
C++
38
star
27

level-users

Store and get users, salt their passwords, persist them to disk, etc.
JavaScript
38
star
28

node-chat

This is very simple code that can be used to demonstrate the caveats and design issues associated with building real-time chat server that uses socket.io.
JavaScript
35
star
29

vines

[not actively maintained] Vines implements the gossip protocol as well as quorum-based voting machinery to facilitate cooperative decision making in distributed systems.
JavaScript
34
star
30

cxx-eventemitter

A minimalist event emitter for C++
C++
33
star
31

debug

A small debugging library for C++
C++
31
star
32

levelweb

LevelDB over http or https.
JavaScript
29
star
33

d

An git based, infrastructure-agnostic deploy tool for node.js
JavaScript
29
star
34

peerdrop

a cross-platform "airdrop" made as a demo for offline camp
JavaScript
27
star
35

route

A simple http router in c++ that can be used with nodeuv-http
C++
27
star
36

level-2pc

A two-phase-commit protocol for leveldb.
JavaScript
26
star
37

cxx-tap

Test Anything Protocol (TAP) Producer for C++
C++
25
star
38

dotfiles

Personal configuration files for unix-like environments.
Vim Script
23
star
39

packvis

package visualizer for the commandline
JavaScript
23
star
40

yl

A tiny flow control module that destructures.
JavaScript
22
star
41

telenode

Multi-vendor cellular network services for node.js
JavaScript
21
star
42

through-cache

a through stream that caches
JavaScript
20
star
43

level-sql

sql for nosql [a work in progress, contributors welcome!]
JavaScript
19
star
44

literate-router

markdown powered routing
JavaScript
17
star
45

cxx-fs

Provides a nodejs-like experience for file I/O in C++ using c++1y and libuv.
C++
17
star
46

level-subtree

build and maintain a tree from the sublevels in a leveldb instance
JavaScript
17
star
47

peerchan

Fully decentralized p2p IRC for your terminal
JavaScript
15
star
48

twitter

A feedless twitter client; broadcast, reply, stay productive. Read a book in your spare time.
JavaScript
15
star
49

to-ml

[deprecated] A tiny module to generate markup
JavaScript
14
star
50

pngify

Text ⇢ PNG ⇢ Text ┈ Pure JS. No dependencies.
JavaScript
13
star
51

cpp-stacktrace

Quick and simple stacktraces for C++
C++
12
star
52

eventemitter

Rust event emitter
Rust
12
star
53

pkp

Public Key Pen! (code from my Nodeconf.eu talk)
JavaScript
12
star
54

redirector

A simple redirect server / URL shortener in C++ backed by nodeuv-http (libuv and http_parser) and leveldb.
C++
12
star
55

forest

Javascript Implementations of SplayTrees, Fusion Trees, BTrees, etc.
JavaScript
11
star
56

node-admin

DEPRECATED
JavaScript
11
star
57

skipfile

Append data, seek forward and seek backward inside a binary file.
JavaScript
10
star
58

TIL

Today I Learned
10
star
59

MacBox

A fork of MacVM that fixes a bunch of UI issues
Swift
10
star
60

json

Better JSON handling for C++
C
10
star
61

tabulate

fit tabular data to the width of your terminal
JavaScript
10
star
62

timeseries-from-gitlog

Represent the git log as time-series data in JSON
JavaScript
10
star
63

cpp-libasync

general purpose asynchronous control flow helpers on thread-safe data structures
C++
9
star
64

stream-response

Respond with `json`, `html`, `text`, or `redirect`. Extend headers easily.
JavaScript
9
star
65

hashd

Recursively hash files in a specified path, appreciate ignore files and patterns.
JavaScript
8
star
66

through2-join

Produce a stream of intersecting data of two or more delimited json streams
JavaScript
8
star
67

go-indexof

The IndexOf() method returns the first index at which a given element can be found in the slice, or -1 if it is not present.
Go
7
star
68

go-tabulate

Column-like layout (padded to fit) for arrays of strings.
Go
7
star
69

ntail

tolerant tailing for your new line delimited json output
JavaScript
7
star
70

node-nosuchmethod

Object.create with a handler for methods that do not exist.
JavaScript
7
star
71

level-key

The easiest way to work with sublevels through multilevel.
JavaScript
7
star
72

subclass

A safe way to extend native types in Node.js and in modern browsers
JavaScript
7
star
73

dtn-website

dtnconf
HTML
7
star
74

level-mget

get multiple values from leveldb
JavaScript
7
star
75

netpeek

this is stupid, dont use it, its just an experiment.
JavaScript
7
star
76

pks

Public Key Server; an invite-only-approach with built in master-master replication.
JavaScript
6
star
77

wc-through

unix wc core util as a through stream, counts into an object
JavaScript
6
star
78

cp-mux

copy files over a multiplexed stream
JavaScript
6
star
79

postmortem

A searchable, issue-only repo for post-mortems (Idea / Template).
6
star
80

admin-template

DEPRECATED
JavaScript
5
star
81

date-at

easily create relative dates. ie: date('+1day')
JavaScript
5
star
82

go-termsize

Get the current size (width and height) of the terminal (nothing more!)
Go
5
star
83

qs

A minimalist dom library
JavaScript
4
star
84

test-env

Conditional output based on an environment variable
JavaScript
3
star
85

platform-module

boilerplate for creating a cross-platform module
JavaScript
3
star
86

getprops

Object.getPropertyDescriptor and Object.getPropertyNames
JavaScript
3
star
87

uvxx

A fork of node.native
C
2
star
88

stream-body

parse a stream correctly (according to the content-type) using raw-body
JavaScript
2
star
89

leveldb

leveldb
C++
2
star
90

electron-issue-17316-poc

Demonstrates issue with missing APIs when contextIsolation is set to true
JavaScript
2
star
91

raylib

C
1
star
92

esauto

Automatic type deduction for javascript
JavaScript
1
star
93

seq-lex

Creates or increments the value of a lexicographically sortable string. useful for creating sequential keys in leveldb
JavaScript
1
star
94

streamordie

A Javascript based band (recorded in front of a async audience)
1
star