UNPKG

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