|
| 1 | +import axios from "axios"; |
| 2 | +import Cookies from "js-cookie"; |
| 3 | + |
| 4 | +import { SERVER_URL } from "@/constants/ServerURL"; |
| 5 | +import { ScriptItem, ExploreScriptsResponse } from "@/api/user/postListApi"; |
| 6 | +const api = axios.create({ |
| 7 | + baseURL: SERVER_URL, // Use environment variables |
| 8 | + timeout: 10000, |
| 9 | +}); |
| 10 | + |
| 11 | +// 좋아한 작품 |
| 12 | +export const fetchLikedPost = async ( |
| 13 | + accessToken?: string |
| 14 | +): Promise<ExploreScriptsResponse> => { |
| 15 | + try { |
| 16 | + const headers: Record<string, string> = {}; |
| 17 | + |
| 18 | + if (accessToken) { |
| 19 | + headers["Authorization"] = `Bearer ${accessToken}`; |
| 20 | + } |
| 21 | + |
| 22 | + const response = await api.get<ExploreScriptsResponse>("/profile/like", { |
| 23 | + headers, |
| 24 | + withCredentials: true, // 쿠키 인증 시 필요 |
| 25 | + }); |
| 26 | + |
| 27 | + const { longPlay, shortPlay } = response.data; |
| 28 | + console.log(longPlay); |
| 29 | + console.log(shortPlay); |
| 30 | + return { |
| 31 | + longPlay: Array.isArray(longPlay) ? longPlay : [], |
| 32 | + shortPlay: Array.isArray(shortPlay) ? shortPlay : [], |
| 33 | + }; |
| 34 | + } catch (error) { |
| 35 | + console.error("Error fetchLikedPost:", error); |
| 36 | + throw new Error(`좋아한 작품 API 호출 실패: ${(error as Error).message}`); |
| 37 | + } |
| 38 | +}; |
| 39 | + |
| 40 | +// 좋아한 장편 작품 목록 조회 |
| 41 | +export const getLikedLongWorks = async ( |
| 42 | + page: number = 0, |
| 43 | + accessToken?: string |
| 44 | +): Promise<ScriptItem[]> => { |
| 45 | + try { |
| 46 | + const headers: Record<string, string> = {}; |
| 47 | + |
| 48 | + if (accessToken) { |
| 49 | + headers["Authorization"] = `Bearer ${accessToken}`; |
| 50 | + } |
| 51 | + |
| 52 | + const response = await api.get<ScriptItem[]>(`/profile/like/long`, { |
| 53 | + params: { page }, |
| 54 | + headers, |
| 55 | + }); |
| 56 | + |
| 57 | + return response.data; |
| 58 | + } catch (error) { |
| 59 | + console.error("Error fetching liked long works:", error); |
| 60 | + throw new Error( |
| 61 | + `좋아한 장편 작품 API 호출 실패: ${(error as Error).message}` |
| 62 | + ); |
| 63 | + } |
| 64 | +}; |
| 65 | + |
| 66 | +// 좋아한 장편 작품 목록 조회 |
| 67 | +export const getLikedShortWorks = async ( |
| 68 | + page: number = 0, |
| 69 | + accessToken?: string |
| 70 | +): Promise<ScriptItem[]> => { |
| 71 | + try { |
| 72 | + const headers: Record<string, string> = {}; |
| 73 | + |
| 74 | + if (accessToken) { |
| 75 | + headers["Authorization"] = `Bearer ${accessToken}`; |
| 76 | + } |
| 77 | + |
| 78 | + const response = await api.get<ScriptItem[]>(`/profile/like/short`, { |
| 79 | + params: { page }, |
| 80 | + headers, |
| 81 | + }); |
| 82 | + console.log(response); |
| 83 | + return response.data; |
| 84 | + |
| 85 | + } catch (error) { |
| 86 | + console.error("Error fetching liked short works:", error); |
| 87 | + throw new Error( |
| 88 | + `좋아한 단편 작품 API 호출 실패: ${(error as Error).message}` |
| 89 | + ); |
| 90 | + } |
| 91 | +}; |
0 commit comments