Permalink
Browse files

prefixed privates with _, documented, made metadata consistent

  • Loading branch information...
1 parent 057c6d7 commit b5de735f0c95b04e808871222dfa9fd14cb9ac19 @coolaj86 committed Jun 14, 2012
Showing with 90 additions and 17 deletions.
  1. +74 −0 http-json/README.md
  2. +14 −15 http-json/index.js
  3. +2 −2 http-json/package.json
View
@@ -0,0 +1,74 @@
+http-json
+===
+
+A `connect` / `http` module which extends `http.ServerResponse.prototype`.
+
+Usage
+===
+
+ (function () {
+ "use strict";
+
+ require('http-json').init(require('http'));
+
+ var connect = require('connect')
+ ;
+
+ connect().use(function () {
+ res.meta('foo-secret', 42);
+
+ // Randomly generate an error
+ if (Date.now() % 3) {
+ res.error(new Error("What time is it? Error time!"));
+ res.json();
+ return;
+ }
+
+ res.json("What time is it? Success time!");
+ }).listen(2323);
+ // http://localhost:2323/
+
+ }());
+
+Response
+---
+
+ {
+ "success": true
+ , "errors": []
+ , "foo-secret": 42
+ , "timestamp": 1339631260781
+ , "result": "What time is it? Success time!"
+ }
+
+API
+===
+
+http.ServerResponse.prototype.json(result, params)
+---
+
+Pass in some `result` object to be attached as `result`
+
+ * sets `Content-Type: application/json; charset=utf-8`
+ * calls `res.write(JSON.stringify({ timestamp: Date.now(), result: result, ... }))`
+ * calls `res.end()`
+
+Optional `params` may be an object such as this
+
+ {
+ debug: false // set `true` if the response should be pretty-printed
+ // also, stack traces should not be removed
+ , stringified: false // set `true` if the result is already `JSON.stringify()`'d
+ }
+
+http.ServerResponse.prototype.meta(key, value)
+---
+
+Add a field to the "header" (sibling of `result`, `success`, etc)
+
+http.ServerResponse.prototype.error(Error)
+---
+
+Add an Error to the `error` array and set `success` to false.
+
+**Note**: By default, stack traces will be removed from errors.
View
@@ -18,26 +18,26 @@
var self = this
;
- self.errors = self.errors || [];
+ self._errors = self._errors || [];
if ('object' !== typeof opts) {
opts = {};
}
opts.message = msg;
opts.code = code;
- self.errors.push(opts);
+ self._errors.push(opts);
};
// For adding things like 'session'
resProto.meta = function (key, val) {
var self = this
;
- self.responseMeta = self.responseMeta || {};
+ self._responseMeta = self._responseMeta || {};
// set
if (undefined !== val) {
- self.responseMeta[key] = val;
+ self._responseMeta[key] = val;
return;
}
@@ -47,38 +47,37 @@
}
// delete
- delete self.responseMeta[key];
+ delete self._responseMeta[key];
};
- resProto.getJson = function (data, opts) {
+ resProto._getJson = function (data, opts) {
var self = this
, json
, response = {}
, space
, replacer
;
- Object.keys(self.responseMeta || {}).forEach(function (key) {
- response[key] = self.responseMeta[key];
+ Object.keys(self._responseMeta || {}).forEach(function (key) {
+ response[key] = self._responseMeta[key];
});
opts = opts || {};
space = (opts.debug) ? ' ': null;
replacer = (opts.debug) ? null : removeStack;
response.timestamp = Date.now();
- response.errors = self.errors || [];
- response.error = !!(response.error || (response.errors.length ? true : false));
- response.success = !response.error;
+ response.errors = self._errors || [];
+ response.success = !response.errors.length;
response.result = data;
- self.statusCode = self.statusCode || opts.statusCode || (response.error ? 400 : 200);
+ self.statusCode = self.statusCode || opts.statusCode || (!response.success ? 400 : 200);
try {
json = JSON.stringify(response, replacer, space);
} catch(e) {
delete e.stack;
self.statusCode = 500;
- json = JSON.stringify({ error: true, errors: [e], statusCode: 500 }, replacer, space);
+ json = JSON.stringify({ timestamp: Date.now(), success: false, errors: [e], statusCode: 500 }, replacer, space);
}
return json;
@@ -93,14 +92,14 @@
opts = opts || {};
if ('string' === typeof data && opts.stringified) {
- jsons = self.getJson('STEVE-TPL-XYZ-987', opts).split('"STEVE-TPL-XYZ-987"');
+ jsons = self._getJson('STEVE-TPL-XYZ-987', opts).split('"STEVE-TPL-XYZ-987"');
jsons = [
jsons[0]
, data
, jsons[1]
];
} else {
- jsons = [self.getJson(data, opts)];
+ jsons = [self._getJson(data, opts)];
}
self.charset = self.charset || 'utf-8';
View
@@ -2,7 +2,7 @@
"author": "AJ ONeal <coolaj86@gmail.com> (http://coolaj86.info/)",
"name": "http-json",
"description": "An extension of the http prototype supporting steve-style json responses in connect",
- "version": "1.0.1",
+ "version": "1.0.2",
"homepage": "http://github.com/coolaj86/steve",
"repository": {
"type": "git",
@@ -15,4 +15,4 @@
"engines": {
"node": "*"
}
-}
+}

0 comments on commit b5de735

Please sign in to comment.