All files / src withStoreConfigValues.ts

100% Statements 14/14
100% Branches 0/0
100% Functions 7/7
100% Lines 14/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82                                                                  4x         4x       32x 3x 3x   3x       4x       32x 5x       5x 5x             5x           4x 32x              
import {
  AndroidConfig,
  ConfigPlugin,
  withAndroidManifest,
  withInfoPlist,
  withPlugins,
} from 'expo/config-plugins';
 
import { type ConfigPluginPropsWithDefaults } from './withIterable.types';
 
/**
 * The keys of the props object that are passed to the plugin.
 *
 * These keys are used to configure the plugin in the apps app.json file.
 */
type JsKey = keyof Pick<
  ConfigPluginPropsWithDefaults,
  'requestPermissionsForPushNotifications'
>;
 
/**
 * Natively stored keys associated with the plugin options.
 *
 * These keys are added to the Info.plist file or the AndroidManifest.xml file,
 * and are associated with the values found in the plugin options of the users
 * app.json file.
 */
type NativeKey = string;
 
/**
 * A map of the plugin options keys to the native keys that are added to the
 * Info.plist file or the AndroidManifest.xml file.
 */
const nativeKeyMap: Record<JsKey, NativeKey> = {
  requestPermissionsForPushNotifications:
    'ITERABLE_REQUEST_PERMISSIONS_FOR_PUSH_NOTIFICATIONS',
};
 
const withStoreValuesOnIos: ConfigPlugin<ConfigPluginPropsWithDefaults> = (
  config,
  props
) => {
  return withInfoPlist(config, (newConfig) => {
    Object.entries(nativeKeyMap).forEach(([configKey, nativeKey]) => {
      newConfig.modResults[nativeKey] = props[configKey as keyof typeof props];
    });
    return newConfig;
  });
};
 
const withStoreValuesOnAndroid: ConfigPlugin<ConfigPluginPropsWithDefaults> = (
  config,
  props
) => {
  return withAndroidManifest(config, (newConfig) => {
    const mainApplication = AndroidConfig.Manifest.getMainApplicationOrThrow(
      newConfig.modResults
    );
 
    Object.entries(nativeKeyMap).forEach(([configKey, nativeKey]) => {
      AndroidConfig.Manifest.addMetaDataItemToMainApplication(
        mainApplication,
        nativeKey,
        String(props[configKey as keyof typeof props])
      );
    });
 
    return newConfig;
  });
};
 
export const withStoreConfigValues: ConfigPlugin<
  ConfigPluginPropsWithDefaults
> = (config, props) => {
  return withPlugins(config, [
    [withStoreValuesOnIos, props],
    [withStoreValuesOnAndroid, props],
  ]);
};
 
export default withStoreConfigValues;