Refactor unfurling related types (#6730)

* wip

* fix: refactor unfurl types
This commit is contained in:
Apoorv Mishra
2024-04-03 07:28:30 +05:30
committed by GitHub
parent e0ae044f4c
commit 6a4628afef
19 changed files with 399 additions and 457 deletions

View File

@@ -1,15 +1,16 @@
import type { Unfurl } from "@shared/types";
import { InternalError } from "@server/errors";
import { JSONObject, UnfurlResourceType } from "@shared/types";
import Logger from "@server/logging/Logger";
import { UnfurlSignature } from "@server/types";
import fetch from "@server/utils/fetch";
import env from "./env";
class Iframely {
public static defaultUrl = "https://iframe.ly";
public static async fetch(
public static async requestResource(
url: string,
type = "oembed"
): Promise<Unfurl | void> {
): Promise<JSONObject | undefined> {
const isDefaultHost = env.IFRAMELY_URL === this.defaultUrl;
// Cloud Iframely requires /api path, while self-hosted does not.
@@ -23,19 +24,20 @@ class Iframely {
);
return res.json();
} catch (err) {
throw InternalError(err);
Logger.error(`Error fetching data from Iframely for url: ${url}`, err);
return;
}
}
/**
* Fetches the preview data for the given url using Iframely oEmbed API
*
* @param url
* @returns Preview data for the url
* @param url Resource url
* @returns An object containing resource details e.g, resource title, description etc.
*/
public static async unfurl(url: string): Promise<Unfurl | void> {
return Iframely.fetch(url);
}
public static unfurl: UnfurlSignature = async (url: string) => {
const data = await Iframely.requestResource(url);
return { ...data, type: UnfurlResourceType.OEmbed };
};
}
export default Iframely;