mirror of
https://github.com/zhigang1992/react-native-firebase.git
synced 2026-04-25 13:04:52 +08:00
@@ -13,6 +13,7 @@ import Storage, { statics as StorageStatics } from './modules/storage';
|
||||
import Database, { statics as DatabaseStatics } from './modules/database';
|
||||
import Messaging, { statics as MessagingStatics } from './modules/messaging';
|
||||
import Firestore, { statics as FirestoreStatics } from './modules/firestore';
|
||||
import Links, { statics as LinksStatics } from './modules/links';
|
||||
import Utils, { statics as UtilsStatics } from './modules/utils';
|
||||
|
||||
const FirebaseCoreModule = NativeModules.RNFirebase;
|
||||
@@ -35,6 +36,7 @@ export default class FirebaseApp {
|
||||
this.crash = this._staticsOrModuleInstance({}, Crash);
|
||||
this.database = this._staticsOrModuleInstance(DatabaseStatics, Database);
|
||||
this.firestore = this._staticsOrModuleInstance(FirestoreStatics, Firestore);
|
||||
this.links = this._staticsOrModuleInstance(LinksStatics, Links);
|
||||
this.messaging = this._staticsOrModuleInstance(MessagingStatics, Messaging);
|
||||
this.perf = this._staticsOrModuleInstance({}, Performance);
|
||||
this.storage = this._staticsOrModuleInstance(StorageStatics, Storage);
|
||||
|
||||
@@ -14,6 +14,7 @@ import Auth, { statics as AuthStatics } from './modules/auth';
|
||||
import Analytics from './modules/analytics';
|
||||
import Crash from './modules/crash';
|
||||
import Performance from './modules/perf';
|
||||
import Links, { statics as LinksStatics } from './modules/links';
|
||||
import RemoteConfig from './modules/config';
|
||||
import Storage, { statics as StorageStatics } from './modules/storage';
|
||||
import Database, { statics as DatabaseStatics } from './modules/database';
|
||||
@@ -42,6 +43,7 @@ class FirebaseCore {
|
||||
this.crash = this._appNamespaceOrStatics({}, Crash);
|
||||
this.database = this._appNamespaceOrStatics(DatabaseStatics, Database);
|
||||
this.firestore = this._appNamespaceOrStatics(FirestoreStatics, Firestore);
|
||||
this.links = this._appNamespaceOrStatics(LinksStatics, Links);
|
||||
this.messaging = this._appNamespaceOrStatics(MessagingStatics, Messaging);
|
||||
this.perf = this._appNamespaceOrStatics(DatabaseStatics, Performance);
|
||||
this.storage = this._appNamespaceOrStatics(StorageStatics, Storage);
|
||||
|
||||
121
lib/modules/links/index.js
Normal file
121
lib/modules/links/index.js
Normal file
@@ -0,0 +1,121 @@
|
||||
import ModuleBase from './../../utils/ModuleBase';
|
||||
import { areObjectKeysContainedInOther, isObject, isString } from './../../utils';
|
||||
|
||||
const EVENT_TYPE = {
|
||||
Link: 'dynamic_link_received',
|
||||
};
|
||||
|
||||
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
|
||||
*/
|
||||
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() {
|
||||
return EVENT_TYPE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the link that triggered application open
|
||||
* @returns {Promise.<String>}
|
||||
*/
|
||||
getInitialLink() {
|
||||
return this._native.getInitialLink();
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscribe to dynamic links
|
||||
* @param listener
|
||||
* @returns {Function}
|
||||
*/
|
||||
onLink(listener: Function): () => any {
|
||||
const rnListener = this._eventEmitter.addListener(EVENT_TYPE.Link, listener);
|
||||
return () => rnListener.remove();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create long Dynamic Link from parameters
|
||||
* @param parameters
|
||||
* @returns {Promise.<String>}
|
||||
*/
|
||||
createDynamicLink(parameters: Object = {}): Promise<String> {
|
||||
try {
|
||||
checkForMandatoryParameters(parameters);
|
||||
validateParameters(parameters);
|
||||
return this._native.createDynamicLink(parameters);
|
||||
} catch (error) {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create short Dynamic Link from parameters
|
||||
* @param parameters
|
||||
* @returns {Promise.<String>}
|
||||
*/
|
||||
createShortDynamicLink(parameters: Object = {}): Promise<String> {
|
||||
try {
|
||||
checkForMandatoryParameters(parameters);
|
||||
validateParameters(parameters);
|
||||
return this._native.createShortDynamicLink(parameters);
|
||||
} catch (error) {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const statics = {
|
||||
EVENT_TYPE,
|
||||
};
|
||||
@@ -70,6 +70,43 @@ export function deepExists(object: Object,
|
||||
return tmp !== undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deep Check if obj1 keys are contained in obj2
|
||||
* @param obj1
|
||||
* @param obj2
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export function areObjectKeysContainedInOther(obj1 : Object, obj2: Object): boolean {
|
||||
if (!isObject(obj1) || !isObject(obj2)) {
|
||||
return false;
|
||||
}
|
||||
const keys1 = Object.keys(obj1);
|
||||
const keys2 = Object.keys(obj2);
|
||||
if (isArrayContainedInOther(keys1, keys2)) {
|
||||
return keys1.filter((key) => {
|
||||
return isObject(obj1[key]);
|
||||
}).reduce((acc, cur) => {
|
||||
return acc && areObjectKeysContainedInOther(obj1[cur], obj2[cur]);
|
||||
}, true);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if arr1 is contained in arr2
|
||||
* @param arr1
|
||||
* @param arr2
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export function isArrayContainedInOther(arr1: Array, arr2: Array): boolean {
|
||||
if (!Array.isArray(arr1) || !Array.isArray(arr2)) {
|
||||
return false;
|
||||
}
|
||||
return arr1.reduce((acc, cur) => {
|
||||
return acc && arr2.includes(cur);
|
||||
}, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple is object check.
|
||||
* @param item
|
||||
|
||||
Reference in New Issue
Block a user