refactor: handlePage
throw error when invalid engine
This commit is contained in:
parent
2e1e4d53be
commit
974ef6cd6a
@ -1,17 +1,22 @@
|
|||||||
import { IHandlerOutput } from "./handler.interface";
|
import { IHandlerOutput } from "./handler.interface";
|
||||||
import { readability } from "./readability";
|
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> {
|
export default function handlePage(
|
||||||
let func: EngineFunction | undefined = engines[engine || ""];
|
url: string,
|
||||||
|
engine?: string
|
||||||
if (func === undefined) {
|
): Promise<IHandlerOutput> {
|
||||||
const host = new URL(url).hostname;
|
if (engine && engineList.indexOf(engine) === -1) {
|
||||||
func = fallback[host] || fallback["*"];
|
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 {
|
interface Engines {
|
||||||
@ -19,9 +24,11 @@ interface Engines {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const engines: Engines = {
|
export const engines: Engines = {
|
||||||
"readability": readability,
|
readability: readability,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const engineList: string[] = Object.keys(engines);
|
||||||
|
|
||||||
const fallback: Engines = {
|
const fallback: Engines = {
|
||||||
"*": engines.readability,
|
"*": engines.readability,
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user