Skip to content

Commit

Permalink
feat: 최소 지연 설정 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
JaeHongDev committed Jul 4, 2024
1 parent 672a61f commit 3d7b67e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
5 changes: 3 additions & 2 deletions frontend/src/api/AxiosInstance.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from "axios";
import {NETWORK} from "@/constants/api";
import {checkAndSetToken, handleTokenError} from "@/api/Interceptors";
import {checkAndSetToken, delayFulfilled, handleTokenError, waitingFulfilled} from "@/api/Interceptors";

export const axiosInstance = axios.create({
baseURL: `${import.meta.env.VITE_API}/api`,
Expand All @@ -10,4 +10,5 @@ export const axiosInstance = axios.create({
})

axiosInstance.interceptors.request.use(checkAndSetToken);
axiosInstance.interceptors.response.use(response => response, handleTokenError);
axiosInstance.interceptors.request.use(delayFulfilled);
axiosInstance.interceptors.response.use(waitingFulfilled, handleTokenError);
26 changes: 25 additions & 1 deletion frontend/src/api/Interceptors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {AxiosError, InternalAxiosRequestConfig} from "axios";
import {AxiosError, AxiosResponse, InternalAxiosRequestConfig} from "axios";
import {TOKEN} from "@/constants/api";
import {PATH} from "@/constants/path";
import {reIssueToken} from "@/api/auth/ReIssueToken";
Expand Down Expand Up @@ -54,3 +54,27 @@ export const handleTokenError = async(error: AxiosError<ErrorResponseData>) => {
throw new HTTPError(status, data.message, data.code);

}


export const delayFulfilled = (config: InternalAxiosRequestConfig )=> ({
...config,
p0: performance.now(),
});

export const waitingFulfilled = async (response: AxiosResponse) => {
const minimumDelay = 1000;
const latency = performance.now() - response.config.p0;
const shouldNotDelay = minimumDelay < latency;

if (shouldNotDelay) {
return response;
}

const remainder = minimumDelay - latency;
const [responseWithDelay] = await Promise.all([
response,
new Promise((resolve) => setTimeout(resolve, remainder)),
]);
return responseWithDelay;
}

4 changes: 4 additions & 0 deletions frontend/src/api/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ declare module 'axios' {
export interface AxiosRequestConfig {
useAuth: boolean;
}

export interface InternalAxiosRequestConfig {
p0: number;
}
}

0 comments on commit 3d7b67e

Please sign in to comment.