Files
sign/apps/openpage-api/app/request-handler.ts

77 lines
1.8 KiB
TypeScript
Raw Normal View History

2024-10-23 17:33:16 +00:00
import type { NextRequest } from 'next/server';
import { NextResponse } from 'next/server';
type RouteHandler<T = Record<string, string | string[]>> = (
req: NextRequest,
ctx: { params: T },
) => Promise<Response> | Response;
const ALLOWED_ORIGINS = new Set(['documenso.com']);
const CORS_HEADERS = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type',
};
2024-10-23 17:33:16 +00:00
function isAllowedOrigin(req: NextRequest): boolean {
const referer = req.headers.get('referer');
const host = req.headers.get('host');
if (host?.includes('localhost')) {
return true;
}
if (!referer || !host) {
return false;
}
try {
const refererUrl = new URL(referer);
const hostUrl = new URL(`http://${host}`);
const isRefererAllowed = ALLOWED_ORIGINS.has(refererUrl.host);
const isHostAllowed = ALLOWED_ORIGINS.has(hostUrl.host);
return isRefererAllowed || isHostAllowed;
} catch (error) {
console.error('Error parsing URLs:', error);
return false;
}
2024-10-23 17:33:16 +00:00
}
export function requestHandler<T = Record<string, string | string[]>>(
handler: RouteHandler<T>,
): RouteHandler<T> {
return async (req: NextRequest, ctx: { params: T }) => {
try {
if (!isAllowedOrigin(req)) {
return NextResponse.json(
{ error: 'Forbidden' },
{
status: 403,
headers: CORS_HEADERS,
},
);
2024-10-23 17:33:16 +00:00
}
const response = await handler(req, ctx);
Object.entries(CORS_HEADERS).forEach(([key, value]) => {
response.headers.set(key, value);
});
2024-10-23 17:33:16 +00:00
return response;
2024-10-23 17:33:16 +00:00
} catch (error) {
console.log(error);
return NextResponse.json(
{ error: 'Internal Server Error' },
{
status: 500,
headers: CORS_HEADERS,
},
);
2024-10-23 17:33:16 +00:00
}
};
}