Closed astahmer closed 2 years ago
found a workaround by passing the zodios api instance & overriding baseUrl directly on the axios instance
const createApiPlugin = (instance: ZodiosInstance<any>, apiId: ApiId) => {
return {
name: 'apiPlugin',
request: async (_endpoint, config) => {
const state = xxx;
const baseUrl = xxx[apiId];
instance.axios.defaults.baseURL = baseUrl;
return config
}
}
}
Hello alex,
I see, this is an oversight on my part, i desactivated baseURL from ZodiosConfig, but in reality it's still there :
export type AnyZodiosMethodOptions = Merge<
{
params?: Record<string, unknown>;
queries?: Record<string, unknown>;
headers?: Record<string, string>;
},
Omit<AxiosRequestConfig, "params" | "headers" | "baseURL" | "url" | "method">
>;
export type AnyZodiosRequestOptions = Merge<
{ method: Method; url: string },
AnyZodiosMethodOptions
>;
So i can just remove the "baseURL" omit here.
So if you just change the config baseURL it will work, because it's just an axios config
i'll create a fast fix
available on version 10.0.2
of @zodios/core
. you can now just do :
const createApiPlugin = (apiId: ApiId) => {
return {
name: 'apiPlugin',
request: async (_endpoint, config) => {
const state = xxx;
return { ...config, baseURL: state[apiId] };
}
}
}
found a workaround by passing the zodios api instance & overriding baseUrl directly on the axios instance
const createApiPlugin = (instance: ZodiosInstance<any>, apiId: ApiId) => { return { name: 'apiPlugin', request: async (_endpoint, config) => { const state = xxx; const baseUrl = xxx[apiId]; instance.axios.defaults.baseURL = baseUrl; return config } } }
for information, this workaround can lead to nasty bug really difficult to track. indeed, since requests are async, one request could change the base URL and the another one change it again before request is sent.
so you should switch to v10.0.2
and change the config per request. this is guaranteed to not have side effects
ok perfect thanks again ! 🙏
Nice just wanted to do this myself, perfect timing :tada:
Hey, I have a use-case where I need to inject a custom base URL depending on the request config, to do so i'm currently using a plugin creator (the
baseUrl
is an env var fetched at runtime and dynamic by country to be specific..)it looks kind of like this:
but then I get an error because the endpoint is not found in the response
would it be ok to allow overriding a
baseUrl
in thereturn
of theplugin.request
rather than overridingurl
completely ? i think this would solve my use-case and would not break anything