From 2172dddd1c7d846c5e323672a19b1a256423c6a8 Mon Sep 17 00:00:00 2001 From: Brian Cao Date: Thu, 30 Mar 2023 11:18:57 -0700 Subject: [PATCH] Convert send to TS. --- lib/{detect.js => detect.ts} | 7 ++++--- lib/middleware.ts | 3 ++- lib/{session.js => session.ts} | 13 +++++++------ pages/api/send.ts | 19 ++++++++++++++++++- 4 files changed, 31 insertions(+), 11 deletions(-) rename lib/{detect.js => detect.ts} (92%) rename lib/{session.js => session.ts} (90%) diff --git a/lib/detect.js b/lib/detect.ts similarity index 92% rename from lib/detect.js rename to lib/detect.ts index 083584d9..f431f02e 100644 --- a/lib/detect.js +++ b/lib/detect.ts @@ -11,6 +11,7 @@ import { LAPTOP_SCREEN_WIDTH, MOBILE_SCREEN_WIDTH, } from './constants'; +import { NextApiRequestCollect } from 'pages/api/send'; let lookup; @@ -77,7 +78,7 @@ export async function getLocation(ip) { return { country, subdivision1, subdivision2, city }; } -export async function getClientInfo(req, { screen }) { +export async function getClientInfo(req: NextApiRequestCollect, { screen }) { const userAgent = req.headers['user-agent']; const ip = getIpAddress(req); const location = await getLocation(ip); @@ -87,12 +88,12 @@ export async function getClientInfo(req, { screen }) { const city = location?.city; const browser = browserName(userAgent); const os = detectOS(userAgent); - const device = getDevice(screen, browser, os); + const device = getDevice(screen, os); return { userAgent, browser, os, ip, country, subdivision1, subdivision2, city, device }; } -export function getJsonBody(req) { +export function getJsonBody(req): T { if ((req.headers['content-type'] || '').indexOf('text/plain') !== -1) { return JSON.parse(req.body); } diff --git a/lib/middleware.ts b/lib/middleware.ts index 431efbdd..039bdac4 100644 --- a/lib/middleware.ts +++ b/lib/middleware.ts @@ -8,6 +8,7 @@ import { getAuthToken, parseShareToken } from 'lib/auth'; import { secret } from 'lib/crypto'; import { ROLES } from 'lib/constants'; import { getUser } from '../queries'; +import { NextApiRequestCollect } from 'pages/api/send'; const log = debug('umami:middleware'); @@ -19,7 +20,7 @@ export const useCors = createMiddleware( ); export const useSession = createMiddleware(async (req, res, next) => { - const session = await findSession(req); + const session = await findSession(req as NextApiRequestCollect); if (!session) { log('useSession: Session not found'); diff --git a/lib/session.js b/lib/session.ts similarity index 90% rename from lib/session.js rename to lib/session.ts index f8b35a76..ffc16e23 100644 --- a/lib/session.js +++ b/lib/session.ts @@ -1,13 +1,14 @@ -import { parseToken } from 'next-basics'; -import { validate } from 'uuid'; -import { secret, uuid } from 'lib/crypto'; import cache from 'lib/cache'; import clickhouse from 'lib/clickhouse'; +import { secret, uuid } from 'lib/crypto'; import { getClientInfo, getJsonBody } from 'lib/detect'; +import { parseToken } from 'next-basics'; +import { CollectRequestBody, NextApiRequestCollect } from 'pages/api/send'; import { createSession, getSession, getWebsite } from 'queries'; +import { validate } from 'uuid'; -export async function findSession(req) { - const { payload } = getJsonBody(req); +export async function findSession(req: NextApiRequestCollect) { + const { payload } = getJsonBody(req); if (!payload) { return null; @@ -92,7 +93,7 @@ export async function findSession(req) { subdivision2, city, }); - } catch (e) { + } catch (e: any) { if (!e.message.toLowerCase().includes('unique constraint')) { throw e; } diff --git a/pages/api/send.ts b/pages/api/send.ts index 11e8f4c1..8c71727b 100644 --- a/pages/api/send.ts +++ b/pages/api/send.ts @@ -8,7 +8,23 @@ import { secret } from 'lib/crypto'; import { NextApiRequest, NextApiResponse } from 'next'; import { Resolver } from 'dns/promises'; +export interface CollectRequestBody { + payload: { + data: { [key: string]: any }; + hostname: string; + language: string; + referrer: string; + screen: string; + title: string; + url: string; + website: string; + name: string; + }; + type: string; +} + export interface NextApiRequestCollect extends NextApiRequest { + body: CollectRequestBody; session: { id: string; websiteId: string; @@ -23,6 +39,7 @@ export interface NextApiRequestCollect extends NextApiRequest { subdivision2: string; city: string; }; + headers: { [key: string]: any }; } export default async (req: NextApiRequestCollect, res: NextApiResponse) => { @@ -32,7 +49,7 @@ export default async (req: NextApiRequestCollect, res: NextApiResponse) => { return ok(res); } - const { type, payload } = getJsonBody(req); + const { type, payload } = getJsonBody(req); if (type !== 'event') { return badRequest(res);