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 CurrentUser {
48 id: number;
49 username: string;
50 is_admin: boolean;
51 avatar_url: string;
52 created_at: string;
53 updated_at: string;
54}
55
56export interface ApiError {
57 error: string;
58}
59
60async function request<T>(path: string, opts: RequestInit = {}): Promise<T> {
61 const res = await fetch(BASE + path, {
62 credentials: 'include',
63 headers: opts.body instanceof FormData ? {} : { 'Content-Type': 'application/json' },
64 ...opts,
65 });
66 if (!res.ok) {
67 const body = await res.json().catch(() => ({ error: res.statusText }));
68 throw new Error((body as ApiError).error || `HTTP ${res.status}`);
69 }
70 return res.json();
71}
72
73export async function login(username: string, password: string): Promise<User> {
74 return request<User>('/auth/login', {
75 method: 'POST',
76 body: JSON.stringify({ username, password }),
77 });
78}
79
80export async function register(username: string, password: string): Promise<User> {
81 return request<User>('/auth/register', {
82 method: 'POST',
83 body: JSON.stringify({ username, password }),
84 });
85}
86
87export async function logout(): Promise<void> {
88 await request('/auth/logout', { method: 'POST' });
89}
90
91export async function listImages(tags: string[] = [], page = 1, perPage = 50): Promise<ImageList> {
92 const params = new URLSearchParams();
93 params.set('page', String(page));
94 params.set('per_page', String(perPage));
95 for (const t of tags) {
96 params.append('tags', t);
97 }
98 return request<ImageList>(`/images?${params}`);
99}
100
101export async function getImage(id: number): Promise<ImageDetail> {
102 return request<ImageDetail>(`/images/${id}`);
103}
104
105export function imageDownloadUrl(id: number): string {
106 return `${BASE}/images/${id}/download`;
107}
108
109export function thumbnailUrl(id: number): string {
110 return `${BASE}/images/${id}/thumbnail`;
111}
112
113export async function uploadImage(file: File, tags: string[]): Promise<Image> {
114 const fd = new FormData();
115 fd.append('file', file);
116 for (const t of tags) {
117 fd.append('tags', t);
118 }
119 return request<Image>('/upload', { method: 'POST', body: fd });
120}
121
122export async function suggestTags(query: string): Promise<Tag[]> {
123 return request<Tag[]>(`/tags/suggest?q=${encodeURIComponent(query)}`);
124}
125
126export async function uploadAvatar(file: File): Promise<{ avatar_url: string }> {
127 const fd = new FormData();
128 fd.append('avatar', file);
129 return request<{ avatar_url: string }>('/users/me/avatar', { method: 'POST', body: fd });
130}
131
132export async function getMe(): Promise<CurrentUser> {
133 return request<CurrentUser>('/users/me');
134}