Skip to content

Instantly share code, notes, and snippets.

@ngimdock
Last active January 14, 2026 22:00
Show Gist options
  • Select an option

  • Save ngimdock/98af0dd93a1becc35b41731295ae3d75 to your computer and use it in GitHub Desktop.

Select an option

Save ngimdock/98af0dd93a1becc35b41731295ae3d75 to your computer and use it in GitHub Desktop.
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