mirror of
https://github.com/kremalicious/umami.git
synced 2025-02-14 21:10:34 +01:00
Convert send to TS.
This commit is contained in:
parent
e4c801e823
commit
2172dddd1c
@ -11,6 +11,7 @@ import {
|
|||||||
LAPTOP_SCREEN_WIDTH,
|
LAPTOP_SCREEN_WIDTH,
|
||||||
MOBILE_SCREEN_WIDTH,
|
MOBILE_SCREEN_WIDTH,
|
||||||
} from './constants';
|
} from './constants';
|
||||||
|
import { NextApiRequestCollect } from 'pages/api/send';
|
||||||
|
|
||||||
let lookup;
|
let lookup;
|
||||||
|
|
||||||
@ -77,7 +78,7 @@ export async function getLocation(ip) {
|
|||||||
return { country, subdivision1, subdivision2, city };
|
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 userAgent = req.headers['user-agent'];
|
||||||
const ip = getIpAddress(req);
|
const ip = getIpAddress(req);
|
||||||
const location = await getLocation(ip);
|
const location = await getLocation(ip);
|
||||||
@ -87,12 +88,12 @@ export async function getClientInfo(req, { screen }) {
|
|||||||
const city = location?.city;
|
const city = location?.city;
|
||||||
const browser = browserName(userAgent);
|
const browser = browserName(userAgent);
|
||||||
const os = detectOS(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 };
|
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) {
|
if ((req.headers['content-type'] || '').indexOf('text/plain') !== -1) {
|
||||||
return JSON.parse(req.body);
|
return JSON.parse(req.body);
|
||||||
}
|
}
|
@ -8,6 +8,7 @@ import { getAuthToken, parseShareToken } from 'lib/auth';
|
|||||||
import { secret } from 'lib/crypto';
|
import { secret } from 'lib/crypto';
|
||||||
import { ROLES } from 'lib/constants';
|
import { ROLES } from 'lib/constants';
|
||||||
import { getUser } from '../queries';
|
import { getUser } from '../queries';
|
||||||
|
import { NextApiRequestCollect } from 'pages/api/send';
|
||||||
|
|
||||||
const log = debug('umami:middleware');
|
const log = debug('umami:middleware');
|
||||||
|
|
||||||
@ -19,7 +20,7 @@ export const useCors = createMiddleware(
|
|||||||
);
|
);
|
||||||
|
|
||||||
export const useSession = createMiddleware(async (req, res, next) => {
|
export const useSession = createMiddleware(async (req, res, next) => {
|
||||||
const session = await findSession(req);
|
const session = await findSession(req as NextApiRequestCollect);
|
||||||
|
|
||||||
if (!session) {
|
if (!session) {
|
||||||
log('useSession: Session not found');
|
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 cache from 'lib/cache';
|
||||||
import clickhouse from 'lib/clickhouse';
|
import clickhouse from 'lib/clickhouse';
|
||||||
|
import { secret, uuid } from 'lib/crypto';
|
||||||
import { getClientInfo, getJsonBody } from 'lib/detect';
|
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 { createSession, getSession, getWebsite } from 'queries';
|
||||||
|
import { validate } from 'uuid';
|
||||||
|
|
||||||
export async function findSession(req) {
|
export async function findSession(req: NextApiRequestCollect) {
|
||||||
const { payload } = getJsonBody(req);
|
const { payload } = getJsonBody<CollectRequestBody>(req);
|
||||||
|
|
||||||
if (!payload) {
|
if (!payload) {
|
||||||
return null;
|
return null;
|
||||||
@ -92,7 +93,7 @@ export async function findSession(req) {
|
|||||||
subdivision2,
|
subdivision2,
|
||||||
city,
|
city,
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e: any) {
|
||||||
if (!e.message.toLowerCase().includes('unique constraint')) {
|
if (!e.message.toLowerCase().includes('unique constraint')) {
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
@ -8,7 +8,23 @@ import { secret } from 'lib/crypto';
|
|||||||
import { NextApiRequest, NextApiResponse } from 'next';
|
import { NextApiRequest, NextApiResponse } from 'next';
|
||||||
import { Resolver } from 'dns/promises';
|
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 {
|
export interface NextApiRequestCollect extends NextApiRequest {
|
||||||
|
body: CollectRequestBody;
|
||||||
session: {
|
session: {
|
||||||
id: string;
|
id: string;
|
||||||
websiteId: string;
|
websiteId: string;
|
||||||
@ -23,6 +39,7 @@ export interface NextApiRequestCollect extends NextApiRequest {
|
|||||||
subdivision2: string;
|
subdivision2: string;
|
||||||
city: string;
|
city: string;
|
||||||
};
|
};
|
||||||
|
headers: { [key: string]: any };
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async (req: NextApiRequestCollect, res: NextApiResponse) => {
|
export default async (req: NextApiRequestCollect, res: NextApiResponse) => {
|
||||||
@ -32,7 +49,7 @@ export default async (req: NextApiRequestCollect, res: NextApiResponse) => {
|
|||||||
return ok(res);
|
return ok(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
const { type, payload } = getJsonBody(req);
|
const { type, payload } = getJsonBody<CollectRequestBody>(req);
|
||||||
|
|
||||||
if (type !== 'event') {
|
if (type !== 'event') {
|
||||||
return badRequest(res);
|
return badRequest(res);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user