Support for custom request-function
See original GitHub issueDescribe the solution you’d like In our last project, we have used a custom axios setup to access the backend. One thing that would not have been possible with “openapi-typescript-codegen” is the axios-interceptor that retrieves the the current language settings from “i18next” and adds a query parameter to the outgoing request. It was very convenient to do it this way, because from that point on, we didn’t have to worry about transmitting the correct language anymore.
I would like to see a mechanism that uses a custom request-function. That way, everybody can write an adapter if needed.
The following changes
// OpenAPI.ts
export type RequestFunction = (options: ApiRequestOptions) => Promise<ApiResult>;
interface Config {
BASE: string;
VERSION: string;
WITH_CREDENTIALS: boolean;
TOKEN: string | (() => Promise<string>);
requestFunction?: RequestFunction; // added
}
// request.ts
export async function request(options: ApiRequestOptions): Promise<ApiResult> {
if (OpenAPI.requestfunction != null) {
return OpenAPI.requestFunction(options);
}
...
}
would allow a configuration similar to the bearer-token:
OpenAPI.requestFunction = async (options) => {
...
return {
...
}
}
I briefly thought about using your library in our project, but it was too late by then. In the next project, maybe. It could be a huge time-saver.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:8 (4 by maintainers)
Top GitHub Comments
@ferdikoomen Do you have an example of registering an interceptor on axios client?
I have had a look at the swagger-spec of our API again. The query-param is currently not part of all endpoints. This is mainly, because we auto-generate the spec from the Spring-Boot endpoints and the query-parameter is processed in a Filter before the Controller classes are invoked. Since the parameter is universal, I would like to treat it in a universal fashion in the frontend as well. And I really don’t want to pull any Java-dependencies into the frontend project…
The change could also be done by adding a new request-type (“customRequest” instead of “node”, “xhr” or “fetch”) which creates a function that delegates to
OpenAPI.requestFunction
. I think the result could still be called “lightweight”. It would not add any dependencies. It would be the responsibility of user to provide the request function, including any dependencies.I can create a small PR to illustrate what I mean.