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 { 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 {
|
||||||
|
@ -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();
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user