diff --git a/.eslintrc.json b/.eslintrc.json index 168665fc..e5f0cc94 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -16,7 +16,8 @@ "rules": { "react/display-name": "off", "react/react-in-jsx-scope": "off", - "react/prop-types": "off" + "react/prop-types": "off", + "import/no-anonymous-default-export": "off" }, "globals": { "React": "writable" diff --git a/lib/middleware.js b/lib/middleware.js index 228b2287..0c6fd081 100644 --- a/lib/middleware.js +++ b/lib/middleware.js @@ -3,11 +3,6 @@ import { getSession } from './session'; import { getAuthToken } from './auth'; import { unauthorized, badRequest, serverError } from './response'; -const corsOptions = { - origin: '*', - credentials: true, -}; - export function createMiddleware(middleware) { return (req, res) => new Promise((resolve, reject) => { @@ -20,7 +15,7 @@ export function createMiddleware(middleware) { }); } -export const useCors = createMiddleware(cors(corsOptions)); +export const useCors = createMiddleware(cors()); export const useSession = createMiddleware(async (req, res, next) => { let session; diff --git a/lib/request.js b/lib/request.js index 725dc383..89e9c655 100644 --- a/lib/request.js +++ b/lib/request.js @@ -86,3 +86,11 @@ export async function getClientInfo(req, { screen }) { return { userAgent, browser, os, ip, country, device }; } + +export function getJsonBody(req) { + if (req.headers['content-type'].indexOf('text/plain') !== -1) { + return JSON.parse(req.body); + } + + return req.body; +} diff --git a/lib/session.js b/lib/session.js index 167fda9c..694465bc 100644 --- a/lib/session.js +++ b/lib/session.js @@ -1,9 +1,9 @@ import { getWebsiteByUuid, getSessionByUuid, createSession } from 'lib/queries'; -import { getClientInfo } from 'lib/request'; +import { getJsonBody, getClientInfo } from 'lib/request'; import { uuid, isValidUuid, parseToken } from 'lib/crypto'; export async function getSession(req) { - const { payload } = req.body; + const { payload } = getJsonBody(req); if (!payload) { throw new Error('Invalid request'); diff --git a/pages/api/collect.js b/pages/api/collect.js index 2f001758..a6ea8f1e 100644 --- a/pages/api/collect.js +++ b/pages/api/collect.js @@ -2,7 +2,7 @@ import isbot from 'isbot'; import ipaddr from 'ipaddr.js'; import { savePageView, saveEvent } from 'lib/queries'; import { useCors, useSession } from 'lib/middleware'; -import { getIpAddress } from 'lib/request'; +import { getJsonBody, getIpAddress } from 'lib/request'; import { ok, badRequest } from 'lib/response'; import { createToken } from 'lib/crypto'; import { removeTrailingSlash } from 'lib/url'; @@ -39,10 +39,11 @@ export default async (req, res) => { await useSession(req, res); const { - body: { type, payload }, session: { website_id, session_id }, } = req; + const { type, payload } = getJsonBody(req); + let { url, referrer, event_type, event_value } = payload; if (process.env.REMOVE_TRAILING_SLASH) { diff --git a/tracker/index.js b/tracker/index.js index e5277ed6..37eea631 100644 --- a/tracker/index.js +++ b/tracker/index.js @@ -47,7 +47,7 @@ import { removeTrailingSlash } from '../lib/url'; const post = (url, data, callback) => { const req = new XMLHttpRequest(); req.open('POST', url, true); - req.setRequestHeader('Content-Type', 'application/json'); + req.setRequestHeader('Content-Type', 'text/plain'); req.onreadystatechange = () => { if (req.readyState === 4) { @@ -114,20 +114,16 @@ import { removeTrailingSlash } from '../lib/url'; const sendEvent = (value, type) => { const payload = getPayload(); + payload.event_type = type; payload.event_value = value; - const blob = new Blob( - [ - JSON.stringify({ - type: 'event', - payload, - }), - ], - { type: 'application/json' }, - ); + const data = JSON.stringify({ + type: 'event', + payload, + }); - navigator.sendBeacon(`${root}/api/collect`, blob); + navigator.sendBeacon(`${root}/api/collect`, data); }; const addEvents = node => {