import { API } from "../../types" ; import { Quote } from "./types" ; async function getDeveloperExcuse ( ) { try { const res = await fetch ( `${ process . env . API_ENDPOINT }/developer-excuses` ) ; const body = await res . json ( ) ; return { quote : body . data , } ; } catch ( err ) { return { quote : "Unable to get a new developer excuse." , } ; } } async function getQuoteOfTheDay ( category ? ) { const res = await fetch ( "https://quotes.rest/qod.json" + ( category ? `?category=${ category }` : "" ) ) ; const body = await res . json ( ) ; if ( res . status === 429 ) { return { author : body . error . message . split ( "." ) [ 1 ] + "." , quote : "Too many requests this hour." , } ; } if ( body && body . contents && body . contents . quotes && body . contents . quotes [ 0 ] ) { return { author : body . contents . quotes [ 0 ] . author , quote : body . contents . quotes [ 0 ] . quote , } ; } return { author : null , quote : null , } ; } async function getBibleVerse ( ) { const res = await fetch ( "https://quotes.rest/bible/vod.json" ) ; const body = await res . json ( ) ; if ( res . status === 429 ) { return { author : body . error . message . split ( "." ) [ 1 ] + "." , quote : "Too many requests this hour." , } ; } if ( body && body . contents ) { return { author : body . contents . book + " " + body . contents . chapter + ":" + body . contents . number , quote : body . contents . verse , } ; } return { author : null , quote : null , } ; } export async function getQuote ( loader , category ? ) { loader . push ( ) ; const data = category === "developerexcuses" ? await getDeveloperExcuse ( ) : category === "bible" ? await getBibleVerse ( ) : await getQuoteOfTheDay ( category ) ; loader . pop ( ) ; const quote = cleanQuote ( data . quote ) ; return { ... data , quote , timestamp : Date . now ( ) , } ; } function cleanQuote ( quote ) { quote = quote . trim ( ) ; const singleStraightQuote = new RegExp ( /(\S)'|"/ , "g" ) ; quote = quote . replace ( singleStraightQuote , "$1’" ) ; const openingStraightQuote = new RegExp ( /(^|\s)'|"/ , "g" ) ; quote = quote . replace ( openingStraightQuote , "$1‘" ) ; const threeDots = new RegExp ( /\.{3,}/ , "g" ) ; quote = quote . replace ( threeDots , "…" ) ; const spaces = new RegExp ( /\s{2,}/ , "g" ) ; quote = quote . replace ( spaces , " " ) ; const dash = new RegExp ( /\s-\s/ , "g" ) ; quote = quote . replace ( dash , "—" ) ; const closingPunctuation = new RegExp ( /[.\?!…’]$/ ) ; if ( ! quote . match ( closingPunctuation ) ) quote = quote + "." ; return quote ; }