Import source: cshare-app/src/lib/api
Import the existing local project snapshot. This commit organizes existing files; it does not represent newly developed functionality.
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
const API_BASE = (
|
||||
import.meta.env.VITE_API_BASE_URL ||
|
||||
(import.meta.env.DEV ? 'http://localhost:4001' : window.location.origin)
|
||||
).replace(/\/$/, '');
|
||||
|
||||
async function request(path, options = {}) {
|
||||
const response = await fetch(`${API_BASE}${path}`, {
|
||||
credentials: 'include',
|
||||
headers: {
|
||||
...(options.body instanceof FormData ? {} : { 'Content-Type': 'application/json' }),
|
||||
...options.headers,
|
||||
},
|
||||
...options,
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const payload = await response.json().catch(() => ({}));
|
||||
throw new Error(payload.error || `Request failed with ${response.status}`);
|
||||
}
|
||||
|
||||
if (response.status === 204) return null;
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export function uploadFile(file, { onProgress } = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const xhr = new XMLHttpRequest();
|
||||
const form = new FormData();
|
||||
form.append('file', file);
|
||||
|
||||
xhr.open('POST', `${API_BASE}/api/v1/files/upload`);
|
||||
xhr.withCredentials = true;
|
||||
|
||||
xhr.upload.onprogress = (event) => {
|
||||
if (event.lengthComputable && onProgress) {
|
||||
onProgress(Math.round((event.loaded / event.total) * 100));
|
||||
}
|
||||
};
|
||||
|
||||
xhr.onload = () => {
|
||||
if (xhr.status >= 200 && xhr.status < 300) {
|
||||
resolve(JSON.parse(xhr.responseText));
|
||||
return;
|
||||
}
|
||||
try {
|
||||
reject(new Error(JSON.parse(xhr.responseText).error || 'Upload failed'));
|
||||
} catch {
|
||||
reject(new Error('Upload failed'));
|
||||
}
|
||||
};
|
||||
|
||||
xhr.onerror = () => reject(new Error('Could not reach upload service'));
|
||||
xhr.send(form);
|
||||
});
|
||||
}
|
||||
|
||||
export async function listFiles() {
|
||||
const payload = await request('/api/v1/files');
|
||||
return payload.data;
|
||||
}
|
||||
|
||||
export async function deleteFile(id) {
|
||||
await request(`/api/v1/files/${id}`, { method: 'DELETE' });
|
||||
}
|
||||
|
||||
export async function getDashboardStats() {
|
||||
const payload = await request('/api/v1/dashboard/stats');
|
||||
return payload.data;
|
||||
}
|
||||
|
||||
export async function getDashboardActivity() {
|
||||
const payload = await request('/api/v1/dashboard/activity');
|
||||
return payload.data;
|
||||
}
|
||||
|
||||
export async function getDashboardStorage() {
|
||||
const payload = await request('/api/v1/dashboard/storage');
|
||||
return payload.data;
|
||||
}
|
||||
|
||||
export async function getFileAnalytics(fileId) {
|
||||
const payload = await request(`/api/v1/files/${fileId}/analytics`);
|
||||
return payload.data;
|
||||
}
|
||||
|
||||
export async function resolveLink(linkId) {
|
||||
const payload = await request(`/api/v1/links/${linkId}`);
|
||||
return payload.data;
|
||||
}
|
||||
|
||||
export function downloadUrl(linkId) {
|
||||
return `${API_BASE}/api/v1/links/${linkId}/download`;
|
||||
}
|
||||
|
||||
export function previewUrl(linkId) {
|
||||
return `${API_BASE}/api/v1/links/${linkId}/preview`;
|
||||
}
|
||||
Reference in New Issue
Block a user