anymaniax / orval

orval is able to generate client with appropriate type-signatures (TypeScript) from any valid OpenAPI v3 or Swagger v2 specification, either in yaml or json formats. 🍺
https://orval.dev
MIT License
2.57k stars 286 forks source link

Axios: Ability to override individual configuration #959

Open usersina opened 9 months ago

usersina commented 9 months ago

Feature request

I sometimes need to do more than the given default options. For example, adding a custom config option to extend the custom axios instance's behavior.

Concrete example: File upload progress

1. Problem

A generated upload hook function with a react-query client and a custom axios instance looks like this:

export const useUploadProjectFilesHook = () => {
  const uploadProjectFiles = useCustomInstance<string[]>();

  return (projectId: string, uploadProjectFilesBody: UploadProjectFilesBody, subpath = "operations") => {
    const formData = new FormData();
    formData.append("projectFileRequest", uploadProjectFilesBody.projectFileRequest);
    uploadProjectFilesBody.files.forEach((value) => formData.append("files", value));

    return uploadProjectFiles({
      url: `/${subpath}/packet/files/upload/${projectId}`,
      method: "post",
      headers: { "Content-Type": "multipart/form-data" },
      data: formData,
    });
  };
};

It would then be used as follows in the application code:

const uploadFilesMutation = useUploadProjectFiles();

The problem here is that if I want to track the upload progress, I currently have no way of doing this.

2. Possible Solution

Achieving this is possible by potentially providing additional configuration information to the internally called hook function.

const uploadFilesMutation = useUploadProjectFiles({
  axiosConfig: {
    onDownloadProgress(progressEvent) {},
    onUploadProgress(progressEvent) {},
  }
})
export const useUploadProjectFilesHook = <T>(axiosConfig?: AxiosRequestConfig<T>) => {
  const uploadProjectFiles = useCustomInstance<string[]>();

  return (projectId: string, uploadProjectFilesBody: UploadProjectFilesBody) => {
    const formData = new FormData();
    formData.append("projectFileRequest", uploadProjectFilesBody.projectFileRequest);
    uploadProjectFilesBody.files.forEach((value) => formData.append("files", value));

    return uploadProjectFiles({
      url: `/${subpath}/packet/files/upload/${projectId}`,
      method: "post",
      headers: { "Content-Type": "multipart/form-data" },
      data: formData,
      ...axiosConfig, // notice the spread operator
    });
  };
};

Related issues

soartec-lab commented 5 months ago

Hi, @usersina

It varies by client, so please refer to your client's reference. As a workaround, it might be possible to reference the http client directly. I checked some client references, but unfortunately it didn't seem possible to access the response headers. Also consider using the HTTP client directly as a workaround.