I found difficult to replace the functionality implemented in blobToText function.
Because File API (FileReader) is not present in server side code (Angular SSR -> Universal), the calls are always failing with error message "ReferenceError: FileReader is not defined".
Not easy to avoid this issue, because the calls and the function itself hard coded.
It would be great if it can be more flexible somehow or avoid using DOM related functions like File API.
You can replace this particular template (File.Utilities.liquid): https://github.com/RSuter/NSwag/wiki/Templates
What wouldbe a good always working replacement?
Thank you. I'll give it a try.
@ruskom
Did you find a solution for this?
We solved this by manually editing the generated code to replace responseType: "blob" with responseType: "json" in the options object. Then we could just cast the response body to our dto model and delete the blobToText function.
It would have been great to have a select in the NSwag UI to use json instead of blob in HttpClient like this, would that be possible in the future?
So ideally it would use "json" for JSON responses and "blob" for file/binary responses, correct?
Exactly :-)
@Chreekar thanks for suggesting the responseType change.
For now I did Creekar's change with responseType: json but to prevent changing everywhere in the swagger file I kept blobToText with a minor change. It's not optimal but it works with Angular SSR.
Would be nice with a permanent fix though.
function blobToText(blob: any): Observable<string> {
return new Observable<string>((observer: any) => {
observer.next(JSON.stringify(blob));
observer.complete();
});
}
@Chreekar
I think that its possible to work with providers for customization instead of manually change the auto generated output.
for instance:
blobToTextProvider which is injectable and the developer can assign the ts class in the UI.
httpRequestOptionsProvider
I haven't understood the concept of providers, is this something I just change in the UI or do I need to create them in code somehow?
Took another go at this. I downloaded AngularClient.liquid and File.Utilities.liquid from https://github.com/RicoSuter/NSwag/tree/master/src/NSwag.CodeGeneration.TypeScript/Templates into a folder I pointed out in the TemplateDirectory setting. Changed the response type and blobToText in them, and now when I generate code I won't have to change anything afterwards.
Most helpful comment
@Chreekar thanks for suggesting the responseType change.
For now I did Creekar's change with responseType: json but to prevent changing everywhere in the swagger file I kept blobToText with a minor change. It's not optimal but it works with Angular SSR.
Would be nice with a permanent fix though.