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

@@ -1,58 +1,12 @@
import { isBot } from '@/bots';
import * as controller from '@/controllers/event.controller';
import { SdkAuthError, validateSdkRequest } from '@/utils/auth';
import { logger } from '@/utils/logger';
import type { FastifyPluginCallback, FastifyRequest } from 'fastify';
import type { FastifyPluginCallback } from 'fastify';
import { createBotEvent } from '@openpanel/db';
import type { PostEventPayload } from '@openpanel/sdk';
import { clientHook } from '@/hooks/client.hook';
import { isBotHook } from '@/hooks/is-bot.hook';
const eventRouter: FastifyPluginCallback = (fastify, opts, done) => {
fastify.addHook(
'preHandler',
async (
req: FastifyRequest<{
Body: PostEventPayload;
}>,
reply,
) => {
try {
const client = await validateSdkRequest(req.headers).catch((error) => {
if (error instanceof SdkAuthError) {
return reply.status(401).send(error.message);
}
logger.error('Failed to validate sdk request', { error });
return reply.status(401).send('Unknown validation error');
});
if (!client?.projectId) {
return reply.status(401).send('No project found for this client');
}
req.projectId = client.projectId;
req.client = client;
const bot = req.headers['user-agent']
? isBot(req.headers['user-agent'])
: null;
if (bot) {
const path = (req.body?.properties?.__path ||
req.body?.properties?.path) as string | undefined;
await createBotEvent({
...bot,
projectId: client.projectId,
path: path ?? '',
createdAt: new Date(req.body?.timestamp),
});
reply.status(202).send('OK');
}
} catch (error) {
logger.error('Failed to create bot event', { error });
reply.status(401).send();
return;
}
},
);
fastify.addHook('preHandler', clientHook);
fastify.addHook('preHandler', isBotHook);
fastify.route({
method: 'POST',

View File

@@ -1,39 +1,11 @@
import { isBot } from '@/bots';
import * as controller from '@/controllers/profile.controller';
import { SdkAuthError, validateSdkRequest } from '@/utils/auth';
import { logger } from '@/utils/logger';
import { clientHook } from '@/hooks/client.hook';
import { isBotHook } from '@/hooks/is-bot.hook';
import type { FastifyPluginCallback } from 'fastify';
const eventRouter: FastifyPluginCallback = (fastify, opts, done) => {
fastify.addHook('preHandler', async (req, reply) => {
try {
const client = await validateSdkRequest(req.headers).catch((error) => {
if (error instanceof SdkAuthError) {
return reply.status(401).send(error.message);
}
logger.error('Failed to validate sdk request', { error });
return reply.status(401).send('Unknown validation error');
});
if (!client?.projectId) {
return reply.status(401).send('No project found for this client');
}
req.projectId = client.projectId;
req.client = client;
const bot = req.headers['user-agent']
? isBot(req.headers['user-agent'])
: null;
if (bot) {
return reply.status(202).send('OK');
}
} catch (error) {
logger.error('Failed to create bot event', { error });
reply.status(401).send();
return;
}
});
fastify.addHook('preHandler', clientHook);
fastify.addHook('preHandler', isBotHook);
fastify.route({
method: 'POST',

View File

@@ -1,62 +1,16 @@
import { isBot } from '@/bots';
import { handler } from '@/controllers/track.controller';
import { SdkAuthError, validateSdkRequest } from '@/utils/auth';
import { logger } from '@/utils/logger';
import type { FastifyPluginCallback, FastifyRequest } from 'fastify';
import { clientHook } from '@/hooks/client.hook';
import { isBotHook } from '@/hooks/is-bot.hook';
import { createBotEvent } from '@openpanel/db';
import type { TrackHandlerPayload } from '@openpanel/sdk';
const trackRouter: FastifyPluginCallback = (fastify, opts, done) => {
fastify.addHook(
'preHandler',
async (
req: FastifyRequest<{
Body: TrackHandlerPayload;
}>,
reply,
) => {
try {
const client = await validateSdkRequest(req.headers).catch((error) => {
if (error instanceof SdkAuthError) {
return reply.status(401).send(error.message);
}
logger.error('Failed to validate sdk request', { error });
return reply.status(401).send('Unknown validation error');
});
if (!client?.projectId) {
return reply.status(401).send('No project found for this client');
}
req.projectId = client.projectId;
req.client = client;
const bot = req.headers['user-agent']
? isBot(req.headers['user-agent'])
: null;
if (bot) {
if (req.body.type === 'track') {
const path = (req.body.payload.properties?.__path ||
req.body.payload.properties?.path) as string | undefined;
await createBotEvent({
...bot,
projectId: client.projectId,
path: path ?? '',
createdAt: new Date(),
});
}
reply.status(202).send('OK');
}
} catch (error) {
logger.error('Failed to create bot event', { error });
reply.status(401).send();
return;
}
},
);
fastify.addHook('preHandler', clientHook);
fastify.addHook('preHandler', isBotHook);
fastify.route({
method: 'POST',