UNPKG

4.16 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};
17import { ConsoleLogger as LoggerClass } from './Logger';
18var logger = new LoggerClass('Amplify');
19var AmplifyClass = /** @class */ (function () {
20 function AmplifyClass() {
21 // Everything that is `register`ed is tracked here
22 this._components = [];
23 this._config = {};
24 // All modules (with `getModuleName()`) are stored here for dependency injection
25 this._modules = {};
26 // for backward compatibility to avoid breaking change
27 // if someone is using like Amplify.Auth
28 this.Auth = null;
29 this.Analytics = null;
30 this.API = null;
31 this.Credentials = null;
32 this.Storage = null;
33 this.I18n = null;
34 this.Cache = null;
35 this.PubSub = null;
36 this.Interactions = null;
37 this.Pushnotification = null;
38 this.UI = null;
39 this.XR = null;
40 this.Predictions = null;
41 this.DataStore = null;
42 this.Logger = LoggerClass;
43 this.ServiceWorker = null;
44 }
45 AmplifyClass.prototype.register = function (comp) {
46 logger.debug('component registered in amplify', comp);
47 this._components.push(comp);
48 if (typeof comp.getModuleName === 'function') {
49 this._modules[comp.getModuleName()] = comp;
50 this[comp.getModuleName()] = comp;
51 }
52 else {
53 logger.debug('no getModuleName method for component', comp);
54 }
55 // Finally configure this new component(category) loaded
56 // With the new modularization changes in Amplify V3, all the Amplify
57 // component are not loaded/registered right away but when they are
58 // imported (and hence instantiated) in the client's app. This ensures
59 // that all new components imported get correctly configured with the
60 // configuration that Amplify.configure() was called with.
61 comp.configure(this._config);
62 };
63 AmplifyClass.prototype.configure = function (config) {
64 var _this = this;
65 if (!config)
66 return this._config;
67 this._config = Object.assign(this._config, config);
68 logger.debug('amplify config', this._config);
69 // Dependency Injection via property-setting.
70 // This avoids introducing a public method/interface/setter that's difficult to remove later.
71 // Plus, it reduces `if` statements within the `constructor` and `configure` of each module
72 Object.entries(this._modules).forEach(function (_a) {
73 var _b = __read(_a, 2), Name = _b[0], comp = _b[1];
74 // e.g. Auth.*
75 Object.keys(comp).forEach(function (property) {
76 // e.g. Auth["Credentials"] = this._modules["Credentials"] when set
77 if (_this._modules[property]) {
78 comp[property] = _this._modules[property];
79 }
80 });
81 });
82 this._components.map(function (comp) {
83 comp.configure(_this._config);
84 });
85 return this._config;
86 };
87 AmplifyClass.prototype.addPluggable = function (pluggable) {
88 if (pluggable &&
89 pluggable['getCategory'] &&
90 typeof pluggable['getCategory'] === 'function') {
91 this._components.map(function (comp) {
92 if (comp['addPluggable'] &&
93 typeof comp['addPluggable'] === 'function') {
94 comp.addPluggable(pluggable);
95 }
96 });
97 }
98 };
99 return AmplifyClass;
100}());
101export { AmplifyClass };
102export var Amplify = new AmplifyClass();
103/**
104 * @deprecated use named import
105 */
106export default Amplify;
107//# sourceMappingURL=Amplify.js.map
\No newline at end of file