-
Notifications
You must be signed in to change notification settings - Fork 2
update rsvp fetch method to avoid erroring #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+19
−28
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c245857
refactor: update ButtonProps to use ReactNode type for children
boykisserchan 464dba5
feat: implement background cache updater for RSVP count
boykisserchan 59b75da
positive test instead of negative one for clarity
boykisserchan 52d8515
potato
jeninh b228742
Update src/pages/index.tsx
boykisserchan a4c06e2
Update src/pages/api/rsvp.ts
boykisserchan 5eef35f
Update src/pages/api/rsvp.ts
boykisserchan cef6a5c
feat: implement background cache updater for RSVP count with improved…
boykisserchan 168403f
hana has had fucking enough.
boykisserchan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,48 +1,38 @@ | ||
| // src/pages/api/rsvp.ts | ||
| import { NextApiRequest, NextApiResponse } from "next"; | ||
|
|
||
| async function getCount() { | ||
| let offset; | ||
| // Rewritten to ensure a single background updater reloads the cached count every 30s. | ||
|
|
||
| async function getCount(): Promise<number> { | ||
| let offset: string | undefined; | ||
| let count = 0; | ||
|
|
||
| do { | ||
| const url = new URL(`https://api.airtable.com/v0/${process.env.RSVP_AIRTABLE_BASE_ID}/RSVPs`); | ||
| if (offset) url.searchParams.set("offset", offset); | ||
|
|
||
| const res = await fetch(url, { | ||
| const res = await fetch(url.toString(), { | ||
| headers: { | ||
| Authorization: `Bearer ${process.env.RSVP_AIRTABLE_API_KEY}`, | ||
| }, | ||
| }); | ||
|
|
||
| if (!res.ok) { | ||
| console.error(res.statusText); | ||
| throw new Error(res.statusText); | ||
| const text = await res.text().catch(() => res.statusText); | ||
| console.error("Airtable request failed:", res.status, text); | ||
| throw new Error(text || "Airtable request failed"); | ||
| } | ||
|
|
||
| const data = await res.json(); | ||
| count += data.records?.length ?? 0; | ||
| count += (data.records?.length ?? 0); | ||
| offset = data.offset; | ||
|
|
||
| if (offset) await new Promise((resolve) => setTimeout(resolve, 200)); | ||
| if (offset) await new Promise((r) => setTimeout(r, 200)); | ||
| } while (offset); | ||
|
|
||
| return count; | ||
| } | ||
|
|
||
| let cached = { value: -1, updated: 0 }; | ||
|
|
||
| // Cache duration in milliseconds | ||
| const CACHE_DURATION = 30000; | ||
|
|
||
| export default async function handler(_req: NextApiRequest, res: NextApiResponse) { | ||
| const now = Date.now(); | ||
| if (now - cached.updated > CACHE_DURATION) { | ||
| try { | ||
| const count = await getCount(); | ||
| cached = { value: count, updated: now }; | ||
| console.log("cached value", cached.value, "updatedAt", new Date(cached.updated).toISOString()); | ||
| } catch (err: unknown) { | ||
| console.error("getCount failed:", err); | ||
| res.status(500).json({ error: "couldnt get count" }); | ||
| } | ||
| } | ||
| res.status(200).json({ count: cached.value }); | ||
| } | ||
| export default function handler(_req: NextApiRequest, res: NextApiResponse) { | ||
boykisserchan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| getCount().then(count => res.status(200).json({ count })); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Inconsistent React type import usage. Line 4 uses
React.ButtonHTMLAttributes(namespace import), but line 9 now usesReactNode(named import). For consistency, either useReact.ReactNodewithout the import on line 2, or change line 4 to use a named import likeButtonHTMLAttributesfrom 'react'. The current mixed approach is valid but inconsistent.