react-native-firebase/lib/modules/links/index.js

122 lines
3.1 KiB
JavaScript
Raw Normal View History

2017-09-30 23:48:50 +00:00
import ModuleBase from './../../utils/ModuleBase';
import { areObjectKeysContainedInOther, isObject, isString } from './../../utils';
const EVENT_TYPE = {
Link: 'dynamic_link_received',
2017-09-30 23:48:50 +00:00
};
function validateParameters(parameters: Object): void {
const suportedParametersObject = {
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',
},
suffix: {
option: 'string',
},
};
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.');
}
}
/**
* @class Links
*/
2017-09-30 23:48:50 +00:00
export default class Links extends ModuleBase {
static _NAMESPACE = 'links';
static _NATIVE_MODULE = 'RNFirebaseLinks';
constructor(firebaseApp: Object, options: Object = {}) {
super(firebaseApp, options, true);
}
get EVENT_TYPE() {
2017-09-30 23:48:50 +00:00
return EVENT_TYPE;
}
/**
* Returns the link that triggered application open
2017-10-08 15:43:59 +00:00
* @returns {Promise.<String>}
*/
getInitialLink() {
2017-09-30 23:48:50 +00:00
return this._native.getInitialLink();
}
/**
* Subscribe to dynamic links
* @param listener
2017-10-08 15:43:59 +00:00
* @returns {Function}
*/
onLink(listener: Function): () => any {
2017-09-30 23:48:50 +00:00
const rnListener = this._eventEmitter.addListener(EVENT_TYPE.Link, listener);
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-08-21 16:20:44 +00:00
createDynamicLink(parameters: Object = {}): Promise<String> {
try {
checkForMandatoryParameters(parameters);
validateParameters(parameters);
return this._native.createDynamicLink(parameters);
} catch (error) {
return Promise.reject(error);
}
}
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> {
try {
checkForMandatoryParameters(parameters);
validateParameters(parameters);
return this._native.createShortDynamicLink(parameters);
} catch (error) {
return Promise.reject(error);
}
2017-08-21 16:20:44 +00:00
}
}
export const statics = {
EVENT_TYPE,
2017-09-30 23:48:50 +00:00
};