Files
MangaDL/frontend/src/api/client.ts
T
2026-09-13 12:59:16 +02:00

44 lines
1.4 KiB
TypeScript

import axios from 'axios';
import type { Manga, Chapter, SearchResult, ChapterPages } from '../types';
const API_BASE = import.meta.env.VITE_API_BASE;
const baseURL = API_BASE && API_BASE !== '' ? API_BASE : (typeof window !== 'undefined' ? window.location.origin : '');
const api = axios.create({
baseURL,
timeout: 30000,
});
export const searchManga = async (query: string): Promise<SearchResult[]> => {
const response = await api.get('/api/search', { params: { q: query } });
return response.data;
};
export const getMangaDetails = async (id: string): Promise<Manga> => {
const response = await api.get(`/api/manga/${id}`);
return response.data;
};
export const getMangaChapters = async (id: string): Promise<Chapter[]> => {
const response = await api.get(`/api/manga/${id}/chapters`);
return response.data;
};
export const getChapterPages = async (chapterId: string): Promise<ChapterPages> => {
const response = await api.get(`/api/chapter/${chapterId}/pages`);
return response.data;
};
export const getImageUrl = (chapterId: string, filename: string): string => {
const base = API_BASE && API_BASE !== '' ? API_BASE : (typeof window !== 'undefined' ? window.location.origin : '');
return `${base}/api/image/${chapterId}/${filename}`;
};
export const checkServerHealth = async (): Promise<boolean> => {
try {
await api.get('/api/search', { params: { q: 'test' }, timeout: 5000 });
return true;
} catch {
return false;
}
};