import type { Board, Feed, ListKind } from "./types"; // 개발은 로컬 백엔드 직접 호출, 운영(nginx)은 같은 도메인의 /api 프록시. NEXT_PUBLIC_API_URL로 덮어쓰기 가능 export const API_URL = process.env.NEXT_PUBLIC_API_URL ?? (process.env.NODE_ENV === "development" ? "http://localhost:8080" : ""); /** 토큰 만료/누락 (백엔드가 401 또는 403으로 응답) */ export class AuthError extends Error {} async function call(path: string, token: string, init?: RequestInit) { const res = await fetch(`${API_URL}${path}`, { ...init, headers: { Authorization: `Bearer ${token}` } }); if (res.status === 401 || res.status === 403) throw new AuthError("※ 로그인이 만료되었습니다. 다시 로그인하세요."); if (!res.ok) throw new Error(`※ 서버 오류 (${res.status})`); return res; } // ponytail: 첫 페이지 30건만 가져옴. 페이지 넘김이 필요해지면 page 파라미터 추가 export async function fetchFeed(token: string, kind: ListKind, board: string | null): Promise { const filter = board ? `&board=${encodeURIComponent(board)}` : ""; const res = await call(`/api/scraped-posts${kind === "viewed" ? "/viewed" : ""}?size=30${filter}`, token); const data = await res.json(); // Spring Page 응답: { content: [...], totalElements, ... } return { kind, board, posts: data.content, total: data.totalElements }; } export const fetchBoards = async (token: string): Promise => (await call("/api/scraped-posts/boards", token)).json(); export const markViewed = (token: string, id: number) => call(`/api/scraped-posts/${id}/view`, token, { method: "POST" });