fix(api): sending duplicated responses for bot events (improved code as well)

This commit is contained in:
Carl-Gerhard Lindesvärd
2024-11-30 21:40:25 +01:00
parent d80754a6fd
commit afff099c5b
6 changed files with 101 additions and 144 deletions

View File

@@ -0,0 +1,26 @@
import { SdkAuthError, validateSdkRequest } from '@/utils/auth';
import type { TrackHandlerPayload } from '@openpanel/sdk';
import type {
FastifyReply,
FastifyRequest,
HookHandlerDoneFunction,
} from 'fastify';
export async function clientHook(
req: FastifyRequest<{
Body: TrackHandlerPayload;
}>,
reply: FastifyReply,
) {
try {
const client = await validateSdkRequest(req.headers);
req.projectId = client.projectId;
req.client = client;
} catch (error) {
if (error instanceof SdkAuthError) {
return reply.status(401).send(error.message);
}
return reply.status(500).send('Internal server error');
}
}