37 lines
928 B
TypeScript
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 };
|
|
}
|
|
}
|