Files
ss-front-react/src/features/bbs/api.ts
T
headporterandClaude Opus 5.5 8914954c55
SpecialSource Frontend (React) CD / deploy (push) Successful in 15s
feat : BBS 게시판 탭·모바일 제스처 추가, 로그인 유지 및 뒤로가기 새로고침 버그 수정
- 로그인 토큰을 sessionStorage에 보관해 새로고침해도 로그인 유지
- 메인 화면에서 L이 로그아웃되던 문제 수정 (L = 목록으로 통일)
- 게시판 목록을 번호/게시판/제목/날짜(YYYYMMDD) 레이아웃으로 변경
- 게시판 종류별 탭 추가 (클릭 또는 B번호 입력, /boards API 사용)
- 모바일 제스처: 좌우 스와이프로 뒤로/앞으로, 당겨서 목록 새로고침
- 뒤로가기 시 Next.js가 페이지를 새로고침해 메인으로 튕기던 버그 수정
- study-notes 5번 정정

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 15:22:50 +09:00

30 lines
1.6 KiB
TypeScript

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<Feed> {
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<Board[]> =>
(await call("/api/scraped-posts/boards", token)).json();
export const markViewed = (token: string, id: number) =>
call(`/api/scraped-posts/${id}/view`, token, { method: "POST" });