api: add first version of export api

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-04-11 21:30:36 +02:00
committed by Carl-Gerhard Lindesvärd
parent 7ea95afe16
commit 7f8d857508
10 changed files with 232 additions and 7 deletions

View File

@@ -0,0 +1,37 @@
import * as controller from '@/controllers/export.controller';
import { validateExportRequest } from '@/utils/auth';
import type { FastifyPluginCallback, FastifyRequest } from 'fastify';
import { Prisma } from '@openpanel/db';
const eventRouter: FastifyPluginCallback = (fastify, opts, done) => {
fastify.addHook('preHandler', async (req: FastifyRequest, reply) => {
try {
const client = await validateExportRequest(req.headers);
req.client = client;
} catch (e) {
if (e instanceof Prisma.PrismaClientKnownRequestError) {
return reply.status(401).send({
error: 'Unauthorized',
message: 'Client ID seems to be malformed',
});
} else if (e instanceof Error) {
return reply
.status(401)
.send({ error: 'Unauthorized', message: e.message });
}
return reply
.status(401)
.send({ error: 'Unauthorized', message: 'Unexpected error' });
}
});
fastify.route({
method: 'GET',
url: '/events',
handler: controller.events,
});
done();
};
export default eventRouter;