【问题标题】:Expo App environments for Dev, UAT and Production用于开发、UAT 和生产的 Expo 应用环境
【发布时间】:2021-03-17 02:02:11
【问题描述】:

我有一个内置在 Expo 中的 React Native 应用程序,它连接到一个 Rest API。其余 api 有三种环境 - dev、uat 和 production,如下所示(示例)。

dev = https://dev.myapi.com/api
uat = https://uat.myapi.com/api
prod = https://prod.myapi.com/api

根据应用的使用位置,它需要连接到正确的环境。

Running in the Expo Client = Dev API
Running in TestFlight or Internal Testing for the Play Store = UAT API
Running in the App Store or Play Store = Production API

实现这一目标的最简单方法是什么?

【问题讨论】:

    标签: react-native google-play expo app-store


    【解决方案1】:

    按照以下步骤操作

    1. 安装 expo-constants 包。要安装软件包,请运行以下命令。

      npm i expo-constants

    2. 添加 environment.js 文件并粘贴以下代码。

    import Constants from 'expo-constants';
    import { Platform } from 'react-native';
    
    const localhost = Platform.OS === 'ios' ? 'localhost:8080' : '10.0.2.2:8080';
    
    const ENV = {
      dev: {
        apiUrl: 'https://dev.myapi.com/api',
        amplitudeApiKey: null,
      },
      staging: {
        apiUrl: 'https://uat.myapi.com/api',
        amplitudeApiKey: '[Enter your key here]',
        // Add other keys you want here
      },
      prod: {
        apiUrl: 'https://prod.myapi.com/api',
        amplitudeApiKey: '[Enter your key here]',
        // Add other keys you want here
      },
    };
    
    const getEnvVars = (env = Constants.manifest.releaseChannel) => {
      // What is __DEV__ ?
      // This variable is set to true when react-native is running in Dev mode.
      // __DEV__ is true when run locally, but false when published.
      if (__DEV__) {
        return ENV.dev;
      } else if (env === 'staging') {
        return ENV.staging;
      } else if (env === 'prod') {
        return ENV.prod;
      }
    };
    
    export default getEnvVars;
    
    1. 访问环境变量
    // Import getEnvVars() from environment.js
    import getEnvVars from '../environment';
    const { apiUrl } = getEnvVars();
    
    /******* SESSIONS::LOG IN *******/
    // LOG IN
    // credentials should be an object containing phone number:
    // {
    //   "phone" : "9876342222"
    // }
    export const logIn = (credentials, jsonWebToken) =>
      fetch(`${apiUrl}/phone`, {
        method: 'POST',
        headers: {
          Authorization: 'Bearer ' + jsonWebToken,
          'Content-Type': 'application/json',
        },
        body: JSON.stringify(credentials),
      });
    
    1. 要创建构建,请使用以下命令。

    开发 - expo build:ios --release-channel dev

    登台 - expo build:ios --release-channel 登台

    生产 - expo build:ios --release-channel prod

    现在 Expo 支持 app.config.js 或 app.config.ts 等配置文件,我们可以使用 dotenv。检查这个:https://docs.expo.io/guides/environment-variables/#using-a-dotenv-file

    【讨论】:

    • 此方法是否允许在运行时动态更新应用程序使用的 URL?还是在构建时确定?如果是在构建时,有什么方法可以让应用在运行时确定服务器 URL?
    • 这是在构建时确定的,因为它基于发布通道名称,要在运行时确定服务器,也许您可​​以创建一个后端端点来检查该用户或设备是否应该在哪个环境中运行。跨度>
    【解决方案2】:

    这可以使用不同的发布频道名称来完成, 假设您以这种方式创建了 3 个发布渠道:

    expo publish --release-channel prod
    expo publish --release-channel staging
    expo publish --release-channel dev
    

    那么你可以有一个函数来相应地设置环境变量:

    import * as Updates from 'expo-updates';
    
    function getEnvironment() {
      if (Updates.releaseChannel.startsWith('prod')) {
        // matches prod*
        return { envName: 'PRODUCTION', dbUrl: 'ccc', apiKey: 'ddd' }; // prod env settings
      } else if (Updates.releaseChannel.startsWith('staging')) {
        // matches staging*
        return { envName: 'STAGING', dbUrl: 'eee', apiKey: 'fff' }; // stage env settings
      } else {
        // assume any other release channel is development
        return { envName: 'DEVELOPMENT', dbUrl: 'aaa', apiKey: 'bbb' }; // dev env settings
      }
    }
    

    更多信息请参考expo documentation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-04
      • 1970-01-01
      • 2013-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-09
      • 2012-11-27
      相关资源
      最近更新 更多