Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: converted src/APIs to TS #677

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 0 additions & 29 deletions src/APIs/FlagsAPI.js

This file was deleted.

56 changes: 56 additions & 0 deletions src/APIs/FlagsAPI.ts
Original file line number Diff line number Diff line change
@@ -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<string | { id?: string }>;
relatedTerms?: Array<string | { id?: string }>;
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);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why the Loadash is used instead of the native Object.assign method?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope, just a decision that was made in the past!

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 };
};
62 changes: 47 additions & 15 deletions src/APIs/RedisAPI.js → src/APIs/RedisAPI.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -11,49 +17,75 @@ 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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any takes away the benefit of using typescript, avoid if possible

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or better still use the unknown type

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 });
}
return updatedData;
};
shaikahmadnawaz marked this conversation as resolved.
Show resolved Hide resolved

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;
};
shaikahmadnawaz marked this conversation as resolved.
Show resolved Hide resolved

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;
};
shaikahmadnawaz marked this conversation as resolved.
Show resolved Hide resolved

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) {
Expand Down