translate.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. // fmt.Printf("Store angular module '%s'\n", path)
  15. // Adiciona os importes necessarios
  16. imports(project, module)
  17. // Cria todas as classes de entidades do projeto
  18. if err = GenSchemas(project); err != nil {
  19. return
  20. }
  21. // Define os objetos de acesso a API
  22. GenResources(project)
  23. return module.Save()
  24. }
  25. func imports(project *Project, file *TS.File) {
  26. file.Raw(`
  27. // import { NgModule, Injectable, EventEmitter, ModuleWithProviders } from '@angular/core';
  28. import { NgModule, Injectable, EventEmitter, ModuleWithProviders, InjectionToken, Inject } from '@angular/core';
  29. // import { BrowserModule } from '@angular/platform-browser';
  30. import { CommonModule } from '@angular/common';
  31. import { HttpClientModule, HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
  32. import { Observable, of, Subject, BehaviorSubject} from 'rxjs';
  33. // import { ActivatedRoute, Router } from '@angular/router';
  34. import { catchError, map, switchMap, tap, take, filter } from 'rxjs/operators';
  35. import { EventSourcePolyfill } from 'event-source-polyfill';
  36. if (!Boolean(window['oauth_ss'])) {
  37. window['oauth_ss'] = Math.random().toString(30).substr(2, 11).toUpperCase();
  38. }
  39. const oauthWindowProp = `).Raw(fmt.Sprintf("`__${window['oauth_ss']}__`")).Raw(`;
  40. // @Injectable({
  41. // providedIn: 'root'
  42. // })
  43. export class `).Raw(project.Name + "ApiOptions").Raw(` {
  44. public COOKIE_DOMAIN:string;
  45. public BASE_URL:string;
  46. public ACCOUNT_URL:string;
  47. public CONSOLE_URL:string;
  48. public REDIRECT_URI:string;
  49. public CLIENT_ID:string;
  50. public THREAD_SOCKET:string;
  51. public CLIENT_SECRET:string;
  52. public SCOPES: string[];
  53. }
  54. export const ApiOptionsParams = new InjectionToken<`).Raw(ApiClassName(project) + "Options").Raw(`>('ApiOptionsParams');
  55. export interface ApiInterface {
  56. Client: Client;
  57. Auth: Auth;
  58. poolReady: any[];
  59. httpOptions: {
  60. headers: HttpHeaders,
  61. withCredentials: boolean,
  62. };
  63. http: HttpClient;
  64. // queryParams: any;
  65. // route: ActivatedRoute;
  66. // router: Router;
  67. apiOptions: any;
  68. options(opts?: HttpOptions): HttpOptions;
  69. onready(fn: () => void);
  70. copyHeader(headers, hopts);
  71. }
  72. `).Line()
  73. }
  74. func getCustom(options map[string]interface{}, path string) (resp interface{}) {
  75. if options != nil {
  76. resp = options[path]
  77. }
  78. return
  79. }