UNPKG

4.23 kBJavaScriptView Raw
1var __read = (this && this.__read) || function (o, n) {
2 var m = typeof Symbol === "function" && o[Symbol.iterator];
3 if (!m) return o;
4 var i = m.call(o), r, ar = [], e;
5 try {
6 while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
7 }
8 catch (error) { e = { error: error }; }
9 finally {
10 try {
11 if (r && !r.done && (m = i["return"])) m.call(i);
12 }
13 finally { if (e) throw e.error; }
14 }
15 return ar;
16};
17// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
18// SPDX-License-Identifier: Apache-2.0
19import { ConsoleLogger as LoggerClass } from './Logger';
20var logger = new LoggerClass('Amplify');
21var AmplifyClass = /** @class */ (function () {
22 function AmplifyClass() {
23 // Everything that is `register`ed is tracked here
24 this._components = [];
25 this._config = {};
26 // All modules (with `getModuleName()`) are stored here for dependency injection
27 this._modules = {};
28 // for backward compatibility to avoid breaking change
29 // if someone is using like Amplify.Auth
30 this.Auth = null;
31 this.Analytics = null;
32 this.API = null;
33 this.Credentials = null;
34 this.Storage = null;
35 this.I18n = null;
36 this.Cache = null;
37 this.PubSub = null;
38 this.Interactions = null;
39 this.Pushnotification = null;
40 this.UI = null;
41 this.XR = null;
42 this.Predictions = null;
43 this.DataStore = null;
44 this.Geo = null;
45 this.Notifications = null;
46 this.Logger = LoggerClass;
47 this.ServiceWorker = null;
48 }
49 AmplifyClass.prototype.register = function (comp) {
50 logger.debug('component registered in amplify', comp);
51 this._components.push(comp);
52 if (typeof comp.getModuleName === 'function') {
53 this._modules[comp.getModuleName()] = comp;
54 this[comp.getModuleName()] = comp;
55 }
56 else {
57 logger.debug('no getModuleName method for component', comp);
58 }
59 // Finally configure this new component(category) loaded
60 // With the new modularization changes in Amplify V3, all the Amplify
61 // component are not loaded/registered right away but when they are
62 // imported (and hence instantiated) in the client's app. This ensures
63 // that all new components imported get correctly configured with the
64 // configuration that Amplify.configure() was called with.
65 comp.configure(this._config);
66 };
67 AmplifyClass.prototype.configure = function (config) {
68 var _this = this;
69 if (!config)
70 return this._config;
71 this._config = Object.assign(this._config, config);
72 logger.debug('amplify config', this._config);
73 // Dependency Injection via property-setting.
74 // This avoids introducing a public method/interface/setter that's difficult to remove later.
75 // Plus, it reduces `if` statements within the `constructor` and `configure` of each module
76 Object.entries(this._modules).forEach(function (_a) {
77 var _b = __read(_a, 2), Name = _b[0], comp = _b[1];
78 // e.g. Auth.*
79 Object.keys(comp).forEach(function (property) {
80 // e.g. Auth["Credentials"] = this._modules["Credentials"] when set
81 if (_this._modules[property]) {
82 comp[property] = _this._modules[property];
83 }
84 });
85 });
86 this._components.map(function (comp) {
87 comp.configure(_this._config);
88 });
89 return this._config;
90 };
91 AmplifyClass.prototype.addPluggable = function (pluggable) {
92 if (pluggable &&
93 pluggable['getCategory'] &&
94 typeof pluggable['getCategory'] === 'function') {
95 this._components.map(function (comp) {
96 if (comp['addPluggable'] &&
97 typeof comp['addPluggable'] === 'function') {
98 comp.addPluggable(pluggable);
99 }
100 });
101 }
102 };
103 return AmplifyClass;
104}());
105export { AmplifyClass };
106export var Amplify = new AmplifyClass();