Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move amplify config into common module #763

Merged
merged 4 commits into from
May 7, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions packages/aws-amplify/__tests__/Common/Amplify-test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
4 changes: 3 additions & 1 deletion packages/aws-amplify/src/API/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -25,5 +25,7 @@ if (!_instance) {
}

const API = _instance;
Amplify.register(API);

export default API;
export { APIClass, graphqlOperation };
2 changes: 1 addition & 1 deletion packages/aws-amplify/src/Analytics/Analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion packages/aws-amplify/src/Analytics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import {
ConsoleLogger as Logger,
Hub,
Linking,
AppState
AppState,
Amplify
} from '../Common';
import Platform from '../Common/Platform';

Expand All @@ -33,6 +34,8 @@ if (!_instance) {
}

const Analytics = _instance;
Amplify.register(Analytics);

export default Analytics;
export { AnalyticsProvider };
export { AnalyticsClass };
Expand Down
4 changes: 3 additions & 1 deletion packages/aws-amplify/src/Auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -25,5 +25,7 @@ if (!_instance) {
}

const Auth = _instance;
Amplify.register(Auth);

export default Auth;
export { AuthClass };
47 changes: 47 additions & 0 deletions packages/aws-amplify/src/Common/Amplify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { ConsoleLogger as Logger } from './Logger';

const logger = new Logger('Amplify');

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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are any of this being used? I think component should have a category so it can be attached.

Copy link
Contributor Author

@powerful23 powerful23 May 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those are just for backward compatibility since they will be assigned in aws-amplify/index.ts and developers may call

import Amplify from 'aws-amplify';

Amplify.Auth.signIn()

instead of

import { Auth } from 'aws-amplify';

Auth.signIn()

Will add some comments on that.

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 => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is checking the category for the component to be the same for the plugin?

Copy link
Contributor Author

@powerful23 powerful23 May 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be checked in each category's addPluggable() method.

if (comp['addPluggable'] && typeof comp['addPluggable'] === 'function') {
comp.addPluggable(pluggable);
}
});
}
}
}
6 changes: 5 additions & 1 deletion packages/aws-amplify/src/Common/I18n/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -23,7 +24,7 @@ let _i18n = null;
/**
* Export I18n APIs
*/
export default class I18n {
class I18n {
/**
* @static
* @method
Expand Down Expand Up @@ -107,3 +108,6 @@ export default class I18n {
return true;
}
}

Amplify.register(I18n);
export default I18n;
1 change: 1 addition & 0 deletions packages/aws-amplify/src/Common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion packages/aws-amplify/src/PubSub/PubSub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion packages/aws-amplify/src/PubSub/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -24,6 +24,8 @@ if (!_instance) {
}

const PubSub = _instance;
Amplify.register(PubSub);

export default PubSub;

export * from './Providers/AWSIotProvider';
4 changes: 3 additions & 1 deletion packages/aws-amplify/src/Storage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -35,5 +35,7 @@ if (!_instance) {
}

const Storage = _instance;
Amplify.register(Storage);

export default Storage;
export { StorageClass };
55 changes: 3 additions & 52 deletions packages/aws-amplify/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 };
Expand Down