Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can some return values in the generated axios be changed to Promise<any> #4887

Open
NianHan-Advantech opened this issue May 11, 2024 · 1 comment

Comments

@NianHan-Advantech
Copy link

During use, the return values of most interfaces are recognized as FileResponse, and the blob type data needs to be converted into corresponding objects.
May I ask if it is possible to change some unrecognized returns to Promise,
Just like this

 if (status === 200 || status === 206) {
            return Promise.resolve(response.data);

Or we can handle the return processing ourselves.

Or is there something I haven't discovered, such as using custom templates to generate API code.

@NianHan-Advantech
Copy link
Author

Now I'm doing this

var code = clientGenerator.GenerateFile();
code = code.Replace("responseType: \"blob\",", "");
code = code.Replace("<FileResponse>", "<any>");
code = code.Replace("const contentDisposition = response.headers ? response.headers[\"content-disposition\"] : undefined;", "");
code = code.Replace("let fileNameMatch = contentDisposition ? /filename\\*=(?:(\\\\?['\"])(.*?)\\1|(?:[^\\s]+'.*?')?([^;\\n]*))/g.exec(contentDisposition) : undefined;", "");
code = code.Replace("let fileName = fileNameMatch && fileNameMatch.length > 1 ? fileNameMatch[3] || fileNameMatch[2] : undefined;", "");
code = code.Replace("if (fileName) {", "if (process.env.NODE_ENV === \"production\") {");
code = code.Replace("fileName = decodeURIComponent(fileName);", "");
code = code.Replace("fileNameMatch = contentDisposition ? /filename=\"?([^\"]*?)\"?(;|$)/g.exec(contentDisposition) : undefined;", "");
code = code.Replace("fileName = fileNameMatch && fileNameMatch.length > 1 ? fileNameMatch[1] : undefined;", "console.log(\"response: \", response);");
code = code.Replace("{ fileName: fileName, status: status, data: new Blob([response.data], { type: response.headers[\"content-type\"] }), headers: _headers }", "response.data");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant