/withAxios.ts Secret
Last active
August 1, 2021 06:10
nexters 5th (1)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const createMockMethod = (instance: AxiosInstance) => {} | |
export interface AxiosInstanceWithMock extends AxiosInstance { | |
mock: () => MockAxiosInstance | |
} | |
const extendWithMock = (instance: AxiosInstance): AxiosInstanceWithMock => { | |
const instanceWithMock = Object.create(instance) | |
instanceWithMock.mock = createMockMethod(instanceWithMock) | |
return instanceWithMock | |
} | |
const createAxiosInstance = () => { | |
const instance = axios.create() | |
instance.interceptors.response.use(AuthInterceptor) | |
return extendWithMock(instance) | |
} | |
export const globalAxiosInstance = createAxiosInstance() | |
export const withAxios = async <T>(request: RequestConfig): Promise<T> => { | |
/** | |
* @requires response 반드시 proxy 통해서 외부 서버와 통신합니다. | |
*/ | |
const response = await globalAxiosInstance.request<T, T>({ | |
...request, | |
baseURL: `${isSSR ? HOST_URL : ''}/api`, | |
}) | |
return response | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment