UNPKG

4.18 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.Geo = null;
43 this.Logger = LoggerClass;
44 this.ServiceWorker = null;
45 }
46 AmplifyClass.prototype.register = function (comp) {
47 logger.debug('component registered in amplify', comp);
48 this._components.push(comp);
49 if (typeof comp.getModuleName === 'function') {
50 this._modules[comp.getModuleName()] = comp;
51 this[comp.getModuleName()] = comp;
52 }
53 else {
54 logger.debug('no getModuleName method for component', comp);
55 }
56 // Finally configure this new component(category) loaded
57 // With the new modularization changes in Amplify V3, all the Amplify
58 // component are not loaded/registered right away but when they are
59 // imported (and hence instantiated) in the client's app. This ensures
60 // that all new components imported get correctly configured with the
61 // configuration that Amplify.configure() was called with.
62 comp.configure(this._config);
63 };
64 AmplifyClass.prototype.configure = function (config) {
65 var _this = this;
66 if (!config)
67 return this._config;
68 this._config = Object.assign(this._config, config);
69 logger.debug('amplify config', this._config);
70 // Dependency Injection via property-setting.
71 // This avoids introducing a public method/interface/setter that's difficult to remove later.
72 // Plus, it reduces `if` statements within the `constructor` and `configure` of each module
73 Object.entries(this._modules).forEach(function (_a) {
74 var _b = __read(_a, 2), Name = _b[0], comp = _b[1];
75 // e.g. Auth.*
76 Object.keys(comp).forEach(function (property) {
77 // e.g. Auth["Credentials"] = this._modules["Credentials"] when set
78 if (_this._modules[property]) {
79 comp[property] = _this._modules[property];
80 }
81 });
82 });
83 this._components.map(function (comp) {
84 comp.configure(_this._config);
85 });
86 return this._config;
87 };
88 AmplifyClass.prototype.addPluggable = function (pluggable) {
89 if (pluggable &&
90 pluggable['getCategory'] &&
91 typeof pluggable['getCategory'] === 'function') {
92 this._components.map(function (comp) {
93 if (comp['addPluggable'] &&
94 typeof comp['addPluggable'] === 'function') {
95 comp.addPluggable(pluggable);
96 }
97 });
98 }
99 };
100 return AmplifyClass;
101}());
102export { AmplifyClass };
103export var Amplify = new AmplifyClass();
104/**
105 * @deprecated use named import
106 */
107export default Amplify;
108//# sourceMappingURL=Amplify.js.map
\No newline at end of file