Skip to content

Commit

Permalink
feat(package): change API signature of module.exports
Browse files Browse the repository at this point in the history
  • Loading branch information
ToQoz committed Dec 15, 2015
1 parent 9652ecb commit 06b320f
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 70 deletions.
39 changes: 20 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,25 @@ var mappingTemplate = require('api-gateway-mapping-template')
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`
- parameters - **required** - `map`
- 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`
11 changes: 8 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ var clone = require('clone');
var Velocity = require('velocityjs');
var jsonpath = workaroundJsonPath(require('JSONPath'));

module.exports = function(template, payload, params, context) {
params = clone(params || {});
module.exports = function(parameters) {
parameters = clone(parameters || {});

var template = parameters.template;
var payload = parameters.payload;

var params = clone(parameters.params || {});
params.path = params.path || {};
params.querystring = params.querystring || {};
params.header = params.header || {};

context = clone(context || {});
var context = clone(parameters.context || {});
context.identity = context.identity || {};

// API Gateway Mapping Template Reference
Expand Down
4 changes: 2 additions & 2 deletions misc/gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,14 +264,14 @@ Result.prototype.toTest = function(out, indentLevel) {
if (this.statusCode !== 200 || this.responseBody.indexOf("{errorMessage=Unable") === 0) {
out.write(sprintf(indent + "describe('H=`%s` P=`%s` ===> T=`%s`', function() {\n", JSON.stringify(this.requestHeaders).replace(/'/g, "\\'"), this.requestBody.replace(/'/g, "\\'"), this.mappingTemplate.replace(/'/g, "\\'")));
out.write(indent + " it('throw error', function() {\n");
out.write(sprintf(indent + " assert.throws(function() { mappingTemplate(%s, %s, {}); });\n", JSON.stringify(this.mappingTemplate), JSON.stringify(this.requestBody)));
out.write(sprintf(indent + " assert.throws(function() { mappingTemplate({template: %s, payload: %s}); });\n", JSON.stringify(this.mappingTemplate), JSON.stringify(this.requestBody)));
out.write( indent + " });\n");
out.write( indent + "});\n");
} else {
out.write(sprintf(indent + "describe('H=`%s` P=`%s` ===> T=`%s`', function() {\n", JSON.stringify(this.requestHeaders).replace(/'/g, "\\'"), this.requestBody.replace(/'/g, "\\'"), this.mappingTemplate.replace(/'/g, "\\'")));
out.write(sprintf(indent + " it('return %s', function() {\n", this.requestBody.replace(/'/g, "\\'")));
out.write(sprintf(indent + " var expected = %s;\n", this.responseBody));
out.write(sprintf(indent + " var actual = JSON.parse(mappingTemplate(%s, %s, {}));\n", JSON.stringify(this.mappingTemplate), JSON.stringify(this.requestBody)));
out.write(sprintf(indent + " var actual = JSON.parse(mappingTemplate({template: %s, payload: %s}));\n", JSON.stringify(this.mappingTemplate), JSON.stringify(this.requestBody)));
out.write( indent + " assert.deepEqual(expected, actual);\n");
out.write( indent + " });\n");
out.write( indent + "});\n");
Expand Down
46 changes: 23 additions & 23 deletions test/_.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,175 +8,175 @@ describe('$input.path|$input.json', function() {
describe('H=`{}` P=`a=b` ===> T=`"$input.path(\'$\')"`', function() {
it('return a=b', function() {
var expected = "a=b";
var actual = JSON.parse(mappingTemplate("\"$input.path('$')\"", "a=b", {}));
var actual = JSON.parse(mappingTemplate({template: "\"$input.path('$')\"", payload: "a=b"}));
assert.deepEqual(expected, actual);
});
});
// https://github.com/ToQoz/api-gateway-mapping-template/blob/master/test/_.md#example-fcaf7ddd
describe('H=`{}` P=`"a=b"` ===> T=`"$input.path(\'$\')"`', function() {
it('return "a=b"', function() {
var expected = "a=b";
var actual = JSON.parse(mappingTemplate("\"$input.path('$')\"", "\"a=b\"", {}));
var actual = JSON.parse(mappingTemplate({template: "\"$input.path('$')\"", payload: "\"a=b\""}));
assert.deepEqual(expected, actual);
});
});
// https://github.com/ToQoz/api-gateway-mapping-template/blob/master/test/_.md#example-a669d28c
describe('H=`{}` P=`a=b` ===> T=`"$input.json(\'$\')"`', function() {
it('throw error', function() {
assert.throws(function() { mappingTemplate("\"$input.json('$')\"", "a=b", {}); });
assert.throws(function() { mappingTemplate({template: "\"$input.json('$')\"", payload: "a=b"}); });
});
});
// https://github.com/ToQoz/api-gateway-mapping-template/blob/master/test/_.md#example-0ce08526
describe('H=`{}` P=`"a=b"` ===> T=`"$input.json(\'$\')"`', function() {
it('throw error', function() {
assert.throws(function() { mappingTemplate("\"$input.json('$')\"", "\"a=b\"", {}); });
assert.throws(function() { mappingTemplate({template: "\"$input.json('$')\"", payload: "\"a=b\""}); });
});
});
// https://github.com/ToQoz/api-gateway-mapping-template/blob/master/test/_.md#example-03be1e25
describe('H=`{}` P=`{}` ===> T=`"$input.path(\'$\')"`', function() {
it('return {}', function() {
var expected = "{}";
var actual = JSON.parse(mappingTemplate("\"$input.path('$')\"", "{}", {}));
var actual = JSON.parse(mappingTemplate({template: "\"$input.path('$')\"", payload: "{}"}));
assert.deepEqual(expected, actual);
});
});
// https://github.com/ToQoz/api-gateway-mapping-template/blob/master/test/_.md#example-1b8d22cd
describe('H=`{}` P=`"{}"` ===> T=`"$input.path(\'$\')"`', function() {
it('return "{}"', function() {
var expected = "{}";
var actual = JSON.parse(mappingTemplate("\"$input.path('$')\"", "\"{}\"", {}));
var actual = JSON.parse(mappingTemplate({template: "\"$input.path('$')\"", payload: "\"{}\""}));
assert.deepEqual(expected, actual);
});
});
// https://github.com/ToQoz/api-gateway-mapping-template/blob/master/test/_.md#example-c1b4a9a5
describe('H=`{}` P=`{}` ===> T=`"$input.json(\'$\')"`', function() {
it('return {}', function() {
var expected = "{}";
var actual = JSON.parse(mappingTemplate("\"$input.json('$')\"", "{}", {}));
var actual = JSON.parse(mappingTemplate({template: "\"$input.json('$')\"", payload: "{}"}));
assert.deepEqual(expected, actual);
});
});
// https://github.com/ToQoz/api-gateway-mapping-template/blob/master/test/_.md#example-25c6993c
describe('H=`{}` P=`name=toqoz` ===> T=`{"name": "$input.path(\'$\')"}`', function() {
it('return name=toqoz', function() {
var expected = {"name":"name=toqoz"};
var actual = JSON.parse(mappingTemplate("{\"name\": \"$input.path('$')\"}", "name=toqoz", {}));
var actual = JSON.parse(mappingTemplate({template: "{\"name\": \"$input.path('$')\"}", payload: "name=toqoz"}));
assert.deepEqual(expected, actual);
});
});
// https://github.com/ToQoz/api-gateway-mapping-template/blob/master/test/_.md#example-122ff6ce
describe('H=`{}` P=`{a` ===> T=`"$input.path(\'$\')"`', function() {
it('throw error', function() {
assert.throws(function() { mappingTemplate("\"$input.path('$')\"", "{a", {}); });
assert.throws(function() { mappingTemplate({template: "\"$input.path('$')\"", payload: "{a"}); });
});
});
// https://github.com/ToQoz/api-gateway-mapping-template/blob/master/test/_.md#example-d4719b3d
describe('H=`{}` P=`a{b` ===> T=`"$input.path(\'$\')"`', function() {
it('return a{b', function() {
var expected = "a{b";
var actual = JSON.parse(mappingTemplate("\"$input.path('$')\"", "a{b", {}));
var actual = JSON.parse(mappingTemplate({template: "\"$input.path('$')\"", payload: "a{b"}));
assert.deepEqual(expected, actual);
});
});
// https://github.com/ToQoz/api-gateway-mapping-template/blob/master/test/_.md#example-2b1f2df4
describe('H=`{}` P=`[a` ===> T=`"$input.path(\'$\')"`', function() {
it('throw error', function() {
assert.throws(function() { mappingTemplate("\"$input.path('$')\"", "[a", {}); });
assert.throws(function() { mappingTemplate({template: "\"$input.path('$')\"", payload: "[a"}); });
});
});
// https://github.com/ToQoz/api-gateway-mapping-template/blob/master/test/_.md#example-c0cfe50d
describe('H=`{}` P=`a[` ===> T=`"$input.path(\'$\')"`', function() {
it('return a[', function() {
var expected = "a[";
var actual = JSON.parse(mappingTemplate("\"$input.path('$')\"", "a[", {}));
var actual = JSON.parse(mappingTemplate({template: "\"$input.path('$')\"", payload: "a["}));
assert.deepEqual(expected, actual);
});
});
// https://github.com/ToQoz/api-gateway-mapping-template/blob/master/test/_.md#example-a7361ae0
describe('H=`{}` P=`null{` ===> T=`"$input.path(\'$\')"`', function() {
it('return null{', function() {
var expected = "null{";
var actual = JSON.parse(mappingTemplate("\"$input.path('$')\"", "null{", {}));
var actual = JSON.parse(mappingTemplate({template: "\"$input.path('$')\"", payload: "null{"}));
assert.deepEqual(expected, actual);
});
});
// https://github.com/ToQoz/api-gateway-mapping-template/blob/master/test/_.md#example-728fbd1f
describe('H=`{}` P=`true{` ===> T=`"$input.path(\'$\')"`', function() {
it('return true{', function() {
var expected = "true{";
var actual = JSON.parse(mappingTemplate("\"$input.path('$')\"", "true{", {}));
var actual = JSON.parse(mappingTemplate({template: "\"$input.path('$')\"", payload: "true{"}));
assert.deepEqual(expected, actual);
});
});
// https://github.com/ToQoz/api-gateway-mapping-template/blob/master/test/_.md#example-461fd8fa
describe('H=`{}` P=`false{` ===> T=`"$input.path(\'$\')"`', function() {
it('return false{', function() {
var expected = "false{";
var actual = JSON.parse(mappingTemplate("\"$input.path('$')\"", "false{", {}));
var actual = JSON.parse(mappingTemplate({template: "\"$input.path('$')\"", payload: "false{"}));
assert.deepEqual(expected, actual);
});
});
// https://github.com/ToQoz/api-gateway-mapping-template/blob/master/test/_.md#example-48665148
describe('H=`{}` P=`undefined{` ===> T=`"$input.path(\'$\')"`', function() {
it('return undefined{', function() {
var expected = "undefined{";
var actual = JSON.parse(mappingTemplate("\"$input.path('$')\"", "undefined{", {}));
var actual = JSON.parse(mappingTemplate({template: "\"$input.path('$')\"", payload: "undefined{"}));
assert.deepEqual(expected, actual);
});
});
// https://github.com/ToQoz/api-gateway-mapping-template/blob/master/test/_.md#example-cd963d7c
describe('H=`{}` P=`` ===> T=`$input`', function() {
it('return ', function() {
var expected = {};
var actual = JSON.parse(mappingTemplate("$input", "", {}));
var actual = JSON.parse(mappingTemplate({template: "$input", payload: ""}));
assert.deepEqual(expected, actual);
});
});
// https://github.com/ToQoz/api-gateway-mapping-template/blob/master/test/_.md#example-ec2bf3b4
describe('H=`{}` P=`` ===> T=`$input.keySet`', function() {
it('return ', function() {
var expected = {};
var actual = JSON.parse(mappingTemplate("$input.keySet", "", {}));
var actual = JSON.parse(mappingTemplate({template: "$input.keySet", payload: ""}));
assert.deepEqual(expected, actual);
});
});
// https://github.com/ToQoz/api-gateway-mapping-template/blob/master/test/_.md#example-3e03488f
describe('H=`{}` P=`` ===> T=`$input.params.keySet`', function() {
it('return ', function() {
var expected = {};
var actual = JSON.parse(mappingTemplate("$input.params.keySet", "", {}));
var actual = JSON.parse(mappingTemplate({template: "$input.params.keySet", payload: ""}));
assert.deepEqual(expected, actual);
});
});
// https://github.com/ToQoz/api-gateway-mapping-template/blob/master/test/_.md#example-a35f2355
describe('H=`{}` P=`` ===> T=`$util`', function() {
it('return ', function() {
var expected = {};
var actual = JSON.parse(mappingTemplate("$util", "", {}));
var actual = JSON.parse(mappingTemplate({template: "$util", payload: ""}));
assert.deepEqual(expected, actual);
});
});
// https://github.com/ToQoz/api-gateway-mapping-template/blob/master/test/_.md#example-29301967
describe('H=`{}` P=`` ===> T=`$input.params`', function() {
it('return ', function() {
var expected = {};
var actual = JSON.parse(mappingTemplate("$input.params", "", {}));
var actual = JSON.parse(mappingTemplate({template: "$input.params", payload: ""}));
assert.deepEqual(expected, actual);
});
});
// https://github.com/ToQoz/api-gateway-mapping-template/blob/master/test/_.md#example-819c3c26
describe('H=`{}` P=`` ===> T=`$input.json`', function() {
it('return ', function() {
var expected = {};
var actual = JSON.parse(mappingTemplate("$input.json", "", {}));
var actual = JSON.parse(mappingTemplate({template: "$input.json", payload: ""}));
assert.deepEqual(expected, actual);
});
});
// https://github.com/ToQoz/api-gateway-mapping-template/blob/master/test/_.md#example-caa3f41d
describe('H=`{}` P=`` ===> T=`$util.urlEncode`', function() {
it('return ', function() {
var expected = {};
var actual = JSON.parse(mappingTemplate("$util.urlEncode", "", {}));
var actual = JSON.parse(mappingTemplate({template: "$util.urlEncode", payload: ""}));
assert.deepEqual(expected, actual);
});
});
Expand Down
Loading

0 comments on commit 06b320f

Please sign in to comment.