Skip to content

Commit 651f984

Browse files
authored
feat: add t3-env for env var
2 parents f52232a + 428b126 commit 651f984

File tree

15 files changed

+141
-65
lines changed

15 files changed

+141
-65
lines changed

app/(docs)/docs/[[...slug]]/page.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { DashboardTableOfContents } from "@/components/toc"
1010
import "@/styles/mdx.css"
1111
import { Metadata } from "next"
1212

13+
import { env } from "@/env.mjs"
1314
import { absoluteUrl } from "@/lib/utils"
1415

1516
interface DocPageProps {
@@ -38,7 +39,7 @@ export async function generateMetadata({
3839
return {}
3940
}
4041

41-
const url = process.env.NEXT_PUBLIC_APP_URL
42+
const url = env.NEXT_PUBLIC_APP_URL
4243

4344
const ogUrl = new URL(`${url}/api/og`)
4445
ogUrl.searchParams.set("heading", doc.description ?? doc.title)

app/(docs)/guides/[...slug]/page.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { DashboardTableOfContents } from "@/components/toc"
1111
import "@/styles/mdx.css"
1212
import { Metadata } from "next"
1313

14+
import { env } from "@/env.mjs"
1415
import { absoluteUrl, cn } from "@/lib/utils"
1516
import { buttonVariants } from "@/components/ui/button"
1617

@@ -40,7 +41,7 @@ export async function generateMetadata({
4041
return {}
4142
}
4243

43-
const url = process.env.NEXT_PUBLIC_APP_URL
44+
const url = env.NEXT_PUBLIC_APP_URL
4445

4546
const ogUrl = new URL(`${url}/api/og`)
4647
ogUrl.searchParams.set("heading", guide.title)

app/(marketing)/[...slug]/page.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Mdx } from "@/components/mdx-components"
66
import "@/styles/mdx.css"
77
import { Metadata } from "next"
88

9+
import { env } from "@/env.mjs"
910
import { siteConfig } from "@/config/site"
1011
import { absoluteUrl } from "@/lib/utils"
1112

@@ -35,7 +36,7 @@ export async function generateMetadata({
3536
return {}
3637
}
3738

38-
const url = process.env.NEXT_PUBLIC_APP_URL
39+
const url = env.NEXT_PUBLIC_APP_URL
3940

4041
const ogUrl = new URL(`${url}/api/og`)
4142
ogUrl.searchParams.set("heading", page.title)

app/(marketing)/blog/[...slug]/page.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { Metadata } from "next"
88
import Image from "next/image"
99
import Link from "next/link"
1010

11+
import { env } from "@/env.mjs"
1112
import { absoluteUrl, cn, formatDate } from "@/lib/utils"
1213
import { buttonVariants } from "@/components/ui/button"
1314
import { Icons } from "@/components/icons"
@@ -38,7 +39,7 @@ export async function generateMetadata({
3839
return {}
3940
}
4041

41-
const url = process.env.NEXT_PUBLIC_APP_URL
42+
const url = env.NEXT_PUBLIC_APP_URL
4243

4344
const ogUrl = new URL(`${url}/api/og`)
4445
ogUrl.searchParams.set("heading", post.title)

app/(marketing)/page.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Link from "next/link"
22

3+
import { env } from "@/env.mjs"
34
import { siteConfig } from "@/config/site"
45
import { cn } from "@/lib/utils"
56
import { buttonVariants } from "@/components/ui/button"
@@ -11,7 +12,7 @@ async function getGitHubStars(): Promise<string | null> {
1112
{
1213
headers: {
1314
Accept: "application/vnd.github+json",
14-
Authorization: `Bearer ${process.env.GITHUB_ACCESS_TOKEN}`,
15+
Authorization: `Bearer ${env.GITHUB_ACCESS_TOKEN}`,
1516
},
1617
next: {
1718
revalidate: 60,

app/api/webhooks/stripe/route.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { headers } from "next/headers"
22
import Stripe from "stripe"
33

4+
import { env } from "@/env.mjs"
45
import { db } from "@/lib/db"
56
import { stripe } from "@/lib/stripe"
67

@@ -14,7 +15,7 @@ export async function POST(req: Request) {
1415
event = stripe.webhooks.constructEvent(
1516
body,
1617
signature,
17-
process.env.STRIPE_WEBHOOK_SECRET || ""
18+
env.STRIPE_WEBHOOK_SECRET
1819
)
1920
} catch (error) {
2021
return new Response(`Webhook Error: ${error.message}`, { status: 400 })

config/subscriptions.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { SubscriptionPlan } from "types"
2+
import { env } from "@/env.mjs"
23

34
export const freePlan: SubscriptionPlan = {
45
name: "Free",
@@ -10,5 +11,5 @@ export const freePlan: SubscriptionPlan = {
1011
export const proPlan: SubscriptionPlan = {
1112
name: "PRO",
1213
description: "The PRO plan has unlimited posts.",
13-
stripePriceId: process.env.STRIPE_PRO_MONTHLY_PLAN_ID || "",
14+
stripePriceId: env.STRIPE_PRO_MONTHLY_PLAN_ID || "",
1415
}

env.mjs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { createEnv } from "@t3-oss/env-nextjs"
2+
import { z } from "zod"
3+
4+
export const env = createEnv({
5+
server: {
6+
// This is optional because it's only used in development.
7+
// See https://next-auth.js.org/deployment.
8+
NEXTAUTH_URL: z.string().url().optional(),
9+
NEXTAUTH_SECRET: z.string().min(1),
10+
GITHUB_CLIENT_ID: z.string().min(1),
11+
GITHUB_CLIENT_SECRET: z.string().min(1),
12+
GITHUB_ACCESS_TOKEN: z.string().min(1),
13+
DATABASE_URL: z.string().min(1),
14+
SMTP_FROM: z.string().min(1),
15+
POSTMARK_API_TOKEN: z.string().min(1),
16+
POSTMARK_SIGN_IN_TEMPLATE: z.string().min(1),
17+
POSTMARK_ACTIVATION_TEMPLATE: z.string().min(1),
18+
STRIPE_API_KEY: z.string().min(1),
19+
STRIPE_WEBHOOK_SECRET: z.string().min(1),
20+
STRIPE_PRO_MONTHLY_PLAN_ID: z.string().min(1),
21+
},
22+
client: {
23+
NEXT_PUBLIC_APP_URL: z.string().min(1),
24+
},
25+
runtimeEnv: {
26+
NEXTAUTH_URL: process.env.NEXTAUTH_URL,
27+
NEXTAUTH_SECRET: process.env.NEXTAUTH_SECRET,
28+
GITHUB_CLIENT_ID: process.env.GITHUB_CLIENT_ID,
29+
GITHUB_CLIENT_SECRET: process.env.GITHUB_CLIENT_SECRET,
30+
GITHUB_ACCESS_TOKEN: process.env.GITHUB_ACCESS_TOKEN,
31+
DATABASE_URL: process.env.DATABASE_URL,
32+
SMTP_FROM: process.env.SMTP_FROM,
33+
POSTMARK_API_TOKEN: process.env.POSTMARK_API_TOKEN,
34+
POSTMARK_SIGN_IN_TEMPLATE: process.env.POSTMARK_SIGN_IN_TEMPLATE,
35+
POSTMARK_ACTIVATION_TEMPLATE: process.env.POSTMARK_ACTIVATION_TEMPLATE,
36+
STRIPE_API_KEY: process.env.STRIPE_API_KEY,
37+
STRIPE_WEBHOOK_SECRET: process.env.STRIPE_WEBHOOK_SECRET,
38+
STRIPE_PRO_MONTHLY_PLAN_ID: process.env.STRIPE_PRO_MONTHLY_PLAN_ID,
39+
NEXT_PUBLIC_APP_URL: process.env.NEXT_PUBLIC_APP_URL,
40+
},
41+
})

lib/auth.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import EmailProvider from "next-auth/providers/email"
44
import GitHubProvider from "next-auth/providers/github"
55
import { Client } from "postmark"
66

7+
import { env } from "@/env.mjs"
78
import { siteConfig } from "@/config/site"
89
import { db } from "@/lib/db"
910

10-
// TODO: Move env vars to env a la t3.
11-
const postmarkClient = new Client(process.env.POSTMARK_API_TOKEN || "")
11+
const postmarkClient = new Client(env.POSTMARK_API_TOKEN)
1212

1313
export const authOptions: NextAuthOptions = {
1414
// huh any! I know.
@@ -23,11 +23,11 @@ export const authOptions: NextAuthOptions = {
2323
},
2424
providers: [
2525
GitHubProvider({
26-
clientId: process.env.GITHUB_CLIENT_ID || "",
27-
clientSecret: process.env.GITHUB_CLIENT_SECRET || "",
26+
clientId: env.GITHUB_CLIENT_ID,
27+
clientSecret: env.GITHUB_CLIENT_SECRET,
2828
}),
2929
EmailProvider({
30-
from: process.env.SMTP_FROM,
30+
from: env.SMTP_FROM,
3131
sendVerificationRequest: async ({ identifier, url, provider }) => {
3232
const user = await db.user.findUnique({
3333
where: {
@@ -39,8 +39,8 @@ export const authOptions: NextAuthOptions = {
3939
})
4040

4141
const templateId = user?.emailVerified
42-
? process.env.POSTMARK_SIGN_IN_TEMPLATE
43-
: process.env.POSTMARK_ACTIVATION_TEMPLATE
42+
? env.POSTMARK_SIGN_IN_TEMPLATE
43+
: env.POSTMARK_ACTIVATION_TEMPLATE
4444
if (!templateId) {
4545
throw new Error("Missing template id")
4646
}

lib/stripe.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import Stripe from "stripe"
22

3-
export const stripe = new Stripe(process.env.STRIPE_API_KEY || "", {
3+
import { env } from "@/env.mjs"
4+
5+
export const stripe = new Stripe(env.STRIPE_API_KEY, {
46
apiVersion: "2022-11-15",
57
typescript: true,
68
})

0 commit comments

Comments
 (0)