diff --git a/src/api/client.ts b/src/api/client.ts index fdbfd39..7185a12 100644 --- a/src/api/client.ts +++ b/src/api/client.ts @@ -1,23 +1,30 @@ import uoslifeBridge from '../bridge'; +import { FetchMethodType } from './types'; export default class APIClient { static async APIclient({ url, + method = 'GET', }: { url: string; + method?: FetchMethodType; }): Promise { try { - const response = await this.fetchWithToken(`/core/${url}`); + const response = await this.fetchWithToken(`/core/${url}`, method); return await response?.json(); } catch (error) { throw Error(); } } - static async fetchWithToken(input: RequestInfo | URL) { + static async fetchWithToken( + input: RequestInfo | URL, + method: FetchMethodType, + ) { const { accessToken } = await uoslifeBridge.getAccessToken(); if (accessToken) return await fetch(input, { headers: { Authorization: `Bearer ${accessToken}` }, + method, }); } } diff --git a/src/api/service.ts b/src/api/service.ts index ac15527..13502ae 100644 --- a/src/api/service.ts +++ b/src/api/service.ts @@ -7,4 +7,10 @@ export default class APIService extends APIClient { url: 'library-histories/recap?year=2023', }); } + static async saveLibraryHistories() { + return await this.APIclient({ + url: 'library-histories/save?year=2023', + method: 'POST', + }); + } } diff --git a/src/api/types/index.ts b/src/api/types/index.ts index 38dbbdb..f811df9 100644 --- a/src/api/types/index.ts +++ b/src/api/types/index.ts @@ -29,3 +29,5 @@ export type RecapInfoType = { export type GetLibraryHistoriesResponse = RecapInfoType & Partial; + +export type FetchMethodType = 'GET' | 'POST'; diff --git a/src/hooks/useInitApp.ts b/src/hooks/useInitApp.ts index 7ade65a..9e291ed 100644 --- a/src/hooks/useInitApp.ts +++ b/src/hooks/useInitApp.ts @@ -19,9 +19,13 @@ const useInitApp = () => { const handleRecapInfo = async () => { const recapInfo = await APIService.getLibraryHistories(); if (recapInfo.status === 400) { - alert('2023년 도서관 이용 기록이 없어요.'); - await uoslifeBridge.goBack(); - return; + await APIService.saveLibraryHistories(); + const retryRecapInfo = await APIService.getLibraryHistories(); + if (retryRecapInfo.status === 400) { + alert('2023년 도서관 이용 기록이 없어요.'); + await uoslifeBridge.goBack(); + return; + } } if (recapInfo.status === 401) { alert('서비스를 이용하기 위해 앱 재접속이 필요합니다.');