Files
CVAtron/resources/js/lib/utils.ts
Matthias Guillitte cb242e59ba
Some checks failed
linter / quality (push) Successful in 3m37s
tests / ci (push) Failing after 13m21s
Minimal Viable Product + Refactor to pinia store + Fix PDF export
2025-09-16 16:30:37 +02:00

41 lines
996 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
);
if (data.value) {
data.value = JSON.parse(data.value as string) as T;
}
if (!error.value) {
return { data: data.value, error: null };
} else {
return { data: data.value, error: error.value };
}
}