Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build event object based on requestParameters/requestTemplates #4

Open
jagthedrummer opened this issue Dec 22, 2015 · 3 comments
Open

Comments

@jagthedrummer
Copy link

In the live AWS environment request parameters are not passed through to the serverless module in the event object unless the requestParameters and requestTemplates options in s-function.json are set up properly. It would be great if serverless-serve mirrored this to make it easier to catch mapping/template problems.

@Nopik
Copy link
Owner

Nopik commented Dec 24, 2015

Yeah, that was on my radar for long time. Sooner or later, it will get implemented.

@kayoub5
Copy link

kayoub5 commented Dec 29, 2015

requestTemplates are actually written using Apache Velocity, a template engine do exist in JavaScript velocity.js.
So all needed to do is recreate the variables $context, $input, $stageVariables and $util.
I would have done it myself, but I am short on time at the moment.

@kayoub5
Copy link

kayoub5 commented Mar 2, 2016

@Nopik this is a PoC I have been working on so far, it's not complete, but it should be a good starting point.
the below snippet generates an event object from an express request object, and a serverless endpoint request template

'use strict';

const jsonpath = require('jsonpath');
class Input {
  constructor(req) {
    var self = this;
    self._body = req.body;
    self._path = {};
    self._querystring = {};
    self._header = {};
    self._params = {
      path: self._path,
      querystring: self._querystring,
      header: self._header
    };
    Object.keys(req.headers).forEach(function(key) {
      let newkey = key.replace(/((?:^|-)[a-z])/g, function(val) {
        return val.toUpperCase();
      });
      // custom hack for X-Parse-Os-Version ==> X-Parse-OS-Version
      newkey = newkey.replace(/(-Os-)/g, function(val) {
        return val.toUpperCase();
      });
      self._header[newkey] = req.headers[key];
      self._params[newkey] = req.headers[key];
    });
    Object.keys(req.query).forEach(function(key) {
      self._querystring[key] = req.query[key];
      self._params[key] = req.query[key];
    });
    Object.keys(req.params).forEach(function(key) {
      self._path[key] = req.params[key];
      self._params[key] = req.params[key];
    });
  }
  path(p) {
    let value = jsonpath.value(this._body, p);
    if (value === null || value === undefined) {
      value = '';
    }
    return value;
  }
  json(p) {
    return JSON.stringify(this.path(p));
  }
  params(key) {
    if (!arguments.length) {
      return this._params;
    }
    return this._params[key] || '';
  }
}

let util = {
  escapeJavaScript: function(obj) {
    return JSON.stringify(obj).slice(1, -1);
  },
  urlDecode: decodeURI,
  urlEncode: encodeURI,
  base64Encode: function(data) {
    return new Buffer(data).toString('base64');
  },
  base64Decode: function(data) {
    return new Buffer(data, 'base64').toString('utf8');
  }
};

function createContext(req) {
  return {
    input: new Input(req),
    util: util
  };
}

module.exports.createEvent = function(template, req) {
  if (!template) {
    return req.body;
  }
  if (typeof template !== 'string') {
    template = JSON.stringify(template);
  }
  let Velocity = require( 'velocityjs' );
  return Velocity.render(template, createContext(req));
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants