From 723c709f77a01410df260af0157b0a87dc3e70aa Mon Sep 17 00:00:00 2001 From: powerful23 Date: Mon, 30 Apr 2018 16:05:43 -0700 Subject: [PATCH 1/2] minor change --- .../__tests__/Common/Amplify-test.ts | 59 +++++++++++++++++++ packages/aws-amplify/src/API/index.ts | 4 +- .../aws-amplify/src/Analytics/Analytics.ts | 2 +- packages/aws-amplify/src/Analytics/index.ts | 5 +- packages/aws-amplify/src/Auth/index.ts | 4 +- packages/aws-amplify/src/Common/Amplify.ts | 45 ++++++++++++++ packages/aws-amplify/src/Common/I18n/index.ts | 6 +- packages/aws-amplify/src/Common/index.ts | 1 + packages/aws-amplify/src/PubSub/PubSub.ts | 2 +- packages/aws-amplify/src/PubSub/index.ts | 4 +- packages/aws-amplify/src/Storage/index.ts | 4 +- packages/aws-amplify/src/index.ts | 55 +---------------- 12 files changed, 131 insertions(+), 60 deletions(-) create mode 100644 packages/aws-amplify/__tests__/Common/Amplify-test.ts create mode 100644 packages/aws-amplify/src/Common/Amplify.ts diff --git a/packages/aws-amplify/__tests__/Common/Amplify-test.ts b/packages/aws-amplify/__tests__/Common/Amplify-test.ts new file mode 100644 index 00000000000..41208ce0e01 --- /dev/null +++ b/packages/aws-amplify/__tests__/Common/Amplify-test.ts @@ -0,0 +1,59 @@ +import { Amplify } from '../../src/Common'; + +describe('Amplify config test', () => { + test('happy case', () => { + const mockComp = { + configure: jest.fn() + } + + Amplify.register(mockComp); + const res = Amplify.configure({ + attr: 'attr' + }); + + expect(mockComp.configure).toBeCalled(); + expect(res).toEqual({ attr: 'attr' }); + }); + + test('empty config', () => { + const mockComp = { + configure: jest.fn() + } + + Amplify.register(mockComp); + const res = Amplify.configure(null); + + expect(mockComp.configure).not.toBeCalled(); + }); +}); + +describe('addPluggable test', () => { + test('happy case', () => { + const pluggable = { + getCategory: jest.fn() + } + + const mockComp = { + addPluggable: jest.fn() + } + + Amplify.register(mockComp); + Amplify.addPluggable(pluggable); + + expect(mockComp.addPluggable).toBeCalled(); + }); + + test('no pluggable', () => { + const pluggable = { + getCategory: jest.fn() + } + + const mockComp = { + addPluggable: jest.fn() + } + + Amplify.addPluggable({}); + + expect(mockComp.addPluggable).not.toBeCalled(); + }); +}); \ No newline at end of file diff --git a/packages/aws-amplify/src/API/index.ts b/packages/aws-amplify/src/API/index.ts index 925f43ae663..98ba60efeef 100644 --- a/packages/aws-amplify/src/API/index.ts +++ b/packages/aws-amplify/src/API/index.ts @@ -13,7 +13,7 @@ import APIClass, { graphqlOperation } from './API'; -import { ConsoleLogger as Logger } from '../Common'; +import { ConsoleLogger as Logger, Amplify } from '../Common'; const logger = new Logger('API'); @@ -25,5 +25,7 @@ if (!_instance) { } const API = _instance; +Amplify.register(API); + export default API; export { APIClass, graphqlOperation }; diff --git a/packages/aws-amplify/src/Analytics/Analytics.ts b/packages/aws-amplify/src/Analytics/Analytics.ts index 9b7166a7847..43f7cbcf620 100644 --- a/packages/aws-amplify/src/Analytics/Analytics.ts +++ b/packages/aws-amplify/src/Analytics/Analytics.ts @@ -102,7 +102,7 @@ export default class AnalyticsClass { const ensureCredentails = await this._getCredentials(); if (!ensureCredentails) return Promise.resolve(false); - if (pluggable) { + if (pluggable && pluggable.getCategory() === 'Analytics') { this._pluggables.push(pluggable); // pluggable.configure(this._config); const config = pluggable.configure(this._config); diff --git a/packages/aws-amplify/src/Analytics/index.ts b/packages/aws-amplify/src/Analytics/index.ts index 906618c835e..c5c5fca3e2e 100644 --- a/packages/aws-amplify/src/Analytics/index.ts +++ b/packages/aws-amplify/src/Analytics/index.ts @@ -18,7 +18,8 @@ import { ConsoleLogger as Logger, Hub, Linking, - AppState + AppState, + Amplify } from '../Common'; import Platform from '../Common/Platform'; @@ -33,6 +34,8 @@ if (!_instance) { } const Analytics = _instance; +Amplify.register(Analytics); + export default Analytics; export { AnalyticsProvider }; export { AnalyticsClass }; diff --git a/packages/aws-amplify/src/Auth/index.ts b/packages/aws-amplify/src/Auth/index.ts index ce0d9944dde..1c37fd70128 100644 --- a/packages/aws-amplify/src/Auth/index.ts +++ b/packages/aws-amplify/src/Auth/index.ts @@ -13,7 +13,7 @@ import AuthClass from './Auth'; -import { ConsoleLogger as Logger } from '../Common'; +import { ConsoleLogger as Logger, Amplify } from '../Common'; const logger = new Logger('Auth'); @@ -25,5 +25,7 @@ if (!_instance) { } const Auth = _instance; +Amplify.register(Auth); + export default Auth; export { AuthClass }; diff --git a/packages/aws-amplify/src/Common/Amplify.ts b/packages/aws-amplify/src/Common/Amplify.ts new file mode 100644 index 00000000000..0a9ca95d3b3 --- /dev/null +++ b/packages/aws-amplify/src/Common/Amplify.ts @@ -0,0 +1,45 @@ +import { ConsoleLogger as Logger } from './Logger'; + +const logger = new Logger('Amplify'); + +export default class Amplify { + private static _components = []; + private static _config = {}; + + static Auth = null; + static Analytics = null; + static API = null; + static Storage = null; + static I18n = null; + static Cache = null; + static PubSub = null; + + static Logger = null; + + static register(comp) { + logger.debug('component registed in amplify', comp); + this._components.push(comp); + } + + static configure(config) { + if (!config) return this._config; + + this._config = Object.assign(this._config, config); + logger.debug('amplify config', this._config); + this._components.map(comp => { + comp.configure(this._config); + }); + + return this._config; + } + + static addPluggable(pluggable) { + if (pluggable && pluggable['getCategory'] && typeof pluggable['getCategory'] === 'function') { + this._components.map(comp => { + if (comp['addPluggable'] && typeof comp['addPluggable'] === 'function') { + comp.addPluggable(pluggable); + } + }); + } + } +} diff --git a/packages/aws-amplify/src/Common/I18n/index.ts b/packages/aws-amplify/src/Common/I18n/index.ts index 67a7c733fd6..2a2bbdcff60 100644 --- a/packages/aws-amplify/src/Common/I18n/index.ts +++ b/packages/aws-amplify/src/Common/I18n/index.ts @@ -14,6 +14,7 @@ import { I18n as I18nClass } from './I18n'; import { ConsoleLogger as Logger } from '../Logger'; +import Amplify from '../Amplify'; const logger = new Logger('I18n'); @@ -23,7 +24,7 @@ let _i18n = null; /** * Export I18n APIs */ -export default class I18n { +class I18n { /** * @static * @method @@ -107,3 +108,6 @@ export default class I18n { return true; } } + +Amplify.register(I18n); +export default I18n; diff --git a/packages/aws-amplify/src/Common/index.ts b/packages/aws-amplify/src/Common/index.ts index a8302468bca..27a7526e59e 100644 --- a/packages/aws-amplify/src/Common/index.ts +++ b/packages/aws-amplify/src/Common/index.ts @@ -24,6 +24,7 @@ export { default as JS } from './JS'; export { default as Signer } from './Signer'; export { default as Parser } from './Parser'; export { FacebookOAuth, GoogleOAuth } from './OAuthHelper'; +export { default as Amplify } from './Amplify'; export * from './RNComponents'; import Platform from './Platform'; diff --git a/packages/aws-amplify/src/PubSub/PubSub.ts b/packages/aws-amplify/src/PubSub/PubSub.ts index 21df1ec0282..706492589d0 100644 --- a/packages/aws-amplify/src/PubSub/PubSub.ts +++ b/packages/aws-amplify/src/PubSub/PubSub.ts @@ -64,7 +64,7 @@ export default class PubSub { * @param {Object} pluggable - an instance of the plugin */ public async addPluggable(pluggable: PubSubProvider) { - if (pluggable) { + if (pluggable && pluggable.getCategory() === 'PubSub') { this._pluggables.push(pluggable); const config = pluggable.configure(this._options); diff --git a/packages/aws-amplify/src/PubSub/index.ts b/packages/aws-amplify/src/PubSub/index.ts index b4230742174..3c51091804f 100644 --- a/packages/aws-amplify/src/PubSub/index.ts +++ b/packages/aws-amplify/src/PubSub/index.ts @@ -12,7 +12,7 @@ */ import PubSubClass from './PubSub'; -import { ConsoleLogger as Logger } from '../Common'; +import { ConsoleLogger as Logger, Amplify } from '../Common'; const logger = new Logger('PubSub'); @@ -24,6 +24,8 @@ if (!_instance) { } const PubSub = _instance; +Amplify.register(PubSub); + export default PubSub; export * from './Providers/AWSIotProvider'; diff --git a/packages/aws-amplify/src/Storage/index.ts b/packages/aws-amplify/src/Storage/index.ts index 1d9e9e47a3d..c36670eeb5c 100644 --- a/packages/aws-amplify/src/Storage/index.ts +++ b/packages/aws-amplify/src/Storage/index.ts @@ -13,7 +13,7 @@ import StorageClass from './Storage'; -import { ConsoleLogger as Logger } from '../Common'; +import { ConsoleLogger as Logger, Amplify } from '../Common'; const logger = new Logger('Storage'); @@ -35,5 +35,7 @@ if (!_instance) { } const Storage = _instance; +Amplify.register(Storage); + export default Storage; export { StorageClass }; diff --git a/packages/aws-amplify/src/index.ts b/packages/aws-amplify/src/index.ts index 060b7ec785e..8d0ab41ec91 100644 --- a/packages/aws-amplify/src/index.ts +++ b/packages/aws-amplify/src/index.ts @@ -23,59 +23,11 @@ import { JS, ClientDevice, Signer, - I18n + I18n, + Amplify } from './Common'; -const logger = new Logger('Amplify'); - -export default class Amplify { - static Auth: AuthClass = null; - static Analytics: AnalyticsClass = null; - static API: APIClass = null; - static Storage: StorageClass = null; - static I18n = null; - static Cache = null; - static PubSub = null; - - static Logger = null; - - static configure(config) { - if (!config) { return; } - Auth.configure(config); - I18n.configure(config); - Analytics.configure(config); - API.configure(config); - Storage.configure(config); - Cache.configure(config); - PubSub.configure(config); - - return config; - } - - static addPluggable(pluggable) { - if (pluggable && pluggable['getCategory'] && typeof pluggable['getCategory'] === 'function') { - const category = pluggable.getCategory(); - switch (category) { - case 'Analytics': - Analytics.addPluggable(pluggable); - break; - case 'Auth': - break; - case 'API': - break; - case 'Cache': - break; - case 'Storage': - break; - case 'PubSub': - PubSub.addPluggable(pluggable); - break; - default: - break; - } - } - } -} +export default Amplify; Amplify.Auth = Auth; Amplify.Analytics = Analytics; @@ -84,7 +36,6 @@ Amplify.Storage = Storage; Amplify.I18n = I18n; Amplify.Cache = Cache; Amplify.PubSub = PubSub; - Amplify.Logger = Logger; export { Auth, Analytics, Storage, API, PubSub, I18n, Logger, Hub, Cache, JS, ClientDevice, Signer }; From 2869d4628e946f60d065ce14c983902da0e8b8c3 Mon Sep 17 00:00:00 2001 From: powerful23 Date: Wed, 2 May 2018 12:56:09 -0700 Subject: [PATCH 2/2] minor change --- packages/aws-amplify/src/Common/Amplify.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/aws-amplify/src/Common/Amplify.ts b/packages/aws-amplify/src/Common/Amplify.ts index 0a9ca95d3b3..fbdb04b9056 100644 --- a/packages/aws-amplify/src/Common/Amplify.ts +++ b/packages/aws-amplify/src/Common/Amplify.ts @@ -6,6 +6,8 @@ export default class Amplify { private static _components = []; private static _config = {}; + // for backward compatibility to avoid breaking change + // if someone is using like Amplify.Auth static Auth = null; static Analytics = null; static API = null;