move invalid parameters validation to javascript and remove validation from android
This commit is contained in:
parent
29ebbbcc00
commit
a085733e03
@ -196,23 +196,10 @@ public class RNFirebaseLinks extends ReactContextBaseJavaModule implements Activ
|
||||
if (dynamicLinkInfo != null) {
|
||||
try {
|
||||
parametersBuilder.setLink(Uri.parse((String) dynamicLinkInfo.get("link")));
|
||||
dynamicLinkInfo.remove("link");
|
||||
|
||||
parametersBuilder.setDynamicLinkDomain((String) dynamicLinkInfo.get("dynamicLinkDomain"));
|
||||
dynamicLinkInfo.remove("dynamicLinkDomain");
|
||||
|
||||
setAndroidParameters(dynamicLinkInfo, parametersBuilder);
|
||||
dynamicLinkInfo.remove("androidInfo");
|
||||
|
||||
setIosParameters(dynamicLinkInfo, parametersBuilder);
|
||||
dynamicLinkInfo.remove("iosInfo");
|
||||
|
||||
setSocialMetaTagParameters(dynamicLinkInfo, parametersBuilder);
|
||||
dynamicLinkInfo.remove("socialMetaTagInfo");
|
||||
|
||||
if (dynamicLinkInfo.size() > 0) {
|
||||
throw new IllegalArgumentException("Invalid arguments: " + dynamicLinkInfo.keySet().toString());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "error while building parameters " + e.getMessage());
|
||||
throw e;
|
||||
@ -244,19 +231,12 @@ public class RNFirebaseLinks extends ReactContextBaseJavaModule implements Activ
|
||||
|
||||
DynamicLink.AndroidParameters.Builder androidParametersBuilder =
|
||||
new DynamicLink.AndroidParameters.Builder((String) androidParameters.get("androidPackageName"));
|
||||
androidParameters.remove("androidPackageName");
|
||||
|
||||
if (androidParameters.containsKey("androidFallbackLink")) {
|
||||
androidParametersBuilder.setFallbackUrl(Uri.parse((String) androidParameters.get("androidFallbackLink")));
|
||||
androidParameters.remove("androidFallbackLink");
|
||||
}
|
||||
if (androidParameters.containsKey("androidMinPackageVersionCode")) {
|
||||
androidParametersBuilder.setMinimumVersion(Integer.parseInt((String) androidParameters.get("androidMinPackageVersionCode")));
|
||||
androidParameters.remove("androidMinPackageVersionCode");
|
||||
}
|
||||
|
||||
if (androidParameters.size() > 0) {
|
||||
throw new IllegalArgumentException("Invalid arguments: " + androidParameters.keySet().toString());
|
||||
}
|
||||
parametersBuilder.setAndroidParameters(androidParametersBuilder.build());
|
||||
}
|
||||
@ -270,35 +250,24 @@ public class RNFirebaseLinks extends ReactContextBaseJavaModule implements Activ
|
||||
}
|
||||
DynamicLink.IosParameters.Builder iosParametersBuilder =
|
||||
new DynamicLink.IosParameters.Builder((String) iosParameters.get("iosBundleId"));
|
||||
iosParameters.remove("iosBundleId");
|
||||
|
||||
if (iosParameters.containsKey("iosAppStoreId")) {
|
||||
iosParametersBuilder.setAppStoreId((String) iosParameters.get("iosAppStoreId"));
|
||||
iosParameters.remove("iosAppStoreId");
|
||||
}
|
||||
if (iosParameters.containsKey("iosCustomScheme")) {
|
||||
iosParametersBuilder.setCustomScheme((String) iosParameters.get("iosCustomScheme"));
|
||||
iosParameters.remove("iosCustomScheme");
|
||||
}
|
||||
if (iosParameters.containsKey("iosFallbackLink")) {
|
||||
iosParametersBuilder.setFallbackUrl(Uri.parse((String) iosParameters.get("iosFallbackLink")));
|
||||
iosParameters.remove("iosFallbackLink");
|
||||
}
|
||||
if (iosParameters.containsKey("iosIpadBundleId")) {
|
||||
iosParametersBuilder.setIpadBundleId((String) iosParameters.get("iosIpadBundleId"));
|
||||
iosParameters.remove("iosIpadBundleId");
|
||||
}
|
||||
if (iosParameters.containsKey("iosIpadFallbackLink")) {
|
||||
iosParametersBuilder.setIpadFallbackUrl(Uri.parse((String) iosParameters.get("iosIpadFallbackLink")));
|
||||
iosParameters.remove("iosIpadFallbackLink");
|
||||
}
|
||||
if (iosParameters.containsKey("iosMinPackageVersionCode")) {
|
||||
iosParametersBuilder.setMinimumVersion((String) iosParameters.get("iosMinPackageVersionCode"));
|
||||
iosParameters.remove("iosMinPackageVersionCode");
|
||||
}
|
||||
|
||||
if (iosParameters.size() > 0) {
|
||||
throw new IllegalArgumentException("Invalid arguments: " + iosParameters.keySet().toString());
|
||||
}
|
||||
parametersBuilder.setIosParameters(iosParametersBuilder.build());
|
||||
}
|
||||
@ -312,19 +281,12 @@ public class RNFirebaseLinks extends ReactContextBaseJavaModule implements Activ
|
||||
|
||||
if (socialMetaTagParameters.containsKey("socialDescription")) {
|
||||
socialMetaTagParametersBuilder.setDescription((String) socialMetaTagParameters.get("socialDescription"));
|
||||
socialMetaTagParameters.remove("socialDescription");
|
||||
}
|
||||
if (socialMetaTagParameters.containsKey("socialImageLink")) {
|
||||
socialMetaTagParametersBuilder.setImageUrl(Uri.parse((String) socialMetaTagParameters.get("socialImageLink")));
|
||||
socialMetaTagParameters.remove("socialImageLink");
|
||||
}
|
||||
if (socialMetaTagParameters.containsKey("socialTitle")) {
|
||||
socialMetaTagParametersBuilder.setTitle((String) socialMetaTagParameters.get("socialTitle"));
|
||||
socialMetaTagParameters.remove("socialTitle");
|
||||
}
|
||||
|
||||
if (socialMetaTagParameters.size() > 0) {
|
||||
throw new IllegalArgumentException("Invalid arguments: " + socialMetaTagParameters.keySet().toString());
|
||||
}
|
||||
parametersBuilder.setSocialMetaTagParameters(socialMetaTagParametersBuilder.build());
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
import ModuleBase from './../../utils/ModuleBase';
|
||||
import { areObjectKeysContainedInOther } from './../../utils';
|
||||
|
||||
const EVENT_TYPE = {
|
||||
Link: 'dynamic_link_received',
|
||||
@ -43,6 +44,9 @@ export default class Links extends ModuleBase {
|
||||
* @returns {Promise.<String>}
|
||||
*/
|
||||
createDynamicLink(parameters: Object = {}): Promise<String> {
|
||||
if (!this._validateParameters(parameters)) {
|
||||
return Promise.reject(new Error('Invalid Parameters.'));
|
||||
}
|
||||
return this._native.createDynamicLink(parameters);
|
||||
}
|
||||
|
||||
@ -52,8 +56,43 @@ export default class Links extends ModuleBase {
|
||||
* @returns {Promise.<String>}
|
||||
*/
|
||||
createShortDynamicLink(parameters: Object = {}): Promise<String> {
|
||||
if (!this._validateParameters(parameters)) {
|
||||
return Promise.reject(new Error('Invalid Parameters.'));
|
||||
}
|
||||
return this._native.createShortDynamicLink(parameters);
|
||||
}
|
||||
|
||||
_validateParameters(parameters: Object): boolean {
|
||||
const suportedParametersObject = {
|
||||
dynamicLinkInfo: {
|
||||
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',
|
||||
},
|
||||
};
|
||||
return areObjectKeysContainedInOther(parameters, suportedParametersObject);
|
||||
}
|
||||
}
|
||||
|
||||
export const statics = {
|
||||
|
@ -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
|
||||
|
@ -101,9 +101,8 @@ function linksTests({ describe, it, firebase, tryCatch }) {
|
||||
reject(new Error('createDynamicLink did not fail.'));
|
||||
}, reject);
|
||||
|
||||
const failure = tryCatch((error) => {
|
||||
const failure = tryCatch(() => {
|
||||
// Assertion
|
||||
error.code.includes('links/failure').should.be.true();
|
||||
resolve();
|
||||
}, reject);
|
||||
|
||||
@ -126,7 +125,7 @@ function linksTests({ describe, it, firebase, tryCatch }) {
|
||||
|
||||
const failure = tryCatch((error) => {
|
||||
// Assertion
|
||||
error.code.includes('links/failure').should.be.true();
|
||||
error.message.should.equal('no iosBundleId was specified.');
|
||||
resolve();
|
||||
}, reject);
|
||||
|
||||
@ -172,7 +171,7 @@ function linksTests({ describe, it, firebase, tryCatch }) {
|
||||
|
||||
const failure = tryCatch((error) => {
|
||||
// Assertion
|
||||
error.code.includes('links/failure').should.be.true();
|
||||
error.message.should.equal('no androidPackageName was specified.');
|
||||
resolve();
|
||||
}, reject);
|
||||
|
||||
@ -218,7 +217,7 @@ function linksTests({ describe, it, firebase, tryCatch }) {
|
||||
|
||||
const failure = tryCatch((error) => {
|
||||
// Assertion
|
||||
error.code.includes('links/failure').should.be.true();
|
||||
error.message.should.equal('Invalid Parameters.');
|
||||
resolve();
|
||||
}, reject);
|
||||
|
||||
@ -247,7 +246,7 @@ function linksTests({ describe, it, firebase, tryCatch }) {
|
||||
|
||||
const failure = tryCatch((error) => {
|
||||
// Assertion
|
||||
error.code.includes('links/failure').should.be.true();
|
||||
error.message.should.equal('Invalid Parameters.');
|
||||
resolve();
|
||||
}, reject);
|
||||
|
||||
@ -283,7 +282,7 @@ function linksTests({ describe, it, firebase, tryCatch }) {
|
||||
|
||||
const failure = tryCatch((error) => {
|
||||
// Assertion
|
||||
error.code.includes('links/failure').should.be.true();
|
||||
error.message.should.equal('Invalid Parameters.');
|
||||
resolve();
|
||||
}, reject);
|
||||
|
||||
@ -319,7 +318,7 @@ function linksTests({ describe, it, firebase, tryCatch }) {
|
||||
|
||||
const failure = tryCatch((error) => {
|
||||
// Assertion
|
||||
error.code.includes('links/failure').should.be.true();
|
||||
error.message.should.equal('Invalid Parameters.');
|
||||
resolve();
|
||||
}, reject);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user