From 35d16b6bd8bd5ab767d70b264186343c492d24cd Mon Sep 17 00:00:00 2001 From: Son Tran-Nguyen Date: Tue, 6 May 2014 14:38:34 -0500 Subject: [PATCH] Support bundle with extension Bundle with extension in its name will has API response served directly instead of the standard JSON response. Currently, the supported extensions are `.kml`, `.xml`, `.json`. Note that when using this type of bundle, only the first key in the bundle will be served. For example, by exporting `sample.kml` instead of just `sample`, the first API's response will be returned when performing request to `/sample.kml`. --- lib/engine.js | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/engine.js b/lib/engine.js index 327b8f9..a69f1a6 100644 --- a/lib/engine.js +++ b/lib/engine.js @@ -68,7 +68,31 @@ var sendResponse = function(jDoc, myRes, ip, bid, callback, gzip) { var responseHeaders = {'Content-Type': responseType, 'vary': 'Accept-Encoding', 'max-age': jDoc.secleft, 'cache-control': 'public, max-age='+jDoc.secleft+', no-transform', "Expires": jDoc.expires, "Last-Modified": jDoc.lastModified}; - doc = JSON.stringify(jDoc); + var ext = bid.match(/\.[\w]+$/); + if (ext && (ext = ext[0])) { + // When the bid has an extension, we only return the first api. + var first = _.find(jDoc, function(api, key) { + return api && api.cname; + }); + doc = first.result; + + switch (ext) { + case ".kml": + case ".xml": + // When expecting response is KML or XML, we set the correct Content-Type + // and wrap it with quotes if it's a JSONP request. + responseHeaders['Content-Type'] = 'text/xml'; + callback && (doc = "'"+ doc+"'"); + break; + case ".json": + // Content-Type for JSON is already set above. + doc = JSON.stringify(doc); + default: + break; + } + } else { + doc = JSON.stringify(jDoc); + } if (callback) { doc = callback + '(' + doc + ');';