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 57c2b31 commit e7b9fa0
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/apis/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import axios from 'axios';
import applyDefaultParams from './applyDefaultParams';

import normalizeAxiosError, {
SERVER_ERROR,
TIMEOUT_ERROR,
NETWORK_ERROR,
CLIENT_ERROR,
UNAUTH_ERROR,
} from './normalizeAxiosError';
import { settings } from '@street-vendor/core';

const API_TIMEOUT = 50000; // 50s

const api = axios.create({
baseURL: getBaseUrl(),
timeout: API_TIMEOUT,
withCredentials: false,
});

function getBaseUrl() {
return settings.api.baseUrl;
}

api.interceptors.request.use(applyDefaultParams);
api.interceptors.response.use(undefined, normalizeAxiosError);
api.interceptors.response.use(undefined, function (err) {
console.log('ERR :: ', err);

if (err.code === TIMEOUT_ERROR || err.code === NETWORK_ERROR) {
//
}
if (err.code === UNAUTH_ERROR) {
//
}
if (err.code === SERVER_ERROR) {
//
}
if (err.code === CLIENT_ERROR) {
//
}
return Promise.reject(err);
});

export default api;
10 changes: 10 additions & 0 deletions src/apis/applyDefaultParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default function applyDefaultParams(config: any) {
config.params = { ...config.defaultParams, ...config.params };
if (config.defaultData && !(config.data instanceof FormData)) {
config.data = { ...config.defaultData, ...config.data };
if (Object.keys(config.data).length === 0) {
config.data = null;
}
}
return config;
}
4 changes: 4 additions & 0 deletions src/apis/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import api from './api';
import sendApi from './sendApi';

export { api as default, api, sendApi };
79 changes: 79 additions & 0 deletions src/apis/normalizeAxiosError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { sessionService } from '../libs/sessionService';
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';

function xhrError({ code, status = 0, data = {} }: any) {
const err: any = new Error();
err.name = 'XhrError';
err.message = err.code = code;
err.data = data;
err.status = status;
return Promise.reject(err);
}

export default function normalizeAxiosError(error: any) {
// timeout
if (error.code === 'ECONNABORTED') {
return xhrError({
code: TIMEOUT_ERROR,
});
}

// cancel
if (axios.isCancel(error)) {
// return xhrError({
// code: CANCEL_ERROR
// })
return; /** ignore */
}

// service error, 400 401 404 500 502...
if (error.response) {
if ([401, 423].includes(error.response.status)) {
sessionService.resetIdSession();
location.replace('/login?warn');

return xhrError({
code: UNAUTH_ERROR,
status: error.response.status,
data: error.response.data,
});
}
if (error.response.status >= 400 && error.response.status < 500) {
return xhrError({
code: CLIENT_ERROR,
status: error.response.status,
data: error.response.data,
});
}
if (error.response.status >= 500 && error.response.status < 600) {
return xhrError({
code: SERVER_ERROR,
status: error.response.status,
data: error.response.data,
});
}
return xhrError({
code: UNKNOWN_ERROR,
});
}

// network error
if (error.request) {
return xhrError({
code: NETWORK_ERROR,
});
}

// throw error on request config?
return xhrError({
code: UNKNOWN_ERROR,
});
}
86 changes: 86 additions & 0 deletions src/apis/sendApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { sessionService } from "../libs/sessionService";
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();

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

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

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

return params !== undefined
? api({
url: url,
method: "delete",
data: params,
headers: {
Authorization: `${idSession}`,
},
})
: api({
url: url,
method: "delete",
headers: {
Authorization: `${idSession}`,
},
});
},
};
11 changes: 11 additions & 0 deletions src/libs/sessionService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const key = {
idSession: 'ID_SESSION',
};

export const sessionService = {
/** ID session */
getIdSession: () => localStorage.getItem(key.idSession) ?? '',
setIdSession: (session: string) =>
localStorage.setItem(key.idSession, `Bearer ${session}`),
resetIdSession: () => localStorage.setItem(key.idSession, ''),
};

0 comments on commit e7b9fa0

Please sign in to comment.