2026-10-07 02:17:30 +04:00
|
|
|
import { get } from 'svelte/store'
|
|
|
|
|
import { jwt } from '../stores/auth.js'
|
|
|
|
|
|
|
|
|
|
const BASE = '/api'
|
|
|
|
|
|
|
|
|
|
async function request(method, path, body) {
|
|
|
|
|
const token = get(jwt)
|
|
|
|
|
const headers = { 'Content-Type': 'application/json' }
|
|
|
|
|
if (token) headers['Authorization'] = `Bearer ${token}`
|
|
|
|
|
|
|
|
|
|
const res = await fetch(BASE + path, {
|
|
|
|
|
method,
|
|
|
|
|
headers,
|
|
|
|
|
body: body !== undefined ? JSON.stringify(body) : undefined,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (!res.ok) {
|
2026-10-09 02:07:18 +04:00
|
|
|
const errCt = res.headers.get('content-type') ?? ''
|
|
|
|
|
if (errCt.includes('json')) {
|
|
|
|
|
const body = await res.json().catch(() => null)
|
|
|
|
|
const err = new Error(body?.detail ?? body?.title ?? `${res.status}`)
|
|
|
|
|
err.errorCode = body?.extensions?.errorCode ?? body?.errorCode ?? null
|
|
|
|
|
err.status = res.status
|
|
|
|
|
throw err
|
|
|
|
|
}
|
2026-10-07 02:17:30 +04:00
|
|
|
const text = await res.text()
|
|
|
|
|
throw new Error(`${res.status}: ${text}`)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const ct = res.headers.get('content-type') ?? ''
|
|
|
|
|
return ct.includes('application/json') ? res.json() : null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const api = {
|
|
|
|
|
// ── Auth ──────────────────────────────────────────────────────────────────
|
2026-10-11 02:57:29 +04:00
|
|
|
auth: (initData) => request('POST', '/auth/telegram', { initData }),
|
|
|
|
|
authInline: (userId, signedAt, sign) => request('POST', '/auth/inline', { userId: Number(userId), signedAt: Number(signedAt), sign }),
|
2026-10-07 02:17:30 +04:00
|
|
|
|
2026-10-09 02:07:18 +04:00
|
|
|
// ── Me ────────────────────────────────────────────────────────────────────
|
|
|
|
|
// GET /api/me → { telegramId, username, firstName, inboxCollectionId }
|
|
|
|
|
getMe: () => request('GET', '/me'),
|
|
|
|
|
// PATCH /api/me { inboxCollectionId } → 204
|
|
|
|
|
setInboxCollection: (collectionId) => request('PATCH', '/me', { inboxCollectionId: collectionId }),
|
|
|
|
|
|
2026-10-07 02:17:30 +04:00
|
|
|
// ── Media ─────────────────────────────────────────────────────────────────
|
2026-10-09 02:07:18 +04:00
|
|
|
createText: (caption, tags) => request('POST', '/media', { caption, tags }),
|
|
|
|
|
getMedia: (q, includeNsfw = false) => {
|
|
|
|
|
const params = new URLSearchParams({ includeNsfw })
|
|
|
|
|
if (q) params.set('q', q)
|
|
|
|
|
return request('GET', `/media?${params}`)
|
|
|
|
|
},
|
2026-10-07 02:17:30 +04:00
|
|
|
getDownloadUrl: (id) => request('GET', `/media/${id}/download-url`).then((r) => r?.url),
|
|
|
|
|
sendToChat: (id, queryId) => request('POST', `/media/${id}/send`, { queryId }),
|
|
|
|
|
updateMedia: (id, patch) => request('PATCH', `/media/${id}`, patch),
|
|
|
|
|
deleteMedia: (id) => request('DELETE', `/media/${id}`),
|
2026-10-09 02:07:18 +04:00
|
|
|
copyMedia: (id, targetCollectionId) => request('POST', `/media/${id}/copy`, { targetCollectionId }),
|
2026-10-07 02:17:30 +04:00
|
|
|
|
|
|
|
|
// ── Collections ───────────────────────────────────────────────────────────
|
|
|
|
|
getCollections: () => request('GET', '/collections'),
|
|
|
|
|
createCollection: (name, description) => request('POST', '/collections', { name, description }),
|
2026-10-09 02:07:18 +04:00
|
|
|
// patch: { name, description?, isNsfw?, visibility? } → { shareToken }
|
|
|
|
|
updateCollection: (id, patch) => request('PATCH', `/collections/${id}`, patch),
|
2026-10-07 02:17:30 +04:00
|
|
|
deleteCollection: (id) => request('DELETE', `/collections/${id}`),
|
|
|
|
|
getCollectionItems: (id) => request('GET', `/collections/${id}/items`),
|
2026-10-09 02:07:18 +04:00
|
|
|
cloneCollection: (id) => request('POST', `/collections/${id}/clone`),
|
|
|
|
|
joinCollection: (id) => request('POST', `/collections/${id}/join`),
|
|
|
|
|
regenerateShareLink: (id) => request('POST', `/collections/${id}/share-link/regenerate`),
|
2026-10-07 02:17:30 +04:00
|
|
|
|
|
|
|
|
// ── Invites ───────────────────────────────────────────────────────────────
|
|
|
|
|
getInvites: (collectionId) => request('GET', `/collections/${collectionId}/invites`),
|
2026-10-09 02:07:18 +04:00
|
|
|
createInvite: (collectionId, role, expiresAt) =>
|
|
|
|
|
request('POST', `/collections/${collectionId}/invites`, { role, expiresAt }),
|
|
|
|
|
revokeInvite: (collectionId, inviteId) =>
|
|
|
|
|
request('DELETE', `/collections/${collectionId}/invites/${inviteId}`),
|
2026-10-07 02:17:30 +04:00
|
|
|
}
|