import axios, {AxiosResponse} from "axios"; import {configs} from "@/lib/configs"; export default class System{ static verifyInput(input: string): boolean { let pattern: RegExp = new RegExp('(<.*?>)|(&.*?;)|({.*?})', 'gmi'); return pattern.test(input); } public static formatHTMLContent(htmlContent: string): string { return htmlContent .replace(/
/g, '
') .replace(/
/g, ''); } public static textContentToHtml(content: string): string { const paragraphs: string[] = content .split(/\n+/) .map((paragraph: string) => paragraph.trim()) .filter((paragraph: string) => paragraph.length > 0); return paragraphs .map((paragraph: string) => `${paragraph}
`) .join(''); } public static async authGetQueryToServer(url: string, auth: string, lang: string = "fr", params: Record = {}): Promise { try { const response: AxiosResponse = await axios({ method: 'GET', headers: { 'Authorization': `Bearer ${auth}` }, params: { lang: lang, plateforme: window.electron.platform, ...params }, url: configs.apiUrl + url, }) return response.data; } catch (e: unknown) { if (axios.isAxiosError(e)) { const serverMessage: string = e.response?.data?.message || e.response?.data || e.message; throw new Error(serverMessage as string); } else if (e instanceof Error) { throw new Error(e.message); } else { throw new Error('An unexpected error occurred'); } } } public static setCookie(name: string, value: string, days: number): void { const date = new Date(); date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); const expires = `expires=${date.toUTCString()}`; let domain: string = ''; if (!/localhost|127\.0\.0\.1/.test(window.location.hostname)) { domain = `domain=${window.location.hostname};`; } const secure = 'Secure;'; const sameSite = 'SameSite=Strict;'; document.cookie = `${name}=${value}; ${expires}; ${domain} path=/; ${secure} ${sameSite}`; } public static async authPutToServer (url: string, data: {}, auth: string, lang: string = "fr"): Promise { try { const response: AxiosResponse = await axios({ method: 'PUT', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${auth}` }, params: { lang: lang, plateforme: window.electron.platform, }, url: configs.apiUrl + url, data: data }) return response.data; } catch (e: unknown) { if (axios.isAxiosError(e)) { const serverMessage: string = e.response?.data?.message || e.response?.data || e.message; throw new Error(serverMessage as string); } else if (e instanceof Error) { throw new Error(e.message); } else { throw new Error('An unexpected error occurred'); } } } public static async authPatchToServer (url: string, data: {}, auth: string, lang: string = "fr"): Promise { try { const response: AxiosResponse = await axios({ method: 'patch', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${auth}` }, params: { lang: lang, plateforme: window.electron.platform, }, url: configs.apiUrl + url, data: data }) return response.data; } catch (e: unknown) { if (axios.isAxiosError(e)) { const serverMessage: string = e.response?.data?.message || e.response?.data || e.message; throw new Error(serverMessage as string); } else if (e instanceof Error) { throw new Error(e.message); } else { throw new Error('An unexpected error occurred'); } } } public static async postToServer (url: string, data: {}, lang: string = "fr"): Promise { try { const response: AxiosResponse = await axios({ method: 'POST', headers: { 'Content-Type': 'application/json', }, url: configs.apiUrl + url, params: { lang: lang, plateforme: window.electron.platform, }, data: data }) return response.data; } catch (e: unknown) { if (axios.isAxiosError(e)) { const serverMessage: string = e.response?.data?.message || e.response?.data || e.message; throw new Error(serverMessage as string); } else if (e instanceof Error) { throw new Error(e.message); } else { throw new Error('An unexpected error occurred'); } } } public static async authPostToServer (url: string, data: {}, auth: string, lang: string = "fr"): Promise { try { const response: AxiosResponse = await axios({ method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${auth}` }, url: configs.apiUrl + url, params: { lang: lang, plateforme: window.electron.platform, }, data: data }) return response.data; } catch (e: unknown) { if (axios.isAxiosError(e)) { const serverMessage: string = e.response?.data?.message || e.response?.data || e.message; throw new Error(serverMessage as string); } else if (e instanceof Error) { throw new Error(e.message); } else { throw new Error('An unexpected error occurred'); } } } static htmlToText(html: string) { return html .replace(/
/gi, '\n') .replace(/<\/?(p|h[1-6]|div)(\s+[^>]*)?>/gi, '\n') .replace(/<\/?[^>]+(>|$)/g, '') .replace(/(\n\s*){2,}/g, '\n\n') .replace(/^\s+|\s+$|(?<=\s)\s+/g, '') .trim(); } public static getCookie(name: string): string | null { const nameEQ = `${name}=`; const allCookies: string[] = document.cookie.split(';'); for (let i: number = 0; i < allCookies.length; i++) { let cookie: string = allCookies[i]; while (cookie.charAt(0) === ' ') cookie = cookie.substring(1, cookie.length); if (cookie.indexOf(nameEQ) === 0) return cookie.substring(nameEQ.length, cookie.length); } return null; } public static removeCookie(name: string): void { let domain: string = ''; if (!/localhost|127\.0\.0\.1/.test(window.location.hostname)) { domain = `domain=${window.location.hostname};`; } const secure = 'Secure;'; const sameSite = 'SameSite=Strict;'; document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 GMT; ${domain} path=/; ${secure} ${sameSite}`; } public static async authDeleteToServer(url: string, data: {}, auth: string, lang: string = "fr"): Promise { try { const response: AxiosResponse = await axios({ method: 'DELETE', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${auth}` }, url: configs.apiUrl + url, params: { lang: lang, plateforme: window.electron.platform, }, data: data }) return response.data; } catch (e: unknown) { if (axios.isAxiosError(e)) { const serverMessage: string = e.response?.data?.message || e.response?.data || e.message; throw new Error(serverMessage as string); } else if (e instanceof Error) { throw new Error(e.message); } else { throw new Error('An unexpected error occurred'); } } } }