mirror of
https://github.com/documenso/documenso.git
synced 2026-07-25 01:15:49 +10:00
fix: cors
This commit is contained in:
@@ -1,12 +1,25 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import cors from '@/lib/cors';
|
||||
|
||||
import { requestHandler } from '@/app/request-handler';
|
||||
|
||||
export const GET = requestHandler(async () => {
|
||||
export async function GET(request: Request) {
|
||||
const res = await fetch('https://api.github.com/repos/documenso/documenso');
|
||||
const { forks_count } = await res.json();
|
||||
|
||||
return NextResponse.json({
|
||||
data: forks_count,
|
||||
});
|
||||
});
|
||||
return cors(
|
||||
request,
|
||||
new Response(JSON.stringify({ data: forks_count }), {
|
||||
status: 200,
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
},
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
export function OPTIONS(request: Request) {
|
||||
return cors(
|
||||
request,
|
||||
new Response(null, {
|
||||
status: 204,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,14 +1,27 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import cors from '@/lib/cors';
|
||||
|
||||
import { requestHandler } from '@/app/request-handler';
|
||||
|
||||
export const GET = requestHandler(async () => {
|
||||
export async function GET(request: Request) {
|
||||
const res = await fetch(
|
||||
'https://api.github.com/search/issues?q=repo:documenso/documenso+type:issue+state:open&page=0&per_page=1',
|
||||
);
|
||||
const { total_count } = await res.json();
|
||||
|
||||
return NextResponse.json({
|
||||
data: total_count,
|
||||
});
|
||||
});
|
||||
return cors(
|
||||
request,
|
||||
new Response(JSON.stringify({ data: total_count }), {
|
||||
status: 200,
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
},
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
export function OPTIONS(request: Request) {
|
||||
return cors(
|
||||
request,
|
||||
new Response(null, {
|
||||
status: 204,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,14 +1,27 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import cors from '@/lib/cors';
|
||||
|
||||
import { requestHandler } from '@/app/request-handler';
|
||||
|
||||
export const GET = requestHandler(async () => {
|
||||
export async function GET(request: Request) {
|
||||
const res = await fetch(
|
||||
'https://api.github.com/search/issues?q=repo:documenso/documenso/+is:pr+merged:>=2010-01-01&page=0&per_page=1',
|
||||
);
|
||||
const { total_count } = await res.json();
|
||||
|
||||
return NextResponse.json({
|
||||
data: total_count,
|
||||
});
|
||||
});
|
||||
return cors(
|
||||
request,
|
||||
new Response(JSON.stringify({ data: total_count }), {
|
||||
status: 200,
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
},
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
export function OPTIONS(request: Request) {
|
||||
return cors(
|
||||
request,
|
||||
new Response(null, {
|
||||
status: 204,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import { type NextRequest, NextResponse } from 'next/server';
|
||||
import type { NextRequest } from 'next/server';
|
||||
|
||||
import cors from '@/lib/cors';
|
||||
|
||||
const paths = [
|
||||
{ path: '/forks', description: 'GitHub Forks' },
|
||||
@@ -13,5 +15,22 @@ export function GET(request: NextRequest) {
|
||||
return { path: url + path, description };
|
||||
});
|
||||
|
||||
return NextResponse.json(apis);
|
||||
return cors(
|
||||
request,
|
||||
new Response(JSON.stringify(apis), {
|
||||
status: 200,
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
},
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
export function OPTIONS(request: Request) {
|
||||
return cors(
|
||||
request,
|
||||
new Response(null, {
|
||||
status: 204,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,12 +1,25 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import cors from '@/lib/cors';
|
||||
|
||||
import { requestHandler } from '@/app/request-handler';
|
||||
|
||||
export const GET = requestHandler(async () => {
|
||||
export async function GET(request: Request) {
|
||||
const res = await fetch('https://api.github.com/repos/documenso/documenso');
|
||||
const { stargazers_count } = await res.json();
|
||||
|
||||
return NextResponse.json({
|
||||
data: stargazers_count,
|
||||
});
|
||||
});
|
||||
return cors(
|
||||
request,
|
||||
new Response(JSON.stringify({ data: stargazers_count }), {
|
||||
status: 200,
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
},
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
export function OPTIONS(request: Request) {
|
||||
return cors(
|
||||
request,
|
||||
new Response(null, {
|
||||
status: 204,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,76 +0,0 @@
|
||||
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', 'prd-openpage-api.vercel.app']);
|
||||
|
||||
const CORS_HEADERS = {
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
'Access-Control-Allow-Methods': 'GET, PUT, DELETE, OPTIONS',
|
||||
'Access-Control-Allow-Headers': 'Content-Type',
|
||||
};
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
// },
|
||||
// );
|
||||
// }
|
||||
|
||||
const response = await handler(req, ctx);
|
||||
|
||||
Object.entries(CORS_HEADERS).forEach(([key, value]) => {
|
||||
response.headers.set(key, value);
|
||||
});
|
||||
|
||||
return response;
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Internal Server Error' },
|
||||
{
|
||||
status: 500,
|
||||
headers: CORS_HEADERS,
|
||||
},
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
import { type NextRequest, NextResponse } from 'next/server';
|
||||
import type { NextRequest } from 'next/server';
|
||||
|
||||
import cors from '@/lib/cors';
|
||||
|
||||
const paths = [{ path: 'github', description: 'GitHub Data' }];
|
||||
|
||||
@@ -8,12 +10,22 @@ export function GET(request: NextRequest) {
|
||||
return { path: url + path, description };
|
||||
});
|
||||
|
||||
return NextResponse.json(apis, {
|
||||
headers: {
|
||||
// TODO: Update for marketing page
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
'Access-Control-Allow-Methods': 'GET, PUT, DELETE, OPTIONS',
|
||||
'Access-Control-Allow-Headers': 'Content-Type',
|
||||
},
|
||||
});
|
||||
return cors(
|
||||
request,
|
||||
new Response(JSON.stringify(apis), {
|
||||
status: 200,
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
},
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
export function OPTIONS(request: Request) {
|
||||
return cors(
|
||||
request,
|
||||
new Response(null, {
|
||||
status: 204,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user