diff --git a/src/app.ts b/src/app.ts index 75737bd..be56c4b 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,5 +1,6 @@ import { IConfigService } from "./config/config.interface"; import { ConfigService } from "./config/config.service"; + import NodeCache from "node-cache"; import Fastify from "fastify"; import middie from "@fastify/middie"; @@ -9,10 +10,12 @@ import parseRoute from "./routes/parseRoute"; class App { config: IConfigService; cache: NodeCache; + constructor() { this.config = new ConfigService(); this.cache = new NodeCache({ stdTTL: 100, checkperiod: 120 }); } + async init() { const fastify = Fastify({ logger: true, @@ -22,9 +25,8 @@ class App { fastify.use((req, res, next) => { const url = req.originalUrl || req.url || "/"; - const purge = req.query.purge ? true : false; - if (purge) { + if (req.query.purge) { this.cache.del(url); next(); } diff --git a/src/config/config.service.ts b/src/config/config.service.ts index a96f195..cb775c9 100644 --- a/src/config/config.service.ts +++ b/src/config/config.service.ts @@ -3,8 +3,10 @@ import { IConfigService } from "./config.interface"; export class ConfigService implements IConfigService { private config: DotenvParseOutput; + constructor() { const { error, parsed } = config(); + if (error) { throw new Error(".env file not found"); } @@ -18,6 +20,7 @@ export class ConfigService implements IConfigService { get(key: string): string { const res = this.config[key]; + if (!res) { throw new Error(`Key ${key} not found`); } diff --git a/src/handlers/main.ts b/src/handlers/main.ts index 1204102..d0923ee 100644 --- a/src/handlers/main.ts +++ b/src/handlers/main.ts @@ -1,11 +1,8 @@ import { IHandlerOutput } from "./handler.interface"; import { readability } from "./readability"; -export default function getCorrespondingReaderView( - url: string -): Promise { +export default function handlePage(url: string): Promise { const host = new URL(url).hostname; - return fallback[host]?.(url) || fallback["*"](url); } diff --git a/src/handlers/readability.ts b/src/handlers/readability.ts index 0a6f39a..cafc43a 100644 --- a/src/handlers/readability.ts +++ b/src/handlers/readability.ts @@ -6,6 +6,7 @@ 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 }); + const reader = new Readability(dom.window.document); const parsed = reader.parse();