2017-12-04 12:07:41 +00:00
|
|
|
/**
|
|
|
|
* @flow
|
|
|
|
* Dynamic Links representation wrapper
|
|
|
|
*/
|
2017-12-22 15:24:31 +00:00
|
|
|
import { SharedEventEmitter } from '../../utils/events';
|
|
|
|
import ModuleBase from '../../utils/ModuleBase';
|
2018-01-05 17:20:02 +00:00
|
|
|
import { areObjectKeysContainedInOther, isObject, isString } from '../../utils';
|
|
|
|
import { getNativeModule } from '../../utils/native';
|
2017-07-19 11:32:17 +00:00
|
|
|
|
2018-01-05 17:20:02 +00:00
|
|
|
import type App from '../core/firebase-app';
|
2017-12-04 12:07:41 +00:00
|
|
|
|
2017-07-19 11:32:17 +00:00
|
|
|
const EVENT_TYPE = {
|
|
|
|
Link: 'dynamic_link_received',
|
2017-09-30 23:48:50 +00:00
|
|
|
};
|
2017-07-19 11:32:17 +00:00
|
|
|
|
2017-12-22 15:57:33 +00:00
|
|
|
const NATIVE_EVENTS = [
|
|
|
|
EVENT_TYPE.Link,
|
|
|
|
];
|
|
|
|
|
2018-01-03 20:00:38 +00:00
|
|
|
export const MODULE_NAME = 'RNFirebaseLinks';
|
|
|
|
export const NAMESPACE = 'links';
|
|
|
|
|
2017-10-10 13:22:03 +00:00
|
|
|
function validateParameters(parameters: Object): void {
|
2017-10-09 20:53:53 +00:00
|
|
|
const suportedParametersObject = {
|
2017-10-10 13:22:03 +00:00
|
|
|
dynamicLinkDomain: 'string',
|
|
|
|
link: 'string',
|
|
|
|
androidInfo: {
|
|
|
|
androidPackageName: 'string',
|
|
|
|
androidFallbackLink: 'string',
|
|
|
|
androidMinPackageVersionCode: 'string',
|
|
|
|
androidLink: 'string',
|
|
|
|
},
|
|
|
|
iosInfo: {
|
|
|
|
iosBundleId: 'string',
|
|
|
|
iosFallbackLink: 'string',
|
|
|
|
iosCustomScheme: 'string',
|
|
|
|
iosIpadFallbackLink: 'string',
|
|
|
|
iosIpadBundleId: 'string',
|
|
|
|
iosAppStoreId: 'string',
|
|
|
|
},
|
|
|
|
socialMetaTagInfo: {
|
|
|
|
socialTitle: 'string',
|
|
|
|
socialDescription: 'string',
|
|
|
|
socialImageLink: 'string',
|
2017-10-09 20:53:53 +00:00
|
|
|
},
|
|
|
|
suffix: {
|
|
|
|
option: 'string',
|
|
|
|
},
|
|
|
|
};
|
2017-10-10 13:22:03 +00:00
|
|
|
if (!areObjectKeysContainedInOther(parameters, suportedParametersObject)) {
|
|
|
|
throw new Error('Invalid Parameters.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkForMandatoryParameters(parameters: Object): void {
|
|
|
|
if (!isString(parameters.dynamicLinkDomain)) {
|
|
|
|
throw new Error('No dynamicLinkDomain was specified.');
|
|
|
|
}
|
|
|
|
if (!isString(parameters.link)) {
|
|
|
|
throw new Error('No link was specified.');
|
|
|
|
}
|
|
|
|
if (isObject(parameters.androidInfo) && !isString(parameters.androidInfo.androidPackageName)) {
|
|
|
|
throw new Error('No androidPackageName was specified.');
|
|
|
|
}
|
|
|
|
if (isObject(parameters.iosInfo) && !isString(parameters.iosInfo.iosBundleId)) {
|
|
|
|
throw new Error('No iosBundleId was specified.');
|
|
|
|
}
|
2017-10-09 20:53:53 +00:00
|
|
|
}
|
|
|
|
|
2017-07-19 11:32:17 +00:00
|
|
|
/**
|
|
|
|
* @class Links
|
|
|
|
*/
|
2017-09-30 23:48:50 +00:00
|
|
|
export default class Links extends ModuleBase {
|
2018-01-05 17:20:02 +00:00
|
|
|
constructor(app: App) {
|
|
|
|
super(app, {
|
2018-01-03 20:00:38 +00:00
|
|
|
events: NATIVE_EVENTS,
|
|
|
|
moduleName: MODULE_NAME,
|
2018-01-09 17:31:00 +00:00
|
|
|
multiApp: false,
|
2018-01-03 20:00:38 +00:00
|
|
|
namespace: NAMESPACE,
|
|
|
|
});
|
2017-07-19 11:32:17 +00:00
|
|
|
}
|
|
|
|
|
2017-12-04 12:07:41 +00:00
|
|
|
get EVENT_TYPE(): Object {
|
2017-09-30 23:48:50 +00:00
|
|
|
return EVENT_TYPE;
|
2017-07-19 11:32:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the link that triggered application open
|
2017-10-08 15:43:59 +00:00
|
|
|
* @returns {Promise.<String>}
|
2017-07-19 11:32:17 +00:00
|
|
|
*/
|
2017-12-04 12:07:41 +00:00
|
|
|
getInitialLink(): Promise<string> {
|
2018-01-05 17:20:02 +00:00
|
|
|
return getNativeModule(this).getInitialLink();
|
2017-07-19 11:32:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Subscribe to dynamic links
|
|
|
|
* @param listener
|
2017-10-08 15:43:59 +00:00
|
|
|
* @returns {Function}
|
2017-07-19 11:32:17 +00:00
|
|
|
*/
|
|
|
|
onLink(listener: Function): () => any {
|
2017-12-22 15:24:31 +00:00
|
|
|
const rnListener = SharedEventEmitter.addListener(EVENT_TYPE.Link, listener);
|
2017-09-30 23:48:50 +00:00
|
|
|
return () => rnListener.remove();
|
2017-08-20 14:57:06 +00:00
|
|
|
}
|
|
|
|
|
2017-10-08 15:43:59 +00:00
|
|
|
/**
|
|
|
|
* Create long Dynamic Link from parameters
|
|
|
|
* @param parameters
|
|
|
|
* @returns {Promise.<String>}
|
|
|
|
*/
|
2017-12-04 12:07:41 +00:00
|
|
|
createDynamicLink(parameters: Object = {}): Promise<string> {
|
2017-10-10 13:22:03 +00:00
|
|
|
try {
|
|
|
|
checkForMandatoryParameters(parameters);
|
|
|
|
validateParameters(parameters);
|
2018-01-05 17:20:02 +00:00
|
|
|
return getNativeModule(this).createDynamicLink(parameters);
|
2017-10-10 13:22:03 +00:00
|
|
|
} catch (error) {
|
|
|
|
return Promise.reject(error);
|
2017-10-09 00:42:57 +00:00
|
|
|
}
|
2017-07-19 11:32:17 +00:00
|
|
|
}
|
2017-08-20 14:57:06 +00:00
|
|
|
|
2017-10-08 15:43:59 +00:00
|
|
|
/**
|
|
|
|
* Create short Dynamic Link from parameters
|
|
|
|
* @param parameters
|
|
|
|
* @returns {Promise.<String>}
|
|
|
|
*/
|
2017-08-21 16:20:44 +00:00
|
|
|
createShortDynamicLink(parameters: Object = {}): Promise<String> {
|
2017-10-10 13:22:03 +00:00
|
|
|
try {
|
|
|
|
checkForMandatoryParameters(parameters);
|
|
|
|
validateParameters(parameters);
|
2018-01-05 17:20:02 +00:00
|
|
|
return getNativeModule(this).createShortDynamicLink(parameters);
|
2017-10-10 13:22:03 +00:00
|
|
|
} catch (error) {
|
|
|
|
return Promise.reject(error);
|
2017-10-09 00:42:57 +00:00
|
|
|
}
|
2017-08-21 16:20:44 +00:00
|
|
|
}
|
2017-07-19 11:32:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const statics = {
|
|
|
|
EVENT_TYPE,
|
2017-09-30 23:48:50 +00:00
|
|
|
};
|