web/src/lib/api.ts (view raw)
1const BASE = '/api';
2
3export interface User {
4 id: number;
5 username: string;
6 is_admin: boolean;
7 created_at: string;
8 updated_at: string;
9}
10
11export interface Image {
12 id: number;
13 filename: string;
14 storage_path: string;
15 thumbnail_path: string | null;
16 uploaded_by: number;
17 uploader_username: string;
18 avatar_url: string;
19 file_size: number;
20 width: number;
21 height: number;
22 mime_type: string;
23 created_at: string;
24 updated_at: string;
25 tags: string[];
26}
27
28export interface ImageList {
29 images: Image[];
30 total: number;
31}
32
33export interface Tag {
34 tag: string;
35 usage_count: number;
36}
37
38export interface TagDetail {
39 tag: string;
40 usage_count: number;
41}
42
43export interface ImageDetail extends Image {
44 tags_detail: TagDetail[];
45}
46
47export interface ApiError {
48 error: string;
49}
50
51async function request<T>(path: string, opts: RequestInit = {}): Promise<T> {
52 const res = await fetch(BASE + path, {
53 credentials: 'include',
54 headers: opts.body instanceof FormData ? {} : { 'Content-Type': 'application/json' },
55 ...opts,
56 });
57 if (!res.ok) {
58 const body = await res.json().catch(() => ({ error: res.statusText }));
59 throw new Error((body as ApiError).error || `HTTP ${res.status}`);
60 }
61 return res.json();
62}
63
64export async function login(username: string, password: string): Promise<User> {
65 return request<User>('/auth/login', {
66 method: 'POST',
67 body: JSON.stringify({ username, password }),
68 });
69}
70
71export async function register(username: string, password: string): Promise<User> {
72 return request<User>('/auth/register', {
73 method: 'POST',
74 body: JSON.stringify({ username, password }),
75 });
76}
77
78export async function logout(): Promise<void> {
79 await request('/auth/logout', { method: 'POST' });
80}
81
82export async function listImages(tags: string[] = [], page = 1, perPage = 50): Promise<ImageList> {
83 const params = new URLSearchParams();
84 params.set('page', String(page));
85 params.set('per_page', String(perPage));
86 for (const t of tags) {
87 params.append('tags', t);
88 }
89 return request<ImageList>(`/images?${params}`);
90}
91
92export async function getImage(id: number): Promise<ImageDetail> {
93 return request<ImageDetail>(`/images/${id}`);
94}
95
96export function imageDownloadUrl(id: number): string {
97 return `${BASE}/images/${id}/download`;
98}
99
100export function thumbnailUrl(id: number): string {
101 return `${BASE}/images/${id}/thumbnail`;
102}
103
104export async function uploadImage(file: File, tags: string[]): Promise<Image> {
105 const fd = new FormData();
106 fd.append('file', file);
107 for (const t of tags) {
108 fd.append('tags', t);
109 }
110 return request<Image>('/upload', { method: 'POST', body: fd });
111}
112
113export async function suggestTags(query: string): Promise<Tag[]> {
114 return request<Tag[]>(`/tags/suggest?q=${encodeURIComponent(query)}`);
115}
116
117export async function uploadAvatar(file: File): Promise<{ avatar_url: string }> {
118 const fd = new FormData();
119 fd.append('avatar', file);
120 return request<{ avatar_url: string }>('/users/me/avatar', { method: 'POST', body: fd });
121}