From 0e8316b9f5d97004c2d0671ad0d55ea1bce6a94a Mon Sep 17 00:00:00 2001 From: Kat Huang Date: Mon, 5 Feb 2024 12:57:25 -0700 Subject: [PATCH 1/2] Bump gql --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index f245095..3a59f2b 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,7 @@ "graphql": "^16.8.1", "jest": "^29.2.1", "jest-expo": "~49.0.0", - "railbird-gql": "git+https://dev.railbird.ai/railbird/railbird-gql.git#db82f66c5d3600d90f09c813f71287e176dc078b", + "railbird-gql": "git+https://dev.railbird.ai/railbird/railbird-gql.git#838304efdd49a093bd8f9cea6cd6ef9e306d888a", "react": "18.2.0", "react-native": "0.72.6", "react-native-dotenv": "^3.4.9", From b88f5bf2e462459942afcca309e3bbaec0a2671f Mon Sep 17 00:00:00 2001 From: Andy Malkin Date: Mon, 5 Feb 2024 22:48:29 -0800 Subject: [PATCH 2/2] added env vars types and start scripts --- .env | 2 -- .env.development | 4 ++-- .env.production | 2 +- App.tsx | 11 +++++++---- config.ts | 21 +++++++++++++++++++++ env.d.tsx | 11 +++++++---- graphql/client.tsx | 8 +++++--- package.json | 2 ++ yarn.lock | 4 ++-- 9 files changed, 47 insertions(+), 18 deletions(-) delete mode 100644 .env create mode 100644 config.ts diff --git a/.env b/.env deleted file mode 100644 index 7e0a5f8..0000000 --- a/.env +++ /dev/null @@ -1,2 +0,0 @@ -# .env.development -API_URI=https://api-dev.railbird.ai/graphql diff --git a/.env.development b/.env.development index 3828d94..444b872 100644 --- a/.env.development +++ b/.env.development @@ -1,3 +1,3 @@ # .env.development -API_URI="http://192.168.1.28:8000/graphql" -DEV_USER_ID=1 +EXPO_PUBLIC_API_URI="http://192.168.1.28:8000/graphql" +EXPO_PUBLIC_DEV_USER_ID=1 diff --git a/.env.production b/.env.production index 11c7832..dda7acd 100644 --- a/.env.production +++ b/.env.production @@ -1,2 +1,2 @@ # .env.production -API_URI=https://api-dev.railbird.ai/graphql +EXPO_PUBLIC_API_URI=https://api-dev.railbird.ai/graphql diff --git a/App.tsx b/App.tsx index 5026945..6fd03f3 100644 --- a/App.tsx +++ b/App.tsx @@ -1,15 +1,18 @@ -import { DEV_USER_ID } from "@env"; import React from "react"; import { ClientProvider, useAuthHeader } from "./graphql/client"; import AppNavigator from "./navigation/app-navigator"; +import { devUserId } from "./config"; + const SetAuthHeaderBasedOnEnv = () => { const { setAuthHeader } = useAuthHeader(); React.useEffect(() => { - if (DEV_USER_ID) { - console.log("Setting fake authorization user to: ", DEV_USER_ID); - setAuthHeader({ key: "user_id", value: DEV_USER_ID }); + if (devUserId) { + console.log("Setting fake authorization user to: ", devUserId); + setAuthHeader({ key: "user_id", value: devUserId }); + } else { + console.log("Production auth."); } }, [setAuthHeader]); diff --git a/config.ts b/config.ts new file mode 100644 index 0000000..a979824 --- /dev/null +++ b/config.ts @@ -0,0 +1,21 @@ +const warnEnv = [ + 'EXPO_PUBLIC_API_URI' +]; + +const errMsg = + 'does not exist in the environment.'; + +const missingEnv: string[] = []; + +for (const key of warnEnv) { + if (!process.env[key]) { + missingEnv.push(key); + } +} + +if (missingEnv.length > 0) { + throw new Error(`${missingEnv.join(', ')} ${errMsg}`); +} + +export const apiUrl = process.env.EXPO_PUBLIC_API_URI; +export const devUserId = process.env.EXPO_PUBLIC_DEV_USER_ID ?? false; diff --git a/env.d.tsx b/env.d.tsx index e534b18..210b888 100644 --- a/env.d.tsx +++ b/env.d.tsx @@ -1,4 +1,7 @@ -declare module "@env" { - export const API_URI: string; - export const DEV_USER_ID: string; -} +// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars +declare namespace NodeJS { + interface ProcessEnv { + EXPO_PUBLIC_API_URI: string; + EXPO_PUBLIC_DEV_USER_ID?: string; + } +} \ No newline at end of file diff --git a/graphql/client.tsx b/graphql/client.tsx index 81c2348..66e3f28 100644 --- a/graphql/client.tsx +++ b/graphql/client.tsx @@ -6,7 +6,7 @@ import { InMemoryCache, from, } from "@apollo/client"; -import { API_URI } from "@env"; + import React, { ReactNode, createContext, @@ -15,6 +15,8 @@ import React, { useState, } from "react"; +import { apiUrl } from '../config' + type Props = { children: ReactNode; }; @@ -40,9 +42,9 @@ export const useAuthHeader = () => { export const ClientProvider: React.FC = ({ children }) => { const [authHeader, setAuthHeader] = useState({ key: "", value: "" }); - console.log(`The api uri is ${API_URI}`); + console.log(`The api uri is ${apiUrl}`); const httpLink = new HttpLink({ - uri: API_URI, + uri: apiUrl, }); const cache = new InMemoryCache({}); diff --git a/package.json b/package.json index 3a59f2b..39b8851 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,8 @@ "start:android": "expo start --android", "start:ios": "expo start --ios", "android": "expo run:android", + "android:dev": "NODE_ENV=development expo run:android", + "android:prod": "NODE_ENV=production expo run:android", "ios": "expo run:ios", "web": "expo start --web", "lint": "eslint . --ext .js,.ts,.tsx", diff --git a/yarn.lock b/yarn.lock index 903653a..01d04f3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -9658,9 +9658,9 @@ queue@6.0.2: dependencies: inherits "~2.0.3" -"railbird-gql@git+https://dev.railbird.ai/railbird/railbird-gql.git#db82f66c5d3600d90f09c813f71287e176dc078b": +"railbird-gql@git+https://dev.railbird.ai/railbird/railbird-gql.git#838304efdd49a093bd8f9cea6cd6ef9e306d888a": version "1.0.0" - resolved "git+https://dev.railbird.ai/railbird/railbird-gql.git#db82f66c5d3600d90f09c813f71287e176dc078b" + resolved "git+https://dev.railbird.ai/railbird/railbird-gql.git#838304efdd49a093bd8f9cea6cd6ef9e306d888a" dependencies: "@apollo/client" "^3.9.2" "@graphql-codegen/cli" "^5.0.0"