improve(api): update api to fastify v5
This commit is contained in:
21
apps/api/src/hooks/deduplicate.hook.ts
Normal file
21
apps/api/src/hooks/deduplicate.hook.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { getLock } from '@openpanel/redis';
|
||||
import fastJsonStableHash from 'fast-json-stable-hash';
|
||||
import type { FastifyReply, FastifyRequest } from 'fastify';
|
||||
|
||||
export async function deduplicateHook(
|
||||
request: FastifyRequest,
|
||||
reply: FastifyReply,
|
||||
) {
|
||||
if (typeof request.body === 'object') {
|
||||
const locked = await getLock(
|
||||
`fastify:deduplicate:${fastJsonStableHash.hash(request.body, 'md5')}`,
|
||||
'1',
|
||||
100,
|
||||
);
|
||||
|
||||
if (locked) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
reply.status(200).send('Duplicated event');
|
||||
}
|
||||
@@ -1,14 +1,6 @@
|
||||
import type {
|
||||
FastifyReply,
|
||||
FastifyRequest,
|
||||
HookHandlerDoneFunction,
|
||||
} from 'fastify';
|
||||
import type { FastifyRequest } from 'fastify';
|
||||
|
||||
export function fixHook(
|
||||
request: FastifyRequest,
|
||||
reply: FastifyReply,
|
||||
done: HookHandlerDoneFunction,
|
||||
) {
|
||||
export async function fixHook(request: FastifyRequest) {
|
||||
const ua = request.headers['user-agent'];
|
||||
// Swift SDK issue: https://github.com/Openpanel-dev/swift-sdk/commit/d588fa761a36a33f3b78eb79d83bfd524e3c7144
|
||||
if (ua) {
|
||||
@@ -21,5 +13,4 @@ export function fixHook(
|
||||
);
|
||||
}
|
||||
}
|
||||
done();
|
||||
}
|
||||
|
||||
@@ -5,14 +5,9 @@ import type {
|
||||
HookHandlerDoneFunction,
|
||||
} from 'fastify';
|
||||
|
||||
export function ipHook(
|
||||
request: FastifyRequest,
|
||||
reply: FastifyReply,
|
||||
done: HookHandlerDoneFunction,
|
||||
) {
|
||||
export async function ipHook(request: FastifyRequest) {
|
||||
const ip = getClientIp(request);
|
||||
if (ip) {
|
||||
request.clientIp = ip;
|
||||
}
|
||||
done();
|
||||
}
|
||||
|
||||
@@ -4,13 +4,8 @@ import type {
|
||||
HookHandlerDoneFunction,
|
||||
} from 'fastify';
|
||||
|
||||
export function requestIdHook(
|
||||
request: FastifyRequest,
|
||||
reply: FastifyReply,
|
||||
done: HookHandlerDoneFunction,
|
||||
) {
|
||||
export async function requestIdHook(request: FastifyRequest) {
|
||||
if (!request.headers['request-id']) {
|
||||
request.headers['request-id'] = request.id;
|
||||
}
|
||||
done();
|
||||
}
|
||||
|
||||
@@ -1,8 +1,4 @@
|
||||
import type {
|
||||
FastifyReply,
|
||||
FastifyRequest,
|
||||
HookHandlerDoneFunction,
|
||||
} from 'fastify';
|
||||
import type { FastifyReply, FastifyRequest } from 'fastify';
|
||||
import { path, pick } from 'ramda';
|
||||
|
||||
const ignoreLog = ['/healthcheck', '/metrics', '/misc'];
|
||||
@@ -19,16 +15,15 @@ const getTrpcInput = (
|
||||
}
|
||||
};
|
||||
|
||||
export function requestLoggingHook(
|
||||
export async function requestLoggingHook(
|
||||
request: FastifyRequest,
|
||||
reply: FastifyReply,
|
||||
done: HookHandlerDoneFunction,
|
||||
) {
|
||||
if (ignoreMethods.includes(request.method)) {
|
||||
return done();
|
||||
return;
|
||||
}
|
||||
if (ignoreLog.some((path) => request.url.startsWith(path))) {
|
||||
return done();
|
||||
return;
|
||||
}
|
||||
if (request.url.includes('trpc')) {
|
||||
request.log.info('request done', {
|
||||
@@ -54,5 +49,4 @@ export function requestLoggingHook(
|
||||
body: request.body,
|
||||
});
|
||||
}
|
||||
done();
|
||||
}
|
||||
|
||||
@@ -1,14 +1,5 @@
|
||||
import type {
|
||||
FastifyReply,
|
||||
FastifyRequest,
|
||||
HookHandlerDoneFunction,
|
||||
} from 'fastify';
|
||||
import type { FastifyRequest } from 'fastify';
|
||||
|
||||
export function timestampHook(
|
||||
request: FastifyRequest,
|
||||
reply: FastifyReply,
|
||||
done: HookHandlerDoneFunction,
|
||||
) {
|
||||
export async function timestampHook(request: FastifyRequest) {
|
||||
request.timestamp = Date.now();
|
||||
done();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user