Files
TelegramMemeVault/miniapp/src/lib/api.js
T
CrazyHackGUT 8169b6474a
CI / CD / Unit tests (push) Successful in 40s
CI / CD / Build & push Helm chart (push) Successful in 11s
CI / CD / Build & push frontend-static (push) Successful in 57s
CI / CD / Build & push frontend (nginx) (push) Successful in 1m2s
CI / CD / Build & push backend (push) Successful in 2m37s
feat: ещё один вариант аутентификации для inline
2026-10-11 02:57:29 +04:00

76 lines
4.2 KiB
JavaScript

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) {
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
}
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 ──────────────────────────────────────────────────────────────────
auth: (initData) => request('POST', '/auth/telegram', { initData }),
authInline: (userId, signedAt, sign) => request('POST', '/auth/inline', { userId: Number(userId), signedAt: Number(signedAt), sign }),
// ── Me ────────────────────────────────────────────────────────────────────
// GET /api/me → { telegramId, username, firstName, inboxCollectionId }
getMe: () => request('GET', '/me'),
// PATCH /api/me { inboxCollectionId } → 204
setInboxCollection: (collectionId) => request('PATCH', '/me', { inboxCollectionId: collectionId }),
// ── Media ─────────────────────────────────────────────────────────────────
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}`)
},
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}`),
copyMedia: (id, targetCollectionId) => request('POST', `/media/${id}/copy`, { targetCollectionId }),
// ── Collections ───────────────────────────────────────────────────────────
getCollections: () => request('GET', '/collections'),
createCollection: (name, description) => request('POST', '/collections', { name, description }),
// patch: { name, description?, isNsfw?, visibility? } → { shareToken }
updateCollection: (id, patch) => request('PATCH', `/collections/${id}`, patch),
deleteCollection: (id) => request('DELETE', `/collections/${id}`),
getCollectionItems: (id) => request('GET', `/collections/${id}/items`),
cloneCollection: (id) => request('POST', `/collections/${id}/clone`),
joinCollection: (id) => request('POST', `/collections/${id}/join`),
regenerateShareLink: (id) => request('POST', `/collections/${id}/share-link/regenerate`),
// ── Invites ───────────────────────────────────────────────────────────────
getInvites: (collectionId) => request('GET', `/collections/${collectionId}/invites`),
createInvite: (collectionId, role, expiresAt) =>
request('POST', `/collections/${collectionId}/invites`, { role, expiresAt }),
revokeInvite: (collectionId, inviteId) =>
request('DELETE', `/collections/${collectionId}/invites/${inviteId}`),
}