added env vars types and start scripts

This commit is contained in:
Andy Malkin 2024-02-05 22:48:29 -08:00
parent 43ff5d416a
commit b88f5bf2e4
9 changed files with 47 additions and 18 deletions

2
.env
View File

@ -1,2 +0,0 @@
# .env.development
API_URI=https://api-dev.railbird.ai/graphql

View File

@ -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

View File

@ -1,2 +1,2 @@
# .env.production
API_URI=https://api-dev.railbird.ai/graphql
EXPO_PUBLIC_API_URI=https://api-dev.railbird.ai/graphql

11
App.tsx
View File

@ -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]);

21
config.ts Normal file
View File

@ -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;

View File

@ -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;
}
}

View File

@ -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<Props> = ({ 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({});

View File

@ -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",

View File

@ -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"