Skip to content

Commit

Permalink
Feat: axios 및 env 설정 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
leewooseong committed Apr 22, 2024
1 parent 12ed398 commit 672b4f4
Show file tree
Hide file tree
Showing 33 changed files with 83 additions and 0 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
76 changes: 76 additions & 0 deletions frontend/app/axios.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
'use client'
// :: Production Axios Instance
import axios, { AxiosError, AxiosResponse, InternalAxiosRequestConfig } from 'axios'

// :: create Instance
const baseConfig = {
baseURL: process.env.NEXT_PUBLIC_BASE_URL,
timeout: 10 * 1000,
}

const instance = axios.create(baseConfig)
const tokenInstance = axios.create(baseConfig)
const multipartInstance = axios.create(baseConfig)
const tokenMultipartInstance = axios.create(baseConfig)

// :: interceptor setting
// 1. request
// - 요청이 전달되기 전에 작업 수행
const requestPrev = (config: InternalAxiosRequestConfig): InternalAxiosRequestConfig => {
config.headers['Content-Type'] = 'application/json'
config.baseURL = process.env.NEXT_PUBLIC_BASE_URL
return config
}
const tokenReqPrev = (config: InternalAxiosRequestConfig): InternalAxiosRequestConfig => {
config.headers['Content-Type'] = 'application/json'
config.baseURL = process.env.NEXT_PUBLIC_BASE_URL
config.withCredentials = true

return config
}
const multipartReqPrev = (config: InternalAxiosRequestConfig): InternalAxiosRequestConfig => {
config.headers['Content-Type'] = 'multipart/form-data'
config.baseURL = process.env.NEXT_PUBLIC_BASE_URL
return config
}
const tokenMultipartReqPrev = (config: InternalAxiosRequestConfig): InternalAxiosRequestConfig => {
config.headers['Content-Type'] = 'multipart/form-data'
config.baseURL = process.env.NEXT_PUBLIC_BASE_URL
config.withCredentials = true

return config
}
// 요청 오류가 있는 작업 수행
const requestError = (error: unknown): Promise<never> => {
return Promise.reject(error)
}

// 2. response
// - 2xx 범위에 있는 상태 코드는 이 함수를 트리거 합니다.
// - 응답 데이터가 있는 작업 수행
const resolveResponse = (response: AxiosResponse): AxiosResponse => response
// - 2xx 외의 범위에 있는 상태 코드는 이 함수를 트리거 합니다.
// - 응답 오류가 있는 작업 수행
const responseError = (error: AxiosError): Promise<never> => {
console.log(error)
return Promise.reject(error)
}

instance.interceptors.request.use(requestPrev, requestError)
instance.interceptors.response.use(resolveResponse, responseError)

multipartInstance.interceptors.request.use(multipartReqPrev, requestError)
multipartInstance.interceptors.response.use(resolveResponse, responseError)

tokenInstance.interceptors.request.use(tokenReqPrev, requestError)
tokenInstance.interceptors.response.use(resolveResponse, responseError)

tokenMultipartInstance.interceptors.request.use(tokenMultipartReqPrev, requestError)
tokenMultipartInstance.interceptors.response.use(resolveResponse, responseError)

// :: axios Error 여부 판단
const isAxiosError = <E>(err: unknown | AxiosError<E>): err is AxiosError => {
return axios.isAxiosError(err)
}

export { instance, tokenInstance, multipartInstance, tokenMultipartInstance, isAxiosError }
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file.
Empty file.
7 changes: 7 additions & 0 deletions frontend/app/lobby/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from 'react'

const Lobby = () => {
return <div>Lobby</div>
}

export default Lobby
Empty file added frontend/app/lobby/ui/.gitkeep
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.

0 comments on commit 672b4f4

Please sign in to comment.