cfb*_*fbd 11 app-store google-play react-native expo
我有一个在 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
Run Code Online (Sandbox Code Playgroud)
根据应用程序的使用位置,它需要连接到正确的环境。
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
Run Code Online (Sandbox Code Playgroud)
实现这一目标的最简单方法是什么?
小智 14
请按照以下步骤操作
安装expo-constants包。要安装该软件包,请运行以下命令。
npm i 展览常数
添加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;
Run Code Online (Sandbox Code Playgroud)
// 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),
});
Run Code Online (Sandbox Code Playgroud)
开发 - 博览会构建: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
这可以使用不同的发布通道名称来完成,假设您已经通过这种方式创建了 3 个发布通道:
expo publish --release-channel prod
expo publish --release-channel staging
expo publish --release-channel dev
Run Code Online (Sandbox Code Playgroud)
那么你可以有一个函数来相应地设置环境变量:
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
}
}
Run Code Online (Sandbox Code Playgroud)
请参阅博览会文档了解更多信息!
对于使用 Expo sdk 46(或任何更新版本)的用户,您可以执行以下操作
export default () => ({
expo: {
name: '',
slug: ''
extra: {
API_URL: process.env.API_URL || null,
},
// ...
},
});
Run Code Online (Sandbox Code Playgroud)
我们可以使用像这样的 expo 常量来访问这个 API(无论我们在哪里)。不要忘记从 Expo 导入常量。
const myApi = Constants.expoConfig.extra.API_URL
axios.get(myApi).... // using API END POINT
Run Code Online (Sandbox Code Playgroud)
对于本地开发访问 API,您可以通过两种方式进行
在eas.json中
{
"production": {
"env": {
"API_URL": "https://prod.example.com"
}
},
"staging": {
"env": {
"API_URL": "https://staging.example.com"
}
}
}
Run Code Online (Sandbox Code Playgroud)
一旦我们运行 eas build,就会设置适当的 API 端点。请参阅 Expo 文档中的相同内容 https://docs.expo.dev/eas-update/environment-variables/
归档时间: |
|
查看次数: |
11364 次 |
最近记录: |