Skip to content

Commit

Permalink
API route fetch fix
Browse files Browse the repository at this point in the history
  • Loading branch information
nuotsu committed Aug 11, 2024
1 parent 05401e5 commit 6465920
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 21 deletions.
5 changes: 4 additions & 1 deletion src/lib/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { BASE_URL } from '@sanity/src/env'
import dev from './env'

const BASE_URL = dev
? `http://localhost:3000`
: process.env.NEXT_PUBLIC_BASE_URL

export async function fetchAPI<T = any>(
endpoint: string,
{
Expand Down
34 changes: 14 additions & 20 deletions src/ui/Reputation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,20 @@ export default async function Reputation({
} & React.HTMLAttributes<HTMLDivElement>) {
if (!reputation) return null

const { count, avatars } = await getStargazers(reputation)
const { count, avatars } = await fetchAPI<{
error?: string
count?: number
avatars?: { avatar_url: string; login: string }[]
}>('/stargazers', {
params: {
repo: stegaClean(reputation.repo),
limit: reputation.limit,
},
next: {
revalidate: 3600,
tags: ['stargazers'],
},
})

const imgClassname = cn(
'aspect-square h-8 w-auto rounded-full border-2 border-canvas object-cover -mr-2 last:mr-0',
Expand Down Expand Up @@ -56,22 +69,3 @@ export default async function Reputation({
</div>
)
}

async function getStargazers(reputation?: Sanity.Reputation) {
if (!reputation?.repo) return {}

return await fetchAPI<{
error?: string
count?: number
avatars?: { avatar_url: string; login: string }[]
}>('/stargazers', {
params: {
repo: stegaClean(reputation.repo),
limit: reputation.limit,
},
next: {
revalidate: 3600,
tags: ['stargazers'],
},
})
}

0 comments on commit 6465920

Please sign in to comment.