diff --git a/src/scripts/fetch-data/fetch-query.ts b/src/scripts/fetch-data/fetch-query.ts index cf5cdea..1829227 100644 --- a/src/scripts/fetch-data/fetch-query.ts +++ b/src/scripts/fetch-data/fetch-query.ts @@ -7,35 +7,26 @@ import fetchThreadHandiworkURLs from "./fetch-thread.js"; import HandiworkSearchQuery from "../classes/query/handiwork-search-query.js"; import LatestSearchQuery from "../classes/query/latest-search-query.js"; import ThreadSearchQuery from "../classes/query/thread-search-query.js"; +import { IQuery } from "../interfaces.js"; //#region Public methods -export default async function executeQuery(query: LatestSearchQuery, limit: number): Promise - -export default async function executeQuery(query: ThreadSearchQuery, limit: number): Promise - -export default async function executeQuery(query: HandiworkSearchQuery, limit: number): Promise /** * @param query Query used for the search * @param limit Maximum number of items to get. Default: 30 * @returns URLs of the fetched games */ -export default async function executeQuery(query: any, limit: number = 30): Promise { - // Local variables - const searchMap = { - "latest": fetchLatestHandiworkURLs, - "thread": fetchThreadHandiworkURLs, - "handiwork": fetchHandiworkURLs, +export default async function getURLsFromQuery(query: IQuery, limit: number = 30): Promise { + switch (query.itype) { + case "HandiworkSearchQuery": + return await fetchHandiworkURLs(query as HandiworkSearchQuery, limit); + case "LatestSearchQuery": + return await fetchLatestHandiworkURLs(query as LatestSearchQuery, limit); + case "ThreadSearchQuery": + return await fetchThreadHandiworkURLs(query as ThreadSearchQuery, limit); + default: + throw Error(`Invalid query type: ${query.itype}`); } - - // Find the key for the mapping dict - const key = query instanceof LatestSearchQuery ? - "latest" : - (query instanceof ThreadSearchQuery ? - "thread" : - "handiwork"); - - // Fetch and return the urls - return searchMap[key](query, limit); } + //#endregion \ No newline at end of file diff --git a/src/scripts/search.ts b/src/scripts/search.ts index bb16719..bd72d34 100644 --- a/src/scripts/search.ts +++ b/src/scripts/search.ts @@ -1,18 +1,9 @@ "use strict"; // Modules from file -import { IBasic } from "./interfaces.js"; -import HandiworkSearchQuery from "./classes/query/handiwork-search-query.js"; -import LatestSearchQuery from "./classes/query/latest-search-query.js"; -import ThreadSearchQuery from "./classes/query/thread-search-query.js"; +import { IBasic, IQuery } from "./interfaces.js"; import getHandiworkInformation from "./scrape-data/handiwork-parse.js"; -import executeQuery from "./fetch-data/fetch-query.js"; - -export async function search(query: LatestSearchQuery, limit: number): Promise - -export async function search(query: HandiworkSearchQuery, limit: number): Promise - -export async function search(query: ThreadSearchQuery, limit: number): Promise +import getURLsFromQuery from "./fetch-data/fetch-query.js"; /** * Gets the handiworks that match the passed parameters. @@ -20,9 +11,9 @@ export async function search(query: ThreadSearchQuery, limit: * @param {Number} limit * Maximum number of items to get. Default: 30 */ -export default async function search(query: any, limit: number = 30): Promise { +export default async function search(query: IQuery, limit: number = 30): Promise { // Fetch the URLs - const urls: string[] = await executeQuery(query, limit); + const urls: string[] = await getURLsFromQuery(query, limit); // Fetch the data const results = urls.map((url, idx) => {