Files
CVAtron/resources/js/lib/utils.ts
Matthias Guillitte 55a52086c1
Some checks failed
linter / quality (push) Failing after 3m25s
tests / ci (push) Failing after 12m2s
Models refactor + Basic functionnalities
2025-08-26 12:12:02 +02:00

37 lines
928 B
TypeScript

import { useFetch, UseFetchOptions } from '@vueuse/core';
import { clsx, type ClassValue } from 'clsx';
import { twMerge } from 'tailwind-merge';
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}
let csrfInitialized = false
async function ensureCsrf() {
if (!csrfInitialized) {
await useFetch('/sanctum/csrf-cookie', {
credentials: 'include',
})
csrfInitialized = true
}
}
export async function httpApi<T>(url: string, options?: RequestInit, useFetchOptions?: UseFetchOptions): Promise<{data: T | null, error: any}> {
await ensureCsrf();
const { data, error } = await useFetch(url, {
credentials: 'include',
...options,
},
useFetchOptions
).json();
if (!error.value && data.value) {
return { data: data.value, error: null };
} else {
return { data: data.value, error: error.value };
}
}