feat: open page api

This commit is contained in:
Ephraim Atta-Duncan
2024-10-23 17:33:16 +00:00
parent b8310237e4
commit 68c8f098b6
15 changed files with 281 additions and 36 deletions
@@ -0,0 +1,12 @@
import { NextResponse } from 'next/server';
import { requestHandler } from '@/app/request-handler';
export const GET = requestHandler(async () => {
const res = await fetch('https://api.github.com/repos/documenso/documenso');
const { forks_count } = await res.json();
return NextResponse.json({
data: forks_count,
});
});
@@ -0,0 +1,14 @@
import { NextResponse } from 'next/server';
import { requestHandler } from '@/app/request-handler';
export const GET = requestHandler(async () => {
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,
});
});
+14
View File
@@ -0,0 +1,14 @@
import { NextResponse } from 'next/server';
import { requestHandler } from '@/app/request-handler';
export const GET = requestHandler(async () => {
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,
});
});
+17
View File
@@ -0,0 +1,17 @@
import { type NextRequest, NextResponse } from 'next/server';
const paths = [
{ path: '/forks', description: 'GitHub Forks' },
{ path: '/stars', description: 'GitHub Stars' },
{ path: '/issues', description: 'GitHub Merged Issues' },
{ path: '/prs', description: 'GitHub Pull Request' },
];
export function GET(request: NextRequest) {
const url = request.nextUrl.toString();
const apis = paths.map(({ path, description }) => {
return { path: url + path, description };
});
return NextResponse.json(apis);
}
@@ -0,0 +1,12 @@
import { NextResponse } from 'next/server';
import { requestHandler } from '@/app/request-handler';
export const GET = requestHandler(async () => {
const res = await fetch('https://api.github.com/repos/documenso/documenso');
const { stargazers_count } = await res.json();
return NextResponse.json({
data: stargazers_count,
});
});
+43
View File
@@ -0,0 +1,43 @@
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;
function isAllowedOrigin(req: NextRequest): boolean {
const referer = req.headers.get('referer');
const host = req.headers.get('host');
if (referer && host) {
const refererUrl = new URL(referer);
return refererUrl.host === host;
}
if (host?.includes('localhost')) {
return true;
}
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 });
}
const result = await handler(req, ctx);
return result;
} catch (error) {
console.log(error);
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
}
};
}
+20
View File
@@ -0,0 +1,20 @@
import { type NextRequest, NextResponse } from 'next/server';
const paths = [{ path: 'github', description: 'GitHub Data' }];
export function GET(request: NextRequest) {
const url = request.nextUrl.toString();
const apis = paths.map(({ path, description }) => {
return { path: url + path, description };
});
return NextResponse.json(apis, {
status: 200,
headers: {
// TODO: Update for marketing page
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type',
},
});
}