-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- useQuery 파일들 추가
- Loading branch information
Showing
6 changed files
with
235 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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; |
This file contains 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
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; | ||
} |
This file contains 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
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 }; |
This file contains 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
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, | ||
}); | ||
} |
This file contains 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
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}`, | ||
}, | ||
}); | ||
}, | ||
}; |
This file contains 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
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, ''), | ||
}; |