refactor: use link preview from nostr-services api
This commit is contained in:
@ -5,11 +5,11 @@ import { LRUCache } from "typescript-lru-cache";
|
|||||||
|
|
||||||
import { MediaElement } from "@/Components/Embed/MediaElement";
|
import { MediaElement } from "@/Components/Embed/MediaElement";
|
||||||
import Spinner from "@/Components/Icons/Spinner";
|
import Spinner from "@/Components/Icons/Spinner";
|
||||||
import SnortApi, { LinkPreviewData } from "@/External/SnortApi";
|
import { LinkPreviewData, NostrServices } from "@/External/NostrServices";
|
||||||
import useImgProxy from "@/Hooks/useImgProxy";
|
import useImgProxy from "@/Hooks/useImgProxy";
|
||||||
|
|
||||||
async function fetchUrlPreviewInfo(url: string) {
|
async function fetchUrlPreviewInfo(url: string) {
|
||||||
const api = new SnortApi();
|
const api = new NostrServices("https://nostr.api.v0l.io");
|
||||||
try {
|
try {
|
||||||
return await api.linkPreview(url.endsWith(")") ? url.slice(0, -1) : url);
|
return await api.linkPreview(url.endsWith(")") ? url.slice(0, -1) : url);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
19
packages/app/src/External/NostrServices.ts
vendored
Normal file
19
packages/app/src/External/NostrServices.ts
vendored
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
import { JsonApi } from ".";
|
||||||
|
|
||||||
|
export interface LinkPreviewData {
|
||||||
|
title?: string;
|
||||||
|
description?: string;
|
||||||
|
image?: string;
|
||||||
|
og_tags?: Array<[name: string, value: string]>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class NostrServices extends JsonApi {
|
||||||
|
constructor(readonly url: string) {
|
||||||
|
super();
|
||||||
|
url = url.endsWith("/") ? url.slice(0, -1) : url;
|
||||||
|
}
|
||||||
|
|
||||||
|
linkPreview(url: string) {
|
||||||
|
return this.getJson<LinkPreviewData>(`/api/v1/preview?url=${encodeURIComponent(url)}`);
|
||||||
|
}
|
||||||
|
}
|
11
packages/app/src/External/SnortApi.ts
vendored
11
packages/app/src/External/SnortApi.ts
vendored
@ -43,13 +43,6 @@ export class SubscriptionError extends Error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface LinkPreviewData {
|
|
||||||
title?: string;
|
|
||||||
description?: string;
|
|
||||||
image?: string;
|
|
||||||
og_tags?: Array<[name: string, value: string]>;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface PushNotifications {
|
export interface PushNotifications {
|
||||||
endpoint: string;
|
endpoint: string;
|
||||||
p256dh: string;
|
p256dh: string;
|
||||||
@ -119,10 +112,6 @@ export default class SnortApi {
|
|||||||
return this.#getJsonAuthd<Array<Subscription>>("api/v1/subscription");
|
return this.#getJsonAuthd<Array<Subscription>>("api/v1/subscription");
|
||||||
}
|
}
|
||||||
|
|
||||||
linkPreview(url: string) {
|
|
||||||
return this.#getJson<LinkPreviewData>(`api/v1/preview?url=${encodeURIComponent(url)}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
onChainDonation() {
|
onChainDonation() {
|
||||||
return this.#getJson<{ address: string }>("p/on-chain");
|
return this.#getJson<{ address: string }>("p/on-chain");
|
||||||
}
|
}
|
||||||
|
38
packages/app/src/External/index.ts
vendored
38
packages/app/src/External/index.ts
vendored
@ -1,2 +1,40 @@
|
|||||||
|
import { throwIfOffline, unwrap } from "@snort/shared";
|
||||||
|
|
||||||
export * from "./NostrBand";
|
export * from "./NostrBand";
|
||||||
export * from "./SemisolDev";
|
export * from "./SemisolDev";
|
||||||
|
|
||||||
|
export abstract class JsonApi {
|
||||||
|
abstract url: string;
|
||||||
|
protected async getJson<T>(
|
||||||
|
path: string,
|
||||||
|
method?: "GET" | string,
|
||||||
|
body?: object,
|
||||||
|
headers?: { [key: string]: string },
|
||||||
|
): Promise<T> {
|
||||||
|
throwIfOffline();
|
||||||
|
const rsp = await fetch(`${this.url}${path}`, {
|
||||||
|
method: method,
|
||||||
|
body: body ? JSON.stringify(body) : undefined,
|
||||||
|
headers: {
|
||||||
|
accept: "application/json",
|
||||||
|
...(body ? { "content-type": "application/json" } : {}),
|
||||||
|
...headers,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (rsp.ok) {
|
||||||
|
const text = (await rsp.text()) as string | null;
|
||||||
|
if ((text?.length ?? 0) > 0) {
|
||||||
|
const obj = JSON.parse(unwrap(text));
|
||||||
|
if ("error" in obj) {
|
||||||
|
throw new Error(obj.error, obj.code);
|
||||||
|
}
|
||||||
|
return obj as T;
|
||||||
|
} else {
|
||||||
|
return {} as T;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw new Error("Invalid response");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user