1
0
Fork 0
mirror of https://github.com/lukevella/rallly.git synced 2025-07-14 06:57:33 +02:00
rallly/packages/posthog/src/server/index.ts
2025-02-12 12:51:04 +07:00

28 lines
800 B
TypeScript

import { waitUntil } from "@vercel/functions";
import type { NextRequest } from "next/server";
import { PostHog } from "posthog-node";
function PostHogClient() {
if (!process.env.NEXT_PUBLIC_POSTHOG_API_KEY) return null;
const posthogClient = new PostHog(process.env.NEXT_PUBLIC_POSTHOG_API_KEY, {
host: process.env.NEXT_PUBLIC_POSTHOG_API_HOST,
flushAt: 1,
flushInterval: 0,
});
return posthogClient;
}
export const posthog = PostHogClient();
export function withPosthog(handler: (req: NextRequest) => Promise<Response>) {
return async (req: NextRequest) => {
const res = await handler(req);
try {
waitUntil(Promise.all([posthog?.shutdown()]));
} catch (error) {
console.error("Failed to flush PostHog events:", error);
}
return res;
};
}