Skip to content

armgit / umami

Public Imported from GitHub
0

Umami is a privacy-first web and product analytics platform. Traffic, campaigns, behavior, conversions, and revenue in one place. No cookies, no surveillance, self-hosted or in the cloud.

Convert send to TS.

Brian Cao2172dddd1c7d
diff --git a/lib/detect.js b/lib/detect.ts
similarity index 92%
rename from lib/detect.js
rename to lib/detect.ts
index 083584d9a..f431f02ed 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<T>(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 431efbdd0..039bdac4a 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 f8b35a769..ffc16e234 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<CollectRequestBody>(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 11e8f4c10..8c71727ba 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<CollectRequestBody>(req);
if (type !== 'event') {
return badRequest(res);