Hey is a decentralized and permissionless social media app built with Lens Protocol 馃尶
1import { parseHTML } from "linkedom";
2import { HEY_USER_AGENT } from "@/utils/constants";
3import getDescription from "./meta/getDescription";
4import getTitle from "./meta/getTitle";
5
6const fetchData = async (url: string) => {
7 const response = await fetch(url, {
8 headers: { "User-Agent": HEY_USER_AGENT }
9 });
10
11 if (!response.ok) {
12 throw new Error(`HTTP error! Status: ${response.status}`);
13 }
14
15 return await response.text();
16};
17
18const extractMetadata = (document: Document, url: string) => {
19 return {
20 description: getDescription(document),
21 title: getTitle(document),
22 url
23 };
24};
25
26const getMetadata = async (url: string) => {
27 try {
28 const data = await fetchData(url);
29 const { document } = parseHTML(data);
30 return extractMetadata(document, url);
31 } catch {
32 return { description: null, title: null, url };
33 }
34};
35
36export default getMetadata;