aboutsummaryrefslogtreecommitdiff
path: root/apps/cf-ai-backend/src/queueConsumer/helpers/processPage.ts
blob: 9a50d7019c1db722341ac5d08e3e73ed78d44e27 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { Result, Ok, Err, isErr } from "../../errors/results";
import { BaseError } from "../../errors/baseError";
import { getMetaData, Metadata } from "../utils/get-metadata";

class ProcessPageError extends BaseError {
	constructor(message?: string, source?: string) {
		super("[Page Proceessing Error]", message, source);
	}
}

type PageProcessResult = { pageContent: string; metadata: Metadata };

export async function processPage(input: {
	url: string;
	securityKey: string;
}): Promise<Result<PageProcessResult, ProcessPageError>> {
	try {
		const response = await fetch("https://md.dhr.wtf/?url=" + input.url, {
			headers: {
				Authorization: "Bearer " + input.securityKey,
			},
		});
		const pageContent = await response.text();
		if (!pageContent) {
			return Err(
				new ProcessPageError(
					"Failed to get response form markdowner",
					"processPage",
				),
			);
		}
		const metadataResult = await getMetaData(input.url);
		if (isErr(metadataResult)) {
			throw metadataResult.error;
		}
		const metadata = metadataResult.value;
		console.log("[this is the metadata]", metadata);
		return Ok({ pageContent, metadata });
	} catch (e) {
		console.error("[Page Processing Error]", e);
		return Err(new ProcessPageError((e as Error).message, "processPage"));
	}
}