all files / firebase/utils/ internals.js

27.59% Statements 8/29
0% Branches 0/10
100% Functions 0/0
27.59% Lines 8/29
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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239                                                                                                                                                                                                                                                                                                                                                                                          16×                         16×                                                            
/**
 * @flow
 */
import { Platform } from 'react-native';
 
const NAMESPACE_PODS = {
  admob: 'Firebase/AdMob',
  analytics: 'Firebase/Analytics',
  auth: 'Firebase/Auth',
  config: 'Firebase/RemoteConfig',
  crash: 'Firebase/Crash',
  database: 'Firebase/Database',
  links: 'Firebase/DynamicLinks',
  messaging: 'Firebase/Messaging',
  perf: 'Firebase/Performance',
  storage: 'Firebase/Storage',
};
 
const GRADLE_DEPS = {
  admob: 'ads',
};
 
const PLAY_SERVICES_CODES = {
  // $FlowExpectedError: Doesn't like numerical object keys: https://github.com/facebook/flow/issues/380
  1: {
    code: 'SERVICE_MISSING',
    message: 'Google Play services is missing on this device.',
  },
  // $FlowExpectedError: Doesn't like numerical object keys: https://github.com/facebook/flow/issues/380
  2: {
    code: 'SERVICE_VERSION_UPDATE_REQUIRED',
    message:
      'The installed version of Google Play services on this device is out of date.',
  },
  // $FlowExpectedError: Doesn't like numerical object keys: https://github.com/facebook/flow/issues/380
  3: {
    code: 'SERVICE_DISABLED',
    message:
      'The installed version of Google Play services has been disabled on this device.',
  },
  // $FlowExpectedError: Doesn't like numerical object keys: https://github.com/facebook/flow/issues/380
  9: {
    code: 'SERVICE_INVALID',
    message:
      'The version of the Google Play services installed on this device is not authentic.',
  },
  // $FlowExpectedError: Doesn't like numerical object keys: https://github.com/facebook/flow/issues/380
  18: {
    code: 'SERVICE_UPDATING',
    message: 'Google Play services is currently being updated on this device.',
  },
  // $FlowExpectedError: Doesn't like numerical object keys: https://github.com/facebook/flow/issues/380
  19: {
    code: 'SERVICE_MISSING_PERMISSION',
    message:
      "Google Play service doesn't have one or more required permissions.",
  },
};
 
export default {
  // default options
  OPTIONS: {
    logLevel: 'warn',
    errorOnMissingPlayServices: true,
    promptOnMissingPlayServices: true,
  },
 
  FLAGS: {
    checkedPlayServices: false,
  },
 
  STRINGS: {
    WARN_INITIALIZE_DEPRECATION:
      "Deprecation: Calling 'initializeApp()' for apps that are already initialised natively " +
      "is unnecessary, use 'firebase.app()' instead to access the already initialized default app instance.",
 
    /**
     * @return {string}
     */
    get ERROR_MISSING_CORE() {
      if (Platform.OS === 'ios') {
        return (
          'RNFirebase core module was not found natively on iOS, ensure you have ' +
          'correctly included the RNFirebase pod in your projects `Podfile` and have run `pod install`.' +
          '\r\n\r\n See http://invertase.link/ios for the ios setup guide.'
        );
      }
 
      return (
        'RNFirebase core module was not found natively on Android, ensure you have ' +
        'correctly added the RNFirebase and Firebase gradle dependencies to your `android/app/build.gradle` file.' +
        '\r\n\r\n See http://invertase.link/android for the android setup guide.'
      );
    },
 
    ERROR_INIT_OBJECT:
      'Firebase.initializeApp(options <-- requires a valid configuration object.',
    ERROR_INIT_STRING_NAME:
      'Firebase.initializeApp(options, name <-- requires a valid string value.',
 
    /**
     * @return {string}
     */
    ERROR_MISSING_CB(method: string) {
      return `Missing required callback for method ${method}().`;
    },
 
    /**
     * @return {string}
     */
    ERROR_MISSING_ARG(type: string, method: string) {
      return `Missing required argument of type '${type}' for method '${method}()'.`;
    },
 
    /**
     * @return {string}
     */
    ERROR_MISSING_ARG_NAMED(name: string, type: string, method: string) {
      return `Missing required argument '${name}' of type '${type}' for method '${method}()'.`;
    },
 
    /**
     * @return {string}
     */
    ERROR_ARG_INVALID_VALUE(name: string, expected: string, got: string) {
      return `Invalid value for argument '${name}' expected value '${expected}' but got '${got}'.`;
    },
 
    /**
     * @return {string}
     */
    ERROR_PROTECTED_PROP(name: string) {
      return `Property '${name}' is protected and can not be overridden by extendApp.`;
    },
 
    /**
     * @return {string}
     * @param namespace
     * @param nativeModule
     */
    ERROR_MISSING_MODULE(namespace: string, nativeModule: string) {
      const snippet = `firebase.${namespace}()`;
      if (Platform.OS === 'ios') {
        return (
          `You attempted to use a firebase module that's not installed natively on your iOS project by calling ${snippet}.` +
          '\r\n\r\nEnsure you have the required Firebase iOS SDK pod for this module included in your Podfile, in this instance ' +
          `confirm you've added "pod '${
            NAMESPACE_PODS[namespace]
          }'" to your Podfile` +
          '\r\n\r\nSee http://invertase.link/ios for full setup instructions.'
        );
      }
 
      const fbSDKDep = `'com.google.firebase:firebase-${GRADLE_DEPS[
        namespace
      ] || namespace}'`;
      const rnFirebasePackage = `'io.invertase.firebase.${namespace}.${nativeModule}Package'`;
      const newInstance = `'new ${nativeModule}Package()'`;
      return (
        `You attempted to use a firebase module that's not installed on your Android project by calling ${snippet}.` +
        `\r\n\r\nEnsure you have:\r\n\r\n1) Installed the required Firebase Android SDK dependency ${fbSDKDep} in your 'android/app/build.gradle' ` +
        `file.\r\n\r\n2) Imported the ${rnFirebasePackage} module in your 'MainApplication.java' file.\r\n\r\n3) Added the ` +
        `${newInstance} line inside of the RN 'getPackages()' method list.` +
        '\r\n\r\nSee http://invertase.link/android for full setup instructions.'
      );
    },
 
    /**
     * @return {string}
     */
    ERROR_APP_NOT_INIT(appName: string) {
      return `The [${appName}] firebase app has not been initialized!`;
    },
 
    /**
     * @param optName
     * @return {string}
     * @constructor
     */
    ERROR_MISSING_OPT(optName: string) {
      return `Failed to initialize app. FirebaseOptions missing or invalid '${optName}' property.`;
    },
 
    /**
     * @return {string}
     */
    ERROR_NOT_APP(namespace: string) {
      return `Invalid App instance passed to firebase.${namespace}(app <--).`;
    },
 
    /**
     * @return {string}
     */
    ERROR_UNSUPPORTED_CLASS_METHOD(className: string, method: string) {
      return `${className}.${method}() is unsupported by the native Firebase SDKs.`;
    },
 
    /**
     * @return {string}
     */
    ERROR_UNSUPPORTED_CLASS_PROPERTY(className: string, property: string) {
      return `${className}.${property} is unsupported by the native Firebase SDKs.`;
    },
 
    /**
     * @return {string}
     */
    ERROR_UNSUPPORTED_MODULE_METHOD(namespace: string, method: string) {
      return `firebase.${namespace}().${method}() is unsupported by the native Firebase SDKs.`;
    },
 
    /**
     * @return {string}
     */
    ERROR_PLAY_SERVICES(statusCode: number) {
      const knownError = PLAY_SERVICES_CODES[statusCode];
      let start =
        'Google Play Services is required to run firebase services on android but a valid installation was not found on this device.';
 
      if (statusCode === 2) {
        start =
          'Google Play Services is out of date and may cause some firebase services like authentication to hang when used. It is recommended that you update it.';
      }
 
      // eslint-disable-next-line prefer-template
      return (
        `${`${start}\r\n\r\n-------------------------\r\n`}${
          knownError
            ? `${knownError.code}: ${knownError.message} (code ${statusCode})`
            : `A specific play store availability reason reason was not available (unknown code: ${statusCode})`
        }\r\n-------------------------` +
        `\r\n\r\n` +
        `For more information on how to resolve this issue, configure Play Services checks or for guides on how to validate Play Services on your users devices see the link below:` +
        `\r\n\r\nhttp://invertase.link/play-services`
      );
    },
  },
};