diff --git a/src/handlers/main.ts b/src/handlers/main.ts index a8b8917..43d2183 100644 --- a/src/handlers/main.ts +++ b/src/handlers/main.ts @@ -1,17 +1,22 @@ import { IHandlerOutput } from "./handler.interface"; import { readability } from "./readability"; -type EngineFunction = (url: string) => Promise +type EngineFunction = (url: string) => Promise; -export default function handlePage(url: string, engine?: string): Promise { - let func: EngineFunction | undefined = engines[engine || ""]; - - if (func === undefined) { - const host = new URL(url).hostname; - func = fallback[host] || fallback["*"]; +export default function handlePage( + url: string, + engine?: string +): Promise { + if (engine && engineList.indexOf(engine) === -1) { + throw new Error("Invalid engine"); } - return func(url); + if (engine) { + return engines[engine](url); + } + + const host = new URL(url).hostname; + return fallback[host](url) || fallback["*"](url); } interface Engines { @@ -19,9 +24,11 @@ interface Engines { } export const engines: Engines = { - "readability": readability, + readability: readability, }; +export const engineList: string[] = Object.keys(engines); + const fallback: Engines = { "*": engines.readability, };