Files
2026-10-05 13:17:03 +02:00

64 lines
1.9 KiB
JavaScript

export const GITEA_ICON_URL = '/img/icons/gitea.png';
// The bundled mark has white-based transparent edges, so it is rendered on a
// white backdrop to avoid fringing.
export const GITEA_ICON_BACKGROUND = '#ffffff';
export const GITEA_ACCEPT = 'application/json';
// Normalizes the user-supplied instance URL to a bare origin. Gitea is always
// self-hosted, so there is no default instance; installs under a subpath are
// not supported (the origin is all we keep).
export function normalizeGiteaUrl(input) {
let value = (input ?? '').trim();
if (!value || /\s/.test(value)) {
return null;
}
if (!/^https?:\/\//i.test(value)) {
value = `https://${value}`;
}
try {
return new URL(value).origin;
} catch {
return null;
}
}
export function apiBaseUrl(instanceUrl) {
const origin = normalizeGiteaUrl(instanceUrl);
return origin ? `${origin}/api/v1` : null;
}
// Gitea exposes a dedicated unread count endpoint, so the badge costs a single
// small request instead of downloading the notification list.
export function notificationsCountUrl(instanceUrl) {
const base = apiBaseUrl(instanceUrl);
return base ? `${base}/notifications/new` : null;
}
// Where the user creates an access token for this instance.
export function applicationsUrl(instanceUrl) {
const origin = normalizeGiteaUrl(instanceUrl);
return origin ? `${origin}/user/settings/applications` : null;
}
// Gitea access tokens must be prefixed with the word "token".
export function authorizationHeader(token) {
return `token ${token}`;
}
export function unreadCountFromResponse(data) {
const count = data?.new;
return Number.isFinite(count) ? count : 0;
}
// Storage key for a connected instance, unique per host (including port) so
// several self-hosted Gitea instances can be connected side by side.
export function integrationKey(instanceUrl) {
const origin = normalizeGiteaUrl(instanceUrl);
return origin ? `gitea-${new URL(origin).host}` : null;
}