Added custom fetch function

This commit is contained in:
lucast committed 2025-12-05 02:34:35 +01:00
1 parent f06ea7c4b3
commit 89f84e571f
3 files changed
+98 -2

No files matched your search

+24
View File
@@ -0,0 +1,24 @@
import axios, { type AxiosRequestConfig, type AxiosResponse } from "axios"
import defu from "defu"
export const cfetch = async (
url: string,
options: AxiosRequestConfig = {},
authorize: boolean = true
): Promise<AxiosResponse> => {
const baseUrl = import.meta.env.VITE_PUBLIC_BACKEND
const defaults: AxiosRequestConfig = {
baseURL: baseUrl,
}
if (authorize) {
let token = (await cookieStore.get("sessionToken"))?.value
if (token) defaults.headers = { Authorization: `Bearer ${token}` }
}
const config: AxiosRequestConfig = defu(options, defaults)
return await axios(url, config)
}