- 로그인 화면 기능 로직 1차 구현 중
This commit is contained in:
41
src/network/AccountNetwork.ts
Normal file
41
src/network/AccountNetwork.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import {
|
||||
CheckDuplicationRequest,
|
||||
SendVerificationCodeRequest,
|
||||
VerifyCodeRequest,
|
||||
SignupRequest,
|
||||
LoginRequest
|
||||
} from "@/data/request";
|
||||
import {
|
||||
CheckDuplicationResponse,
|
||||
SendVerificationCodeResponse,
|
||||
VerifyCodeResponse,
|
||||
SignupResponse,
|
||||
LoginResponse
|
||||
} from "@/data/response";
|
||||
import { BaseNetwork } from "./BaseNetwork";
|
||||
|
||||
export class AccountNetwork extends BaseNetwork {
|
||||
private baseUrl = "/account";
|
||||
|
||||
async checkDuplication(data: CheckDuplicationRequest) {
|
||||
const { type, value } = data;
|
||||
|
||||
return await this.instance.get<CheckDuplicationResponse>(`${this.baseUrl}/check-duplication?type=${type}&value=${value}`);
|
||||
}
|
||||
|
||||
async sendVerificationCode(data: SendVerificationCodeRequest) {
|
||||
return await this.instance.post<SendVerificationCodeResponse>(this.baseUrl + "/send-verification-code", data);
|
||||
}
|
||||
|
||||
async verifyCode(data: VerifyCodeRequest) {
|
||||
return await this.instance.post<VerifyCodeResponse>(this.baseUrl + "/verify-code", data);
|
||||
}
|
||||
|
||||
async signup(data: SignupRequest) {
|
||||
return await this.instance.post<SignupResponse>(this.baseUrl + "/signup", data);
|
||||
}
|
||||
|
||||
async login(data: LoginRequest) {
|
||||
return await this.instance.post<LoginResponse>(this.baseUrl + "/login", data);
|
||||
}
|
||||
}
|
||||
71
src/network/BaseNetwork.ts
Normal file
71
src/network/BaseNetwork.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import axios from 'axios';
|
||||
import type {
|
||||
AxiosInstance,
|
||||
AxiosRequestConfig,
|
||||
AxiosError,
|
||||
AxiosResponse,
|
||||
} from "axios";
|
||||
|
||||
export class BaseNetwork {
|
||||
protected instance: AxiosInstance;
|
||||
|
||||
constructor() {
|
||||
this.instance = axios.create({
|
||||
baseURL: import.meta.env.VITE_API_URL || "http://localhost:3000",
|
||||
timeout: 10_000,
|
||||
withCredentials: true,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
|
||||
this.setInterceptors();
|
||||
}
|
||||
|
||||
/**
|
||||
* 요청/응답 인터셉터 설정
|
||||
*/
|
||||
protected setInterceptors() {
|
||||
// ★ 요청 인터셉터
|
||||
this.instance.interceptors.request.use(
|
||||
(config) => {
|
||||
// 예: 자동 토큰 추가
|
||||
// const token = localStorage.getItem("token");
|
||||
// if (token) {
|
||||
// config.headers.Authorization = `Bearer ${token}`;
|
||||
// }
|
||||
|
||||
return config;
|
||||
},
|
||||
(error: AxiosError) => Promise.reject(error)
|
||||
);
|
||||
|
||||
// ★ 응답 인터셉터
|
||||
this.instance.interceptors.response.use(
|
||||
(response: AxiosResponse) => response,
|
||||
(error: AxiosError) => {
|
||||
const message =
|
||||
(error.response?.data as any)?.message ||
|
||||
error.message ||
|
||||
"Unknown error";
|
||||
|
||||
return Promise.reject({
|
||||
status: error.response?.status,
|
||||
message,
|
||||
raw: error,
|
||||
});
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 기본 CRUD 메서드
|
||||
*/
|
||||
protected async get<T = any>(url: string, config?: AxiosRequestConfig) {
|
||||
return await this.instance.get<T>(url, config);
|
||||
}
|
||||
|
||||
protected async post<T = any>(url: string, data?: any, config?: AxiosRequestConfig) {
|
||||
return await this.instance.post<T>(url, data, config);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user