Skip to content

Commit

Permalink
fix(words): allow for filtering for wordClasses in production
Browse files Browse the repository at this point in the history
  • Loading branch information
ijemmao committed Jan 20, 2025
1 parent b6dd1e1 commit 7ec5e05
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 39 deletions.
2 changes: 2 additions & 0 deletions src/controllers/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ export const handleQueries = async ({
.replace(/[[\]']/g, '')
.split(',')
.map((wordClass) => wordClass.trim())
.sort()
: [];
const resolve = resolveQuery === 'true';
const flags: Flags = {
Expand All @@ -159,6 +160,7 @@ export const handleQueries = async ({
...(tags?.length ? { tags: { $in: tags } } : {}),
...(wordClasses?.length ? { 'definitions.wordClass': { $in: wordClasses } } : {}),
};

return {
id,
version,
Expand Down
35 changes: 18 additions & 17 deletions src/controllers/utils/searchWordUsingEnglish.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import { RedisClientType } from 'redis';
import { handleWordFlags } from '../../APIs/FlagsAPI';
import { getCachedWords, setCachedWords } from '../../APIs/RedisAPI';
import Version from '../../shared/constants/Version';
import { SearchRegExp } from '../../shared/utils/createRegExp';
import { findWordsWithMatch } from './buildDocs';
import { sortDocsBy } from './sortDocsBy';
import { searchEnglishRegexQuery } from './queries';
import { getCachedWords, setCachedWords } from '../../APIs/RedisAPI';
import { handleWordFlags } from '../../APIs/FlagsAPI';
import { SearchRegExp } from '../../shared/utils/createRegExp';
import { sortDocsBy } from './sortDocsBy';

type EnglishSearch = {
redisClient: RedisClientType | undefined;
version: Version;
regex: SearchRegExp;
searchWord: string;
skip: number;
limit: number;
redisClient: RedisClientType | undefined,
version: Version,
regex: SearchRegExp,
searchWord: string,
skip: number,
limit: number,
flags: {
examples: boolean;
dialects: boolean;
resolve: boolean;
};
filters: any;
examples: boolean,
dialects: boolean,
resolve: boolean,
},
filters: any,
};

/* Searches for word with English stored in MongoDB */
Expand All @@ -34,7 +34,7 @@ const searchWordUsingEnglish = async ({
filters,
}: EnglishSearch) => {
let responseData = { words: [], contentLength: 0 };
const redisWordsCacheKey = `"${searchWord}"-${version}`;
const redisWordsCacheKey = `"${searchWord}"-${JSON.stringify(filters)}-${version}`;
const cachedWords = await getCachedWords({ key: redisWordsCacheKey, redisClient });

if (cachedWords) {
Expand All @@ -53,7 +53,8 @@ const searchWordUsingEnglish = async ({
});
}

const sortKey = version === Version.VERSION_1 ? 'definitions[0]' : 'definitions[0].definitions[0]';
const sortKey =
version === Version.VERSION_1 ? 'definitions[0]' : 'definitions[0].definitions[0]';
let sortedWords = sortDocsBy(searchWord, responseData.words, sortKey, version, regex);
sortedWords = sortedWords.slice(skip, skip + limit);

Expand Down
18 changes: 9 additions & 9 deletions src/controllers/utils/searchWordUsingIgbo.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { RedisClientType } from 'redis';
import { compact, uniqWith } from 'lodash';
import { RedisClientType } from 'redis';
import { handleWordFlags } from '../../APIs/FlagsAPI';
import { getCachedWords, setCachedWords } from '../../APIs/RedisAPI';
import Version from '../../shared/constants/Version';
import { SearchRegExp } from '../../shared/utils/createRegExp';
import { Filters } from '../types';
import { findWordsWithMatch } from './buildDocs';
import {
searchDefinitionsWithinIgboTextSearch,
searchIgboTextSearch,
strictSearchIgboQuery,
searchDefinitionsWithinIgboTextSearch,
} from './queries';
import { findWordsWithMatch } from './buildDocs';
import { sortDocsBy } from './sortDocsBy';
import { getCachedWords, setCachedWords } from '../../APIs/RedisAPI';
import { handleWordFlags } from '../../APIs/FlagsAPI';
import Version from '../../shared/constants/Version';
import { SearchRegExp } from '../../shared/utils/createRegExp';
import { Filters } from '../types';
import { Keyword } from './types';

type IgboSearch = {
Expand Down Expand Up @@ -47,7 +47,7 @@ const searchWordUsingIgbo = async ({
filters,
}: IgboSearch) => {
let responseData = { words: [], contentLength: 0 };
const redisWordsCacheKey = `${searchWord}-${version}`;
const redisWordsCacheKey = `${searchWord}-${JSON.stringify(filters)}-${version}`;
const cachedWords = await getCachedWords({ key: redisWordsCacheKey, redisClient });

if (cachedWords) {
Expand Down
20 changes: 10 additions & 10 deletions src/controllers/words.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import isWord from 'is-word';
import { map, omit } from 'lodash';
import mongoose from 'mongoose';
import isWord from 'is-word';
import removePrefix from '../shared/utils/removePrefix';
import { handleWordFlags } from '../APIs/FlagsAPI';
import { wordSchema } from '../models/Word';
import { findSearchWord } from '../services/words';
import { NO_PROVIDED_TERM } from '../shared/constants/errorMessages';
import { getDocumentsIds } from '../shared/utils/documentUtils';
import createRegExp from '../shared/utils/createRegExp';
import { packageResponse, handleQueries } from './utils';
import { getDocumentsIds } from '../shared/utils/documentUtils';
import removePrefix from '../shared/utils/removePrefix';
import { IncomingWord, MiddleWare } from '../types';
import { createExample } from './examples';
import { WordResponseData } from './types';
import { handleQueries, packageResponse } from './utils';
import { findWordsWithMatch } from './utils/buildDocs';
import minimizeWords from './utils/minimizeWords';
import searchWordUsingEnglish from './utils/searchWordUsingEnglish';
import searchWordUsingIgbo from './utils/searchWordUsingIgbo';
import { createExample } from './examples';
import { wordSchema } from '../models/Word';
import { handleWordFlags } from '../APIs/FlagsAPI';
import minimizeWords from './utils/minimizeWords';
import { MiddleWare, IncomingWord } from '../types';
import { WordResponseData } from './types';

const isEnglish = isWord('american-english');
const IGNORE_ENGLISH_WORDS = ['ego', 'la'];
Expand Down
6 changes: 3 additions & 3 deletions src/middleware/validateApiKey.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { authorizeDeveloperUsage } from './helpers/authorizeDeveloperUsage';
import { findDeveloper } from './helpers/findDeveloper';
import { MAIN_KEY, isDevelopment, isProduction } from '../config';
import { MiddleWare } from '../types';
import { authorizeDeveloperUsage } from './helpers/authorizeDeveloperUsage';
import { findDeveloper } from './helpers/findDeveloper';

const FALLBACK_API_KEY = 'fallback_api_key';

const validateApiKey: MiddleWare = async (req, res, next) => {
try {
console.log('validating API key');
console.log('Validating API key');
let apiKey = (req.headers['X-API-Key'] || req.headers['x-api-key']) as string;

/* Official sites can bypass validation */
Expand Down

0 comments on commit 7ec5e05

Please sign in to comment.