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
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import createAxiosInstance from '@service/feature/common/axios/axiosInstance.ts';
import {
LoginRequest,
LoginResponse,
RegisterRequest,
RegisterResponse,
} from '@service/feature/auth/types/auth.ts';

const axios = createAxiosInstance('members');

export const login = async (data: LoginRequest): Promise<LoginResponse> => {
const response = await axios.post('/sign-in', data);
return response.data.data;
};

export const register = async (
data: RegisterRequest,
): Promise<RegisterResponse> => {
const response = await axios.post('/sign-up', data);
return response.data;
};
import createAxiosInstance from '@service/feature/common/axios/axiosInstance.ts';
import {
LoginRequest,
LoginResponse,
RegisterRequest,
RegisterResponse,
} from '@service/feature/auth/types/auth.ts';
const axios = createAxiosInstance();
export const login = async (data: LoginRequest): Promise<LoginResponse> => {
const response = await axios.post('/sign-in', data);
return response.data.data;
};
export const register = async (
data: RegisterRequest,
): Promise<RegisterResponse> => {
const response = await axios.post('/sign-up', data);
return response.data;
};
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import createAxiosInstance from '@service/feature/common/axios/axiosInstance.ts';
import { UserProfile } from '@service/feature/auth/types/profile.ts';
import { ApiResponse } from '@service/feature/common/axios/apiType.ts';

const axios = createAxiosInstance('members');

export const getProfile = async (): Promise<UserProfile> => {
const response = await axios.get<ApiResponse<UserProfile>>('/members');
return response.data.data;
};
import createAxiosInstance from '@service/feature/common/axios/axiosInstance.ts';
import { UserProfile } from '@service/feature/auth/types/profile.ts';
import { ApiResponse } from '@service/feature/common/axios/apiType.ts';
const axios = createAxiosInstance('members');
export const getProfile = async (): Promise<UserProfile> => {
const response = await axios.get<ApiResponse<UserProfile>>('/members');
return response.data.data;
};
23 changes: 0 additions & 23 deletions src/service/feature/auth/types/auth.ts
Original file line number Diff line number Diff line change
@@ -1,23 +0,0 @@
export interface LoginRequest {
email: string;
password: string;
}

export interface LoginResponse {
id: string;
name: string;
type: 'MEMBER' | 'ADMIN' | string;
token: string;
}

export interface RegisterRequest {
email: string;
password: string;
nickname: string;
}

export interface RegisterResponse {
userId: string;
email: string;
nickname: string;
}
13 changes: 0 additions & 13 deletions src/service/feature/auth/types/profile.ts
Original file line number Diff line number Diff line change
@@ -1,13 +0,0 @@
export type MemberState = 'ONLINE' | 'OFFLINE' | 'IDLE' | 'DO_NOT_DISTURB';

export interface UserProfile {
id: string;
email: string;
nickname: string;
name: string;
birth: string;
type: 'MEMBER' | 'ADMIN' | string;
avatarUrl: string | null;
state: MemberState;
createdAt: string;
}
5 changes: 0 additions & 5 deletions src/service/feature/common/axios/apiType.ts
Original file line number Diff line number Diff line change
@@ -1,5 +0,0 @@
export interface ApiResponse<T> {
status: number;
message: string | null;
data: T;
}
73 changes: 0 additions & 73 deletions src/service/feature/common/axios/axiosInstance.ts
Original file line number Diff line number Diff line change
@@ -1,73 +0,0 @@
import axios, { AxiosInstance, AxiosError, AxiosResponse } from 'axios';
import { toast } from 'sonner';
import { ERROR_MESSAGES } from '../../../lib/const/toast/errorMessage';
import { getCookie } from '../../auth/lib/getCookie';

export type ServiceType = 'members' | 'teams' | 'dialog';

const API_CONFIG = {
BASE_DOMAIN: 'https://flowchat.shop:30200',
HEADERS: {
JSON: 'application/json',
},
} as const;

interface ErrorResponse {
message: string;
}

const handleAxiosError = (error: AxiosError<ErrorResponse>) => {
const { response } = error;
if (!response) {
toast.error('네트워크 오류 또는 서버 응답 없음');
return Promise.reject(error);
}

const errorMessages: Record<number, string> = {
401: ERROR_MESSAGES.UNAUTHORIZED,
403: ERROR_MESSAGES.FORBIDDEN,
500: ERROR_MESSAGES.SERVER_ERROR,
};

toast.error(
errorMessages[response.status] ||
(response.data && response.data.message) ||
ERROR_MESSAGES.DEFAULT_ERROR,
);

if (response.status === 401) {
// TODO: logout 처리 또는 /login 리디렉션
}

return Promise.reject(error);
};

export const createAxiosInstance = (): AxiosInstance => {
const instance = axios.create({
baseURL: API_CONFIG.BASE_DOMAIN,
headers: {
'Content-Type': API_CONFIG.HEADERS.JSON,
},
withCredentials: true,
});

instance.interceptors.request.use(
(config) => {
const token = getCookie('accessToken');
if (token) {
config.headers.Authorization = `Bearer ${token}`;
}
return config;
},
(error) => Promise.reject(error)
);

instance.interceptors.response.use(
(response: AxiosResponse) => response,
handleAxiosError
);

return instance;
};

export default createAxiosInstance;
Empty file.