Skip to content

Commit

Permalink
Cleaning up API such that it's less juggling to send a response
Browse files Browse the repository at this point in the history
Updating README

Updating README & turtle.io

Updating default HTML page

Updating copy

Updating CHANGELOG

Updating README
  • Loading branch information
avoidwork committed Aug 2, 2014
1 parent 182d9c6 commit f456bdc
Show file tree
Hide file tree
Showing 19 changed files with 224 additions and 71 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ build/Release
# see https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git
node_modules
.sass-cache/*
.idea/*
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# Change Log

## 0.0.3
- Created `response()` & added it to the exports
- Updated `turtle.io` to 2.1.9 to gain customization of JSON formatting
- Updated routing such that handlers execute with the context of the Tensō instance
- Created `Tenso.prototype.respond()` to provide a very simple method for creating a response from custom routes
- Updated README with more content
- Updated default HTML page when no routes are loaded

## 0.0.2
- Initial implementation of routing via external module
Expand Down
4 changes: 4 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ module.exports = function (grunt) {
files : "package.json",
tasks : "default"
},
readme : {
files : "README.md",
tasks : "default"
},
sass: {
files : "sass/style.scss",
tasks : "default"
Expand Down
70 changes: 49 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,43 @@ Tensō is a REST framework for node.js, designed to simplify the implementation
Creating an API with Tensō is as simple as three statements.

```javascript
var tenso = require( "tenso" ).factory,
var tenso = require( "tenso" ),
routes = require( "./routes.js" ),
app = tenso( {routes: routes} );
```

### Creating Routes
Routes are loaded as a module, with each HTTP method as an export, affording a very customizable API server. The following example will create GET routes that will return an empty `Array` at `/`, an `Error` at `/reports/tps`, & a random number at `/random`. Route handlers have the context of the API server, i.e. `this` will allow you to send a response with `this.respond( req, res, body[, status, headers] )`.
Routes are loaded as a module, with each HTTP method as an export, affording a very customizable API server.

Route handlers have the context of the Tensō server, i.e. `this` will allow you to send a response with `this.respond( req, res, body[, status, headers] )`.

The following example will create GET routes that will return an empty `Array` at `/`, an `Error` at `/reports/tps`, & a version 4 UUID at `/uuid`.

```javascript
var random = require( "keigai" ).util.number.random,
response = require( "tenso" ).response;
var uuid = require( "keigai" ).util.uuid;

module.exports.get = {
"/": [],
"/reports/tps": function ( req, res ) {
this.respond( req, res, response( new Error( "TPS Cover Sheet not attached" ), 785 ), 785 );
this.respond( req, res, new Error( "TPS Cover Sheet not attached" ), 785 );
},
"/random": function ( req, res ) {
this.respond( req, res, response( random() ) );
"/uuid": function ( req, res ) {
this.respond( req, res, uuid() );
}
}
```

## API
### factory( [config] )
Tensō factory, which accepts a configuration Object

### prepare( data[, error, status] )
Prepares a standard response body, use `response()` unless you need to by pass validation

### response( arg[, status] )
Quick way to prepare a response body

## Configuration
This is a sample configuration for Tensō, without authentication or SSL. This would be ideal for development, but not production! Enabling is as easy as providing file paths for the two keys.
This is a sample configuration for Tensō, without authentication or SSL. This would be ideal for development, but not production! Enabling SSL is as easy as providing file paths for the two keys.

```json
{
"hostname": "localhost",
"pageSize": 5,
"port": 8000,
"routes": "routes.js",
"log": {
"routes": require( "./routes.js" ),
"logs": {
"level": "info",
"stdout": true,
"dtrace": false,
"syslog": false
},
Expand All @@ -60,6 +54,40 @@ This is a sample configuration for Tensō, without authentication or SSL. This w
}
```

## Logging
Standard log levels are supported, and are emitted (by configuration) to `stdout` & `stderr`, & `syslog`.


## Dtrace
Dtrace probes are can be enabled by configuration (disabled by default), and can be observed as `turtle-io`; Tensō is built on `turtle.io`.

```
"allowed", "char *", "char *", "char *", "int"
"allows", "char *", "char *", "int"
"compress", "char *", "char *", "int"
"compression", "char *", "int"
"error", "char *", "char *", "int", "char *", "int"
"headers", "int", "int"
"log", "char *", "int", "int", "int"
"proxy", "char *", "char *", "char *", "char *", "int"
"middleware", "char *", "char *", "int"
"request", "char *", "int"
"respond", "char *", "char *", "char *", "int", "int"
"status", "int", "int", "int", "int", "int"
"write", "char *", "char *", "char *", "char *", "int"
```

## Responses
Responses will have a standard shape. Hypermedia (pagination, links, etc.) will be in `data` as `link:[ {"uri": "...", "rel": "..."}, ...]`. Pagination will also be present via the `Link` header.

```json
{
"data": {{ `null` or the response }},
"error": {{ `null` or an `Error` stack trace / message }},
"status": {{ HTTP status code }}
}
```

## License
Copyright (c) 2014 Jason Mulligan
Licensed under the BSD-3 license.
3 changes: 1 addition & 2 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
{
"auth": null,
"hostname": "localhost",
"pageSize": 5,
"port": 8000,
"routes": null,
"log": {
"logs": {
"dtrace": false,
"syslog": false
},
Expand Down
12 changes: 6 additions & 6 deletions doc/global.html
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ <h5>Parameters:</h5>

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="tenso.js.html">tenso.js</a>, <a href="tenso.js.html#sunlight-1-line-34">line 34</a>
<a href="tenso.js.html">tenso.js</a>, <a href="tenso.js.html#sunlight-1-line-60">line 60</a>
</li></ul></dd>


Expand Down Expand Up @@ -353,7 +353,7 @@ <h4 class="name" id="error"><span class="type-signature"></span>error<span class

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="tenso.js.html">tenso.js</a>, <a href="tenso.js.html#sunlight-1-line-75">line 75</a>
<a href="tenso.js.html">tenso.js</a>, <a href="tenso.js.html#sunlight-1-line-103">line 103</a>
</li></ul></dd>


Expand Down Expand Up @@ -494,7 +494,7 @@ <h5>Parameters:</h5>

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="tenso.js.html">tenso.js</a>, <a href="tenso.js.html#sunlight-1-line-85">line 85</a>
<a href="tenso.js.html">tenso.js</a>, <a href="tenso.js.html#sunlight-1-line-113">line 113</a>
</li></ul></dd>


Expand Down Expand Up @@ -681,7 +681,7 @@ <h5>Parameters:</h5>

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="tenso.js.html">tenso.js</a>, <a href="tenso.js.html#sunlight-1-line-113">line 113</a>
<a href="tenso.js.html">tenso.js</a>, <a href="tenso.js.html#sunlight-1-line-141">line 141</a>
</li></ul></dd>


Expand Down Expand Up @@ -845,7 +845,7 @@ <h5>Parameters:</h5>

<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<a href="tenso.js.html">tenso.js</a>, <a href="tenso.js.html#sunlight-1-line-134">line 134</a>
<a href="tenso.js.html">tenso.js</a>, <a href="tenso.js.html#sunlight-1-line-162">line 162</a>
</li></ul></dd>


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

<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.2.2</a>
on Sat Aug 02 2014 09:00:16 GMT-0400 (EDT) using the <a href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
on Sat Aug 02 2014 10:47:22 GMT-0400 (EDT) using the <a href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
</span>
</footer>
</div>
Expand Down
55 changes: 37 additions & 18 deletions doc/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -107,38 +107,33 @@
<p>Tensō is a REST framework for node.js, designed to simplify the implementation of APIs.</p>
<h2>Example</h2>
<p>Creating an API with Tensō is as simple as three statements.</p>
<pre><code class="lang-javascript">var tenso = require( &quot;tenso&quot; ).factory,
<pre><code class="lang-javascript">var tenso = require( &quot;tenso&quot; ),
routes = require( &quot;./routes.js&quot; ),
app = tenso( {routes: routes} );</code></pre>
<h3>Creating Routes</h3>
<p>Routes are loaded as a module, with each HTTP method as an export, affording a very customizable API server. The following example will create GET routes that will return an empty <code>Array</code> at <code>/</code>, an <code>Error</code> at <code>/reports/tps</code>, &amp; a random number at <code>/random</code>. Route handlers have the context of the API server, i.e. <code>this</code> will allow you to send a response with <code>this.respond( req, res, body[, status, headers] )</code>.</p>
<pre><code class="lang-javascript">var random = require( &quot;keigai&quot; ).util.number.random,
response = require( &quot;tenso&quot; ).response;
<p>Routes are loaded as a module, with each HTTP method as an export, affording a very customizable API server.</p>
<p>Route handlers have the context of the Tensō server, i.e. <code>this</code> will allow you to send a response with <code>this.respond( req, res, body[, status, headers] )</code>.</p>
<p>The following example will create GET routes that will return an empty <code>Array</code> at <code>/</code>, an <code>Error</code> at <code>/reports/tps</code>, &amp; a version 4 UUID at <code>/uuid</code>.</p>
<pre><code class="lang-javascript">var uuid = require( &quot;keigai&quot; ).util.uuid;

module.exports.get = {
&quot;/&quot;: [],
&quot;/reports/tps&quot;: function ( req, res ) {
this.respond( req, res, response( new Error( &quot;TPS Cover Sheet not attached&quot; ), 785 ), 785 );
this.respond( req, res, new Error( &quot;TPS Cover Sheet not attached&quot; ), 785 );
},
&quot;/random&quot;: function ( req, res ) {
this.respond( req, res, response( random() ) );
&quot;/uuid&quot;: function ( req, res ) {
this.respond( req, res, uuid() );
}
}</code></pre>
<h2>API</h2>
<h3>factory( [config] )</h3>
<p>Tensō factory, which accepts a configuration Object</p>
<h3>prepare( data[, error, status] )</h3>
<p>Prepares a standard response body, use <code>response()</code> unless you need to by pass validation</p>
<h3>response( arg[, status] )</h3>
<p>Quick way to prepare a response body</p>
<h2>Configuration</h2>
<p>This is a sample configuration for Tensō, without authentication or SSL. This would be ideal for development, but not production! Enabling is as easy as providing file paths for the two keys.</p>
<pre><code class="lang-json">{
&quot;hostname&quot;: &quot;localhost&quot;,
&quot;pageSize&quot;: 5,
&quot;port&quot;: 8000,
&quot;routes&quot;: &quot;routes.js&quot;,
&quot;log&quot;: {
&quot;routes&quot;: require( &quot;./routes.js&quot; ),
&quot;logs&quot;: {
&quot;level&quot;: &quot;info&quot;,
&quot;stdout&quot;: true,
&quot;dtrace&quot;: false,
&quot;syslog&quot;: false
},
Expand All @@ -147,6 +142,30 @@ <h2>Configuration</h2>
&quot;cert&quot;: null
}
}</code></pre>
<h2>Logging</h2>
<p>Standard log levels are supported, and are emitted (by configuration) to <code>stdout</code> &amp; <code>stderr</code>, &amp; <code>syslog</code>.</p>
<h2>Dtrace</h2>
<p>Dtrace probes are can be enabled by configuration (disabled by default), and can be observed as <code>turtle-io</code>; Tensō is built on <code>turtle.io</code>.</p>
<pre><code>&quot;allowed&quot;, &quot;char *&quot;, &quot;char *&quot;, &quot;char *&quot;, &quot;int&quot;
&quot;allows&quot;, &quot;char *&quot;, &quot;char *&quot;, &quot;int&quot;
&quot;compress&quot;, &quot;char *&quot;, &quot;char *&quot;, &quot;int&quot;
&quot;compression&quot;, &quot;char *&quot;, &quot;int&quot;
&quot;error&quot;, &quot;char *&quot;, &quot;char *&quot;, &quot;int&quot;, &quot;char *&quot;, &quot;int&quot;
&quot;headers&quot;, &quot;int&quot;, &quot;int&quot;
&quot;log&quot;, &quot;char *&quot;, &quot;int&quot;, &quot;int&quot;, &quot;int&quot;
&quot;proxy&quot;, &quot;char *&quot;, &quot;char *&quot;, &quot;char *&quot;, &quot;char *&quot;, &quot;int&quot;
&quot;middleware&quot;, &quot;char *&quot;, &quot;char *&quot;, &quot;int&quot;
&quot;request&quot;, &quot;char *&quot;, &quot;int&quot;
&quot;respond&quot;, &quot;char *&quot;, &quot;char *&quot;, &quot;char *&quot;, &quot;int&quot;, &quot;int&quot;
&quot;status&quot;, &quot;int&quot;, &quot;int&quot;, &quot;int&quot;, &quot;int&quot;, &quot;int&quot;
&quot;write&quot;, &quot;char *&quot;, &quot;char *&quot;, &quot;char *&quot;, &quot;char *&quot;, &quot;int&quot;</code></pre>
<h2>Responses</h2>
<p>Responses will have a standard shape. Hypermedia (pagination, links, etc.) will be in <code>data</code> as <code>link:[ {&quot;uri&quot;: &quot;...&quot;, &quot;rel&quot;: &quot;...&quot;}, ...]</code>. Pagination will also be present via the <code>Link</code> header.</p>
<pre><code class="lang-json">{
&quot;data&quot;: {{ `null` or the response }},
&quot;error&quot;: {{ `null` or an `Error` stack trace / message }},
&quot;status&quot;: {{ HTTP status code }}
}</code></pre>
<h2>License</h2>
<p>Copyright (c) 2014 Jason Mulligan<br>Licensed under the BSD-3 license.</p></article>
</section>
Expand All @@ -170,7 +189,7 @@ <h2>License</h2>

<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.2.2</a>
on Sat Aug 02 2014 09:00:16 GMT-0400 (EDT) using the <a href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
on Sat Aug 02 2014 10:47:22 GMT-0400 (EDT) using the <a href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
</span>
</footer>
</div>
Expand Down
2 changes: 1 addition & 1 deletion doc/module-tenso.html
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ <h2>

<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.2.2</a>
on Sat Aug 02 2014 09:00:16 GMT-0400 (EDT) using the <a href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
on Sat Aug 02 2014 10:47:22 GMT-0400 (EDT) using the <a href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
</span>
</footer>
</div>
Expand Down
2 changes: 1 addition & 1 deletion doc/modules.list.html
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ <h2>

<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.2.2</a>
on Sat Aug 02 2014 09:00:16 GMT-0400 (EDT) using the <a href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
on Sat Aug 02 2014 10:47:22 GMT-0400 (EDT) using the <a href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
</span>
</footer>
</div>
Expand Down
36 changes: 31 additions & 5 deletions doc/tenso.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ <h1 class="page-title">Source: tenso.js</h1>
CONFIG = require( __dirname + "/../config.json" ),
keigai = require( "keigai" ),
util = keigai.util,
array = util.array,
clone = util.clone,
iterate = util.iterate,
merge = util.merge;
Expand All @@ -116,6 +117,31 @@ <h1 class="page-title">Source: tenso.js</h1>
this.version = "0.0.3";
}

/**
* Setting constructor loop
*
* @method constructor
* @memberOf Tenso
* @type {Function}
*/
Tenso.prototype.constructor = Tenso;

/**
* Sends a response to the Client
*
* @method respond
* @memberOf Tenso
* @param {Object} req Client request
* @param {Object} res Client response
* @param {Mixed} arg Response body
* @param {Number} status Response status
* @param {Object} headers Response headers
* @return {Undefined} undefined
*/
Tenso.prototype.respond = function ( req, res, arg, status, headers ) {
this.server.respond( req, res, response( arg, status ), status, headers );
};

/**
* Bootstraps an instance of Tenso
*
Expand All @@ -138,7 +164,9 @@ <h1 class="page-title">Source: tenso.js</h1>
iterate( config.routes, function ( routes, method ) {
iterate( routes, function ( arg, route ) {
if ( typeof arg == "function" ) {
obj.server[method]( route, arg );
obj.server[method]( route, function () {
arg.apply( obj, array.cast( arguments ) );
} );
}
else {
obj.server[method]( route, function ( req, res ) {
Expand Down Expand Up @@ -239,9 +267,7 @@ <h1 class="page-title">Source: tenso.js</h1>
}
}

module.exports.factory = factory;
module.exports.prepare = prepare;
module.exports.response = response;
module.exports = factory;
} )();
</pre>
</article>
Expand All @@ -264,7 +290,7 @@ <h1 class="page-title">Source: tenso.js</h1>

<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.2.2</a>
on Sat Aug 02 2014 09:00:16 GMT-0400 (EDT) using the <a href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
on Sat Aug 02 2014 10:47:22 GMT-0400 (EDT) using the <a href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
</span>
</footer>
</div>
Expand Down
Loading

0 comments on commit f456bdc

Please sign in to comment.