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

[typescript-angular] service configuration/basePath modification after construction #7949

Closed
chrismellard opened this issue Apr 2, 2018 · 5 comments

Comments

@chrismellard
Copy link

chrismellard commented Apr 2, 2018

Description

From what I can tell I cannot achieve what I describe below with current versions of codegen - I'm prepared to stand corrected ;)

A nice feature to have for typescript-angular would be the ability to modify configuration settings on the client stubs after the services have been constructed. Currently, basePath is set on service construction and remain impossible to modify for the lifetime of that service once the constructor has completed. There are scenarios where when using ngrx/effects where swagger services can be injected prior to APP_INITIALIZER completing. It would therefore be nice to have the ability to modify the api service configuration post construction.

Swagger-codegen version

Whatever dockerhub - swaggerapi/swagger-codegen-cli:latest is currently building - this is unclear from the docker hub info.

Suggest a fix/enhancement

I've currently worked around this by extending the generated angular service and injecting a BehaviorSubject of Configuration. The api service subscribes to changes to this stream and updates its configuration/basePath as appropriate. There's obviously a reason the existing basePath property is defined as protected rather than public so I'm clearly circumventing this for my own evil needs.

@macjohnny
Copy link
Contributor

@chrismellard I would say your solution of extending the service and updating the basePath from an observable sounds reasonable. Moreover, basePath is protected, meaning that it is fine changing it in a derived class.

In order to allow this behavior without having to extend the generated services, I would suggest to have a setBasePath(basePath: string) method that can be called externally, e.g. from your observable subscription, since you can also change the Configuration object later. The basePath configuration property is a little special because it gets copied.

@chrismellard
Copy link
Author

chrismellard commented Apr 3, 2018

I have a PR ready to go if you're interested that is injecting an optional injection token Observable<string> in to the constructor of each api service. This updates the basePath based on this subscription firing.

The benefit here above and beyond a setBasePath method (if I understand your proposal correctly) is that I can let DI take of the problem for me - so long as I create an Observable<string> for the injection token angular will DI that in to all my api services automatically for me rather than having to explicitly calling a setter all over the place for each api service I use.

@macjohnny
Copy link
Contributor

@chrismellard ok sounds good. please open a PR

@crhistianramirez
Copy link

This would be neat to have. Where are we on the PR?

@chrismellard
Copy link
Author

@crhistianramirez I'll merge from master and push it up later today.

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