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

[SDK-2326] Add setAPIUrl to the JS layer #967

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ def safeExtGet(prop, fallback) {
dependencies {
implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0'
implementation 'com.facebook.react:react-native:+' // From node_modules
api 'io.branch.sdk.android:library:5.9.0'
api 'io.branch.sdk.android:library:5.10.1'
}
5 changes: 5 additions & 0 deletions android/src/main/java/io/branch/rnbranch/RNBranchModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -1251,4 +1251,9 @@ public void setDMAParamsForEEA(boolean eeaRegion, boolean adPersonalizationConse
Branch branch = Branch.getInstance();
branch.setDMAParamsForEEA(eeaRegion, adPersonalizationConsent, adUserDataUsageConsent);
}

@ReactMethod
public void setAPIUrl(String apiUrl) {
Branch.setAPIUrl(apiUrl + "/");
}
}
3 changes: 2 additions & 1 deletion branchreactnativetestbed/components/BranchWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export default class BranchWrapper {

componentDidMount() {
console.log('BranchWrapper componentDidMount');

branch.setAPIUrl('https://api3.branch.io');

this._unsubscribeFromBranch = branch.subscribe({
onOpenStart: ({uri, cachedInitialEvent}) => {
console.log(
Expand Down
5 changes: 5 additions & 0 deletions ios/RNBranch.m
Original file line number Diff line number Diff line change
Expand Up @@ -744,4 +744,9 @@ - (CGFloat) colorComponentFrom: (NSString *) string start: (NSUInteger) start le
[Branch setDMAParamsForEEA:eeaRegion AdPersonalizationConsent:adPersonalizationConsent AdUserDataUsageConsent:adUserDataUsageConsent];
}

#pragma mark setAPIUrl
RCT_EXPORT_METHOD(setAPIUrl:(NSString *)apiUrl) {
[Branch setAPIUrl:apiUrl];
}

@end
1 change: 1 addition & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ interface Branch {
setPreInstallCampaign: (campaign: string) => void;
setPreInstallPartner: (partner: string) => void;
setDMAParamsForEEA: (eeaRegion: boolean, adPersonalizationConsent: boolean, adUserDataUsageConsent: boolean) => void;
setAPIUrl: (url: string) => void;
}
declare const branch: Branch;
export default branch;
11 changes: 11 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { NativeModules, Platform } from "react-native";

Check failure on line 1 in src/index.js

View workflow job for this annotation

GitHub Actions / Lint

Strings must use singlequote

const { RNBranch } = NativeModules;

import createBranchUniversalObject from "./branchUniversalObject";

Check failure on line 5 in src/index.js

View workflow job for this annotation

GitHub Actions / Lint

Strings must use singlequote
import BranchEvent from "./BranchEvent";

Check failure on line 6 in src/index.js

View workflow job for this annotation

GitHub Actions / Lint

Strings must use singlequote
import BranchSubscriber from "./BranchSubscriber";

Check failure on line 7 in src/index.js

View workflow job for this annotation

GitHub Actions / Lint

Strings must use singlequote

const packageFile = require("./../package.json");

Check failure on line 9 in src/index.js

View workflow job for this annotation

GitHub Actions / Lint

Strings must use singlequote
export const VERSION = packageFile.version;

class Branch {
Expand All @@ -17,11 +17,11 @@
constructor(options = {}) {
if (options.debug) this._debug = true;

console.info("Initializing react-native-branch v. " + VERSION);

Check failure on line 20 in src/index.js

View workflow job for this annotation

GitHub Actions / Lint

Strings must use singlequote
}

subscribe(options) {
if (typeof options === "function") {

Check failure on line 24 in src/index.js

View workflow job for this annotation

GitHub Actions / Lint

Strings must use singlequote
/*
* Support for legacy API, passing a single callback function:
* branch.subscribe(({params, error, uri}) => { ... }). This is
Expand All @@ -36,7 +36,7 @@
* You can specify checkCachedEvents in the subscribe options to control
* this per subscriber.
*/
if (!("checkCachedEvents" in options)) {

Check failure on line 39 in src/index.js

View workflow job for this annotation

GitHub Actions / Lint

Strings must use singlequote
options.checkCachedEvents = this._checkCachedEvents;
}
this._checkCachedEvents = false;
Expand Down Expand Up @@ -66,13 +66,13 @@
setIdentityAsync = (identity) => RNBranch.setIdentityAsync(identity);
setRequestMetadata = (key, value) => {
console.info(
"[Branch] setRequestMetadata has limitations when called from JS. Some network calls are made prior to the JS layer being available, those calls will not have the metadata."

Check failure on line 69 in src/index.js

View workflow job for this annotation

GitHub Actions / Lint

Strings must use singlequote
);
return RNBranch.setRequestMetadataKey(key, value);
};
addFacebookPartnerParameter = (name, value) => {
console.info(
"[Branch] addFacebookPartnerParameter has limitations when called from JS. Some network calls are made prior to the JS layer being available, those calls will not have the partner parameters."

Check failure on line 75 in src/index.js

View workflow job for this annotation

GitHub Actions / Lint

Strings must use singlequote
);
return RNBranch.addFacebookPartnerParameter(name, value);
};
Expand Down Expand Up @@ -164,6 +164,17 @@
console.warn("setDMAParamsForEEA: Unable to set DMA params.");
}
};

/*** Set the Branch API's base URL (eg. "https://api2.branch.io") ***/
setAPIUrl = (apiUrl) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I recommend removing the JavaScript API and moving this to the native JSON api. The JS <> native runtime switch will be a race condition against this setting which must be called before session init.

Instead, the json can be read on the native init's start up.

if (!apiUrl || typeof apiUrl !== "string" || !apiUrl.startsWith("http")) {

Choose a reason for hiding this comment

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

A full url validation

const urlPattern = new RegExp('^(https?)://((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|((\\d{1,3}\\.){3}\\d{1,3}))(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*(\\?[;&a-z\\d%_.~+=-]*)?(\\#[-a-z\\d_]*)?$', 'i');
urlPattern.test(apiUrl);

console.warn(

Check warning on line 171 in src/index.js

View check run for this annotation

Codecov / codecov/patch

src/index.js#L171

Added line #L171 was not covered by tests
"setAPIUrl: Invalid URL. URL must be a non-empty string starting with 'http'."
);
return;

Check warning on line 174 in src/index.js

View check run for this annotation

Codecov / codecov/patch

src/index.js#L174

Added line #L174 was not covered by tests
}
RNBranch.setAPIUrl(apiUrl);

Check warning on line 176 in src/index.js

View check run for this annotation

Codecov / codecov/patch

src/index.js#L176

Added line #L176 was not covered by tests
};
}

const validateParam = (param, paramName) => {
Expand Down
Loading