INNER CODE UNIT · TypeScript
validQuotes
PiyushSuthar/github-readme-quotes · src/fetcher/fetch-quotes.ts:23
const validQuotes = data.filter(quote => quote && typeof quote.text === 'string');
// If no valid quotes exist, throw an error.
if (validQuotes.length === 0) {
throw new Error('No valid quotes found in the data.');
}
// Get a random quote.
let randQuote = validQuotes[Math.floor(Math.random() * validQuotes.length)];
// Check length and recurse if necessary.
return randQuote.text.length < 220 ? randQuote : randomQuote(validQuotes);
};
export async function fetchQuotes(): Promise<ParseDataReturn> {
const response = await axios.get(
'https://github.com/mudroljub/programming-quotes-api/raw/master/data/quotes.json'
);