-
Notifications
You must be signed in to change notification settings - Fork 19
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
Comments
Yeah, that was on my radar for long time. Sooner or later, it will get implemented. |
|
@Nopik this is a PoC I have been working on so far, it's not complete, but it should be a good starting point. '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));
}; |
In the live AWS environment request parameters are not passed through to the serverless module in the
event
object unless therequestParameters
andrequestTemplates
options ins-function.json
are set up properly. It would be great if serverless-serve mirrored this to make it easier to catch mapping/template problems.The text was updated successfully, but these errors were encountered: