UNPKG

2.76 kBPlain TextView Raw
1// tslint:disable
2/*
3 * Copyright 2017-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
6 * the License. A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0/
9 *
10 * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
11 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
12 * and limitations under the License.
13 */
14// tslint:enable
15
16import { Injectable, Optional, Inject } from '@angular/core';
17import { Observable, Subject } from 'rxjs';
18import Amplify, { Logger, I18n } from '@aws-amplify/core';
19import { AuthState } from './auth.state';
20import { authDecorator } from './auth.decorator';
21
22import { Analytics } from '@aws-amplify/analytics';
23
24import { Auth } from '@aws-amplify/auth';
25import { Storage } from '@aws-amplify/storage';
26import { API } from '@aws-amplify/api';
27import { PubSub } from '@aws-amplify/pubsub';
28import { Interactions } from '@aws-amplify/interactions';
29import { XR } from '@aws-amplify/xr';
30
31@Injectable()
32export 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 // i18n and logger instantiated by default (do not change)
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}