umami/pages/api/realtime/update.js

28 lines
683 B
JavaScript
Raw Normal View History

2022-08-29 05:20:54 +02:00
import { ok, methodNotAllowed, badRequest, parseToken } from 'next-basics';
import { useAuth } from 'lib/middleware';
2022-07-12 23:14:36 +02:00
import { getRealtimeData } from 'queries';
import { SHARE_TOKEN_HEADER } from 'lib/constants';
2022-08-29 05:20:54 +02:00
import { secret } from 'lib/crypto';
export default async (req, res) => {
await useAuth(req, res);
if (req.method === 'GET') {
const { start_at } = req.query;
const token = req.headers[SHARE_TOKEN_HEADER];
if (!token) {
return badRequest(res);
}
2022-08-29 05:20:54 +02:00
const { websites } = parseToken(token, secret());
const data = await getRealtimeData(websites, new Date(+start_at));
return ok(res, data);
}
return methodNotAllowed(res);
};