From 469836d803207a455954ca4014e6a83b65af5daa Mon Sep 17 00:00:00 2001 From: dherault Date: Tue, 1 Mar 2016 17:14:02 +0100 Subject: [PATCH] v1.0.1, bugfixes --- README.md | 2 +- package.json | 2 +- src/index.js | 9 +++++---- src/renderVelocityTemplateObject.js | 15 ++++++++++----- 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 668bd1817..1cfea210c 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ All CLI options are optionnal. ### Usage -Just send your requests to `http://localhost:3000/` as it would be API Gateway. +Just send your requests to `http://localhost:3000/` as it would be API Gateway. The first request might take a few seconds. ### Usage with Babel diff --git a/package.json b/package.json index 7f198618b..d394979a8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "serverless-offline", - "version": "1.0.0", + "version": "1.0.1", "description": "A Serverless plugin to emulate AWS APIG and Lambda offline.", "main": "src/index.js", "scripts": {}, diff --git a/src/index.js b/src/index.js index 61c9197c7..5ef4ff078 100644 --- a/src/index.js +++ b/src/index.js @@ -241,7 +241,7 @@ module.exports = function(ServerlessPlugin, serverlessPath) { if (requestTemplate) { try { - const velocityContext = createVelocityContext(request, this.options.contextOptions); + const velocityContext = createVelocityContext(request, this.options.contextOptions, request.payload || {}); event = renderVelocityTemplateObject(requestTemplate, velocityContext); event.isOffline = true; // console.log('event', event); @@ -289,8 +289,11 @@ module.exports = function(ServerlessPlugin, serverlessPath) { finalResult = finalResult || result; // If there is a responseTemplates, we apply it to the finalResult + // BAD IMPLEMENTATION: first key in responseTemplates const responseTemplates = finalResponse.responseTemplates; - if (responseTemplates) { + const responseTemplate = responseTemplates[Object.keys(responseTemplates)[0]]; + + if (responseTemplate) { // Load the models (Empty and Error from source, others fron user-defined dir...) // Select correct model given in finalResponse @@ -299,8 +302,6 @@ module.exports = function(ServerlessPlugin, serverlessPath) { // respond // not for tonight... - // BAD IMPLEMENTATION: first key in responseTemplates - const responseTemplate = responseTemplates[Object.keys(responseTemplates)[0]]; try { // const JSONResult = JSON.stringify(finalResult); // finalResult = { _offline_root_: finalResult }; diff --git a/src/renderVelocityTemplateObject.js b/src/renderVelocityTemplateObject.js index b475a00e0..75781e8d7 100644 --- a/src/renderVelocityTemplateObject.js +++ b/src/renderVelocityTemplateObject.js @@ -5,8 +5,7 @@ const isPlainObject = require('lodash.isplainobject'); const Compile = Velocity.Compile; const parse = Velocity.parse; -// Set to true for debugging -const VERBOSE = false; +const verbose = process.argv.indexOf('--debugOffline') !== -1; /* This function deeply traverses a plain object's keys (the serverless template, previously JSON) @@ -18,14 +17,14 @@ module.exports = function renderVelocityTemplateObject(templateObject, context) for (let key in templateObject) { const value = templateObject[key]; - if (VERBOSE) console.log('Processing key', key, 'value', value); + if (verbose) console.log('Processing key', key, 'value', value); if (typeof value === 'string') { // This line can throw, but this function does not handle errors const renderResult = (new Compile(parse(value), { escape: false })).render(context); - if (VERBOSE) console.log('-->', renderResult); + if (verbose) console.log('-->', renderResult); // When unable to process a velocity string, render returns the string. // This typically happens when it looks for a value and gets a JS typeerror @@ -50,7 +49,13 @@ module.exports = function renderVelocityTemplateObject(templateObject, context) break; default: - result[key] = renderResult; + let parsed; + try { + parsed = JSON.parse(renderResult); + } + finally { + result[key] = parsed || renderResult; + } break; }