- Implemented auto-update logic in `ScribeTopBar` with update notification and user interaction. - Integrated `@tauri-apps/plugin-updater` and `@tauri-apps/plugin-process` for updater functionality. - Added automatic migration feature with `autoMigrateElectron` support and UI feedback. - Refactored app architecture with new routing, components, and layout for better modularity. - Enhanced JSON response handling in API client for robust data parsing. - Updated locales to include new translations for update and migration-related UI.
98 lines
3.7 KiB
TypeScript
98 lines
3.7 KiB
TypeScript
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);
|
|
}
|
|
const contentType = response.headers.get('content-type') ?? '';
|
|
if (contentType.includes('application/json')) {
|
|
return response.json() as Promise<T>;
|
|
}
|
|
return response.text() as unknown 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);
|
|
}
|