Convert send to TS.

This commit is contained in:
Brian Cao 2023-03-30 11:18:57 -07:00
parent e4c801e823
commit 2172dddd1c
4 changed files with 31 additions and 11 deletions

View file

@ -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);
}

View file

@ -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');

View file

@ -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;
}