mirror of
https://github.com/kremalicious/umami.git
synced 2024-11-15 09:45:04 +01:00
Convert send to TS.
This commit is contained in:
parent
e4c801e823
commit
2172dddd1c
@ -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);
|
||||
}
|
@ -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');
|
||||
|
@ -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;
|
||||
}
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user