umami/pages/api/realtime/[id].ts

26 lines
722 B
TypeScript
Raw Normal View History

2023-02-15 11:27:18 +01:00
import { subMinutes } from 'date-fns';
import { RealtimeInit, NextApiRequestAuth } from 'lib/types';
import { useAuth } from 'lib/middleware';
import { NextApiResponse } from 'next';
import { methodNotAllowed, ok } from 'next-basics';
import { getRealtimeData } from 'queries';
export default async (req: NextApiRequestAuth, res: NextApiResponse<RealtimeInit>) => {
await useAuth(req, res);
if (req.method === 'GET') {
2023-02-18 06:42:42 +01:00
const { id, startAt } = req.query;
let startTime = subMinutes(new Date(), 30);
2023-02-15 11:27:18 +01:00
2023-02-18 06:42:42 +01:00
if (+startAt > startTime.getTime()) {
startTime = new Date(+startAt);
}
const data = await getRealtimeData(id, startTime);
2023-02-15 11:27:18 +01:00
return ok(res, data);
}
return methodNotAllowed(res);
};