mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-10 15:58:38 +01:00
add api keys
This commit is contained in:
44
dashboard/server/api/v1/events.ts
Normal file
44
dashboard/server/api/v1/events.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
|
||||
import { ApiSettingsModel } from '@schema/ApiSettingsSchema';
|
||||
import { EventModel } from '@schema/metrics/EventSchema';
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
const { row, from, to, limit } = getQuery(event);
|
||||
|
||||
const authorization = getHeader(event, 'Authorization');
|
||||
if (!authorization) return setResponseStatus(event, 403, 'Authorization is required');
|
||||
|
||||
const [type, token] = authorization.split(' ');
|
||||
if (type != 'Bearer') return setResponseStatus(event, 401, 'Malformed authorization');
|
||||
|
||||
const apiSettings = await ApiSettingsModel.findOne({ apiKey: token });
|
||||
if (!apiSettings) return setResponseStatus(event, 401, 'ApiKey not valid');
|
||||
|
||||
if (!row) return setResponseStatus(event, 400, 'row is required');
|
||||
|
||||
|
||||
const rows: string[] = Array.isArray(row) ? row as string[] : [row as string];
|
||||
|
||||
const projection: any = {};
|
||||
|
||||
for (const row of rows) {
|
||||
projection[row] = 1;
|
||||
}
|
||||
|
||||
const limitNumber = parseInt((limit as string));
|
||||
|
||||
const limitValue = isNaN(limitNumber) ? 100 : limitNumber;
|
||||
|
||||
const visits = await EventModel.find({
|
||||
project_id: apiSettings.project_id,
|
||||
created_at: {
|
||||
$gte: from || new Date(2023, 0),
|
||||
$lte: to || new Date(3000, 0)
|
||||
}
|
||||
}, { _id: 0, ...projection }, { limit: limitValue });
|
||||
|
||||
return visits.map(e => e.toJSON());
|
||||
|
||||
|
||||
});
|
||||
44
dashboard/server/api/v1/visits.ts
Normal file
44
dashboard/server/api/v1/visits.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
|
||||
import { ApiSettingsModel } from '@schema/ApiSettingsSchema';
|
||||
import { VisitModel } from '@schema/metrics/VisitSchema';
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
|
||||
const { row, from, to, limit } = getQuery(event);
|
||||
|
||||
const authorization = getHeader(event, 'Authorization');
|
||||
if (!authorization) return setResponseStatus(event, 403, 'Authorization is required');
|
||||
|
||||
const [type, token] = authorization.split(' ');
|
||||
if (type != 'Bearer') return setResponseStatus(event, 401, 'Malformed authorization');
|
||||
|
||||
const apiSettings = await ApiSettingsModel.findOne({ apiKey: token });
|
||||
if (!apiSettings) return setResponseStatus(event, 401, 'ApiKey not valid');
|
||||
|
||||
if (!row) return setResponseStatus(event, 400, 'row is required');
|
||||
|
||||
|
||||
const rows: string[] = Array.isArray(row) ? row as string[] : [row as string];
|
||||
|
||||
const projection: any = {};
|
||||
|
||||
for (const row of rows) {
|
||||
projection[row] = 1;
|
||||
}
|
||||
|
||||
const limitNumber = parseInt((limit as string));
|
||||
|
||||
const limitValue = isNaN(limitNumber) ? 100 : limitNumber;
|
||||
|
||||
const visits = await VisitModel.find({
|
||||
project_id: apiSettings.project_id,
|
||||
created_at: {
|
||||
$gte: from || new Date(2023, 0),
|
||||
$lte: to || new Date(3000, 0)
|
||||
}
|
||||
}, { _id: 0, ...projection }, { limit: limitValue });
|
||||
|
||||
return visits.map(e => e.toJSON());
|
||||
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user