base_ui/composables/useHadithaSearchComposable.ts

32 lines
967 B
TypeScript
Raw Normal View History

2025-03-15 10:11:45 +00:00
// composables/useHadithaSearchComposable.js
import { useStorage } from "@vueuse/core";
export const useHadithaSearchComposable = <T>(url, options = {}) => {
const loading = ref(false); // Track loading state
const data = ref<T | null>(null);
const error = ref<any>(null);
const status = ref<string | null>(null);
// Get the authentication token (e.g., from a cookie or local storage)
// const token = useCookie('auth-token') // Assuming you store the token in a cookie
let token = useStorage("id_token", "GuestAccess");
const config = useRuntimeConfig();
console.info(config);
const baseURL =
config.public.NUXT_PUBLIC_BASE_URL + config.public.NUXT_PUBLIC_API_NAME;
// Add headers to the request
const headers = {
Authorization: token.value,
...options.headers, // Merge with any existing headers
};
// Use useFetch with the headers
return useFetch<T>(url, {
...options,
baseURL: baseURL,
headers,
});
};