From 81b388af4aed53d353c4ed6f55f7f7718c3b9e69 Mon Sep 17 00:00:00 2001 From: Christian Fehmer Date: Thu, 22 Feb 2024 23:05:37 +0100 Subject: [PATCH] impr: remove usage of jQuery getJSON (fehmer) (#5109) * impr: remove usage of jQuery getJSON * use generic * throwing if nothing found --------- Co-authored-by: Miodec --- frontend/src/ts/controllers/quotes-controller.ts | 10 ++-------- frontend/src/ts/test/british-english.ts | 14 ++++++-------- frontend/src/ts/utils/misc.ts | 8 ++++++-- 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/frontend/src/ts/controllers/quotes-controller.ts b/frontend/src/ts/controllers/quotes-controller.ts index 3001a3db101d..4535c521bbdb 100644 --- a/frontend/src/ts/controllers/quotes-controller.ts +++ b/frontend/src/ts/controllers/quotes-controller.ts @@ -1,11 +1,11 @@ import { + cachedFetchJson, randomElementFromArray, removeLanguageSize, shuffle, } from "../utils/misc"; import { subscribe } from "../observables/config-event"; import * as DB from "../db"; -import $ from "jquery"; type JsonQuote = { text: string; @@ -48,8 +48,7 @@ class QuotesController { const normalizedLanguage = removeLanguageSize(language); if (this.quoteCollection.language !== normalizedLanguage) { - // try { - const data: QuoteData = await $.getJSON( + const data = await cachedFetchJson( `quotes/${normalizedLanguage}.json` ); @@ -96,11 +95,6 @@ class QuotesController { if (quoteLengths !== undefined) { this.updateQuoteQueue(quoteLengths); } - // } catch (e) { - // console.error(e); - // throw new Error("Failed to parse quotes: " + e.message); - // return defaultQuoteCollection; - // } } return this.quoteCollection; diff --git a/frontend/src/ts/test/british-english.ts b/frontend/src/ts/test/british-english.ts index 880e678403b0..ac7e1878f9d6 100644 --- a/frontend/src/ts/test/british-english.ts +++ b/frontend/src/ts/test/british-english.ts @@ -1,7 +1,9 @@ import Config from "../config"; -import { capitalizeFirstLetterOfEachWord } from "../utils/misc"; +import { + cachedFetchJson, + capitalizeFirstLetterOfEachWord, +} from "../utils/misc"; import * as CustomText from "../test/custom-text"; -import $ from "jquery"; type BritishEnglishReplacement = { 0: string; @@ -13,13 +15,9 @@ let list: BritishEnglishReplacement[] = []; export async function getList(): Promise { if (list.length === 0) { - return $.getJSON("languages/britishenglish.json", function (data) { - list = data; - return list; - }); - } else { - return list; + list = await cachedFetchJson("languages/britishenglish.json"); } + return list; } export async function replace( diff --git a/frontend/src/ts/utils/misc.ts b/frontend/src/ts/utils/misc.ts index d80db85b46ec..db5b6c6cba39 100644 --- a/frontend/src/ts/utils/misc.ts +++ b/frontend/src/ts/utils/misc.ts @@ -429,16 +429,20 @@ export function median(arr: number[]): number { } export async function getLatestReleaseFromGitHub(): Promise { - const releases = await $.getJSON( + type releaseType = { name: string }; + const releases = await cachedFetchJson( "https://api.github.com/repos/monkeytypegame/monkeytype/releases?per_page=1" ); + if (releases[0] === undefined || releases[0].name === undefined) { + throw new Error("No release found"); + } return releases[0].name; } export async function getReleasesFromGitHub(): Promise< MonkeyTypes.GithubRelease[] > { - return $.getJSON( + return cachedFetchJson( "https://api.github.com/repos/monkeytypegame/monkeytype/releases?per_page=5" ); }