Skip to content

Commit

Permalink
Fixing error() such that it routes through the instance of Tenso,…
Browse files Browse the repository at this point in the history
… adding support for `HTML`, `YAML`, & `XML` output via new renderers (`JSON` remains of course); `HTML` is not implemented yet

Building
  • Loading branch information
avoidwork committed Mar 29, 2015
1 parent ab991b9 commit d2fa0dd
Show file tree
Hide file tree
Showing 15 changed files with 299 additions and 50 deletions.
1 change: 1 addition & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module.exports = function (grunt) {
"src/keymaster.js",
"src/prepare.js",
"src/rate.js",
"src/renderers.js",
"src/response.js",
"src/zuul.js",
"src/outro.js"
Expand Down
6 changes: 6 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@
"dtrace": false,
"stack": false
},
"renderers": [
"html",
"yaml",
"xml",
"json"
],
"security": {
"key": "x-csrf-token",
"secret": "tenso",
Expand Down
8 changes: 4 additions & 4 deletions doc/global.html
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ <h5>Returns:</h5>


<dt>
<h4 class="name" id="hypermedia"><span class="type-signature"></span>hypermedia<span class="signature">(server, req, rep, headers)</span><span class="type-signature"> &rarr; {Undefined}</span></h4>
<h4 class="name" id="hypermedia"><span class="type-signature"></span>hypermedia<span class="signature">(server, req, rep, headers)</span><span class="type-signature"> &rarr; {Object}</span></h4>


</dt>
Expand Down Expand Up @@ -903,7 +903,7 @@ <h5>Returns:</h5>


<div class="param-desc">
<p>undefined</p>
<p>HTTP response body</p>
</div>


Expand All @@ -914,7 +914,7 @@ <h5>Returns:</h5>
</dt>
<dd>

<span class="param-type">Undefined</span>
<span class="param-type">Object</span>


</dd>
Expand Down Expand Up @@ -1826,7 +1826,7 @@ <h5>Returns:</h5>

<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.2.2</a>
on 2015-03-26T07:17:54-04:00 using the <a
on 2015-03-29T12:02:13-04:00 using the <a
href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
</span>
</footer>
Expand Down
2 changes: 1 addition & 1 deletion doc/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ <h2>License</h2>

<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.2.2</a>
on 2015-03-26T07:17:54-04:00 using the <a
on 2015-03-29T12:02:13-04:00 using the <a
href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
</span>
</footer>
Expand Down
65 changes: 63 additions & 2 deletions doc/module-tenso.html
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ <h2>
<dt class="tag-version">Version:</dt>
<dd class="tag-version">
<ul class="dummy">
<li>1.2.6</li>
<li>1.3.0</li>
</ul>
</dd>

Expand Down Expand Up @@ -242,6 +242,67 @@ <h5>Type:</h5>










</dl>



</dd>



<dt class="name" id="renderers">
<h4><span class="type-signature">&lt;inner> </span>renderers<span class="type-signature"> :Object</span></h4>


</dt>
<dd>

<div class="description">
<p>Renderers</p>
</div>



<h5>Type:</h5>
<ul>
<li>

<span class="param-type">Object</span>


</li>
</ul>



<dl class="details">























Expand Down Expand Up @@ -283,7 +344,7 @@ <h5>Type:</h5>

<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.2.2</a>
on 2015-03-26T07:17:54-04:00 using the <a
on 2015-03-29T12:02:13-04:00 using the <a
href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
</span>
</footer>
Expand Down
2 changes: 1 addition & 1 deletion doc/modules.list.html
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ <h2>

<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.2.2</a>
on 2015-03-26T07:17:54-04:00 using the <a
on 2015-03-29T12:02:13-04:00 using the <a
href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
</span>
</footer>
Expand Down
74 changes: 68 additions & 6 deletions lib/tenso.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
* @license BSD-3 <https://raw.github.com/avoidwork/tenso/master/LICENSE>
* @link http://avoidwork.github.io/tenso
* @module tenso
* @version 1.2.6
* @version 1.3.0
*/
const CONFIG = require( __dirname + "/../config.json" );
const VERSION = "1.2.6";
const VERSION = "1.3.0";
const SERVER = "tenso/" + VERSION;

let keigai = require( "keigai" ),
Expand All @@ -22,6 +22,8 @@ let keigai = require( "keigai" ),
merge = util.merge,
string = util.string,
uuid = util.uuid,
xml = util.xml,
yaml = require( "yamljs" ),
turtleio = require( "turtle.io" ),
session = require( "express-session" ),
cookie = require( "cookie-parser" ),
Expand Down Expand Up @@ -155,6 +157,32 @@ class Tenso {
return this;
}

/**
* Renders a response body, defaults to JSON
*
* @method render
* @memberOf Tenso
* @param {Object} req Client request
* @param {Object} arg HTTP response body
* @param {Object} headers HTTP response headers
* @return {String} HTTP response body
*/
render ( req, arg, headers ) {
let accept = req.headers.accept || "application/json";
let format = "json";

array.iterate( this.server.config.renderers || [], function ( i ) {
if ( accept.indexOf( i ) > -1 ) {
format = i;
return false;
}
} );

headers["content-type"] = renderers[ format ].header;

return renderers[ format ].fn( arg );
}

/**
* Sends a response to the Client
*
Expand Down Expand Up @@ -187,7 +215,7 @@ class Tenso {
ref[ 0 ][ this.server.config.security.key ] = res.locals[ this.server.config.security.key ];
}

this.server.respond( req, res, hypermedia( this.server, req, response( arg, status ), ref[ 0 ] ), status, ref[ 0 ] );
this.server.respond( req, res, this.render( req, hypermedia( this.server, req, response( arg, status ), ref[ 0 ] ), ref[ 0 ] ), status, ref[ 0 ] );
}

return this;
Expand Down Expand Up @@ -722,7 +750,9 @@ let bootstrap = ( obj, config ) => {

// Starting API server
obj.server.start( config, ( req, res, status, msg ) => {
error( obj.server, req, res, status, msg || obj.messages[ status ] );
var err = msg instanceof Error ? msg : new Error( msg || obj.messages[ status ] );

error( obj, req, res, status, err, obj );
} );

if ( notify ) {
Expand All @@ -739,7 +769,7 @@ let bootstrap = ( obj, config ) => {
* @return {Undefined} undefined
*/
let error = ( server, req, res, status, err ) => {
server.respond( req, res, prepare( null, err, status ), status );
server.respond( req, res, err, status );
};

/**
Expand Down Expand Up @@ -784,7 +814,7 @@ let factory = ( arg ) => {
* @param {Object} req Client request
* @param {Object} rep Serialized representation
* @param {Object} headers HTTP response headers
* @return {Undefined} undefined
* @return {Object} HTTP response body
*/
let hypermedia = ( server, req, rep, headers ) => {
let seen = {},
Expand Down Expand Up @@ -1034,6 +1064,38 @@ let rate = ( obj, req, res, next ) => {
}
};

/**
* Renderers
*
* @type {Object}
*/
let renderers = {
html: {
fn: function ( arg ) {
return arg;
},
header: "text/html"
},
json: {
fn: function ( arg ) {
return arg;
},
header: "application/json"
},
yaml: {
fn: function ( arg ) {
return yaml.stringify( arg, 4 );
},
header: "application/yaml"
},
xml: {
fn: function ( arg ) {
return xml.encode( arg );
},
header: "application/xml"
}
};

/**
* Creates a response
*
Expand Down
Loading

0 comments on commit d2fa0dd

Please sign in to comment.