Some tweaks

This commit is contained in:
cdricms
2026-03-21 11:53:25 +01:00
parent 4cf85981eb
commit 83eddf89cd
5 changed files with 22 additions and 32 deletions

View File

@@ -1,6 +1,7 @@
"use client"; "use client";
import { API_URL } from "@/lib/constants"; import { API_URL } from "@/lib/constants";
import { ApiResponse } from "@/types/types"; import { ApiResponse } from "@/types/types";
import { getApiUrl } from "@/utils/api";
import { getCookie } from "cookies-next"; import { getCookie } from "cookies-next";
import { useState, useRef, useCallback } from "react"; import { useState, useRef, useCallback } from "react";
@@ -24,7 +25,7 @@ const useFileUpload = (): UseFileUploadReturn => {
const uploadFile = useCallback( const uploadFile = useCallback(
(file: File, url: string, onSuccess?: (response: any) => void) => { (file: File, url: string, onSuccess?: (response: any) => void) => {
url = `${API_URL}${url}`; url = `${getApiUrl()}${url}`;
if (!file || !url) { if (!file || !url) {
setError("File and upload URL are required."); setError("File and upload URL are required.");
return; return;
@@ -35,7 +36,7 @@ const useFileUpload = (): UseFileUploadReturn => {
return; return;
} }
fetch(`${API_URL}/media/verify`, { fetch(`${getApiUrl()}/media/verify`, {
method: "POST", method: "POST",
body: JSON.stringify({ body: JSON.stringify({
name: file.name, name: file.name,

View File

@@ -3,6 +3,7 @@ import { API_URL } from "./constants";
import { ApiResponse } from "@/types/types"; import { ApiResponse } from "@/types/types";
import IUser from "@/interfaces/IUser"; import IUser from "@/interfaces/IUser";
import { cookies } from "next/headers"; import { cookies } from "next/headers";
import { getApiUrl } from "@/utils/api";
const getMe = cache( const getMe = cache(
async (sessionCookie?: string): Promise<ApiResponse<IUser> | null> => { async (sessionCookie?: string): Promise<ApiResponse<IUser> | null> => {
@@ -12,7 +13,7 @@ const getMe = cache(
if (!token) return null; if (!token) return null;
sessionCookie = token; sessionCookie = token;
} }
const res = await fetch(`${API_URL}/users/me`, { const res = await fetch(`${getApiUrl()}/users/me`, {
headers: { Authorization: `Bearer ${sessionCookie}` }, headers: { Authorization: `Bearer ${sessionCookie}` },
}); });
return await res.json(); return await res.json();

View File

@@ -1,5 +1,6 @@
import { API_URL } from "@/lib/constants"; import { API_URL } from "@/lib/constants";
import { ApiResponse } from "@/types/types"; import { ApiResponse } from "@/types/types";
import { getApiUrl } from "@/utils/api";
import { getCookie } from "cookies-next"; import { getCookie } from "cookies-next";
import { ReadonlyRequestCookies } from "next/dist/server/web/spec-extension/adapters/request-cookies"; import { ReadonlyRequestCookies } from "next/dist/server/web/spec-extension/adapters/request-cookies";
export default async function request<T>( export default async function request<T>(
@@ -20,7 +21,7 @@ export default async function request<T>(
if (options.csrfToken) { if (options.csrfToken) {
const res: ApiResponse<{ csrf: string }> = await ( const res: ApiResponse<{ csrf: string }> = await (
await fetch(`${API_URL}/csrf-token`, { credentials: "include" }) await fetch(`${getApiUrl()}/csrf-token`, { credentials: "include" })
).json(); ).json();
if (res.data) headers["X-CSRF-Token"] = res.data.csrf; if (res.data) headers["X-CSRF-Token"] = res.data.csrf;
} }
@@ -39,7 +40,7 @@ export default async function request<T>(
headers.Authorization = `Bearer ${authToken}`; headers.Authorization = `Bearer ${authToken}`;
} }
const response = await fetch(`${API_URL}${endpoint}`, { const response = await fetch(`${getApiUrl()}${endpoint}`, {
method, method,
headers, headers,
body: body ? JSON.stringify(body) : undefined, body: body ? JSON.stringify(body) : undefined,

View File

@@ -1,41 +1,19 @@
import type { NextConfig } from "next"; import type { NextConfig } from "next";
const apiUrl =
process.env.NODE_ENV !== "production"
? `http://localhost:${process.env.BACKEND_PORT ?? 3001}`
: `https://${process.env.SERVER_NAME}/api`;
const nextConfig: NextConfig = { const nextConfig: NextConfig = {
/* config options here */ transpilePackages: ["@mdxeditor/editor"],
transpilePackages: ['@mdxeditor/editor'],
output: "standalone", output: "standalone",
compiler: { compiler: {
removeConsole: process.env.NODE_ENV === "production", removeConsole: process.env.NODE_ENV === "production",
}, },
images: { images: {
remotePatterns: [ remotePatterns: [
{ { protocol: "https", hostname: "img.youtube.com" },
protocol: "https", { protocol: "https", hostname: "avatar.vercel.sh" },
hostname: "img.youtube.com", { protocol: "http", hostname: "localhost" },
}, { protocol: "https", hostname: "latosa.cems.dev" },
{
protocol: "https",
hostname: "avatar.vercel.sh",
},
{
protocol: "http",
hostname: "localhost",
},
{
protocol: "https",
hostname: "latosa.cems.dev",
},
], ],
}, },
env: {
NEXT_PUBLIC_BACKEND_PORT: process.env.BACKEND_PORT,
NEXT_PUBLIC_API_URL: apiUrl,
},
}; };
export default nextConfig; export default nextConfig;

9
frontend/utils/api.ts Normal file
View File

@@ -0,0 +1,9 @@
export const getApiUrl = () => {
// If window is undefined, we are running on the server (SSR in Docker)
if (typeof window === "undefined") {
return process.env.INTERNAL_API_URL || "http://latosa-backend:4001";
}
// Otherwise, we are running in the user's browser
return process.env.NEXT_PUBLIC_API_URL;
};