Skip to content

Commit

Permalink
feat: delete 메소드에 refreshToken 로직 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
jiwonh423 committed Sep 21, 2023
1 parent 479ff97 commit f65b2f0
Showing 1 changed file with 24 additions and 28 deletions.
52 changes: 24 additions & 28 deletions frontend/src/apis/deleteApi.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,35 @@
// const API_URL =
// process.env.NODE_ENV === 'production'
// ? process.env.REACT_APP_API_DEFAULT_PROD
// : process.env.REACT_APP_API_DEFAULT_DEV;

const API_URL = process.env.NODE_ENV === 'development' ? 'http://localhost:3000' : DEFAULT_PROD_URL

import { DEFAULT_PROD_URL } from '../constants';
import { ContentTypeType } from '../types/Api';
import withTokenRefresh from './utils';

interface Headers {
'content-type': string;
[key: string]: string;
}

export const deleteApi = async (url: string, contentType?: ContentTypeType) => {
const apiUrl = `${DEFAULT_PROD_URL + url}`;
const userToken = localStorage.getItem('userToken');
const headers: Headers = {
'content-type': 'application/json',
};

if (userToken) {
headers['Authorization'] = `Bearer ${userToken}`;
}

if (contentType) {
headers['content-type'] = contentType;
}

const response = await fetch(apiUrl, {
method: 'DELETE',
headers,
return await withTokenRefresh(async () => {
const apiUrl = `${DEFAULT_PROD_URL + url}`;
const userToken = localStorage.getItem('userToken');
const headers: Headers = {
'content-type': 'application/json',
};

if (userToken) {
headers['Authorization'] = `Bearer ${userToken}`;
}

if (contentType) {
headers['content-type'] = contentType;
}

const response = await fetch(apiUrl, {
method: 'DELETE',
headers,
});

if (response.status >= 400) {
throw new Error('[SERVER] DELETE 요청에 실패했습니다.');
}
});

if (response.status >= 400) {
throw new Error('[SERVER] DELETE 요청에 실패했습니다.');
}
};

0 comments on commit f65b2f0

Please sign in to comment.