diff --git a/src/lib/types/index.ts b/src/lib/types/index.ts index 0d4372f..4cbf327 100644 --- a/src/lib/types/index.ts +++ b/src/lib/types/index.ts @@ -1,3 +1,7 @@ +////////////////////////// +// product domain // +////////////////////////// + // product export * from './product.type'; @@ -16,8 +20,15 @@ export * from './review.type'; // session export * from './session.type'; +/////////////////////// +// common type // +/////////////////////// + // storage export * from './storage.type'; // weekday export * from './weekday.type'; + +// oauth provider +export * from './ouath-provider.type'; diff --git a/src/lib/types/ouath-provider.type.ts b/src/lib/types/ouath-provider.type.ts new file mode 100644 index 0000000..d67d176 --- /dev/null +++ b/src/lib/types/ouath-provider.type.ts @@ -0,0 +1 @@ +export type OAuthProviderType = 'NAVER' | 'KAKAO' | 'APPLE' | 'BASIC'; diff --git a/src/lib/types/session.type.ts b/src/lib/types/session.type.ts index e40dbcd..119d552 100644 --- a/src/lib/types/session.type.ts +++ b/src/lib/types/session.type.ts @@ -1,3 +1,5 @@ +import {OAuthProviderType} from './ouath-provider.type'; + export type SessionType = { accessToken: string; refreshToken?: string; @@ -9,5 +11,5 @@ export type SessionType = { * @description refreshToken 만료 시간 (Oauth 로그인 시) */ refreshTokenExpiresAt?: number; - OAuthProvider: 'NAVER' | 'KAKAO' | 'APPLE' | 'BASIC'; + OAuthProvider: OAuthProviderType; }; diff --git a/src/network/api-client/api-client.ts b/src/network/api-client/api-client.ts index a87aeee..877dfea 100644 --- a/src/network/api-client/api-client.ts +++ b/src/network/api-client/api-client.ts @@ -20,34 +20,9 @@ class ApiClient { private sessionOptions: ApiClientOptions['sessionOptions']; - private async expiredSession() { - await this.sessionOptions.setStorage('session', {}); - this._jwt = null; - const expiredError = new CustomError({ - errorCode: 401, - errorMessage: '세션이 만료되어 로그아웃되었습니다.', - }); - throw expiredError; - } - - private async setAuthorizationHeader(config: InternalAxiosRequestConfig): Promise { - const session: SessionType | null = await this.sessionOptions.getStorage('session'); - this._jwt = session?.accessToken ?? null; - - if (!this._jwt) { - return; - } - - // accessToken이 있으면 Authorization 헤더에 추가 - if (this._jwt) { - config.headers.Authorization = `Bearer ${this._jwt}`; - } else { - config.headers.Authorization = null; - } - } - private constructor({serverApiBaseUrl, sessionOptions}: ApiClientOptions) { this.sessionOptions = sessionOptions; + this.axiosInstance = axios.create({ baseURL: serverApiBaseUrl, headers: { @@ -107,6 +82,32 @@ class ApiClient { ); } + private async expiredSession() { + await this.sessionOptions.setStorage('session', {}); + this._jwt = null; + const expiredError = new CustomError({ + errorCode: 401, + errorMessage: '세션이 만료되어 로그아웃되었습니다.', + }); + throw expiredError; + } + + private async setAuthorizationHeader(config: InternalAxiosRequestConfig): Promise { + const session: SessionType | null = await this.sessionOptions.getStorage('session'); + this._jwt = session?.accessToken ?? null; + + if (!this._jwt) { + return; + } + + // accessToken이 있으면 Authorization 헤더에 추가 + if (this._jwt) { + config.headers.Authorization = `Bearer ${this._jwt}`; + } else { + config.headers.Authorization = null; + } + } + public static getInstance(): ApiClient { if (!ApiClient.instance) { throw new Error('ApiClient is not initialized:\nApiClient.create() must be called before getInstance()');