refactor: handlePage

throw error when invalid engine
This commit is contained in:
Artemy 2023-08-14 16:45:06 +03:00
parent 2e1e4d53be
commit 974ef6cd6a

View File

@ -1,17 +1,22 @@
import { IHandlerOutput } from "./handler.interface";
import { readability } from "./readability";
type EngineFunction = (url: string) => Promise<IHandlerOutput>
type EngineFunction = (url: string) => Promise<IHandlerOutput>;
export default function handlePage(url: string, engine?: string): Promise<IHandlerOutput> {
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<IHandlerOutput> {
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,
};