refactor(api): ♻️ Use URL literal instead of strings

This commit is contained in:
Jesse Wierzbinski 2025-02-01 16:32:18 +01:00
parent 99fac323c8
commit 76d1ccc859
No known key found for this signature in database
50 changed files with 343 additions and 256 deletions

View file

@ -32,7 +32,7 @@ export const applyToHono = (app: OpenAPIHono<HonoEnv>): void => {
},
boardLogo: {
path:
config.instance.logo ??
config.instance.logo?.toString() ??
"https://cdn.versia.pub/branding/icon.svg",
height: 40,
},

View file

@ -1,4 +1,4 @@
import { config } from "~/packages/config-manager/index.ts";
export const localObjectUri = (id: string): string =>
new URL(`/objects/${id}`, config.http.base_url).toString();
export const localObjectUri = (id: string): URL =>
new URL(`/objects/${id}`, config.http.base_url);

View file

@ -30,28 +30,25 @@ export const getBestContentType = (
};
export const urlToContentFormat = (
url: string,
url: URL,
contentType?: string,
): ContentFormat | null => {
if (!url) {
return null;
}
if (url.startsWith("https://api.dicebear.com/")) {
if (url.href.startsWith("https://api.dicebear.com/")) {
return {
"image/svg+xml": {
content: url,
content: url.toString(),
remote: true,
},
};
}
const mimeType =
contentType ||
lookup(url.replace(new URL(url).search, "")) ||
lookup(url.toString().replace(url.search, "")) ||
"application/octet-stream";
return {
[mimeType]: {
content: url,
content: url.toString(),
remote: true,
},
};

View file

@ -9,12 +9,9 @@ export type Json =
| Json[]
| { [key: string]: Json };
export const proxyUrl = (url: string | null = null): string | null => {
const urlAsBase64Url = Buffer.from(url || "").toString("base64url");
return url
? new URL(
`/media/proxy/${urlAsBase64Url}`,
config.http.base_url,
).toString()
: url;
export const proxyUrl = (url: URL): URL => {
const urlAsBase64Url = Buffer.from(url.toString() || "").toString(
"base64url",
);
return new URL(`/media/proxy/${urlAsBase64Url}`, config.http.base_url);
};

View file

@ -136,7 +136,11 @@ export const sanitizeHtml = async (
element(element): void {
element.setAttribute(
"src",
proxyUrl(element.getAttribute("src") ?? "") ?? "",
element.getAttribute("src")
? proxyUrl(
new URL(element.getAttribute("src") as string),
).toString()
: "",
);
},
})