refactor: move request to handlePage

This commit is contained in:
Artemy 2023-08-14 16:54:05 +03:00
parent 3674ee269f
commit 27d0db2f6f
2 changed files with 10 additions and 9 deletions

View File

@ -1,9 +1,11 @@
import axios from "../types/axios";
import { IHandlerOutput } from "./handler.interface"; import { IHandlerOutput } from "./handler.interface";
import { readability } from "./readability"; 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, url: string,
engine?: string engine?: string
): Promise<IHandlerOutput> { ): Promise<IHandlerOutput> {
@ -11,12 +13,15 @@ export default function handlePage(
throw new Error("Invalid engine"); throw new Error("Invalid engine");
} }
const response = await axios.get(url);
const dom = new JSDOM(response.data, { url: url });
if (engine) { if (engine) {
return engines[engine](url); return engines[engine](dom);
} }
const host = new URL(url).hostname; const host = new URL(url).hostname;
return fallback[host](url) || fallback["*"](url); return fallback[host](dom) || fallback["*"](dom);
} }
interface Engines { interface Engines {

View File

@ -1,12 +1,8 @@
import { Readability } from "@mozilla/readability"; import { Readability } from "@mozilla/readability";
import axios from "../types/axios";
import { JSDOM } from "jsdom"; import { JSDOM } from "jsdom";
import { IHandlerOutput } from "./handler.interface"; import { IHandlerOutput } from "./handler.interface";
export async function readability(url: string): Promise<IHandlerOutput> { export async function readability(dom: JSDOM): Promise<IHandlerOutput> {
const response = await axios.get(url);
const dom = new JSDOM(response.data, { url: url });
const reader = new Readability(dom.window.document); const reader = new Readability(dom.window.document);
const parsed = reader.parse(); const parsed = reader.parse();