add a constant file to read the env variables
This commit is contained in:
parent
7c5ab7c65f
commit
4007efcc57
4 changed files with 37 additions and 17 deletions
|
@ -1,6 +1,7 @@
|
||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import fs from "node:fs/promises";
|
import fs from "node:fs/promises";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
|
import { VALIDATED_ENV } from "../../../../lib/constants";
|
||||||
|
|
||||||
export async function GET(request: Request, { params }: { params: { id: string } }) {
|
export async function GET(request: Request, { params }: { params: { id: string } }) {
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -1,17 +1,15 @@
|
||||||
import Link from "next/link";
|
import { Metadata } from "next";
|
||||||
import { ArrowLeft } from "lucide-react";
|
import React from "react";
|
||||||
|
import { ENV, assertEnvVariables } from "../../../lib/constants";
|
||||||
|
|
||||||
export default function NewsDetailLayout({ children }: { children: React.ReactNode }) {
|
export const metadata: Metadata = {
|
||||||
return (
|
title: "News - Perplexica",
|
||||||
<div className="max-w-4xl mx-auto p-4">
|
};
|
||||||
<Link
|
|
||||||
href="/news"
|
assertEnvVariables(ENV);
|
||||||
className="inline-flex items-center mb-4 text-sm font-medium text-black dark:text-white hover:underline"
|
|
||||||
>
|
const Layout = ({ children }: { children: React.ReactNode }) => {
|
||||||
<ArrowLeft className="w-4 h-4 mr-1" />
|
return <div>{children}</div>;
|
||||||
Back
|
};
|
||||||
</Link>
|
|
||||||
{children}
|
export default Layout;
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
import NewsDetail from "../../../components/NewsDetail";
|
import NewsDetail from "../../../components/NewsDetail";
|
||||||
|
import { VALIDATED_ENV } from "../../../lib/constants";
|
||||||
|
|
||||||
async function getNewsData(id: string) {
|
async function getNewsData(id: string) {
|
||||||
const res = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/news/${id}`, { next: { revalidate: 60 } });
|
const res = await fetch(`${VALIDATED_ENV.API_URL}/news/${id}`, { next: { revalidate: 60 } });
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
throw new Error("Failed to fetch news");
|
throw new Error("Failed to fetch news");
|
||||||
}
|
}
|
||||||
|
|
20
ui/lib/constants.ts
Normal file
20
ui/lib/constants.ts
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
export const ENV = {
|
||||||
|
WS_URL: process.env.NEXT_PUBLIC_WS_URL,
|
||||||
|
API_URL: process.env.NEXT_PUBLIC_API_URL,
|
||||||
|
} as const;
|
||||||
|
|
||||||
|
export type ENV = typeof ENV;
|
||||||
|
|
||||||
|
// Type guard function
|
||||||
|
export function assertEnvVariables(ENV: ENV): asserts ENV is Required<ENV> {
|
||||||
|
const missingVariables = Object.entries(ENV).filter(([_, value]) => value === undefined);
|
||||||
|
if (missingVariables.length > 0) {
|
||||||
|
throw new Error(`Missing environment variables: ${missingVariables.map(([key]) => key).join(", ")}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call this function early in your app's initialization
|
||||||
|
assertEnvVariables(ENV);
|
||||||
|
|
||||||
|
// After assertion, we can safely use ENV
|
||||||
|
export const VALIDATED_ENV: Required<ENV> = ENV as Required<ENV>;
|
Loading…
Add table
Add a link
Reference in a new issue