mirror of
https://github.com/Andre-Bzk/AnyClip.git
synced 2026-11-15 09:33:08 +01:00
89 lines
2.0 KiB
JavaScript
89 lines
2.0 KiB
JavaScript
(function () {
|
|
const DEFAULT_API_BASE = "http://localhost:31009";
|
|
const API_VERSION = "2025-11-08";
|
|
|
|
function normalizeApiBase(apiBase) {
|
|
const value = (apiBase || DEFAULT_API_BASE).trim().replace(/\/+$/, "");
|
|
return value || DEFAULT_API_BASE;
|
|
}
|
|
|
|
async function anytypeRequest({ apiBase, apiKey, path, method = "GET", body }) {
|
|
if (!apiKey) {
|
|
throw new Error("Es fehlt ein Anytype API-Key.");
|
|
}
|
|
|
|
const response = await fetch(`${normalizeApiBase(apiBase)}${path}`, {
|
|
method,
|
|
headers: {
|
|
Authorization: `Bearer ${apiKey}`,
|
|
"Anytype-Version": API_VERSION,
|
|
"Content-Type": "application/json"
|
|
},
|
|
body: body ? JSON.stringify(body) : undefined
|
|
});
|
|
|
|
if (!response.ok) {
|
|
let details = "";
|
|
|
|
try {
|
|
const payload = await response.json();
|
|
details = payload.message || payload.error || JSON.stringify(payload);
|
|
} catch (error) {
|
|
details = await response.text();
|
|
}
|
|
|
|
throw new Error(`Anytype API Fehler (${response.status}): ${details || "Unbekannter Fehler"}`);
|
|
}
|
|
|
|
if (response.status === 204) {
|
|
return null;
|
|
}
|
|
|
|
return response.json();
|
|
}
|
|
|
|
async function listSpaces({ apiBase, apiKey }) {
|
|
const result = await anytypeRequest({
|
|
apiBase,
|
|
apiKey,
|
|
path: "/v1/spaces"
|
|
});
|
|
|
|
if (Array.isArray(result)) {
|
|
return result;
|
|
}
|
|
|
|
if (Array.isArray(result?.data)) {
|
|
return result.data;
|
|
}
|
|
|
|
if (Array.isArray(result?.spaces)) {
|
|
return result.spaces;
|
|
}
|
|
|
|
return [];
|
|
}
|
|
|
|
async function createObject({ apiBase, apiKey, spaceId, payload }) {
|
|
if (!spaceId) {
|
|
throw new Error("Es wurde kein Anytype-Space ausgewählt.");
|
|
}
|
|
|
|
return anytypeRequest({
|
|
apiBase,
|
|
apiKey,
|
|
path: `/v1/spaces/${encodeURIComponent(spaceId)}/objects`,
|
|
method: "POST",
|
|
body: payload
|
|
});
|
|
}
|
|
|
|
self.AnytypeApi = {
|
|
DEFAULT_API_BASE,
|
|
API_VERSION,
|
|
normalizeApiBase,
|
|
listSpaces,
|
|
createObject
|
|
};
|
|
})();
|