improve(api): update api to fastify v5

This commit is contained in:
Carl-Gerhard Lindesvärd
2025-02-28 09:19:27 +01:00
parent f0b0f27a8f
commit 7750ca117f
25 changed files with 520 additions and 1642 deletions

View File

@@ -2,9 +2,11 @@ import * as controller from '@/controllers/event.controller';
import type { FastifyPluginCallback } from 'fastify';
import { clientHook } from '@/hooks/client.hook';
import { deduplicateHook } from '@/hooks/deduplicate.hook';
import { isBotHook } from '@/hooks/is-bot.hook';
const eventRouter: FastifyPluginCallback = (fastify, opts, done) => {
const eventRouter: FastifyPluginCallback = async (fastify) => {
fastify.addHook('preHandler', deduplicateHook);
fastify.addHook('preHandler', clientHook);
fastify.addHook('preHandler', isBotHook);
@@ -13,7 +15,6 @@ const eventRouter: FastifyPluginCallback = (fastify, opts, done) => {
url: '/',
handler: controller.postEvent,
});
done();
};
export default eventRouter;

View File

@@ -4,7 +4,7 @@ import { activateRateLimiter } from '@/utils/rate-limiter';
import { Prisma } from '@openpanel/db';
import type { FastifyPluginCallback, FastifyRequest } from 'fastify';
const exportRouter: FastifyPluginCallback = async (fastify, opts, done) => {
const exportRouter: FastifyPluginCallback = async (fastify) => {
await activateRateLimiter({
fastify,
max: 10,
@@ -46,7 +46,6 @@ const exportRouter: FastifyPluginCallback = async (fastify, opts, done) => {
url: '/charts',
handler: controller.charts,
});
done();
};
export default exportRouter;

View File

@@ -4,7 +4,7 @@ import type { FastifyPluginCallback, FastifyRequest } from 'fastify';
import { Prisma } from '@openpanel/db';
const importRouter: FastifyPluginCallback = (fastify, opts, done) => {
const importRouter: FastifyPluginCallback = async (fastify) => {
fastify.addHook('preHandler', async (req: FastifyRequest, reply) => {
try {
const client = await validateImportRequest(req.headers);
@@ -34,8 +34,6 @@ const importRouter: FastifyPluginCallback = (fastify, opts, done) => {
url: '/events',
handler: controller.importEvents,
});
done();
};
export default importRouter;

View File

@@ -2,36 +2,31 @@ import * as controller from '@/controllers/live.controller';
import fastifyWS from '@fastify/websocket';
import type { FastifyPluginCallback } from 'fastify';
// TODO: `as any` is a workaround since it starts to break after changed module resolution to bundler
// which is needed for @polar/sdk (dont have time to resolve this now)
const liveRouter: FastifyPluginCallback = (fastify, opts, done) => {
const liveRouter: FastifyPluginCallback = async (fastify) => {
fastify.register(fastifyWS);
fastify.register((fastify, _, done) => {
fastify.register(async (fastify) => {
fastify.get(
'/organization/:organizationId',
{ websocket: true },
controller.wsOrganizationEvents as any,
controller.wsOrganizationEvents,
);
fastify.get(
'/visitors/:projectId',
{ websocket: true },
controller.wsVisitors as any,
controller.wsVisitors,
);
fastify.get(
'/events/:projectId',
{ websocket: true },
controller.wsProjectEvents as any,
controller.wsProjectEvents,
);
fastify.get(
'/notifications/:projectId',
{ websocket: true },
controller.wsProjectNotifications as any,
controller.wsProjectNotifications,
);
done();
});
done();
};
export default liveRouter;

View File

@@ -1,7 +1,7 @@
import * as controller from '@/controllers/misc.controller';
import type { FastifyPluginCallback } from 'fastify';
const miscRouter: FastifyPluginCallback = (fastify, opts, done) => {
const miscRouter: FastifyPluginCallback = async (fastify) => {
fastify.route({
method: 'POST',
url: '/ping',
@@ -25,8 +25,6 @@ const miscRouter: FastifyPluginCallback = (fastify, opts, done) => {
url: '/favicon/clear',
handler: controller.clearFavicons,
});
done();
};
export default miscRouter;

View File

@@ -1,7 +1,7 @@
import * as controller from '@/controllers/oauth-callback.controller';
import type { FastifyPluginCallback } from 'fastify';
const router: FastifyPluginCallback = (fastify, opts, done) => {
const router: FastifyPluginCallback = async (fastify) => {
fastify.route({
method: 'GET',
url: '/github/callback',
@@ -12,7 +12,6 @@ const router: FastifyPluginCallback = (fastify, opts, done) => {
url: '/google/callback',
handler: controller.googleCallback,
});
done();
};
export default router;

View File

@@ -1,9 +1,11 @@
import * as controller from '@/controllers/profile.controller';
import { clientHook } from '@/hooks/client.hook';
import { deduplicateHook } from '@/hooks/deduplicate.hook';
import { isBotHook } from '@/hooks/is-bot.hook';
import type { FastifyPluginCallback } from 'fastify';
const eventRouter: FastifyPluginCallback = (fastify, opts, done) => {
const eventRouter: FastifyPluginCallback = async (fastify) => {
fastify.addHook('preHandler', deduplicateHook);
fastify.addHook('preHandler', clientHook);
fastify.addHook('preHandler', isBotHook);
@@ -24,7 +26,6 @@ const eventRouter: FastifyPluginCallback = (fastify, opts, done) => {
url: '/decrement',
handler: controller.decrementProfileProperty,
});
done();
};
export default eventRouter;

View File

@@ -2,9 +2,11 @@ import { handler } from '@/controllers/track.controller';
import type { FastifyPluginCallback } from 'fastify';
import { clientHook } from '@/hooks/client.hook';
import { deduplicateHook } from '@/hooks/deduplicate.hook';
import { isBotHook } from '@/hooks/is-bot.hook';
const trackRouter: FastifyPluginCallback = (fastify, opts, done) => {
const trackRouter: FastifyPluginCallback = (fastify) => {
fastify.addHook('preHandler', deduplicateHook);
fastify.addHook('preHandler', clientHook);
fastify.addHook('preHandler', isBotHook);
@@ -29,8 +31,6 @@ const trackRouter: FastifyPluginCallback = (fastify, opts, done) => {
},
},
});
done();
};
export default trackRouter;

View File

@@ -1,7 +1,7 @@
import * as controller from '@/controllers/webhook.controller';
import type { FastifyPluginCallback } from 'fastify';
const webhookRouter: FastifyPluginCallback = (fastify, opts, done) => {
const webhookRouter: FastifyPluginCallback = async (fastify) => {
fastify.route({
method: 'GET',
url: '/slack',
@@ -15,7 +15,6 @@ const webhookRouter: FastifyPluginCallback = (fastify, opts, done) => {
rawBody: true,
},
});
done();
};
export default webhookRouter;