-
Notifications
You must be signed in to change notification settings - Fork 60
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
feat: add onRequest hook to createAPIClient #1469
base: main
Are you sure you want to change the base?
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
I wouldn't encourage people to add custom headers via the hook, rather use global headers or |
The source of this use case is similar to the timeout PR: it’s something we could easily do with the old axios client but found less straightforward with the new ofetch implementation. In our previous HTTP client, we could globally modify headers dynamically based on the context, like so:
With the new ofetch client, I wasn’t able to find a straightforward way to achieve similar behavior. The strongest use case for the onRequest hook is that it allows us to configure headers once when initializing the HTTP client and have them applied dynamically for every request. This avoids the need to manually set headers in each request or implement redundant wrapper logic, keeping our code cleaner and more maintainable. For example, setting X-Forwarded-For or User-Agent headers dynamically based on the request’s context can be achieved through the hook without needing per-request configuration. This ensures a centralized and consistent approach to managing such headers. That said, I’d love to hear your thoughts. Do you think there’s a better way to dynamically modify headers in a centralized manner without adding logic to individual requests? If using hooks isn’t the ideal method, I’m happy to explore other options based on your input! |
@shopware/api-client
@shopware-pwa/cms-base
@shopware-pwa/helpers-next
@shopware-pwa/nuxt3-module
@shopware-pwa/composables-next
@shopware/api-gen
commit: |
CodSpeed Performance ReportMerging #1469 will not alter performanceComparing Summary
|
Description
Allow for usage of the
onRequest
interceptor that's provided by ofetch. Added onRequest to the ApiClientHooks and applied it in a similar way that was done for other hooks.Added four testcases to make sure it works in the way you'd expect when using this hook.
Added a bit of info to the readme to make sure people know they can use it.
Type of change
New feature (non-breaking change which adds functionality)
ToDo's
None