From 620011a837937226b7b25a528a411161c7f10705 Mon Sep 17 00:00:00 2001 From: Mike Cao Date: Wed, 30 Aug 2023 16:40:32 -0700 Subject: [PATCH] Removed getJsonBody. --- src/lib/detect.ts | 8 -------- src/lib/session.ts | 6 +++--- src/pages/api/send.ts | 4 ++-- 3 files changed, 5 insertions(+), 13 deletions(-) diff --git a/src/lib/detect.ts b/src/lib/detect.ts index b16bab7a..3b2f9021 100644 --- a/src/lib/detect.ts +++ b/src/lib/detect.ts @@ -130,11 +130,3 @@ export async function getClientInfo(req: NextApiRequestCollect, { screen }) { return { userAgent, browser, os, ip, country, subdivision1, subdivision2, city, device }; } - -export function getJsonBody(req): T { - if ((req.headers['content-type'] || '').indexOf('text/plain') !== -1) { - return JSON.parse(req.body); - } - - return req.body; -} diff --git a/src/lib/session.ts b/src/lib/session.ts index 85c173c5..0f388db9 100644 --- a/src/lib/session.ts +++ b/src/lib/session.ts @@ -1,7 +1,7 @@ import { isUuid, secret, uuid } from 'lib/crypto'; -import { getClientInfo, getJsonBody } from 'lib/detect'; +import { getClientInfo } from 'lib/detect'; import { parseToken } from 'next-basics'; -import { CollectRequestBody, NextApiRequestCollect } from 'pages/api/send'; +import { NextApiRequestCollect } from 'pages/api/send'; import { createSession } from 'queries'; import cache from './cache'; import clickhouse from './clickhouse'; @@ -22,7 +22,7 @@ export async function findSession(req: NextApiRequestCollect): Promise<{ city: any; ownerId: string; }> { - const { payload } = getJsonBody(req); + const { payload } = req.body; if (!payload) { throw new Error('Invalid payload.'); diff --git a/src/pages/api/send.ts b/src/pages/api/send.ts index 56a748e1..e220711c 100644 --- a/src/pages/api/send.ts +++ b/src/pages/api/send.ts @@ -3,7 +3,7 @@ import ipaddr from 'ipaddr.js'; import isbot from 'isbot'; import { COLLECTION_TYPE, HOSTNAME_REGEX } from 'lib/constants'; import { secret } from 'lib/crypto'; -import { getIpAddress, getJsonBody } from 'lib/detect'; +import { getIpAddress } from 'lib/detect'; import { useCors, useSession, useValidate } from 'lib/middleware'; import { CollectionType, YupRequest } from 'lib/types'; import { NextApiRequest, NextApiResponse } from 'next'; @@ -77,7 +77,7 @@ export default async (req: NextApiRequestCollect, res: NextApiResponse) => { return ok(res); } - const { type, payload } = getJsonBody(req); + const { type, payload } = req.body; if (!type || !payload) { return badRequest(res);