blue-veery-gmbh / spring-rest-2-ts

spring rest 2 ts is typescript generator which produces data model and services in typescript based on Spring MVC annotations. It supports generation for Angular and React
MIT License
64 stars 17 forks source link

{providedIn:'root'} to injectable tag on angular conversion, and modify URL on httpService calls #48

Open webwatt opened 1 year ago

webwatt commented 1 year ago

Fantstic JOB! After spending all day working with this and tweaking there are only 2 things I can't see a way to overcome and really hampers the point of using this. 1) need to add {providedIn:'root'} in @Injectable() tag when creating the service. Since we are generating code and services will constantly be added by others, don't want to have to declare each time in 2) our application servers (yes a couple) are not at the localhost:4200 (or whatever your local dev is) and even if we dev the server side locally, still a different port, so need a way to modify url calls in local build based on application server hosting the Java Controller.

may already by possible and I just haven't seen it yet :-)

We are going to just use interface classes for the models, although would be really nice in the ModelClassesToTsClassesConverter if it could create empty contructors(){set initial vars of class} for the classes as well...just an observation as class is unusable in Angular until it is set. For us the interface will work just fine, for now at least.

tomasz-wozniak75 commented 1 year ago

Hey Thanks, we are glad to hear that tool is helpful :)

  1. We decided for code configuration to enable user to customize generator with custom implementations of interfaces from which tool is composed. One of such interfaces is com.blueveery.springrest2ts.converters.ConversionListener You can check SwaggerConversionListener as an example, it adds Ts comment based on Java swagger annotation. This line adds conversion listener to converter: restClassesConverter.getConversionListener().getConversionListenerSet().add(new SwaggerConversionListener()); You need to create own conversion listener and implement method tsScopedTypeCreated - it will be called with service class. In this method You need to take "injectable" decorator, from the decorator list. Decorator has literal list, you need to add literal like in this example: TsDecorator injectableDecorator = <take injectable decorator from TsClass#getTsDecoratorList() > TSJsonLiteral literal = new TSJsonLiteral(); literal.getFieldMap().put("providedIn", new TSLiteral('', TypeMapper.tsString, "root")); injectableDecorator.getTsLiteralList().add(literal);

  2. Generated Angular service is injected with httpService: HttpClient, it is up to You how you configure httpService you can set domain and port which You want or credentials

  3. To use model classes in TypeScript we need JavaScript deserializer library like Jakson in Java cos JSON class changes JSON into JavaScript objects without type, it is like map of values to which we can only apply TS interfaces thanks to duck typing. With typed class we could have new spectrum of possibilities, TypeScript interfaces are not retained in runtime. I tried jackson-js library I think that author did great job but it is not supported anymore so for production use cases it is safer to use converter which converts java model classes to Ts interfaces

If you will have any problem just let me know, comments allow us to improve tool thanks