Last active
January 14, 2026 22:00
-
-
Save ngimdock/98af0dd93a1becc35b41731295ae3d75 to your computer and use it in GitHub Desktop.
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
| const CAT_FACT_API_BASE_URL = "https://catfact.ninja"; | |
| type CatFactResponse = { | |
| fact: string; | |
| length: number; | |
| }; | |
| type CleanCatFact = { | |
| text: string; | |
| length: number; | |
| source: string; | |
| }; | |
| async function fetchCatFact(): Promise<CleanCatFact> { | |
| try { | |
| const response = await fetch(`${CAT_FACT_API_BASE_URL}/fact`); | |
| if (!response.ok) { | |
| throw new Error(`API error: ${response.status} ${response.statusText}`); | |
| } | |
| const data: CatFactResponse = await response.json(); | |
| if (!data.fact || typeof data.length !== "number") { | |
| throw new Error("Invalid API response structure"); | |
| } | |
| return { | |
| text: data.fact, | |
| length: data.length, | |
| source: CAT_FACT_API_BASE_URL | |
| }; | |
| } catch (error) { | |
| console.error("Failed to fetch cat fact:", error); | |
| throw new Error("Unable to fetch cat fact at this time"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment