-
-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature] Support custom headers for useSanctumClient
instance
#95
Comments
Hey @npldevfr 👋 I'll be back from vacation in a week and I can check what other options we can do about it. Maybe I will be able to implement a custom set of headers as a module config or user function that returns header collection or something like this. |
I also want that, i need to add multi language but i cannot because i need to add manually to all calls.
|
After a quick look, I think I found a possible way of extending functionality by defining custom interceptors in export default defineAppConfig({
sanctum: {
interceptors: {
onRequest: async (nuxtApp: NuxtApp, context: FetchContext) => {
// Here you can add your own headers or add custom callbacks for each request like telemetry and etc
console.log('onRequest interceptor');
},
onResponse: async (nuxtApp: NuxtApp, context: FetchContext, response: FetchResponse<T>) => {
// Here you can add your own headers or add custom callbacks for each response like telemetry and etc
console.log('onResponse interceptor');
},
},
},
}); I will try to implement it this week and open a PR, stay tuned 😄 |
useSanctumClient
instance
I made the composable around useSanctumClient and it works well. Here's an example of how I use it. Composable: export function useApiClient() {
const { locale } = useNuxtApp().$i18n;
const client = useSanctumClient();
return client.create({
headers: {
locale: locale.value
}
});
}
export function useApiCall(key, url, body, handlerOptions, useAsyncDataOptions, globalError = false) {
const client = useApiClient();
const response = useAsyncData(key, () => client(url, { body: body?.value, ...handlerOptions}), useAsyncDataOptions);
if(globalError) {
watch(response.error, () => {
if(response.error) {
throw createError(response.error.value);
}
})
}
return response;
} Usage: <script setup>
const { error, execute, status } = await useApiCall('login', '/login', requestFields, {
method: 'POST',
}, {
lazy: true,
immediate: false,
watch: false
});
</script> |
The feature has been merged and will be published soon as well as documentation. I'll write here once it is ready. |
New version |
Great! Thanks for your reactivity 😄 |
Hi, I would like to know if it is possible to inherit the client from useSanctumClient to add a permanent header like
X-Localization
However, $sanctumClient is sometimes undefined, which causes the requests to fail. Is there a cleaner way to handle this situation and ensure that the client is always properly initialized? Thank you!
The text was updated successfully, but these errors were encountered: