diff --git a/src/APIs/FlagsAPI.js b/src/APIs/FlagsAPI.js deleted file mode 100644 index 9731036b..00000000 --- a/src/APIs/FlagsAPI.js +++ /dev/null @@ -1,29 +0,0 @@ -import compact from 'lodash/compact'; -import assign from 'lodash/assign'; -import omit from 'lodash/omit'; - -/* FlagsAPI cleans returned MongoDB data to match client-provided flags */ -export const handleWordFlags = ({ - data: { words, contentLength }, - flags: { examples, dialects, resolve }, -}) => { - const updatedWords = compact(words.map((word) => { - let updatedWord = assign(word); - if (!examples) { - updatedWord = omit(updatedWord, ['examples']); - } - if (!dialects) { - updatedWord = omit(updatedWord, ['dialects']); - } - if (!resolve) { - if (updatedWord.stems) { - updatedWord.stems = updatedWord.stems.map((stem) => stem._id || stem.id); - } - if (updatedWord.relatedTerms) { - updatedWord.relatedTerms = updatedWord.relatedTerms.map((relatedTerm) => relatedTerm._id || relatedTerm.id); - } - } - return updatedWord; - })); - return { words: updatedWords, contentLength }; -}; diff --git a/src/APIs/FlagsAPI.ts b/src/APIs/FlagsAPI.ts new file mode 100644 index 00000000..577f919e --- /dev/null +++ b/src/APIs/FlagsAPI.ts @@ -0,0 +1,56 @@ +import compact from 'lodash/compact'; +import assign from 'lodash/assign'; +import omit from 'lodash/omit'; + +interface Word { + id?: string; + stems?: Array; + relatedTerms?: Array; + examples?: any; // Update the type of `examples` accordingly + dialects?: any; // Update the type of `dialects` accordingly +} + +interface Data { + words: Word[]; + contentLength: number; +} + +interface Flags { + examples?: boolean; + dialects?: boolean; + resolve?: boolean; +} + +interface HandleWordFlagsParams { + data: Data; + flags: Flags; +} + +export const handleWordFlags = ({ + data: { words, contentLength }, + flags: { examples, dialects, resolve }, +}: HandleWordFlagsParams) => { + const updatedWords = compact( + words.map((word) => { + let updatedWord = assign({}, word); + if (!examples) { + updatedWord = omit(updatedWord, ['examples']); + } + if (!dialects) { + updatedWord = omit(updatedWord, ['dialects']); + } + if (!resolve) { + if (updatedWord.stems) { + updatedWord.stems = updatedWord.stems.map((stem) => (typeof stem === 'string' ? stem : stem.id)); + } + if (updatedWord.relatedTerms) { + updatedWord.relatedTerms = updatedWord.relatedTerms.map((relatedTerm) => + typeof relatedTerm === 'string' ? relatedTerm : relatedTerm.id + ); + } + } + return updatedWord; + }) + ); + return { words: updatedWords, contentLength }; +}; diff --git a/src/APIs/RedisAPI.js b/src/APIs/RedisAPI.ts similarity index 56% rename from src/APIs/RedisAPI.js rename to src/APIs/RedisAPI.ts index 2cfe1a54..d3e6cc28 100644 --- a/src/APIs/RedisAPI.js +++ b/src/APIs/RedisAPI.ts @@ -2,7 +2,12 @@ import assign from 'lodash/assign'; import { REDIS_CACHE_EXPIRATION } from '../config'; import minimizeWords from '../controllers/utils/minimizeWords'; -export const getCachedWords = async ({ key, redisClient }) => { +interface GetCachedWordsParams { + key: string; + redisClient: RedisClient; +} + +export const getCachedWords = async ({ key, redisClient }: GetCachedWordsParams) => { console.time('Getting cached words'); const rawCachedWords = await redisClient.get(key); const cachedWords = typeof rawCachedWords === 'string' ? JSON.parse(rawCachedWords) : rawCachedWords; @@ -11,13 +16,15 @@ export const getCachedWords = async ({ key, redisClient }) => { return cachedWords; }; -export const setCachedWords = async ({ - key, - data, - redisClient, - version, -}) => { - const updatedData = assign(data); +interface SetCachedWordsParams { + key: string; + data: any; // Update the type of `data` accordingly + redisClient: RedisClient; + version: string; // Update the type of `version` accordingly +} + +export const setCachedWords = async ({ key, data, redisClient, version }: SetCachedWordsParams) => { + const updatedData = assign({}, data); updatedData.words = minimizeWords(data.words, version); if (!redisClient.isFake) { await redisClient.set(key, JSON.stringify(updatedData), { EX: REDIS_CACHE_EXPIRATION }); @@ -25,35 +32,59 @@ export const setCachedWords = async ({ return updatedData; }; -export const getCachedExamples = async ({ key, redisClient }) => { +interface GetCachedExamplesParams { + key: string; + redisClient: RedisClient; +} + +export const getCachedExamples = async ({ key, redisClient }: GetCachedExamplesParams) => { const rawCachedExamples = await redisClient.get(key); const cachedExamples = typeof rawCachedExamples === 'string' ? JSON.parse(rawCachedExamples) : rawCachedExamples; console.log(`Retrieved cached data for examples ${key}:`, !!cachedExamples); return cachedExamples; }; -export const setCachedExamples = async ({ key, data, redisClient }) => { +interface SetCachedExamplesParams { + key: string; + data: any; // Update the type of `data` accordingly + redisClient: RedisClient; +} + +export const setCachedExamples = async ({ key, data, redisClient }: SetCachedExamplesParams) => { if (!redisClient.isFake) { await redisClient.set(key, JSON.stringify(data), { EX: REDIS_CACHE_EXPIRATION }); } return data; }; -export const getAllCachedVerbsAndSuffixes = async ({ key, redisClient }) => { +interface GetAllCachedVerbsAndSuffixesParams { + key: string; + redisClient: RedisClient; +} + +export const getAllCachedVerbsAndSuffixes = async ({ key, redisClient }: GetAllCachedVerbsAndSuffixesParams) => { const redisAllVerbsAndSuffixesKey = `verbs-and-suffixes-${key}`; const rawCachedAllVerbsAndSuffixes = await redisClient.get(redisAllVerbsAndSuffixesKey); - const cachedAllVerbsAndSuffixes = typeof rawCachedAllVerbsAndSuffixes === 'string' - ? JSON.parse(rawCachedAllVerbsAndSuffixes) - : rawCachedAllVerbsAndSuffixes; + const cachedAllVerbsAndSuffixes = + typeof rawCachedAllVerbsAndSuffixes === 'string' + ? JSON.parse(rawCachedAllVerbsAndSuffixes) + : rawCachedAllVerbsAndSuffixes; return cachedAllVerbsAndSuffixes; }; +interface SetAllCachedVerbsAndSuffixesParams { + key: string; + data: any; // Update the type of `data` accordingly + redisClient: RedisClient; + version: string; // Update the type of `version` accordingly +} + export const setAllCachedVerbsAndSuffixes = async ({ key, data, redisClient, version, -}) => { +}: SetAllCachedVerbsAndSuffixesParams) => { const redisAllVerbsAndSuffixesKey = `verbs-and-suffixes-${key}`; const updatedData = minimizeWords(data, version); if (!redisClient.isFake) {