Remove unnecessary overloads

pull/75/head
MillenniumEarl 2021-03-04 11:21:21 +01:00
parent 8036372d1f
commit 05e18eccd5
2 changed files with 16 additions and 34 deletions

View File

@ -7,35 +7,26 @@ import fetchThreadHandiworkURLs from "./fetch-thread.js";
import HandiworkSearchQuery from "../classes/query/handiwork-search-query.js"; import HandiworkSearchQuery from "../classes/query/handiwork-search-query.js";
import LatestSearchQuery from "../classes/query/latest-search-query.js"; import LatestSearchQuery from "../classes/query/latest-search-query.js";
import ThreadSearchQuery from "../classes/query/thread-search-query.js"; import ThreadSearchQuery from "../classes/query/thread-search-query.js";
import { IQuery } from "../interfaces.js";
//#region Public methods //#region Public methods
export default async function executeQuery(query: LatestSearchQuery, limit: number): Promise<string[]>
export default async function executeQuery(query: ThreadSearchQuery, limit: number): Promise<string[]>
export default async function executeQuery(query: HandiworkSearchQuery, limit: number): Promise<string[]>
/** /**
* @param query Query used for the search * @param query Query used for the search
* @param limit Maximum number of items to get. Default: 30 * @param limit Maximum number of items to get. Default: 30
* @returns URLs of the fetched games * @returns URLs of the fetched games
*/ */
export default async function executeQuery(query: any, limit: number = 30): Promise<string[]> { export default async function getURLsFromQuery(query: IQuery, limit: number = 30): Promise<string[]> {
// Local variables switch (query.itype) {
const searchMap = { case "HandiworkSearchQuery":
"latest": fetchLatestHandiworkURLs, return await fetchHandiworkURLs(query as HandiworkSearchQuery, limit);
"thread": fetchThreadHandiworkURLs, case "LatestSearchQuery":
"handiwork": fetchHandiworkURLs, 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 //#endregion

View File

@ -1,18 +1,9 @@
"use strict"; "use strict";
// Modules from file // Modules from file
import { IBasic } from "./interfaces.js"; import { IBasic, IQuery } 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 getHandiworkInformation from "./scrape-data/handiwork-parse.js"; import getHandiworkInformation from "./scrape-data/handiwork-parse.js";
import executeQuery from "./fetch-data/fetch-query.js"; import getURLsFromQuery from "./fetch-data/fetch-query.js";
export async function search<T extends IBasic>(query: LatestSearchQuery, limit: number): Promise<T[]>
export async function search<T extends IBasic>(query: HandiworkSearchQuery, limit: number): Promise<T[]>
export async function search<T extends IBasic>(query: ThreadSearchQuery, limit: number): Promise<T[]>
/** /**
* Gets the handiworks that match the passed parameters. * Gets the handiworks that match the passed parameters.
@ -20,9 +11,9 @@ export async function search<T extends IBasic>(query: ThreadSearchQuery, limit:
* @param {Number} limit * @param {Number} limit
* Maximum number of items to get. Default: 30 * Maximum number of items to get. Default: 30
*/ */
export default async function search<T extends IBasic>(query: any, limit: number = 30): Promise<T[]> { export default async function search<T extends IBasic>(query: IQuery, limit: number = 30): Promise<T[]> {
// Fetch the URLs // Fetch the URLs
const urls: string[] = await executeQuery(query, limit); const urls: string[] = await getURLsFromQuery(query, limit);
// Fetch the data // Fetch the data
const results = urls.map((url, idx) => { const results = urls.map((url, idx) => {