Skip to content

Commit

Permalink
Add Env Lib (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
timoclsn authored Feb 23, 2024
1 parent 87e4d49 commit 7359130
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 7 deletions.
3 changes: 1 addition & 2 deletions apps/website/src/app/api/draft/route.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { draftMode } from "next/headers";
import { redirect } from "next/navigation";
import { z } from "zod";

const DRAFT_MODE_SECRET = z.string().parse(process.env.DRAFT_MODE_SECRET);
const { DRAFT_MODE_SECRET } = process.env;

export const GET = async (request: Request) => {
const { searchParams } = new URL(request.url);
Expand Down
1 change: 1 addition & 0 deletions apps/website/src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ReactNode } from "react";
import { DraftModeBanner } from "../components/DraftModeBanner/DraftModeBanner";
import { Footer } from "../components/Footer/Footer";
import { Navigation } from "../components/Navigation/Navigation";
import "../lib/env";
import { createGenerateMetadata, ogImage } from "../lib/metadata";
import { getMetadata } from "../lib/queries";
import "../styles/global.css";
Expand Down
2 changes: 1 addition & 1 deletion apps/website/src/components/Carousel/Carousel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export const Carousel = <Item extends {}>({
<AnimatePresence initial={false} custom={direction()}>
<m.div
className="absolute h-full w-full bg-background-primary py-16"
key={Math.random()}
key={selectedItemIndex}
variants={variants}
initial="enter"
animate="center"
Expand Down
16 changes: 16 additions & 0 deletions apps/website/src/lib/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { z } from "zod";

const envSchema = z.object({
DRAFT_MODE_SECRET: z.string().min(1),
SANITY_PROJECT_ID: z.string().min(1),
SANITY_AUTH_TOKEN: z.string().min(1),
NEXT_PUBLIC_VERCEL_ENV: z.enum(["preview", "production"]).optional(),
});

envSchema.parse(process.env);

declare global {
namespace NodeJS {
interface ProcessEnv extends z.infer<typeof envSchema> {}
}
}
4 changes: 2 additions & 2 deletions apps/website/src/lib/sanity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import "server-only";
import { draftMode } from "next/headers";
import { z } from "zod";

const SANITY_PROJECT_ID = z.string().parse(process.env.SANITY_PROJECT_ID);
const SANITY_AUTH_TOKEN = z.string().parse(process.env.SANITY_AUTH_TOKEN);
const { SANITY_PROJECT_ID, SANITY_AUTH_TOKEN } = process.env;

const DATASET = "production";
const API_Version = "2023-10-12";

Expand Down
6 changes: 4 additions & 2 deletions apps/website/src/lib/tracking.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { NEXT_PUBLIC_VERCEL_ENV, NODE_ENV } = process.env;

interface TrackingEvents {
"Carousel item selected": {
item: any;
Expand Down Expand Up @@ -25,10 +27,10 @@ export const track = <TEventKey extends keyof TrackingEvents>(
: [event: TEventKey, data: TrackingEvents[TEventKey]]
) => {
const [event, data] = args;
if (process.env.NEXT_PUBLIC_VERCEL_ENV === "production") {
if (NEXT_PUBLIC_VERCEL_ENV === "production") {
splitbee.track(event, data);
}
if (process.env.NODE_ENV === "development") {
if (NODE_ENV === "development") {
console.info("Tracking event:", {
event,
data,
Expand Down

0 comments on commit 7359130

Please sign in to comment.