-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
1,281 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
npm-debug.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/examples | ||
/misc | ||
/web |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2015 Takatoshi Matsumoto | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# api-gateway-mapping-template | ||
|
||
make AWS API Gateway's Mapping Template testable | ||
|
||
![Image](http://toqoz.net/art/api-gateway-mapping-template.png) | ||
|
||
## Installation | ||
|
||
``` | ||
npm install api-gateway-mapping-template | ||
``` | ||
|
||
## Examples | ||
|
||
See [./examples](/examples) | ||
|
||
## API | ||
|
||
```node | ||
var mappingTemplate = require('api-gateway-mapping-template') | ||
``` | ||
|
||
### mappingTemplate(template, payload, params, context) | ||
|
||
This function renders AWS API Gateway's Mapping Template by using given payload, params and context. | ||
|
||
- Arguments | ||
- template - **required** - `String|Buffer` | ||
- payload - **required** - `String|Buffer` | ||
- params - `map` | ||
- path - `map<String, String|Number|Boolean|null>` | ||
- querystring - `map<String, String|Number|Boolean|null>` | ||
- header - `map<String, String|Number|Boolean|null>` | ||
- context - `map` | ||
- indentity - `map<String, String>` | ||
- cognitoAuthenticationType - `String` | ||
- cognitoIdentityId - `String` | ||
- cognitoIdentityPoolId - `String` | ||
- sourceIp - `String` | ||
- user - `String` | ||
- userAgent - `String` | ||
- userArn - `String` | ||
- requestId - `String` | ||
- resourceId - `String` | ||
- resourcePath - `String` | ||
- stage - `String` | ||
- Return value | ||
- rendered template - `String` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Express + Mapping Template | ||
|
||
1. run server `node app.js` | ||
2. request `curl -X POST -d 'aaa=bbb' 'http://localhost:3000'` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
var fs = require('fs'); | ||
|
||
var express = require('express'); | ||
var app = express(); | ||
// enable to get request body in a handler... | ||
app.use(function(req, res, next) { | ||
req.rawBody = ''; | ||
req.setEncoding('utf8'); | ||
req.on('data', function(chunk) { req.rawBody += chunk; }); | ||
req.on('end', function() { next(); }); | ||
}); | ||
|
||
var mappingTemplate = require("../.."); | ||
|
||
app.post("/", function(req, res) { | ||
fs.readFile(__dirname + "/mapping_template.vtl", function(err, data) { | ||
var template = data.toString(); | ||
var payload = req.rawBody; | ||
var params = {header: req.headers, path: req.params, querystring: req.query}; | ||
|
||
var json = mappingTemplate(template, payload, params); | ||
var event = JSON.parse(json); | ||
|
||
require("./lambda").handler(event, {succeed: res.send.bind(res)}); | ||
}); | ||
}); | ||
|
||
app.listen(3000); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
exports.handler = function(event, context) { | ||
var response = "--- HEADERS ---\n"; | ||
|
||
Object.keys(event.headers).forEach(function(k) { | ||
response += k + " = " + event.headers[k] + "\n"; | ||
}); | ||
|
||
response += "\n--- QUERYSTRINGS ---\n"; | ||
|
||
Object.keys(event.querystring).forEach(function(k) { | ||
response += k + " = " + event.querystring[k] + "\n"; | ||
}); | ||
|
||
response += "\n--- FORM VALUES ---\n"; | ||
|
||
Object.keys(event.form).forEach(function(k) { | ||
response += k + " = " + event.form[k] + "\n"; | ||
}); | ||
|
||
context.succeed(response); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"headers": { | ||
#foreach($key in $input.params().header.keySet()) | ||
"$key": "$input.params().header.get($key)"#if($foreach.hasNext), #end | ||
#end | ||
}, | ||
"querystring": { | ||
#foreach($key in $input.params().querystring.keySet()) | ||
"$key": "$input.params().querystring.get($key)"#if($foreach.hasNext), #end | ||
#end | ||
}, | ||
"form": { | ||
#set($form = $input.path('$').split('&')) | ||
#foreach($kvString in $form) | ||
#set($kv = $kvString.split('=')) | ||
"$kv[0]": "$util.urlDecode($kv[1])"#if($foreach.hasNext),#end | ||
#end | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
var clone = require('clone'); | ||
var Velocity = require('velocityjs'); | ||
var _jsonpath = require('JSONPath'); | ||
var jsonpath = function(obj, path) { | ||
if (path === '$') { | ||
return obj; | ||
} | ||
|
||
var result = _jsonpath({ | ||
json: obj, | ||
path: path | ||
}); | ||
|
||
if (result.length === 1) { | ||
return result[0]; | ||
} else { | ||
return result; | ||
} | ||
}; | ||
|
||
module.exports = function(template, payload, params, context) { | ||
params = clone(params || {}); | ||
params.path = params.path || {}; | ||
params.querystring = params.querystring || {}; | ||
params.header = params.header || {}; | ||
|
||
context = clone(context || {}); | ||
context.identity = context.identity || {}; | ||
|
||
// API Gateway Mapping Template Reference | ||
// http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html | ||
var data = { | ||
context: context, | ||
input: { | ||
_payload: payload.toString(), | ||
path: function(path) { | ||
var obj; | ||
// if payload starts with `{` or `[` or `"`, treat as JSON | ||
if (/^\s*(?:{|\[|")/.test(this._payload)) { | ||
obj = JSON.parse(this._payload); | ||
} else { | ||
// treat as string | ||
obj = this._payload; | ||
} | ||
|
||
return jsonpath(obj, path); | ||
}, | ||
json: function(path) { | ||
var obj = JSON.parse(payload); | ||
if (typeof obj === 'string') { | ||
// re-parse when parsed payload is string. | ||
// because of | ||
// - https://github.com/ToQoz/api-gateway-mapping-template/blob/master/test/_.md#example-0ce08526 | ||
// - https://github.com/ToQoz/api-gateway-mapping-template/blob/master/test/_.md#example-1b8d22cd | ||
obj = JSON.parse(obj); | ||
} | ||
|
||
return JSON.stringify(jsonpath(obj, path)); | ||
}, | ||
params: function(x) { | ||
switch (true) { | ||
case x === undefined: | ||
return params; | ||
case x in params.path: | ||
return params.path[x]; | ||
case x in params.querystring: | ||
return params.querystring[x]; | ||
case x in params.header: | ||
return params.header[x]; | ||
} | ||
}, | ||
}, | ||
util: { | ||
escapeJavaScript: escapeJavaScript, | ||
urlEncode: encodeURIComponent, | ||
urlDecode: decodeURIComponent, | ||
base64Encode: base64Encode, | ||
base64Decode: base64Decode, | ||
} | ||
}; | ||
|
||
// API Gateway convert function to "{}" on toString. | ||
var returnEmptyObject = function() { return "{}"; }; | ||
[ | ||
data.input, | ||
data.input.params, | ||
data.input.path, | ||
data.input.json, | ||
data.util, | ||
data.util.escapeJavaScript, | ||
data.util.urlEncode, | ||
data.util.urlDecode, | ||
data.util.base64Encode, | ||
data.util.base64Decode, | ||
].forEach(function(f) { | ||
f.toString = returnEmptyObject; | ||
}); | ||
|
||
var ast = Velocity.parse(template.toString()); | ||
return (new Velocity.Compile(ast)).render(data); | ||
}; | ||
|
||
function base64Encode(x) { | ||
return (new Buffer(x)).toString('base64'); | ||
} | ||
|
||
function base64Decode(x) { | ||
return (new Buffer(x, 'base64')).toString(); | ||
} | ||
|
||
// I recognize $util.escapeJavaScript as almost `escapeJSONString` and implemented so. | ||
// c.f. 24.3.2.2 Runtime Semantics: QuoteJSONString ( value ) | ||
// http://www.ecma-international.org/ecma-262/6.0/index.html#sec-quotejsonstring | ||
// DO: 2.a -> 2.b -> 2.c -> 2.d | ||
var escapeJavaScriptTable = { | ||
'"': '\"', // 2.a | ||
'\\': '\\\\', | ||
'\b': '\\b', // 2.b (skip abbrev) | ||
'\f': '\\f', | ||
'\n': '\\n', | ||
'\r': '\\r', | ||
'\t': '\\t', | ||
}; | ||
// 2.c | ||
for (var code = 0; code < 20; code++) { | ||
escapeJavaScriptTable[String.fromCharCode(code)] = ((code < 16) ? '\\u000' : '\\u00') + code.toString(16); | ||
} | ||
function escapeJavaScript(x) { | ||
return x.split("").map(function(c) { | ||
// 2.a - 2.c | ||
if (c in escapeJavaScriptTable) { | ||
return escapeJavaScriptTable[c]; | ||
} | ||
|
||
// 2.d | ||
return c; | ||
}).join(""); | ||
} |
Oops, something went wrong.