RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.61k stars 1.22k forks source link

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

Open NianHan-Advantech opened 1 month ago

NianHan-Advantech commented 1 month ago

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 commented 1 month ago

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");