question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Our Angular2/Typescript client generated with NSwag has quickly grown to over 32,000 lines. The large file size makes it difficult to navigate, and we also suspect it is having a substantial negative performance impact on our webpack build time.

A few questions:

  1. Is it possible to have NSwag generate multiple files instead of a single file? Note: We have it automatically picking up every controller in our ASP.NET Web API and want to maintain this without having to manually edit the config file to add new controllers.
  2. Every single service call has a corresponding processFoo method that has the exact same code duplicated over and over. This adds a huge amount of bloat to the file and unnecessary time to our webpack build.
protected processFoo(response: HttpResponseBase): Observable<void> {
        const status = response.status;
        const responseBlob = 
            response instanceof HttpResponse ? response.body : 
            (<any>response).error instanceof Blob ? (<any>response).error : undefined;

        let _headers: any = {}; if (response.headers) { for (let key of response.headers.keys()) { _headers[key] = response.headers.get(key); }};
        if (status === 204) {
            return blobToText(responseBlob).flatMap(_responseText => {
            return Observable.of<void>(<any>null);
            });
        } else if (status !== 200 && status !== 204) {
            return blobToText(responseBlob).flatMap(_responseText => {
            return throwException("An unexpected server error occurred.", status, _responseText, _headers);
            });
        }
        return Observable.of<void>(<any>null);
    }
  1. Every service call has extremely similar code in the body of the method itself:
foo(message: Bar | null): Observable<void> {
        let url_ = this.baseUrl + "/api/Foo/Bar";
        url_ = url_.replace(/[?&]$/, "");

        const content_ = JSON.stringify(message);

        let options_ : any = {
            body: content_,
            observe: "response",
            responseType: "blob",
            headers: new HttpHeaders({
                "Content-Type": "application/json", 
            })
        };

        return Observable.fromPromise(this.transformOptions(options_)).flatMap(transformedOptions_ => {
            return this.http.request("get", url_, transformedOptions_);
        }).flatMap((response_: any) => {
            return this.transformResult(url_, response_, (r) => this.processFoo(<any>r));
        }).catch((response_: any) => {
            if (response_ instanceof HttpResponseBase) {
                try {
                    return this.transformResult(url_, response_, (r) => this.processFoo(<any>r));
                } catch (e) {
                    return <Observable<void>><any>Observable.throw(e);
                }
            } else
                return <Observable<void>><any>Observable.throw(response_);
        });
    }

I believe most of the 2 and 3 methods could easily be extracted to a couple utility methods that are called from everywhere. This could dramatically decrease the size of the code generated. What is the feasibility of doing this?

Thanks.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:7
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
KeterSCPcommented, May 3, 2022

@RicoSuter any updates on this? We have a large generated ts file, which after gzipping is around 900kB and affects load performance

0reactions
Shaddixcommented, Nov 2, 2022

It’s not per se reducing the file size, but it’s possible to move from Classes to Modules, so that the code becomes treeshakable (and also code-splittable). Check out: https://github.com/Shaddix/react-query-swagger/#clients-as-modules

Lib is based on NSwag and is originally meant to generate react-query hooks, but you could skip the hooks via /no-hooks option.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Send and Transfer Large Files
All of your large file transfer needs covered—easily send large videos, project files, and more with Dropbox. Share files directly from your cloud...
Read more >
The 11 Best Ways to Send Large Files
Using a cloud storage space like Google Drive, Dropbox, or OneDrive is one of the easiest and most popular methods for sending large...
Read more >
How to Send Large Files Over the Internet
Wormhole is a simple but effective site that helps you securely share files up to 10GB in size using end-to-end encryption and links...
Read more >
TransferNow: Send Large Files - Free Secure File Transfer
TransferNow is a simple, quick and secure free solution to send large files and big documents up to 200 GB per transfer. No...
Read more >
Filemail: Send Large Files Free - Fast Secure File Transfer
Send large files free via email and links. Paid accounts share files of any size. Fast secure online file transfer using our file...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found