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

[Review] Reduce file size of the Angular typescript client #1469

Closed
wants to merge 2 commits into from

Conversation

yhnavein
Copy link
Contributor

File reduction is achieved by ignoring all the existing processing of the responses. Very simple construction can be used instead, which is just enough for us.

Example with a diff:

-    return this.http.request("get", url_, options_).pipe(_observableMergeMap((response_ : any) => {
-        return this.processGetPermissions(response_);
-    })).pipe(_observableCatch((response_: any) => {
-        if (response_ instanceof HttpResponseBase) {
-            try {
-                return this.processGetPermissions(<any>response_);
-            } catch (e) {
-                return <Observable<string[]>><any>_observableThrow(e);
-            }
-        } else
-            return <Observable<string[]>><any>_observableThrow(response_);
-    }));
+    return this.http.get<string[]>(url_, options_);
-}
-
-protected processGetPermissions(response: HttpResponseBase): Observable<string[]> {
-    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 === 200) {
-        return blobToText(responseBlob).pipe(_observableMergeMap(_responseText => {
-        let result200: any = null;
-        result200 = _responseText === "" ? null : <string[]>JSON.parse(_responseText, this.jsonParseReviver);
-        return _observableOf(result200);
-        }));
-    } else if (status !== 200 && status !== 204) {
-        return blobToText(responseBlob).pipe(_observableMergeMap(_responseText => {
-        return throwException("An unexpected server error occurred.", status, _responseText, _headers);
-        }));
-    }
-    return _observableOf<string[]>(<any>null);
-}

So it's 1 line instead of existing 35.

I know that this may be a breaking change for someone, so this change is done in a way that it is possible to enable / disable the new shorter template using a new flag: UseAngularResultProcessing. By default (false) it's an existing behaviour.

Additional thing that happened in this PR is using the same flag to adjust behaviour of the Result Type generated in the Observable. In our codebase it was been generating always as a nullable Observable, which was not true (and more importantly it was just a PITA)

Please let me know your thoughts.

BTW - this PR is similar to the #1248 but in this, a different approach is used.

additionally fix issue with nullable Observables flood.
File size reduction is optional and can be enabled with flag: `UseAngularResultProcessing: true`
@RicoSuter RicoSuter changed the title Reduce file size of the Angular typescript client [Review] Reduce file size of the Angular typescript client Sep 20, 2018
@yhnavein
Copy link
Contributor Author

Guys, any chance it will be merged?
I'm tired of maintaining my own fork of nswag, releasing it, etc.

@RicoSuter
Copy link
Owner

I think there are too many breaking changes in this - there are lots of changes. I’m quite busy at the moment but hopefully will find time to do a review

@ApocDev
Copy link

ApocDev commented May 1, 2019

Any update on this one? The changes don't actually seem to be breaking (mostly whitespace changes)

This vastly improves Angular clients, and removes superfluous types in the process.

@daiplusplus
Copy link
Contributor

daiplusplus commented May 16, 2019

@yhnavein

I'm tired of maintaining my own fork of nswag, releasing it, etc.

You can use your own *.liquid templates with NSwag + NSwagStudio by supplying the path to a private templates folder in the NSwag project properties - this is is how I work on my own templates (with my own proprietary extensions) without needing to keep my own fork. My own fork now is only for changes I'd like to see adopted upstream.

@RicoSuter RicoSuter added this to To look into in Improve TS file size Jul 15, 2020
@dylanvdmerwe
Copy link

This is still something that would be a great enhancement to the Typescript files!

@yhnavein yhnavein closed this Nov 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

None yet

5 participants