| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- const API_BASE = '/api/v1/admin'
- export const api = {
- async login(username, password) {
- const res = await fetch(`${API_BASE}/login`, {
- method: 'POST',
- headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify({ username, password }),
- })
- if (!res.ok) {
- const err = await res.json()
- throw new Error(err.detail || 'Login failed')
- }
- return res.json()
- },
- async uploadApp(file, onProgress) {
- const token = localStorage.getItem('auth_token')
- const formData = new FormData()
- formData.append('file', file)
- return new Promise((resolve, reject) => {
- const xhr = new XMLHttpRequest()
- xhr.open('POST', `${API_BASE}/apps/upload`)
- xhr.setRequestHeader('Authorization', `Bearer ${token}`)
- xhr.upload.onprogress = (e) => {
- if (e.lengthComputable && onProgress) {
- onProgress(Math.round((e.loaded / e.total) * 100))
- }
- }
- xhr.onload = () => {
- if (xhr.status >= 200 && xhr.status < 300) {
- resolve(JSON.parse(xhr.responseText))
- } else {
- try {
- const err = JSON.parse(xhr.responseText)
- reject(new Error(err.detail || 'Upload failed'))
- } catch {
- reject(new Error('Upload failed'))
- }
- }
- }
- xhr.onerror = () => reject(new Error('Network error'))
- xhr.send(formData)
- })
- },
- async getApps() {
- const res = await fetch('/api/v1/apps')
- if (!res.ok) throw new Error('Failed to fetch apps')
- return res.json()
- },
- }
|