elm-pages-v3-beta/examples/docs/functions/render/index.js

53 lines
1.3 KiB
JavaScript
Raw Normal View History

2021-03-24 08:28:00 +03:00
const path = require("path");
exports.handler =
/**
* @param {import('aws-lambda').APIGatewayProxyEvent} event
* @param {any} context
*/
async function (event, context) {
console.log(JSON.stringify(event));
2021-04-25 02:01:31 +03:00
global.staticHttpCache = {};
2021-03-24 08:28:00 +03:00
const compiledElmPath = path.join(__dirname, "elm-pages-cli.js");
const renderer = require("../../../../generator/src/render");
try {
2021-04-25 02:01:31 +03:00
const renderResult = await renderer(
compiledElmPath,
event.path,
event,
function () {}
);
if (renderResult.kind === "json") {
return {
body: renderResult.contentJson,
headers: {
"Content-Type": "application/json",
"x-powered-by": "elm-pages",
},
statusCode: 200,
};
} else {
return {
body: renderResult.htmlString,
headers: {
"Content-Type": "text/html",
"x-powered-by": "elm-pages",
},
statusCode: 200,
};
}
} catch (error) {
2021-04-25 02:01:31 +03:00
console.error(error);
return {
body: `<body><h1>Error</h1><pre>${error}</pre></body>`,
statusCode: 500,
headers: {
"Content-Type": "text/html",
"x-powered-by": "elm-pages",
},
};
}
};