MCPcopy
hub / github.com/ampproject/amphtml / saveVariables

Function saveVariables

build-system/server/variable-substitution.js:9–41  ·  view source on GitHub ↗

* @param {*} req require('express').Request * @param {*} res require('express').Response

(req, res)

Source from the content-addressed store, hash-verified

7 * @param {*} res require('express').Response
8 */
9function saveVariables(req, res) {
10 const requestVariables = {};
11 // For when a JSON is entered
12 if (req.query && Object.keys(req.query).length === 2) {
13 const entries = Object.entries(req.query);
14 try {
15 Object.assign(requestVariables, JSON.parse(entries[1][0]));
16 } catch (e) {
17 res.send(`<!doctype html>
18 <html>
19 <head>
20 <title>AMP Analytics</title>
21 </head>
22 <body>
23 <p>Error:</p>
24 ${e}
25 </body>
26 </html>`);
27 return;
28 }
29 } else {
30 // Remove variables that don't have values
31 const keys = Object.keys(req.query);
32 for (let i = 0; i < keys.length; i++) {
33 if (req.query[keys[i]]) {
34 requestVariables[keys[i]] = req.query[keys[i]];
35 }
36 }
37 }
38 variables = requestVariables;
39 res.json({'vars': variables});
40 return;
41}
42
43/**
44 * @param {*} req require('express').Request

Callers

nothing calls this directly

Calls 5

assignMethod · 0.80
entriesMethod · 0.45
parseMethod · 0.45
sendMethod · 0.45
jsonMethod · 0.45

Tested by

no test coverage detected