Skip to content

Commit

Permalink
adds locale to mortareval
Browse files Browse the repository at this point in the history
  • Loading branch information
jhmullen committed Feb 4, 2019
1 parent 7c112a4 commit f40914c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
7 changes: 5 additions & 2 deletions packages/cms/src/api/mortarRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ module.exports = function(app) {

app.get("/api/variables/:slug/:id", (req, res) => {
// app.get("/api/variables/:slug/:id", jsonCache, (req, res) => {
const locale = req.query.locale ? req.query.locale : "en";
req.setTimeout(1000 * 60 * 30); // 30 minute timeout for non-cached cube queries
const {slug, id} = req.params;

Expand Down Expand Up @@ -226,7 +227,7 @@ module.exports = function(app) {
const requiredGenerators = generators.filter(g => g.api === requests[i]);
// Build the return object using a reducer, one generator at a time
returnVariables = requiredGenerators.reduce((acc, g) => {
const evalResults = mortarEval("resp", r.data, g.logic, formatterFunctions);
const evalResults = mortarEval("resp", r.data, g.logic, formatterFunctions, locale);
const {vars} = evalResults;
// genStatus is used to track the status of each individual generator
genStatus[g.id] = evalResults.error ? {error: evalResults.error} : evalResults.vars;
Expand Down Expand Up @@ -316,14 +317,16 @@ module.exports = function(app) {
app.get("/api/topic/:slug/:pid/:topicId", async(req, res) => {
req.setTimeout(1000 * 60 * 30); // 30 minute timeout for non-cached cube queries
const {slug, pid, topicId} = req.params;
const {locale} = req.query;
const localeString = locale ? `?locale=${locale}` : "";
const origin = `http${ req.connection.encrypted ? "s" : "" }://${ req.headers.host }`;

const attribute = await db.search.findOne({where: {[sequelize.Op.or]: {id: pid, slug: pid}}});
const {id} = attribute;

// As with profiles above, we need formatters, variables, and the topic itself in order to
// create a "postProcessed" topic that can be returned to the requester.
const getVariables = axios.get(`${origin}/api/variables/${slug}/${id}`);
const getVariables = axios.get(`${origin}/api/variables/${slug}/${id}${localeString}`);
const getFormatters = db.formatter.findAll();

const where = {};
Expand Down
6 changes: 3 additions & 3 deletions packages/cms/src/utils/mortarEval.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const libs = require("./libs");

module.exports = (varInnerName, varOuterValue, logic, formatterFunctions) => {
module.exports = (varInnerName, varOuterValue, logic, formatterFunctions, locale = "en") => {
let vars = {};
// Because logic is arbitrary javascript, it may be malformed. We need to wrap the
// entire execution in a try/catch.
try {
if (varOuterValue) {
eval(`
let f = (${varInnerName}, libs, formatters) => {${logic}};
vars = f(varOuterValue, libs, formatterFunctions);
let f = (${varInnerName}, libs, formatters, locale) => {${logic}};
vars = f(varOuterValue, libs, formatterFunctions, locale);
`);
// A successfully run eval will return the vars generated
return {vars, error: null};
Expand Down

0 comments on commit f40914c

Please sign in to comment.