Replies: 1 comment 2 replies
-
|
Hi @dasdawidt! I'm Dosu and I’m helping the kubb team. Great question! Currently, Kubb doesn't automatically process OpenAPI security schemes during code generation, so there's no built-in way for the generated client to know which endpoints require which auth method. Authentication is treated as a runtime concern rather than a codegen feature. That said, here are a few approaches that might help your use case: 1. Use the Axios client with interceptors If you switch to the axios client, you get access to import { axiosInstance } from '@kubb/plugin-client/clients/axios'
axiosInstance.interceptors.request.use((config) => {
// Route auth based on URL or custom logic
if (config.url?.includes('/public')) {
// No auth
} else if (config.url?.includes('/admin')) {
config.headers['X-API-Key'] = getApiKey()
} else {
config.headers.Authorization = `Bearer ${getBearerToken()}`
}
return config
})2. Use The 3. Injectable Recent updates allow you to pass a custom As for the roadmap—the v5 roadmap mentions "make the client smarter" but doesn't currently include automatic security scheme handling. This could be a good feature request though, since the underlying To reply, just mention @dosu. Docs are dead. Just use Dosu. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello everyone!
In my team we use OpenAPI Security Schemes for defining different authentication methods for our endpoints. We are currently figuring out how to generate clients for using endpoints with auth.
Currently, our best approach is to create a helper function that builds and returns a
RequestConfig & { client: Client }object, the client being a wrapper function around the fetch client that comes with @kubb/plugin-client. This works pretty well and we don't need to re-implement everything that is already included in the fetch client (as we would need to when implementing our own client).However, when it comes to auth this gets a little difficult as not all our endpoint require (the same) auth. For example, imagine three endpoints:
Now the client needs some way to know which headers to append to which request. As far as I know, there is no way to know inside the fetch function (based on the params) whether this path requires auth (e.g. has a securityScheme associated with it). Maybe there would be a way using generators or custom plugins.
I know that other solutions such as Hey API have a concept for auth or alternativeley offer the option to register interceptors (which would make our wrapper function obsolete).
Are there any existring solutions for this problem? I guess there are already plans to implement similar options, as "make the client smarter" is part of the roadmap?
Beta Was this translation helpful? Give feedback.
All reactions