Skip to content

Commit b13537e

Browse files
committed
feat: Add appConfig for env vars & expo-constants
1 parent 2b2773d commit b13537e

File tree

10 files changed

+50
-13
lines changed

10 files changed

+50
-13
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
node_modules/
22
node_modules
33
.env
4+
.env.local
45

56
# -- @generated: @expo/next-adapter@2.1.0 --
67

@@ -25,7 +26,7 @@ yarn-error.log*
2526

2627
# misc
2728
.DS_Store
28-
.env*
29+
.env
2930
.next
3031

3132
# -- @generated: @expo/next-adapter@2.1.52 --

apps/expo/.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
EXPO_PUBLIC_BASE_URL=
2+
EXPO_PUBLIC_BACKEND_URL=
3+
EXPO_PUBLIC_API_URL=
4+
EXPO_PUBLIC_GRAPH_URL=

apps/expo/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@
2525
"typescript": "5.3.3"
2626
},
2727
"scripts": {
28-
"dev": "expo start",
29-
"start": "expo start",
28+
"dev": "expo start --clear",
29+
"start": "expo start --clear",
3030
"android": "expo start --android",
3131
"ios": "expo start --ios",
3232
"web": "expo start --web",
33-
"add-dependencies": "expo install"
33+
"add-dependencies": "expo install",
34+
"env:local": "cp .env.example .env.local"
3435
}
3536
}

apps/next/.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
NEXT_PUBLIC_BASE_URL=http://localhost:3000
2+
NEXT_PUBLIC_BACKEND_URL=http://localhost:3000
3+
NEXT_PUBLIC_API_URL=http://localhost:3000/api
4+
NEXT_PUBLIC_GRAPH_URL=http://localhost:3000/api/graphql

apps/next/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"devDependencies": {},
99
"scripts": {
1010
"dev": "next dev",
11-
"build": "next build"
11+
"build": "next build",
12+
"env:local": "cp .env.example .env.local"
1213
}
1314
}

features/app-core/appConfig.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import Constants from 'expo-constants'
2+
import { Platform } from 'react-native'
3+
4+
export const expoDebuggerHost = Constants?.expoGoConfig?.debuggerHost || Constants.manifest2?.extra?.expoGo?.debuggerHost // prettier-ignore
5+
export const localURL = expoDebuggerHost?.split?.(':').shift()
6+
7+
export const fallbackBaseURL = localURL ? `http://${localURL}:3000` : ''
8+
9+
/** --- appConfig ------------------------------------------------------------------------------ */
10+
/** -i- App config variables powered by env vars universally, and including some expo contants config on mobile */
11+
export const appConfig = {
12+
baseURL: process.env.NEXT_PUBLIC_BASE_URL || process.env.EXPO_PUBLIC_BASE_URL || `${fallbackBaseURL}`, // prettier-ignore
13+
backendURL: process.env.NEXT_PUBLIC_BACKEND_URL || process.env.EXPO_PUBLIC_BACKEND_URL || `${fallbackBaseURL}`, // prettier-ignore
14+
apiURL: process.env.NEXT_PUBLIC_API_URL || process.env.EXPO_PUBLIC_API_URL || `${fallbackBaseURL}/api`, // prettier-ignore
15+
graphURL: process.env.NEXT_PUBLIC_GRAPH_URL || process.env.EXPO_PUBLIC_GRAPH_URL || `${fallbackBaseURL}/api/graphql`, // prettier-ignore
16+
} as const
17+
18+
/* --- Debug ----------------------------------------------------------------------------------- */
19+
20+
if (Platform.OS !== 'web') {
21+
if (appConfig.baseURL === '') console.warn('appConfig.baseURL is empty, check your environment variables')
22+
}

features/app-core/resolvers/healthCheck.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as OS from 'os'
22
import type { NextRequest } from 'next/server'
33
import type { RequestContext } from '../middleware/createRequestContext'
4+
import { appConfig } from '../appConfig'
45

56
/* --- Constants ------------------------------------------------------------------------------- */
67

@@ -9,7 +10,7 @@ const ALIVE_SINCE = new Date()
910
/* --- Types ----------------------------------------------------------------------------------- */
1011

1112
type HealthCheckArgs = {
12-
echo: string
13+
echo?: string
1314
}
1415

1516
type HealthCheckInputs = {
@@ -33,9 +34,8 @@ export const healthCheck = async ({ args, context }: HealthCheckInputs) => {
3334
const rn = req as NextRequest
3435
const requestHost = rn?.headers?.get?.('host')
3536
const requestProtocol = rn?.headers?.get?.['x-forwarded-proto'] ?? 'http'
36-
const requestURL = r?.url ?? `${requestProtocol}://${requestHost}/api/health`
37-
const baseURL = process.env.BACKEND_URL || requestURL?.split('/api/')[0]
38-
const apiURL = baseURL ? `${baseURL}/api` : null
37+
const requestURL = r?.url || `${requestProtocol}://${requestHost}/api/health`
38+
const { baseURL, backendURL, apiURL, graphURL } = appConfig
3939

4040
// -- Respond --
4141

@@ -55,8 +55,10 @@ export const healthCheck = async ({ args, context }: HealthCheckInputs) => {
5555
requestHost,
5656
requestProtocol,
5757
requestURL,
58-
baseURL,
59-
apiURL,
58+
baseURL: requestHost ? `${requestProtocol}://${requestHost}` : baseURL,
59+
backendURL: requestHost ? `${requestProtocol}://${requestHost}` : backendURL,
60+
apiURL: requestHost ? `${requestProtocol}://${requestHost}/api` : apiURL,
61+
graphURL: requestHost ? `${requestProtocol}://${requestHost}/api/graphql` : graphURL,
6062
port: process.env.PORT ? Number(process.env.PORT) : null,
6163
debugPort: process.debugPort && Number(process.debugPort),
6264
// VERSIONS

features/app-core/screens/UniversalRootLayout.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type UniversalRootLayoutProps = {
1313
children: React.ReactNode
1414
}
1515

16-
/* --- <UniversalRootLayout/> --------------------------------------------------------------------------- */
16+
/* --- <UniversalRootLayout/> ------------------------------------------------------------------ */
1717

1818
const UniversalRootLayout = ({ children }: UniversalRootLayoutProps) => (
1919
<View style={styles.container}>

features/app-core/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
},
2020
"include": [
2121
"next-env.d.ts",
22+
"appConfig.ts",
2223
"**/*.ts",
2324
"**/*.tsx",
2425
"../../apps/expo/app/ExpoRootLayout.tsx",

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"ios": "npm -w @app/expo run ios",
1616
"expo:web": "npm -w @app/expo run web",
1717
"build": "npm -w @app/next run build",
18-
"add-dependencies": "npm -w @app/expo run add-dependencies"
18+
"add-dependencies": "npm -w @app/expo run add-dependencies",
19+
"env:local": "npm -w @app/next run env:local & npm -w @app/expo run env:local"
1920
}
2021
}

0 commit comments

Comments
 (0)