Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/lib/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
//////////////////////////
// product domain //
//////////////////////////

// product
export * from './product.type';

Expand All @@ -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';
1 change: 1 addition & 0 deletions src/lib/types/ouath-provider.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type OAuthProviderType = 'NAVER' | 'KAKAO' | 'APPLE' | 'BASIC';
4 changes: 3 additions & 1 deletion src/lib/types/session.type.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {OAuthProviderType} from './ouath-provider.type';

export type SessionType = {
accessToken: string;
refreshToken?: string;
Expand All @@ -9,5 +11,5 @@ export type SessionType = {
* @description refreshToken 만료 시간 (Oauth 로그인 시)
*/
refreshTokenExpiresAt?: number;
OAuthProvider: 'NAVER' | 'KAKAO' | 'APPLE' | 'BASIC';
OAuthProvider: OAuthProviderType;
};
53 changes: 27 additions & 26 deletions src/network/api-client/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
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: {
Expand Down Expand Up @@ -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<void> {
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()');
Expand Down
Loading