From 27d0db2f6fa85df8c103c1af783f8dd94e9f8469 Mon Sep 17 00:00:00 2001 From: Artemy Date: Mon, 14 Aug 2023 16:54:05 +0300 Subject: [PATCH] refactor: move request to handlePage --- src/handlers/main.ts | 13 +++++++++---- src/handlers/readability.ts | 6 +----- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/handlers/main.ts b/src/handlers/main.ts index 43d2183..277c6a2 100644 --- a/src/handlers/main.ts +++ b/src/handlers/main.ts @@ -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; +type EngineFunction = (url: JSDOM) => Promise; -export default function handlePage( +export default async function handlePage( url: string, engine?: string ): Promise { @@ -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 { diff --git a/src/handlers/readability.ts b/src/handlers/readability.ts index dc66edb..42d4b6c 100644 --- a/src/handlers/readability.ts +++ b/src/handlers/readability.ts @@ -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 { - const response = await axios.get(url); - const dom = new JSDOM(response.data, { url: url }); - +export async function readability(dom: JSDOM): Promise { const reader = new Readability(dom.window.document); const parsed = reader.parse();