1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-09-18 15:25:33 -04:00

replace request with axios in send_view API

This commit is contained in:
2020-08-12 10:09:10 -04:00
parent 28f0590f8e
commit a0671a6dea
3 changed files with 91 additions and 309 deletions

View File

@@ -1,4 +1,4 @@
const request = require("request");
const axios = require("axios");
exports.handler = function (event, context, callback) {
// if triggered as an image without JS (i.e. from AMP pages) set `?noscript=true`
@@ -10,49 +10,49 @@ exports.handler = function (event, context, callback) {
// pass these headers along to SA
const reqHeaders = {
referer: event.headers["referer"],
"user-agent": event.headers["user-agent"],
referer: event.headers["referer"] || "",
"user-agent": event.headers["user-agent"] || "",
};
// pass these URL parameters along to SA
const reqQuery = event.queryStringParameters;
reqQuery["ignore-dnt"] = "true"; // this isn't nefarious, we're not tracking in the first place!
const reqOptions = {
const reqConfig = {
method: "GET",
url: endpointUrl,
headers: reqHeaders,
qs: reqQuery,
params: reqQuery,
timeout: 3000,
};
request(reqOptions, (error, response, body) => {
console.info(`Proxying ${event.queryStringParameters["type"]} to ${endpointPath} ...`);
console.info(`Proxying ${reqQuery["type"]} to ${endpointPath} ...`);
if (error) {
// this indicates a function error, NOT an error returned from SA
console.error(error);
callback(null, {
statusCode: 500,
});
} else {
console.info(response.statusCode, response.headers["simple-analytics-feedback"]);
axios
.request(reqConfig)
.then(function (response) {
console.info(response.status, response.headers["simple-analytics-feedback"]);
// imitate the headers that would normally be sent back from SA's pixel
const resHeaders = {
"content-type": response.headers["content-type"],
"cache-control": "no-cache, no-store, must-revalidate",
expires: "0",
pragma: "no-cache",
"content-type": response.headers["content-type"],
"x-api-feedback": response.headers["simple-analytics-feedback"],
"x-api-destination": response.headers["simple-analytics-location"],
};
callback(null, {
statusCode: response.statusCode,
statusCode: response.status,
headers: resHeaders,
body: body,
body: response.body,
});
}
});
})
.catch(function (error) {
// this indicates a function error, NOT an error returned from SA
console.log(error);
callback(null, {
statusCode: 500,
});
});
};