translate.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package tst
  2. import (
  3. "fmt"
  4. . "git.eugeniocarvalho.dev/eugeniucarvalho/apicodegen/common"
  5. TS "git.eugeniocarvalho.dev/eugeniucarvalho/gg/generators/typescript"
  6. )
  7. var (
  8. module *TS.File
  9. )
  10. func Translate(project *Project) (err error) {
  11. ts := project.Client("angular6")
  12. path := fmt.Sprintf("%s/%s.module.ts", ts.OutputDir, project.ID)
  13. module = TS.NewFile(path)
  14. // Adiciona os importes necessarios
  15. imports(project, module)
  16. // Cria todas as classes de entidades do projeto
  17. if err = GenSchemas(project); err != nil {
  18. return
  19. }
  20. // Define os objetos de acesso a API
  21. GenResources(project)
  22. return module.Save()
  23. }
  24. func imports(project *Project, file *TS.File) {
  25. file.Raw(`
  26. // import { NgModule, Injectable, EventEmitter, ModuleWithProviders } from '@angular/core';
  27. import { NgModule, Injectable, EventEmitter, ModuleWithProviders, InjectionToken, Inject } from '@angular/core';
  28. // import { BrowserModule } from '@angular/platform-browser';
  29. import { CommonModule } from '@angular/common';
  30. import { HttpClientModule, HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
  31. import { Observable, of, Subject, BehaviorSubject, Subscription} from 'rxjs';
  32. // import { ActivatedRoute, Router } from '@angular/router';
  33. import { catchError, map, switchMap, tap, take, filter } from 'rxjs/operators';
  34. import { EventSourcePolyfill } from 'event-source-polyfill';
  35. if (!Boolean(window['oauth_ss'])) {
  36. window['oauth_ss'] = Math.random().toString(30).substr(2, 11).toUpperCase();
  37. }
  38. const oauthWindowProp = `).Raw(fmt.Sprintf("`__${window['oauth_ss']}__`")).Raw(`;
  39. // @Injectable({
  40. // providedIn: 'root'
  41. // })
  42. export class `).Raw(project.Name + "ApiOptions").Raw(` {
  43. public COOKIE_DOMAIN:string;
  44. public BASE_URL:string;
  45. public ACCOUNT_URL:string;
  46. public CONSOLE_URL:string;
  47. public REDIRECT_URI:string;
  48. public CLIENT_ID:string;
  49. public THREAD_SOCKET:string;
  50. public CLIENT_SECRET:string;
  51. public SCOPES: string[];
  52. }
  53. export const ApiOptionsParams = new InjectionToken<`).Raw(ApiClassName(project) + "Options").Raw(`>('ApiOptionsParams');
  54. export interface ApiInterface {
  55. Client: Client;
  56. Auth: Auth;
  57. poolReady: any[];
  58. httpOptions: {
  59. headers: HttpHeaders,
  60. withCredentials: boolean,
  61. };
  62. http: HttpClient;
  63. // queryParams: any;
  64. // route: ActivatedRoute;
  65. // router: Router;
  66. apiOptions: any;
  67. options(opts?: HttpOptions): HttpOptions;
  68. onready(fn: () => void);
  69. copyHeader(headers, hopts);
  70. }
  71. `).Line()
  72. }
  73. func getCustom(options map[string]interface{}, path string, fallback ...interface{}) (resp interface{}) {
  74. found := false
  75. if options != nil {
  76. if resp, found = options[path]; found {
  77. return
  78. }
  79. }
  80. for _, value := range fallback {
  81. resp = value
  82. }
  83. return
  84. }
  85. // func getCustom(options map[string]interface{}, path string) (resp interface{}) {
  86. // if options != nil {
  87. // resp = options[path]
  88. // }
  89. // return
  90. // }