Skip to content

Commit

Permalink
[#1] 환경설정변경
Browse files Browse the repository at this point in the history
- useQuery 파일들 변경
  • Loading branch information
JoGeumJu committed Apr 15, 2023
1 parent 7b44951 commit d7eaf11
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 37 deletions.
26 changes: 13 additions & 13 deletions src/apis/normalizeAxiosError.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { sessionService } from '../libs/sessionService';
import axios from 'axios';
import { tokenService } from "../libs/tokenService";
import axios from "axios";

export const UNAUTH_ERROR = 'UNAUTH_ERROR';
export const CLIENT_ERROR = 'CLIENT_ERROR';
export const SERVER_ERROR = 'SERVER_ERROR';
export const CANCEL_ERROR = 'CANCEL_ERROR';
export const TIMEOUT_ERROR = 'TIMEOUT_ERROR';
export const NETWORK_ERROR = 'NETWORK_ERROR';
export const UNKNOWN_ERROR = 'UNKNOWN_ERROR';
export const UNAUTH_ERROR = "UNAUTH_ERROR";
export const CLIENT_ERROR = "CLIENT_ERROR";
export const SERVER_ERROR = "SERVER_ERROR";
export const CANCEL_ERROR = "CANCEL_ERROR";
export const TIMEOUT_ERROR = "TIMEOUT_ERROR";
export const NETWORK_ERROR = "NETWORK_ERROR";
export const UNKNOWN_ERROR = "UNKNOWN_ERROR";

function xhrError({ code, status = 0, data = {} }: any) {
const err: any = new Error();
err.name = 'XhrError';
err.name = "XhrError";
err.message = err.code = code;
err.data = data;
err.status = status;
Expand All @@ -20,7 +20,7 @@ function xhrError({ code, status = 0, data = {} }: any) {

export default function normalizeAxiosError(error: any) {
// timeout
if (error.code === 'ECONNABORTED') {
if (error.code === "ECONNABORTED") {
return xhrError({
code: TIMEOUT_ERROR,
});
Expand All @@ -37,8 +37,8 @@ export default function normalizeAxiosError(error: any) {
// service error, 400 401 404 500 502...
if (error.response) {
if ([401, 423].includes(error.response.status)) {
sessionService.resetIdSession();
location.replace('/login?warn');
// tokenService.resetToken();
location.replace("/login?warn");

return xhrError({
code: UNAUTH_ERROR,
Expand Down
26 changes: 13 additions & 13 deletions src/apis/sendApi.ts
Original file line number Diff line number Diff line change
@@ -1,85 +1,85 @@
import { sessionService } from "../libs/sessionService";
import { tokenService } from "../libs/tokenService";
import api from "./api";

// eslint-disable-next-line import/no-anonymous-default-export
export default {
get: async (url: string, params?: any) => {
const idSession = await sessionService.getIdSession();
const token = await tokenService.getToken();

return params !== undefined
? api({
url: url,
method: "get",
params: params,
headers: {
Authorization: `${idSession}`,
Authorization: `${token}`,
},
})
: api({
url: url,
method: "get",
headers: {
Authorization: `${idSession}`,
Authorization: `${token}`,
},
});
},
post: async (url: string, params: any) => {
const idSession = await sessionService.getIdSession();
const token = await tokenService.getToken();

return params !== undefined
? api({
url: url,
method: "post",
data: params,
headers: {
Authorization: `${idSession}`,
Authorization: `${token}`,
},
})
: api({
url: url,
method: "post",
headers: {
Authorization: `${idSession}`,
Authorization: `${token}`,
},
});
},
put: async (url: string, params: any) => {
const idSession = await sessionService.getIdSession();
const token = await tokenService.getToken();

return params !== undefined
? api({
url: url,
method: "put",
params: params,
headers: {
Authorization: `${idSession}`,
Authorization: `${token}`,
},
})
: api({
url: url,
method: "put",
headers: {
Authorization: `${idSession}`,
Authorization: `${token}`,
},
});
},
delete: async (url: string, params?: any) => {
const idSession = await sessionService.getIdSession();
const token = await tokenService.getToken();

return params !== undefined
? api({
url: url,
method: "delete",
data: params,
headers: {
Authorization: `${idSession}`,
Authorization: `${token}`,
},
})
: api({
url: url,
method: "delete",
headers: {
Authorization: `${idSession}`,
Authorization: `${token}`,
},
});
},
Expand Down
11 changes: 0 additions & 11 deletions src/libs/sessionService.ts

This file was deleted.

10 changes: 10 additions & 0 deletions src/libs/tokenService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const key = {
token: "token",
};

export const tokenService = {
/** ID session */
getToken: () => localStorage.getItem(key.token) ?? "",
setToken: (token: string) => localStorage.setItem(key.token, token),
resetToken: () => localStorage.setItem(key.token, ""),
};

0 comments on commit d7eaf11

Please sign in to comment.