next/composables/useApiFetch.ts

44 lines
956 B
TypeScript
Raw Normal View History

2023-12-29 20:11:07 -05:00
import { AsyncData } from "#app";
2023-10-05 02:10:53 -04:00
const useApiFetch = async <T>(url: string, options?: any) => {
// const { token } = useAuth();
let head = {
...(options?.headers || {}),
};
// 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}`);
// }
console.log(`API => /api${url}`);
return (await useFetch<T>(`/api${url}`, {
// @ts-ignore
method: "get",
headers: head,
...options,
})) as AsyncData<T, any>;
};
export default useApiFetch;