From 585d1eaa1dfebc1ef90b1c91bf6a7ccbe371488e Mon Sep 17 00:00:00 2001 From: Shaik Ahmad Nawaz Date: Wed, 21 Jun 2023 10:17:12 +0530 Subject: [PATCH 1/3] feat: converted src/APIs to TS --- src/APIs/FlagsAPI.js | 29 ------------- src/APIs/FlagsAPI.ts | 54 +++++++++++++++++++++++ src/APIs/{RedisAPI.js => RedisAPI.ts} | 62 ++++++++++++++++++++------- 3 files changed, 101 insertions(+), 44 deletions(-) delete mode 100644 src/APIs/FlagsAPI.js create mode 100644 src/APIs/FlagsAPI.ts rename src/APIs/{RedisAPI.js => RedisAPI.ts} (55%) 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..b0c5f8b2 --- /dev/null +++ b/src/APIs/FlagsAPI.ts @@ -0,0 +1,54 @@ +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) => stem.id); + } + if (updatedWord.relatedTerms) { + updatedWord.relatedTerms = updatedWord.relatedTerms.map((relatedTerm) => relatedTerm.id); + } + } + return updatedWord; + }) + ); + return { words: updatedWords, contentLength }; +}; diff --git a/src/APIs/RedisAPI.js b/src/APIs/RedisAPI.ts similarity index 55% rename from src/APIs/RedisAPI.js rename to src/APIs/RedisAPI.ts index 2cfe1a54..b0ca3c1b 100644 --- a/src/APIs/RedisAPI.js +++ b/src/APIs/RedisAPI.ts @@ -1,8 +1,14 @@ +import { RedisClient } from 'redis'; // Import the appropriate Redis client type 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 +17,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 +33,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) { From 2cda28b64406ebb3391fcee4c6df238c7c8b3279 Mon Sep 17 00:00:00 2001 From: Shaik Ahmad Nawaz Date: Thu, 22 Jun 2023 07:35:22 +0530 Subject: [PATCH 2/3] feat: fixed type mismatch --- src/APIs/FlagsAPI.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/APIs/FlagsAPI.ts b/src/APIs/FlagsAPI.ts index b0c5f8b2..577f919e 100644 --- a/src/APIs/FlagsAPI.ts +++ b/src/APIs/FlagsAPI.ts @@ -41,10 +41,12 @@ export const handleWordFlags = ({ } if (!resolve) { if (updatedWord.stems) { - updatedWord.stems = updatedWord.stems.map((stem) => stem.id); + updatedWord.stems = updatedWord.stems.map((stem) => (typeof stem === 'string' ? stem : stem.id)); } if (updatedWord.relatedTerms) { - updatedWord.relatedTerms = updatedWord.relatedTerms.map((relatedTerm) => relatedTerm.id); + updatedWord.relatedTerms = updatedWord.relatedTerms.map((relatedTerm) => + typeof relatedTerm === 'string' ? relatedTerm : relatedTerm.id + ); } } return updatedWord; From 3e4bafab5c4ac9d6d61e2fd95f956227ba3baeb8 Mon Sep 17 00:00:00 2001 From: Shaik Ahmad Nawaz <96189881+shaikahmadnawaz@users.noreply.github.com> Date: Tue, 4 Jul 2023 11:03:15 +0530 Subject: [PATCH 3/3] Update RedisAPI.ts RedisAPI is throwing the error saying that redis doesn't export RedisClient, so I removed import of this. --- src/APIs/RedisAPI.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/APIs/RedisAPI.ts b/src/APIs/RedisAPI.ts index b0ca3c1b..d3e6cc28 100644 --- a/src/APIs/RedisAPI.ts +++ b/src/APIs/RedisAPI.ts @@ -1,4 +1,3 @@ -import { RedisClient } from 'redis'; // Import the appropriate Redis client type import assign from 'lodash/assign'; import { REDIS_CACHE_EXPIRATION } from '../config'; import minimizeWords from '../controllers/utils/minimizeWords';