2023-12-06 21:27:01 -05:00
|
|
|
import { AsyncData, UseFetchOptions } from "#app";
|
|
|
|
import { NitroFetchRequest } from "nitropack";
|
2023-09-28 21:55:19 -04:00
|
|
|
|
2023-10-05 02:10:53 -04:00
|
|
|
const useApiFetch = async <T>(url: string, options?: any) => {
|
2023-10-10 22:22:45 -04:00
|
|
|
// const { token } = useAuth();
|
2023-10-03 01:06:40 -04:00
|
|
|
let head = {
|
|
|
|
...(options?.headers || {}),
|
|
|
|
};
|
2023-10-10 22:22:45 -04:00
|
|
|
// if (token.value) {
|
|
|
|
// head.Authorization = token.value;
|
|
|
|
// }
|
2023-10-05 02:10:53 -04:00
|
|
|
/* const cached = useSessionStorage<T>(url, null, {
|
|
|
|
serializer: StorageSerializers.object,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!cached.value) {
|
|
|
|
const { data, error } = await useFetch<T>("/api" + url, {
|
|
|
|
method: "get",
|
|
|
|
headers: head,
|
|
|
|
...options,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (error.value) {
|
|
|
|
throw createError({
|
|
|
|
...error.value,
|
|
|
|
statusMessage: `Could not fetch data from ${url}`,
|
|
|
|
});
|
|
|
|
} */
|
|
|
|
|
|
|
|
// Update the cache
|
|
|
|
// cached.value = data.value as T;
|
|
|
|
// } else {
|
|
|
|
// console.log(`Getting value from cache for ${url}`);
|
|
|
|
// }
|
2023-12-06 21:27:01 -05:00
|
|
|
console.log(`API => /api${url}`);
|
|
|
|
return (await useFetch<T>(`/api${url}`, {
|
|
|
|
// @ts-ignore
|
2023-09-28 21:55:19 -04:00
|
|
|
method: "get",
|
2023-10-03 01:06:40 -04:00
|
|
|
headers: head,
|
2023-09-28 21:55:19 -04:00
|
|
|
...options,
|
2023-12-06 21:27:01 -05:00
|
|
|
})) as AsyncData<T, any>;
|
2023-09-28 21:55:19 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
export default useApiFetch;
|