|
| 1 | +import axios from "axios"; |
| 2 | +import Cookies from "js-cookie"; |
| 3 | +import { StringOptionsWithImporter } from "sass"; |
| 4 | + |
| 5 | +const apiUrl = import.meta.env.VITE_API_URL; |
| 6 | + |
| 7 | +const api = axios.create({ |
| 8 | + baseURL: apiUrl, // Use environment variables |
| 9 | + timeout: 10000, |
| 10 | +}); |
| 11 | + |
| 12 | +export interface ScriptItem { |
| 13 | + id: string; |
| 14 | + title: string; |
| 15 | + writer: string; |
| 16 | + imagePath: string; |
| 17 | + script: boolean; |
| 18 | + scriptPrice: number; |
| 19 | + performance: boolean; |
| 20 | + performancePrice: number; |
| 21 | + checked: string; |
| 22 | + date: string; // ISO 8601 string |
| 23 | + like: boolean; |
| 24 | + likeCount: number; |
| 25 | + viewCount: number; |
| 26 | +} |
| 27 | + |
| 28 | +export interface ExploreScriptsResponse { |
| 29 | + longPlay: ScriptItem[]; |
| 30 | + shortPlay: ScriptItem[]; |
| 31 | +} |
| 32 | + |
| 33 | +export const fetchExploreScripts = async ( |
| 34 | + accessToken?: string |
| 35 | +): Promise<ExploreScriptsResponse> => { |
| 36 | + try { |
| 37 | + const headers: Record<string, string> = {}; |
| 38 | + |
| 39 | + if (accessToken) { |
| 40 | + headers["Authorization"] = `Bearer ${accessToken}`; |
| 41 | + } |
| 42 | + |
| 43 | + const response = await api.get<ExploreScriptsResponse>("/scripts", { |
| 44 | + headers, |
| 45 | + withCredentials: true, // 쿠키 인증 시 필요 |
| 46 | + }); |
| 47 | + |
| 48 | + console.log(response); |
| 49 | + return response.data; |
| 50 | + } catch (error) { |
| 51 | + console.error("Error fetchExploreScripts:", error); |
| 52 | + throw new Error(`작품 둘러보기 API 호출 실패: ${(error as Error).message}`); |
| 53 | + } |
| 54 | +}; |
| 55 | + |
| 56 | +export const toggleLikeScript = async ( |
| 57 | + id: string, |
| 58 | + accessToken?: string |
| 59 | +): Promise<"like" | "cancel like"> => { |
| 60 | + try { |
| 61 | + const headers: Record<string, string> = { |
| 62 | + "Content-Type": "application/json", |
| 63 | + }; |
| 64 | + |
| 65 | + if (accessToken) { |
| 66 | + headers["Authorization"] = `Bearer ${accessToken}`; |
| 67 | + } |
| 68 | + |
| 69 | + const response = await api.post<{ message: "like" | "cancel like" }>( |
| 70 | + `/scripts/like/${id}`, |
| 71 | + null, |
| 72 | + { headers, withCredentials: true } |
| 73 | + ); |
| 74 | + |
| 75 | + return response.data.message; |
| 76 | + } catch (error) { |
| 77 | + console.error("Error toggleLikeScript:", error); |
| 78 | + throw new Error(`좋아요 토글 실패: ${(error as Error).message}`); |
| 79 | + } |
| 80 | +}; |
0 commit comments