Skip to content

Commit

Permalink
v1.0.1, bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dherault committed Mar 1, 2016
1 parent 25498a4 commit 469836d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion 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": {},
Expand Down
9 changes: 5 additions & 4 deletions src/index.js
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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 };
Expand Down
15 changes: 10 additions & 5 deletions src/renderVelocityTemplateObject.js
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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;
}

Expand Down

0 comments on commit 469836d

Please sign in to comment.