Merge pull request #7 from TxtDot/fix-handlePage
refactor: move request to handlePage
This commit is contained in:
commit
d9fd968792
@ -1,9 +1,11 @@
|
||||
import axios from "../types/axios";
|
||||
import { IHandlerOutput } from "./handler.interface";
|
||||
import { readability } from "./readability";
|
||||
import { JSDOM } from "jsdom";
|
||||
|
||||
type EngineFunction = (url: string) => Promise<IHandlerOutput>;
|
||||
type EngineFunction = (url: JSDOM) => Promise<IHandlerOutput>;
|
||||
|
||||
export default function handlePage(
|
||||
export default async function handlePage(
|
||||
url: string,
|
||||
engine?: string
|
||||
): Promise<IHandlerOutput> {
|
||||
@ -11,12 +13,15 @@ export default function handlePage(
|
||||
throw new Error("Invalid engine");
|
||||
}
|
||||
|
||||
const response = await axios.get(url);
|
||||
const dom = new JSDOM(response.data, { url: url });
|
||||
|
||||
if (engine) {
|
||||
return engines[engine](url);
|
||||
return engines[engine](dom);
|
||||
}
|
||||
|
||||
const host = new URL(url).hostname;
|
||||
return fallback[host](url) || fallback["*"](url);
|
||||
return fallback[host](dom) || fallback["*"](dom);
|
||||
}
|
||||
|
||||
interface Engines {
|
||||
|
@ -1,12 +1,8 @@
|
||||
import { Readability } from "@mozilla/readability";
|
||||
import axios from "../types/axios";
|
||||
import { JSDOM } from "jsdom";
|
||||
import { IHandlerOutput } from "./handler.interface";
|
||||
|
||||
export async function readability(url: string): Promise<IHandlerOutput> {
|
||||
const response = await axios.get(url);
|
||||
const dom = new JSDOM(response.data, { url: url });
|
||||
|
||||
export async function readability(dom: JSDOM): Promise<IHandlerOutput> {
|
||||
const reader = new Readability(dom.window.document);
|
||||
const parsed = reader.parse();
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user