This repository was archived by the owner on May 6, 2022. It is now read-only.
forked from domderen/react-lambda-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
69 lines (59 loc) · 2.72 KB
/
main.js
File metadata and controls
69 lines (59 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import co from 'co';
import { render } from './server/render';
/**
* Transforms strange AWS properties syntax, into a proper JSON string.
* @param {String} str String containing the JSON-like structure of parameters.
* @return {String} Returns a JSON string obtained from reading the parameters.
*/
function JSONize(str) {
return str
.replace(/([\$\w]+)\s*:/g, function(_, $1){ return '"'+$1+'":'; })
.replace(/:([\$\w]+)\s*/g, function(_, $1) { return ':"'+$1+'"'; })
.replace(/'([^']+)'/g, function(_, $1){ return '"'+$1+'"'; });
}
/**
* Transforms strange AWS properties syntax, into a proper JS Object.
* @param {String} str String containing the JSON-like structure of parameters.
* @return {Object} Returns a JS Object obtained from reading the parameters.
*/
function parse(str) {
return JSON.parse(JSONize(str.replace(/=/g, ':')));
}
/**
* Creates a full URL path from parameters obtained as input parameters to AWS Lambda function.
* @param {Object} pathParts Object containing the list of path parameters eg.
* {0: 'level1', 1: 'level2'}
* @param {Object} queryString Object containing query string parameters that were passed to Lambda function, eg.
* {param1: 'someValue', param2: 'otherValue'}
* @return {String} Returns a full path of the request as it came from AWS API Gateway, for the examples above result would be:
* '/level1/level2?param1=someValue¶m2=otherValue'
*/
function createFullPath(pathParts, queryString) {
let path = '/';
path += Object.keys(pathParts).sort((a, b) => a - b).map(partKey => pathParts[partKey]).join('/');
const queryStringKeys = Object.keys(queryString);
if(queryStringKeys.length > 0) {
path += '?';
path += queryStringKeys.map(queryStringKey => `${queryStringKey}=${queryString[queryStringKey]}`).join('&');
}
return path;
}
/**
* Main Lambda function handler. It takes parameters passed from AWS API Gateway and returns generated HTML content,
* based on the path of the request, and query string parameters.
* @param {[type]} event Event object containing parameters passed from AWS API Gateway call.
* @param {[type]} context Object used to return result from the AWS Lambda function.
*/
export const mainHandler = co.wrap(function *(event, context) {
try {
console.log(event, context);
const path = event.path;
const querystring = event.querystring;
const fullPath = createFullPath(path, querystring);
const [urlPath, renderedContent] = yield render(fullPath);
context.succeed({urlPath, variableHTML: renderedContent});
} catch (error) {
console.error('ERROR: ', error, error.stack);
context.succeed({error});
}
});