This commit is contained in:
Carl-Gerhard Lindesvärd
2024-08-01 22:01:46 +02:00
committed by Carl-Gerhard Lindesvärd
parent 15e997129a
commit 2226cb463d
10 changed files with 73 additions and 83 deletions

View File

@@ -1,68 +1,21 @@
import { NextResponse } from 'next/server';
const VALID_PATHS = [
'/profile',
'/profile/increment',
'/profile/decrement',
'/event',
];
function getIp(req: Request) {
if (req.headers.get('X-Forwarded-For')) {
return req.headers.get('X-Forwarded-For')?.split(',')[0];
}
return req.headers.get('x-real-ip') ?? '0.0.0.0';
}
function getPath(params?: Record<string, unknown>) {
const segments = params?.op;
if (segments && Array.isArray(segments)) {
const path = `/${segments.join('/')}`;
if (VALID_PATHS.includes(path)) {
return path;
}
}
return null;
}
export function createNextRouteHandler({
clientId,
clientSecret,
url = 'https://api.openpanel.dev',
apiUrl = 'https://api.openpanel.dev',
}: {
clientId: string;
clientSecret: string;
url?: string;
apiUrl?: string;
}) {
return {
POST: async function POST(
req: Request,
{ params }: { params: Record<string, unknown> }
) {
const path = getPath(params);
if (!path) {
return NextResponse.json('Invalid path');
}
const headers = {
'user-agent': req.headers.get('user-agent')!,
'Content-Type': req.headers.get('Content-Type')!,
'openpanel-client-id': clientId,
'openpanel-client-secret': clientSecret,
'x-client-ip': getIp(req)!,
};
try {
const res = await fetch(`${url}${path}`, {
method: 'POST',
headers,
body: JSON.stringify(await req.json()),
});
return NextResponse.json(await res.text());
} catch (e) {
return NextResponse.json(e);
}
},
return async function POST(req: Request) {
const headers = new Headers(req.headers);
try {
const res = await fetch(`${apiUrl}/track`, {
method: 'POST',
headers,
body: JSON.stringify(await req.json()),
});
return NextResponse.json(await res.text());
} catch (e) {
return NextResponse.json(e);
}
};
}