Remove Act, AutoUpdater, and Book IPC modules alongside associated database logic.
This commit is contained in:
@@ -1,104 +1,93 @@
|
||||
import axios, {AxiosResponse, Method} from "axios";
|
||||
import {configs, isDesktop} from "@/lib/configs";
|
||||
|
||||
type ContentType = 'application/json' | 'multipart/form-data';
|
||||
|
||||
interface ApiRequestConfig {
|
||||
method: Method;
|
||||
url: string;
|
||||
auth: string;
|
||||
lang?: string;
|
||||
params?: Record<string, unknown>;
|
||||
data?: unknown;
|
||||
contentType?: ContentType;
|
||||
}
|
||||
|
||||
export class ApiError extends Error {
|
||||
statusCode: number;
|
||||
constructor(message: string, statusCode: number) {
|
||||
super(message);
|
||||
this.statusCode = statusCode;
|
||||
this.name = 'ApiError';
|
||||
}
|
||||
}
|
||||
|
||||
function handleApiError(error: unknown): never {
|
||||
if (axios.isAxiosError(error)) {
|
||||
const serverMessage: string = error.response?.data?.message || error.response?.data || error.message;
|
||||
const statusCode: number = error.response?.status ?? 500;
|
||||
throw new ApiError(serverMessage, statusCode);
|
||||
} else if (error instanceof Error) {
|
||||
throw new Error(error.message);
|
||||
}
|
||||
throw new Error('An unexpected error occurred');
|
||||
}
|
||||
|
||||
async function apiRequest<T>(config: ApiRequestConfig): Promise<T> {
|
||||
try {
|
||||
const headers: Record<string, string> = {
|
||||
'Authorization': `Bearer ${config.auth}`
|
||||
};
|
||||
|
||||
if (config.contentType) {
|
||||
headers['Content-Type'] = config.contentType;
|
||||
}
|
||||
|
||||
const response: AxiosResponse<T> = await axios({
|
||||
method: config.method,
|
||||
headers,
|
||||
params: {
|
||||
lang: config.lang ?? 'fr',
|
||||
plateforme: isDesktop ? 'desktop' : 'web',
|
||||
...config.params
|
||||
},
|
||||
url: configs.apiUrl + config.url,
|
||||
data: config.data
|
||||
});
|
||||
|
||||
return response.data;
|
||||
} catch (error: unknown) {
|
||||
handleApiError(error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function apiGet<T>(url: string, auth: string, lang: string = "fr", params: Record<string, unknown> = {}): Promise<T> {
|
||||
return apiRequest<T>({method: 'GET', url, auth, lang, params});
|
||||
}
|
||||
|
||||
export async function apiPost<T>(url: string, data: object, auth: string, lang: string = "fr"): Promise<T> {
|
||||
return apiRequest<T>({method: 'POST', url, auth, lang, data, contentType: 'application/json'});
|
||||
}
|
||||
|
||||
export async function apiPut<T>(url: string, data: object, auth: string, lang: string = "fr"): Promise<T> {
|
||||
return apiRequest<T>({method: 'PUT', url, auth, lang, data, contentType: 'application/json'});
|
||||
}
|
||||
|
||||
export async function apiPatch<T>(url: string, data: object, auth: string, lang: string = "fr"): Promise<T> {
|
||||
return apiRequest<T>({method: 'PATCH', url, auth, lang, data, contentType: 'application/json'});
|
||||
}
|
||||
|
||||
export async function apiDelete<T>(url: string, data: object, auth: string, lang: string = "fr"): Promise<T> {
|
||||
return apiRequest<T>({method: 'DELETE', url, auth, lang, data, contentType: 'application/json'});
|
||||
}
|
||||
|
||||
export async function apiPostPublic<T>(url: string, data: object, lang: string = "fr"): Promise<T> {
|
||||
try {
|
||||
const response: AxiosResponse<T> = await axios({
|
||||
method: 'POST',
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
params: {lang, plateforme: isDesktop ? 'desktop' : 'web'},
|
||||
url: configs.apiUrl + url,
|
||||
data
|
||||
});
|
||||
return response.data;
|
||||
} catch (error: unknown) {
|
||||
handleApiError(error);
|
||||
}
|
||||
}
|
||||
|
||||
export async function apiUpload<T>(url: string, file: File, auth: string, lang: string = "fr"): Promise<T> {
|
||||
const formData: FormData = new FormData();
|
||||
formData.append('file', file);
|
||||
|
||||
return apiRequest<T>({method: 'POST', url, auth, lang, data: formData, contentType: 'multipart/form-data'});
|
||||
}
|
||||
import {fetch} from "@tauri-apps/plugin-http";
|
||||
import {configs} from "@/lib/configs";
|
||||
|
||||
export class ApiError extends Error {
|
||||
statusCode: number;
|
||||
constructor(message: string, statusCode: number) {
|
||||
super(message);
|
||||
this.statusCode = statusCode;
|
||||
this.name = 'ApiError';
|
||||
}
|
||||
}
|
||||
|
||||
function buildUrl(url: string, params: Record<string, unknown> = {}, lang: string = "fr"): string {
|
||||
const fullUrl = new URL(url, configs.apiUrl);
|
||||
fullUrl.searchParams.set("lang", lang);
|
||||
fullUrl.searchParams.set("plateforme", "desktop");
|
||||
for (const [key, value] of Object.entries(params)) {
|
||||
if (value !== undefined && value !== null) fullUrl.searchParams.set(key, String(value));
|
||||
}
|
||||
return fullUrl.toString();
|
||||
}
|
||||
|
||||
async function handleResponse<T>(response: Response): Promise<T> {
|
||||
if (!response.ok) {
|
||||
const body = await response.json().catch(() => ({message: response.statusText}));
|
||||
throw new ApiError(body.message || body || response.statusText, response.status);
|
||||
}
|
||||
return response.json() as Promise<T>;
|
||||
}
|
||||
|
||||
export async function apiGet<T>(url: string, auth: string, lang: string = "fr", params: Record<string, unknown> = {}): Promise<T> {
|
||||
const response = await fetch(buildUrl(url, params, lang), {
|
||||
method: "GET",
|
||||
headers: {"Authorization": `Bearer ${auth}`},
|
||||
});
|
||||
return handleResponse<T>(response);
|
||||
}
|
||||
|
||||
export async function apiPost<T>(url: string, data: object, auth: string, lang: string = "fr"): Promise<T> {
|
||||
const response = await fetch(buildUrl(url, {}, lang), {
|
||||
method: "POST",
|
||||
headers: {"Authorization": `Bearer ${auth}`, "Content-Type": "application/json"},
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
return handleResponse<T>(response);
|
||||
}
|
||||
|
||||
export async function apiPut<T>(url: string, data: object, auth: string, lang: string = "fr"): Promise<T> {
|
||||
const response = await fetch(buildUrl(url, {}, lang), {
|
||||
method: "PUT",
|
||||
headers: {"Authorization": `Bearer ${auth}`, "Content-Type": "application/json"},
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
return handleResponse<T>(response);
|
||||
}
|
||||
|
||||
export async function apiPatch<T>(url: string, data: object, auth: string, lang: string = "fr"): Promise<T> {
|
||||
const response = await fetch(buildUrl(url, {}, lang), {
|
||||
method: "PATCH",
|
||||
headers: {"Authorization": `Bearer ${auth}`, "Content-Type": "application/json"},
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
return handleResponse<T>(response);
|
||||
}
|
||||
|
||||
export async function apiDelete<T>(url: string, data: object, auth: string, lang: string = "fr"): Promise<T> {
|
||||
const response = await fetch(buildUrl(url, {}, lang), {
|
||||
method: "DELETE",
|
||||
headers: {"Authorization": `Bearer ${auth}`, "Content-Type": "application/json"},
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
return handleResponse<T>(response);
|
||||
}
|
||||
|
||||
export async function apiPostPublic<T>(url: string, data: object, lang: string = "fr"): Promise<T> {
|
||||
const response = await fetch(buildUrl(url, {}, lang), {
|
||||
method: "POST",
|
||||
headers: {"Content-Type": "application/json"},
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
return handleResponse<T>(response);
|
||||
}
|
||||
|
||||
export async function apiUpload<T>(url: string, file: File, auth: string, lang: string = "fr"): Promise<T> {
|
||||
const formData = new FormData();
|
||||
formData.append("file", file);
|
||||
const response = await fetch(buildUrl(url, {}, lang), {
|
||||
method: "POST",
|
||||
headers: {"Authorization": `Bearer ${auth}`},
|
||||
body: formData,
|
||||
});
|
||||
return handleResponse<T>(response);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user