From 97b8af923e3c53ba3dabb06cd2e058989c372772 Mon Sep 17 00:00:00 2001 From: Jeff Hann Date: Thu, 20 Aug 2015 14:15:44 -0300 Subject: [PATCH] Adding pretty JSON + addition of [prettyjson](https://github.com/rafeca/prettyjson) to `src/prettyify-response.coffee` so that body and schema objects that are of content type `application/json' are easier to read + update to how we check content type, since types such as `application/json; charset=utf-8` are valid we should not do an exact match on `application/json` and instead do an indexOf --- package.json | 5 +++-- src/prettify-response.coffee | 22 ++++++++++++++-------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index 48822cc19..fe1a7381a 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "dependencies": { "advisable": "~0.2.0", "async": "^0.9.x", + "blueprint-transactions": "^0.0.2", "chai": "^2.1.0", "clone": "^1.0.0", "coffee-script": "^1.9.1", @@ -34,13 +35,13 @@ "node-uuid": "~1.4.2", "optimist": "~0.6.1", "pitboss-ng": "^0.3.1", + "prettyjson": "^1.1.3", "proxyquire": "~1.4.x", "request": "^2.53.0", "setimmediate": "^1.0.2", "spawn-sync": "^1.0.11", "uri-template": "~1.0.0", - "winston": "^1.0.0", - "blueprint-transactions": "^0.0.2" + "winston": "^1.0.0" }, "devDependencies": { "body-parser": "^1.12.0", diff --git a/src/prettify-response.coffee b/src/prettify-response.coffee index afd4b649e..aa4a7b169 100644 --- a/src/prettify-response.coffee +++ b/src/prettify-response.coffee @@ -1,4 +1,5 @@ html = require 'html' +prettyjson = require 'prettyjson' logger = require './logger' @@ -12,15 +13,20 @@ prettifyResponse = (response) -> logger.debug "Could not stringify: " + obj obj + prettifySchema = (schema, contentType) -> + if typeof contentType != 'undefined' && contentType.indexOf('application/json') > -1 + schema = prettyjson.render JSON.parse(schema) + else + schema = stringify schema + schema + prettifyBody = (body, contentType) -> - switch contentType - when 'application/json' - body = stringify body - when 'text/html' - body = html.prettyPrint body, {indent_size: 2} + if typeof contentType != 'undefined' && contentType.indexOf('application/json') > -1 + body = prettyjson.render JSON.parse(body) + else + body = html.prettyPrint body, {indent_size: 2} body - contentType = (response?.headers['content-type'] || response?.headers['Content-Type']) if response?.headers? stringRepresentation = "" @@ -28,8 +34,8 @@ prettifyResponse = (response) -> for own key, value of response if key is 'body' value = '\n' + prettifyBody value, contentType - else if key is 'schema' - value = '\n' + stringify value + else if key is 'schema' || key is 'bodySchema' + value = '\n' + prettifySchema value, contentType else if key is 'headers' header = '\n' for own hkey, hval of value