|
1 | | -import { ExpoConfig } from "expo/config"; |
2 | 1 | import { |
3 | 2 | AndroidConfig, |
4 | 3 | ConfigPlugin, |
5 | 4 | withAndroidManifest, |
6 | 5 | } from "expo/config-plugins"; |
7 | 6 |
|
| 7 | +import { BranchKeys, ConfigData } from "./types"; |
| 8 | + |
8 | 9 | const { |
9 | 10 | addMetaDataItemToMainApplication, |
10 | 11 | getMainApplicationOrThrow, |
11 | 12 | removeMetaDataItemFromMainApplication, |
12 | 13 | } = AndroidConfig.Manifest; |
13 | 14 |
|
14 | 15 | const META_BRANCH_KEY = "io.branch.sdk.BranchKey"; |
| 16 | +const META_BRANCH_KEY_TEST = "io.branch.sdk.BranchKey.test"; |
| 17 | +const META_BRANCH_KEY_TEST_MODE = "io.branch.sdk.TestMode"; |
15 | 18 |
|
16 | | -export function getBranchApiKey(config: ExpoConfig) { |
17 | | - return config.android?.config?.branch?.apiKey ?? null; |
18 | | -} |
19 | | - |
20 | | -export function setBranchApiKey( |
21 | | - apiKey: string, |
| 19 | +export function setBranchApiKeys( |
| 20 | + { apiKey, testApiKey }: BranchKeys, |
22 | 21 | androidManifest: AndroidConfig.Manifest.AndroidManifest, |
23 | 22 | ) { |
24 | 23 | const mainApplication = getMainApplicationOrThrow(androidManifest); |
25 | 24 |
|
26 | 25 | if (apiKey) { |
27 | | - // If the item exists, add it back |
28 | 26 | addMetaDataItemToMainApplication(mainApplication, META_BRANCH_KEY, apiKey); |
29 | 27 | } else { |
30 | | - // Remove any existing item |
31 | 28 | removeMetaDataItemFromMainApplication(mainApplication, META_BRANCH_KEY); |
32 | 29 | } |
33 | 30 |
|
| 31 | + if (testApiKey) { |
| 32 | + addMetaDataItemToMainApplication( |
| 33 | + mainApplication, |
| 34 | + META_BRANCH_KEY_TEST, |
| 35 | + testApiKey, |
| 36 | + ); |
| 37 | + } else { |
| 38 | + removeMetaDataItemFromMainApplication( |
| 39 | + mainApplication, |
| 40 | + META_BRANCH_KEY_TEST, |
| 41 | + ); |
| 42 | + } |
| 43 | + |
34 | 44 | return androidManifest; |
35 | 45 | } |
36 | 46 |
|
37 | | -export const withBranchAndroid: ConfigPlugin<{ apiKey?: string }> = ( |
38 | | - config, |
39 | | - data, |
40 | | -) => { |
41 | | - const apiKey = data.apiKey ?? getBranchApiKey(config); |
42 | | - if (!apiKey) { |
43 | | - throw new Error( |
44 | | - "Branch API key is required: expo.android.config.branch.apiKey", |
| 47 | +export function enableBranchTestEnvironment( |
| 48 | + enableTestEnvironment: boolean, |
| 49 | + androidManifest: AndroidConfig.Manifest.AndroidManifest, |
| 50 | +) { |
| 51 | + const mainApplication = getMainApplicationOrThrow(androidManifest); |
| 52 | + |
| 53 | + addMetaDataItemToMainApplication( |
| 54 | + mainApplication, |
| 55 | + META_BRANCH_KEY_TEST_MODE, |
| 56 | + `${enableTestEnvironment}`, |
| 57 | + ); |
| 58 | + |
| 59 | + return androidManifest; |
| 60 | +} |
| 61 | + |
| 62 | +export const withBranchAndroid: ConfigPlugin<ConfigData> = (config, data) => { |
| 63 | + // Fall back to the Expo Config `branch.apiKey` if not provided in plugin |
| 64 | + // config. The `branch` property in the Expo Config is deprecated and will be |
| 65 | + // removed in SDK 56. |
| 66 | + // TODO(@hassankhan): Remove fallback when updating for SDK 56 |
| 67 | + if (config.android?.config?.branch?.apiKey) { |
| 68 | + console.warn( |
| 69 | + "react-native-branch: Using `config.android.config.branch.apiKey` is deprecated. " + |
| 70 | + "Pass `apiKey` directly in the plugin config instead.", |
45 | 71 | ); |
46 | 72 | } |
| 73 | + const apiKey = data.apiKey ?? config.android?.config?.branch?.apiKey; |
| 74 | + const { testApiKey, enableTestEnvironment = false } = data; |
| 75 | + |
| 76 | + if (!apiKey) { |
| 77 | + throw new Error("Branch API key is required: apiKey must be provided"); |
| 78 | + } |
47 | 79 |
|
48 | 80 | config = withAndroidManifest(config, (config) => { |
49 | | - config.modResults = setBranchApiKey(apiKey, config.modResults); |
| 81 | + config.modResults = setBranchApiKeys( |
| 82 | + { apiKey, testApiKey }, |
| 83 | + config.modResults, |
| 84 | + ); |
| 85 | + |
| 86 | + config.modResults = enableBranchTestEnvironment( |
| 87 | + enableTestEnvironment, |
| 88 | + config.modResults, |
| 89 | + ); |
| 90 | + |
50 | 91 | return config; |
51 | 92 | }); |
52 | 93 |
|
|
0 commit comments