Skip to content

Commit 29d9c21

Browse files
committed
chore: use getEnv in oauth module for platform agnosticity
We'd like to avoid hardcoding platform-specific logic and assumptions in this codebase. We'll eventually add documentation for zero-config deployment to any platform.
1 parent 7483fbe commit 29d9c21

File tree

2 files changed

+59
-8
lines changed

2 files changed

+59
-8
lines changed

config/env.ts

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,49 @@ export const isPreview =
4040
process.env.CONTEXT === 'dev' ||
4141
process.env.VERCEL_ENV === 'preview' ||
4242
process.env.VERCEL_ENV === 'development'
43+
export const isProduction =
44+
process.env.CONTEXT === 'production' || process.env.VERCEL_ENV === 'production'
45+
46+
/**
47+
* Environment variable `URL` provided by Netlify.
48+
* This is always the current deploy URL, regardless of env.
49+
* @see {@link https://docs.netlify.com/build/functions/environment-variables/#functions}
50+
*
51+
* Environment variable `VERCEL_URL` provided by Vercel.
52+
* This is always the current deploy URL, regardless of env.
53+
* NOTE: Not a valid URL, as the protocol is omitted.
54+
* @see {@link https://vercel.com/docs/environment-variables/system-environment-variables#VERCEL_URL}
55+
*
56+
* Preview URL for the current deployment, only available in preview environments.
57+
*/
58+
export const getPreviewUrl = () =>
59+
isPreview
60+
? process.env.NUXT_ENV_URL
61+
? process.env.NUXT_ENV_URL
62+
: process.env.NUXT_ENV_VERCEL_URL
63+
? `https://${process.env.NUXT_ENV_VERCEL_URL}`
64+
: undefined
65+
: undefined
66+
67+
/**
68+
* Environment variable `URL` provided by Netlify.
69+
* This is always the current deploy URL, regardless of env.
70+
* @see {@link https://docs.netlify.com/build/functions/environment-variables/#functions}
71+
*
72+
* Environment variable `VERCEL_PROJECT_PRODUCTION_URL` provided by Vercel.
73+
* NOTE: Not a valid URL, as the protocol is omitted.
74+
* @see {@link https://vercel.com/docs/environment-variables/system-environment-variables#VERCEL_PROJECT_PRODUCTION_URL}
75+
*
76+
* Production URL for the current deployment, only available in production environments.
77+
*/
78+
export const getProductionUrl = () =>
79+
isProduction
80+
? process.env.NUXT_ENV_URL
81+
? process.env.NUXT_ENV_URL
82+
: process.env.NUXTENV_VERCEL_PROJECT_PRODUCTION_URL
83+
? `https://${process.env.NUXT_ENV_VERCEL_PROJECT_PRODUCTION_URL}`
84+
: undefined
85+
: undefined
4386

4487
const git = Git()
4588
export async function getGitInfo() {
@@ -92,5 +135,14 @@ export async function getEnv(isDevelopment: boolean) {
92135
: branch === 'main'
93136
? 'canary'
94137
: 'release'
95-
return { commit, shortCommit, branch, env } as const
138+
const previewUrl = getPreviewUrl()
139+
const productionUrl = getProductionUrl()
140+
return {
141+
commit,
142+
shortCommit,
143+
branch,
144+
env,
145+
previewUrl,
146+
productionUrl,
147+
} as const
96148
}

modules/oauth.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,22 @@ import process from 'node:process'
33
import { join } from 'node:path'
44
import { appendFileSync, existsSync, readFileSync } from 'node:fs'
55
import { randomUUID } from 'node:crypto'
6+
import { getEnv } from '../config/env.ts'
67

78
export default defineNuxtModule({
89
meta: {
910
name: 'oauth',
1011
},
11-
setup() {
12+
async setup() {
1213
const nuxt = useNuxt()
1314

14-
const env = process.env.NUXT_ENV_VERCEL_ENV
15-
const previewUrl = process.env.NUXT_ENV_VERCEL_URL
16-
const prodUrl = process.env.NUXT_ENV_VERCEL_PROJECT_PRODUCTION_URL
15+
const { env, previewUrl, productionUrl } = await getEnv(nuxt.options.dev)
1716

1817
let clientUri: string
1918
if (env === 'preview' && previewUrl) {
20-
clientUri = `https://${previewUrl}`
21-
} else if (env === 'production' && prodUrl) {
22-
clientUri = `https://${prodUrl}`
19+
clientUri = previewUrl
20+
} else if (env === 'release' && productionUrl) {
21+
clientUri = productionUrl
2322
} else {
2423
clientUri = 'http://127.0.0.1:3000'
2524
}

0 commit comments

Comments
 (0)