Being able to provide your own client instance
See original GitHub issueAbility to provide a custom client instance
Let’s say I’ve set --client
to be axios
as the generated client, it would be nice to have control over the generated client and for example being able to configure the request/response interceptors or other config.
Of course there are multiple ways to provide such functionality, but an example would be to add an additional optional parameter to the command like --clientInstance
(or -ci
in short) which points to a js/ts file which exports the client with the desired config.
An example for the latter when using axios, could be:
$ openapi --input ./api/swagger.json --output ./api/client --client axios --clientInstance ./api/axios-instance.ts
Then have your own instance located at /api/axios-instance.ts
, with for example the following setup
import axios, { AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse } from "axios";
const axiosInstance: AxiosInstance = axios.create({
// custom config
});
const onRequest = (config: AxiosRequestConfig): AxiosRequestConfig => {
console.log(`[request] - ${config.method}`);
return config;
}
const onRequestError = (error: AxiosError): Promise<AxiosError> => {
console.log(`[request error] - ${error.code}`)
return Promise.reject(error);
}
const onResponse = (response: AxiosResponse): AxiosResponse => {
console.log(`[response] - ${response.status}`);
return response;
}
const onResponseError = (error: AxiosError): Promise<AxiosError> => {
console.log(`[response error] - ${error.code}`);
return Promise.reject(error);
}
axiosInstance.interceptors.request.use(onRequest, onRequestError);
axiosInstance.interceptors.response.use(onResponse, onResponseError);
export default axiosInstance;
Issue Analytics
- State:
- Created a year ago
- Reactions:3
- Comments:5 (1 by maintainers)
Top GitHub Comments
@jvdwijngaard @Lighttree I added a section to the documentation that explains how you can use your own request file, this might be a solution for your case: https://github.com/ferdikoomen/openapi-typescript-codegen/blob/master/docs/custom-request-file.md
@ferdikoomen Thanks! I think this solves raised issue.