Skip to content

Commit

Permalink
fix(api): add timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonsaldan committed Jan 12, 2025
1 parent d48a810 commit d546c1f
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/app/api/v1/sponsors/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,38 @@ interface GitHubPullRequest {
merged_at: string | null
}

const fetchWithTimeout = async (
url: string,
options: RequestInit,
timeout = 5000,
) => {
const controller = new AbortController()
const timeoutId = setTimeout(() => controller.abort(), timeout)

try {
const response = await fetch(url, {
...options,
signal: controller.signal,
})
return response
} finally {
clearTimeout(timeoutId)
}
}

async function fetchGitHubContributors(): Promise<string[]> {
try {
const allPRs = await Promise.all(
repos.map(async (repo) => {
const response = await fetch(
const response = await fetchWithTimeout(
`https://api.github.com/repos/${repo}/pulls?state=closed&per_page=100`,
{
headers: {
Accept: 'application/vnd.github.v3+json',
Authorization: `Bearer ${process.env.GITHUB_TOKEN}`,
},
},
5000,
)

if (!response.ok) {
Expand Down Expand Up @@ -96,13 +116,14 @@ async function fetchGitHubContributors(): Promise<string[]> {
async function fetchBuyMeACoffeePage(
page: number,
): Promise<BuyMeACoffeeResponse> {
const response = await fetch(
const response = await fetchWithTimeout(
`https://developers.buymeacoffee.com/api/v1/supporters?page=${page}`,
{
headers: {
Authorization: `Bearer ${process.env.BUYMEACOFFEE_ACCESS_TOKEN}`,
},
},
5000,
)

if (!response.ok) {
Expand Down Expand Up @@ -196,6 +217,7 @@ export async function GET(): Promise<NextResponse<SponsorsResponse>> {
'Content-Type': 'application/json',
'Cache-Control': 'public, s-maxage=3600, stale-while-revalidate=7200',
'Access-Control-Allow-Origin': '*',
'Content-Disposition': 'inline',
},
})
} catch (error: unknown) {
Expand Down

0 comments on commit d546c1f

Please sign in to comment.