1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | import { Injectable, Optional, Inject } from '@angular/core';
|
17 | import { Observable, Subject } from 'rxjs';
|
18 | import Amplify, { Logger, I18n } from '@aws-amplify/core';
|
19 | import { AuthState } from './auth.state';
|
20 | import { authDecorator } from './auth.decorator';
|
21 |
|
22 | import { Analytics } from '@aws-amplify/analytics';
|
23 |
|
24 | import { Auth } from '@aws-amplify/auth';
|
25 | import { Storage } from '@aws-amplify/storage';
|
26 | import { API } from '@aws-amplify/api';
|
27 | import { PubSub } from '@aws-amplify/pubsub';
|
28 | import { Interactions } from '@aws-amplify/interactions';
|
29 | import { XR } from '@aws-amplify/xr';
|
30 |
|
31 | @Injectable()
|
32 | export class AmplifyService {
|
33 | private _auth: any;
|
34 | private _analytics: any;
|
35 | private _storage: any;
|
36 | private _api: any;
|
37 | private _cache: any;
|
38 | private _pubsub: any;
|
39 | private _interactions: any;
|
40 | private _logger: any;
|
41 | private _xr: any;
|
42 | private _i18n: any;
|
43 | private _authState = new Subject<AuthState>();
|
44 | authStateChange$ = this._authState.asObservable();
|
45 |
|
46 | constructor(
|
47 | @Inject('modules')
|
48 | @Optional()
|
49 | private modules: any = {}
|
50 | ) {
|
51 | const source = modules || Amplify;
|
52 |
|
53 | authDecorator(this._authState, source.Auth);
|
54 |
|
55 | this._auth = source.Auth;
|
56 | this._analytics = source.Analytics;
|
57 | this._storage = source.Storage;
|
58 | this._api = source.API;
|
59 | this._cache = source.Cache;
|
60 | this._pubsub = source.PubSub;
|
61 | this._interactions = source.Interactions;
|
62 | this._xr = source.XR;
|
63 |
|
64 |
|
65 | this._i18n = I18n;
|
66 | this._logger = Logger;
|
67 | }
|
68 |
|
69 | auth(): any {
|
70 | return this._auth || Auth;
|
71 | }
|
72 | analytics(): any {
|
73 | return this._analytics || Analytics;
|
74 | }
|
75 | storage(): any {
|
76 | return this._storage || Storage;
|
77 | }
|
78 | api(): any {
|
79 | return this._api || API;
|
80 | }
|
81 | interactions(): any {
|
82 | return this._interactions || Interactions;
|
83 | }
|
84 | cache(): any {
|
85 | return this._cache;
|
86 | }
|
87 | pubsub(): any {
|
88 | return this._pubsub || PubSub;
|
89 | }
|
90 | logger(name, level?): Logger {
|
91 | return new this._logger(name, level);
|
92 | }
|
93 | xr(): any {
|
94 | return this._xr || XR;
|
95 | }
|
96 | i18n(): any {
|
97 | return this._i18n;
|
98 | }
|
99 |
|
100 | authState() {
|
101 | return this._authState;
|
102 | }
|
103 | setAuthState(state: AuthState) {
|
104 | this._authState.next(state);
|
105 | }
|
106 | }
|