axios?
- ๋ธ๋ผ์ฐ์ ๋ฅผ ์ํ Promise ๊ธฐ๋ฐ์ ํด๋ผ์ด์ธํธ
- ์๋๋ “XHR”์ด๋ผ๋ ๋ธ๋ผ์ฐ์ ๋ด์ฅ ๊ฐ์ฒด๋ฅผ ํ์ฉํด AJAX ์์ฒญ์ ์ฒ๋ฆฌํ๋๋ฐ, ์ด๋ณด๋ค ํธ๋ฆฌํ AJAX ์์ฒญ์ด ๊ฐ๋ฅํ๋๋ก ๋์์ ์ค
- ํ์ฅ ๊ฐ๋ฅํ ์ธํฐํ์ด์ค์ ํจ๊ป ํจํค์ง๋ก ์ฌ์ฉ์ด ๊ฐํธํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ ๊ณต
์ค์น
# npm ์ฌ์ฉํ๊ธฐ:
$ npm install axios
# yarn ์ฌ์ฉํ๊ธฐ:
$ yarn add axios
์ฌ์ฉ
axios.get('<https://jsonplaceholder.typicode.com/todos/1>') //Promise return
.then(..)
.catch(..)
//Axios ์ฌ์ฉํ๊ธฐ
const URL = '<https://jsonplaceholder.typicode.com/todos/1><https://jsonplaceholder.typicode.com/todos/1>>'
async function test(){
const response = await axios.get(URL)
const data = response.data
console.log(data.title)
}
test().catch(err=>console.log(err))
Axios API
axios์ ํด๋น config๋ฅผ ์ ์กํ๋ฉด ์์ฒญ์ด ๊ฐ๋ฅ
- axios(config)
// node.js์์ GET ์์ฒญ์ผ๋ก ์๊ฒฉ ์ด๋ฏธ์ง ๊ฐ์ ธ์ค๊ธฐ axios({ method: 'get', url: '<http://bit.ly/2mTM3nY>', responseType: 'stream' }) .then(function (response) { response.data.pipe(fs.createWriteStream('ada_lovelace.jpg')) });
// POST ์์ฒญ ์ ์ก
axios({
method: 'post',
url: '/user/12345',
data: {
firstName: 'Fred',
lastName: 'Flintstone'
}
});
- axios(url[,config])
// GET ์์ฒญ ์ ์ก (๊ธฐ๋ณธ๊ฐ)
axios('/user/12345');
Axios ์ธ์คํด์ค
์ฌ์ฉ์ ์ง์ config๋ก ์๋ก์ด Axios ์ธ์คํด์ค๋ฅผ ๋ง๋ค์ ์์
- axios.create([config])
const instance = axios.create({
baseURL: 'https://some-domain.com/api/',
timeout: 1000,
headers: {'X-Custom-Header': 'foobar'}
});
์์ฒญ config
- ์์ฒญ์ ๋ง๋๋๋ฐ ์ฌ์ฉํ ์ ์๋ config ์ต์ ๋ค
- url ๋ง ํ์
- method ๋ฅผ ์ง์ ํ์ง ์์ผ๋ฉด GET ๋ฐฉ์์ด ๊ธฐ๋ณธ๊ฐ
{
// `url`์ ์์ฒญ์ ์ฌ์ฉ๋ ์๋ฒ URL์
๋๋ค.
**url: '/user',**
// `method`๋ ์์ฒญ์ ์์ฑํ ๋ ์ฌ์ฉ๋๋ ๋ฉ์๋์
๋๋ค.
**method: 'get', // ๊ธฐ๋ณธ๊ฐ**
// `url`์ด ์ ๋๊ฐ์ด ์๋ ๊ฒฝ์ฐ `baseURL`์ URL ์์ ๋ถ์ต๋๋ค.
// ์๋์ ์ธ URL์ ์ธ์คํด์ค ๋ฉ์๋์ ์ ๋ฌํ๋ ค๋ฉด `baseURL`์ ์ค์ ํ๋ ๊ฒ์ ํธ๋ฆฌํฉ๋๋ค.
**baseURL: '<https://some-domain.com/api>',**
// `transformRequest`๋ ์์ฒญ ๋ฐ์ดํฐ๋ฅผ ์๋ฒ๋ก ์ ์กํ๊ธฐ ์ ์ ๋ณ๊ฒฝํ ์ ์๊ฒ ํด์ค๋๋ค.
// ์ด๊ฒ์ 'PUT', 'POST', 'PATCH', 'DELETE' ๋ฉ์๋์์๋ง ์ ์ฉ๋ฉ๋๋ค.
// ๋ง์ง๋ง ํจ์๋ Buffer, ArrayBuffer, FormData ๋๋ Stream์ ์ธ์คํด์ค ๋๋ ๋ฌธ์์ด์ ๋ฐํํด์ผ ํฉ๋๋ค.
// ํค๋ ๊ฐ์ฒด๋ฅผ ์์ ํ ์ ์์ต๋๋ค.
transformRequest: [function (data, headers) {
// ๋ฐ์ดํฐ๋ฅผ ๋ณํํ๋ ค๋ ์์
์ํ
return data;
}],
// `transformResponse`๋ ์๋ต ๋ฐ์ดํฐ๊ฐ then/catch๋ก ์ ๋ฌ๋๊ธฐ ์ ์ ๋ณ๊ฒฝํ ์ ์๊ฒ ํด์ค๋๋ค.
transformResponse: [function (data) {
// ๋ฐ์ดํฐ๋ฅผ ๋ณํํ๋ ค๋ ์์
์ํ
return data;
}],
// `headers`๋ ์ฌ์ฉ์ ์ง์ ํค๋์
๋๋ค.
**headers**: {'X-Requested-With': 'XMLHttpRequest'},
// `params`์ ์์ฒญ๊ณผ ํจ๊ป ์ ์ก๋๋ URL ํ๋ผ๋ฏธํฐ์
๋๋ค.
// ๋ฐ๋์ ์ผ๋ฐ ๊ฐ์ฒด๋ URLSearchParams ๊ฐ์ฒด์ฌ์ผ ํฉ๋๋ค.
// ์ฐธ๊ณ : null์ด๋ undefined๋ URL์ ๋ ๋๋ง๋์ง ์์ต๋๋ค.
**params**: {
ID: 12345
},
// `paramsSerializer`๋ `params`์ ์๋ฆฌ์ผ๋ผ์ด์ฆ๋ฅผ ๋ด๋นํ๋ ์ต์
ํจ์์
๋๋ค.
// (์: <https://www.npmjs.com/package/qs>, <http://api.jquery.com/jquery.param/>)
paramsSerializer: function (params) {
return Qs.stringify(params, {arrayFormat: 'brackets'})
},
// **`data`๋ ์์ฒญ ๋ฐ๋๋ก ์ ์ก๋ ๋ฐ์ดํฐ**์
๋๋ค.
// 'PUT', 'POST', 'PATCH', 'DELETE' ๋ฉ์๋์์๋ง ์ ์ฉ ๊ฐ๋ฅํฉ๋๋ค.
// `transformRequest`๊ฐ ์ค์ ๋์ง ์์ ๊ฒฝ์ฐ ๋ค์ ํ์
์ค ํ๋์ฌ์ผ ํฉ๋๋ค.
// - string, plain object, ArrayBuffer, ArrayBufferView, URLSearchParams
// - ๋ธ๋ผ์ฐ์ ์ ์ฉ: FormData, File, Blob
// - Node ์ ์ฉ: Stream, Buffer
data: {
firstName: 'Fred'
},
// ๋ฐ๋๋ก ์ ์กํ๋ ๋ฐ์ดํฐ์ ๋์ ๋ฌธ๋ฒ
// POST ๋ฉ์๋
// ํค๊ฐ ์๋ ๊ฐ๋ง ์ ์ก๋ฉ๋๋ค.
data: 'Country=Brasil&City=Belo Horizonte',
// `timeout`์ ์์ฒญ์ด ์๊ฐ ์ด๊ณผ๋๊ธฐ ์ ์ ์๊ฐ(๋ฐ๋ฆฌ์ด)์ ์ง์ ํฉ๋๋ค.
// ์์ฒญ์ด `timeout`๋ณด๋ค ์ค๋ ๊ฑธ๋ฆฌ๋ฉด ์์ฒญ์ด ์ค๋จ๋ฉ๋๋ค.
timeout: 1000, // ๊ธฐ๋ณธ๊ฐ์ `0` (ํ์์์ ์์)
// `withCredentials`์ ์๊ฒฉ ์ฆ๋ช
์ ์ฌ์ฉํ์ฌ ์ฌ์ดํธ ๊ฐ ์ก์ธ์ค ์ ์ด ์์ฒญ์ ํด์ผ ํ๋์ง ์ฌ๋ถ๋ฅผ ๋ํ๋
๋๋ค.
withCredentials: false, // ๊ธฐ๋ณธ๊ฐ
// `adapter`'์ ์ปค์คํ
ํธ๋ค๋ง ์์ฒญ์ ์ฒ๋ฆฌํ ์ ์์ด ํ
์คํธ๊ฐ ์ฌ์์ง๋๋ค.
// ์ ํจํ Promise ์๋ต์ ๋ฐํํด์ผ ํฉ๋๋ค. (lib/adapters/README.md ์ฐธ๊ณ )
adapter: function (config) {
/* ... */
},
// `auth`๋ HTTP Basic ์ธ์ฆ์ด ์ฌ์ฉ๋๋ฉฐ, ์๊ฒฉ ์ฆ๋ช
์ ์ ๊ณตํฉ๋๋ค.
// `auth`๋ฅผ ์ฌ์ฉํ๋ฉด, `Authorization` ํค๋๊ฐ ์ค์ ๋์ด
/ //`headers`๋ฅผ ์ฌ์ฉํ์ฌ ์ค์ ํ ๊ธฐ์กด์ `Authorization` ์ฌ์ฉ์ ์ง์ ํค๋๋ฅผ ๋ฎ์ด์๋๋ค.
// ์ด ํ๋ผ๋ฏธํฐ๋ฅผ ํตํด HTTP Basic ์ธ์ฆ๋ง ๊ตฌ์ฑํ ์ ์์์ ์ฐธ๊ณ ํ์ธ์.
// Bearer ํ ํฐ ๋ฑ์ ๊ฒฝ์ฐ `Authorization` ์ฌ์ฉ์ ์ง์ ํค๋๋ฅผ ๋์ ์ฌ์ฉํฉ๋๋ค.
auth: {
username: 'janedoe',
password: 's00pers3cret'
},
// `responseType`์ ์๋ฒ์์ ๋ฐ๋ ๋ฐ์ดํฐ์ ํ์
์
๋๋ค.
// ์ต์
: 'arraybuffer', 'document', 'json', 'text', 'stream'
// ๋ธ๋ผ์ฐ์ ์ ์ฉ: 'blob'
**responseType: 'json', // ๊ธฐ๋ณธ๊ฐ**
// `responseEncoding`์ ์๋ต ๋์ฝ๋ฉ์ ์ฌ์ฉํ ์ธ์ฝ๋ฉ์
๋๋ค.
// Node.js ์ ์ฉ
// ์ฐธ๊ณ : ํด๋ผ์ด์ธํธ ์ฌ์ด๋ ์์ฒญ ๋๋ `responseType`์ด 'stream'์ด๋ฉด ๋ฌด์ํฉ๋๋ค.
responseEncoding: 'utf8', // ๊ธฐ๋ณธ๊ฐ
// `xsrfCookieName`์ xsrf ํ ํฐ ๊ฐ์ผ๋ก ์ฌ์ฉํ ์ฟ ํค์ ์ด๋ฆ์
๋๋ค.
xsrfCookieName: 'XSRF-TOKEN', // ๊ธฐ๋ณธ๊ฐ
// `xsrfHeaderName`์ xsrf ํ ํฐ ๊ฐ์ ์ด๋ฐํ๋ HTTP ํค๋์ ์ด๋ฆ์
๋๋ค.
xsrfHeaderName: 'X-XSRF-TOKEN', // ๊ธฐ๋ณธ๊ฐ
// `onUploadProgress`๋ ์
๋ก๋ ์งํ ์ด๋ฒคํธ๋ฅผ ์ฒ๋ฆฌํฉ๋๋ค.
// ๋ธ๋ผ์ฐ์ ์ ์ฉ
onUploadProgress: function (progressEvent) {
// ์
๋ก๋ ์งํ ์ด๋ฒคํธ ์์
์ํ
},
// `onDownloadProgress`๋ ๋ค์ด๋ก๋๋ก๋ ์งํ ์ด๋ฒคํธ๋ฅผ ์ฒ๋ฆฌํฉ๋๋ค.
// ๋ธ๋ผ์ฐ์ ์ ์ฉ
onDownloadProgress: function (progressEvent) {
// ๋ค์ด๋ก๋ ์งํ ์ด๋ฒคํธ ์์
์ํ
},
// `maxContentLength`๋ node.js์์ ํ์ฉ๋๋ http ์๋ต ์ฝํ
์ธ ์ ์ต๋ ํฌ๊ธฐ๋ฅผ
//๋ฐ์ดํธ ๋จ์๋ก ์ ์ํฉ๋๋ค.
maxContentLength: 2000,
// `maxBodyLength`๋ ํ์ฉ๋๋ http ์์ฒญ ์ฝํ
์ธ ์ ์ต๋ ํฌ๊ธฐ๋ฅผ ๋ฐ์ดํธ ๋จ์๋ก ์ ์ํฉ๋๋ค.
// Node.js ์ ์ฉ
maxBodyLength: 2000,
// `validateStatus`๋ ์ง์ ๋ HTTP ์๋ต ์ํ ์ฝ๋์ ๋ํ Promise๋ฅผ ์ดํํ ์ง ๋๋ ๊ฑฐ๋ถํ ์ง ์ฌ๋ถ๋ฅผ ์ ์ํฉ๋๋ค.
// ๋ง์ฝ `validateStatus`๊ฐ true๋ฅผ ๋ฐํํ๋ฉด(๋๋ 'null' ๋๋ 'undefined'์ผ๋ก ์ค์ ) Promise๋ ์ดํ๋ฉ๋๋ค.
// ๊ทธ๋ ์ง ์์ผ๋ฉด, ๊ทธ Promise๋ ๊ฑฐ๋ถ๋ ๊ฒ์ด๋ค.
**validateStatus**: function (status) {
return status >= 200 && status < 300; // ๊ธฐ๋ณธ๊ฐ
},
// `maxRedirects`๋ node.js์์ ๋ฆฌ๋๋ ์
์ต๋๊ฐ์ ์ ์ํฉ๋๋ค.
// 0์ผ๋ก ์ค์ ํ๋ฉด ๋ฆฌ๋๋ ์
๋์ง ์์ต๋๋ค.
maxRedirects: 5, // ๊ธฐ๋ณธ๊ฐ
// `socketPath`๋ node.js์์ ์ฌ์ฉ๋ UNIX ์์ผ์ ์ ์ํฉ๋๋ค.
// ์: '/var/run/docker.sock' ๋์ปค ๋ฐ๋ชฌ์ ์์ฒญ์ ๋ณด๋
๋๋ค.
// ์ค์ง `socketPath` ๋๋ `proxy`๋ง ์ง์ ํ ์ ์์ต๋๋ค.
// ๋ ๋ค ์ง์ ๋๋ฉด `socketPath`๊ฐ ์ฌ์ฉ๋ฉ๋๋ค.
socketPath: null, // ๊ธฐ๋ณธ๊ฐ
// `httpAgent`์ `httpsAgent`๋ ๊ฐ๊ฐ node.js์์ http ๋ฐ https ์์ฒญ์ ์ํํ ๋ ์ฌ์ฉํ ์ฌ์ฉ์ ์ง์ ์์ด์ ํธ๋ฅผ ์ ์ํฉ๋๋ค.
// ์ด๋ ๊ฒ ํ๋ฉด ๊ธฐ๋ณธ์ ์ผ๋ก ํ์ฑํ๋์ง ์์ `keepAlive`์ ๊ฐ์ ์ต์
์ ์ถ๊ฐํ ์ ์์ต๋๋ค.
httpAgent: new http.Agent({ keepAlive: true }),
httpsAgent: new https.Agent({ keepAlive: true }),
// `proxy`๋ ํ๋ก์ ์๋ฒ์ ํธ์คํธ์ด๋ฆ, ํฌํธ, ํ๋กํ ์ฝ์ ์ ์ํฉ๋๋ค.
// ๊ธฐ์กด์ `http_proxy`์ `https_proxy` ํ๊ฒฝ ๋ณ์๋ฅผ ์ฌ์ฉํ์ฌ
// ํ๋ก์๋ฅผ ์ ์ํ ์๋ ์์ต๋๋ค.
// ํ๋ก์ ๊ตฌ์ฑ์ ํ๊ฒฝ ๋ณ์๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ, 'no_proxy' ํ๊ฒฝ ๋ณ์๋ฅผ
// ์ผํ๋ก ๊ตฌ๋ถ๋ ํ๋ก์๊ฐ ๋์ง ์๋ ๋๋ฉ์ธ ๋ชฉ๋ก์ผ๋ก ์ ์ํ ์๋ ์์ต๋๋ค.
// `false`๋ฅผ ์ฌ์ฉํ๋ฉด ํ๋ก์๋ฅผ ์ฌ์ฉํ์ง ์๊ณ , ํ๊ฒฝ ๋ณ์๋ฅผ ๋ฌด์ํฉ๋๋ค.
// `auth`๋ ํ๋ก์์ ์ฐ๊ฒฐํ๋๋ฐ HTTP Basic auth๋ฅผ ์ฌ์ฉํด์ผ ํจ์ ๋ํ๋ด๋ฉฐ,
// ์๊ฒฉ ์ฆ๋ช
์ ์ ๊ณตํฉ๋๋ค. ๊ทธ๋ฌ๋ฉด `Proxy-Authorization` ํค๋๊ฐ ์ค์ ๋๊ณ ,
// `headers`๋ฅผ ์ฌ์ฉํ์ฌ ๊ธฐ์กด์ `Proxy-Authorization` ์ฌ์ฉ์ ์ง์ ํด๋๋ฅผ ๋ฎ์ด์๋๋ค.
// ๋ง์ฝ ํ๋ก์ ์๋ฒ๊ฐ HTTPS๋ฅผ ์ฌ์ฉํ๋ค๋ฉด, ํ๋กํ ์ฝ์ ๋ฐ๋์ `https`๋ก ์ค์ ํด์ผ ํฉ๋๋ค.
proxy: {
protocol: 'https',
host: '127.0.0.1',
port: 9000,
auth: {
username: 'mikeymike',
password: 'rapunz3l'
}
},
// `cancelToken`์ ์์ฒญ์ ์ทจ์ํ๋ ๋ฐ ์ฌ์ฉํ ์ ์๋ ์ทจ์ ํ ํฐ์ ์ง์ ํฉ๋๋ค.
// (์์ธํ ๋ด์ฉ์ ์์ฒญ ์ทจ์ ์น์
์ฐธ์กฐ)
cancelToken: new CancelToken(function (cancel) {
}),
// `decompress`๋ ์๋ต ๋ฐ๋์ ์๋ ์์ถ ํด์ ์ฌ๋ถ๋ฅผ ๋ํ๋
๋๋ค.
// `true`๋ก ์ค์ ํ๋ฉด, ์์ถ ํด์ ๋ ๋ชจ๋ ์๋ต์์ 'content-encoding' ํค๋๋ ์ ๊ฑฐ๋ฉ๋๋ค.
// Node.js ์ ์ฉ (XHR์ ์์ถ ํด์ ํ ์ ์์ต๋๋ค)
decompress: true // ๊ธฐ๋ณธ๊ฐ
}
์๋ต ์คํค๋ง
{
// `data`๋ ์๋ฒ๊ฐ ์ ๊ณตํ๋ ์๋ต์
๋๋ค.
data: {},
// `status`๋ HTTP ์ํ ์ฝ๋์
๋๋ค.
status: 200,
// `statusText`๋ HTTP ์ํ ๋ฉ์์ง์
๋๋ค.
statusText: 'OK',
// `headers`๋ HTTP ํค๋์
๋๋ค.
// ๋ชจ๋ ํค๋ ์ด๋ฆ์ ์๋ฌธ์์ด๋ฉฐ, ๊ดํธ ํ๊ธฐ๋ฒ์ ์ฌ์ฉํ์ฌ ์ ๊ทผํ ์ ์์ต๋๋ค.
// ์์: `response.headers['content-type']`
headers: {},
// `config`๋ ์์ฒญ์ ์ํด `Axios`๊ฐ ์ ๊ณตํ๋ ๊ตฌ์ฑ์
๋๋ค.
config: {},
// `request`๋ ์ด๋ฒ ์๋ต์ผ๋ก ์์ฑ๋ ์์ฒญ์
๋๋ค.
// ์ด๊ฒ์ node.js์์ ๋ง์ง๋ง ClientRequest ์ธ์คํด์ค ์
๋๋ค.
// ๋ธ๋ผ์ฐ์ ์์๋ XMLHttpRequest์
๋๋ค.
request: {}
}
- then์ ์ฌ์ฉ์ ๋ฐ๋ ์๋ต
axios.get('/user/12345')
.then(function (response) {
console.log(response.data);
console.log(response.status);
console.log(response.statusText);
console.log(response.headers);
console.log(response.config);
});
Config ๊ธฐ๋ณธ๊ฐ
๋ชจ๋ ์์ฒญ์ ์ ์ฉ๋ config ๊ธฐ๋ณธ๊ฐ ์ง์ ๊ฐ๋ฅ
*์ ์ญ Axios ๊ธฐ๋ณธ๊ฐ
axios.defaults.baseURL = '<https://api.example.com>';
axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
*์ปค์คํ ์ธ์คํด์ค ๊ธฐ๋ณธ๊ฐ
// ์ธ์คํด์ค๋ฅผ ์์ฑํ ๋ config ๊ธฐ๋ณธ๊ฐ ์ค์ ํ๊ธฐ
const instance = axios.create({
baseURL: '<https://api.example.com>'
});
// ์ธ์คํด์ค๋ฅผ ๋ง๋ ํ ๊ธฐ๋ณธ๊ฐ ๋ณ๊ฒฝํ๊ธฐ
instance.defaults.headers.common['Authorization'] = AUTH_TOKEN;
*Config ์ฐ์ ์์
Config๋ ์ฐ์ ์์์ ๋ฐ๋ผ ๋ณํฉ
lib/defaults.js๋ผ์ด๋ธ๋ฌ๋ฆฌ์์์ ๊ธฐ๋ณธ๊ฐ, ์ธ์คํด์ค์ defaults์์ฑ, ์์ฒญ์ config์ธ์๋ฅผ ์์๋๋ก ์ฐพ์. ํ์๊ฐ ์ ์๋ณด๋ค ์ฐ์ ์์๊ฐ ๋์..
// ๋ผ์ด๋ธ๋ฌ๋ฆฌ์์ ์ ๊ณตํ๋ config ๊ธฐ๋ณธ๊ฐ์ ์ฌ์ฉํ์ฌ ์ธ์คํด์ค ๋ง๋ค๊ธฐ
// ์ด ๋ timeout ๊ฐ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๊ธฐ๋ณธ๊ฐ์ธ '0'
const instance = axios.create();
// ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๋ํ timeout ๊ฐ ์ฌ์ ์
// ์ด์ ๋ชจ๋ ์์ฒญ์ ์๊ฐ ์ด๊ณผ ์ 2.5์ด ๋๊ธฐํ๋ ์ธ์คํด์ค๋ฅผ ์ฌ์ฉ
instance.defaults.timeout = 2500;
// ์๊ฐ์ด ์ค๋ ๊ฑธ๋ฆฌ๋ ์์ฒญ์ ๋ํ timeout ๊ฐ ์ฌ์ ์
instance.get('/longRequest', {
timeout: 5000
});
์ธํฐ์ ํฐ
then ๋๋ catch๋ก ์ฒ๋ฆฌ ๋๊ธฐ ์ ์ ์์ฒญ๊ณผ ์๋ต์ ๊ฐ๋ก์ฑ ์ ์์
// ์์ฒญ ์ธํฐ์
ํฐ ์ถ๊ฐํ๊ธฐ
axios.interceptors.request.use(function (config) {
// ์์ฒญ์ด ์ ๋ฌ๋๊ธฐ ์ ์ ์์
์ํ
return config;
}, function (error) {
// ์์ฒญ ์ค๋ฅ๊ฐ ์๋ ์์
์ํ
return Promise.reject(error);
});
// ์๋ต ์ธํฐ์
ํฐ ์ถ๊ฐํ๊ธฐ
axios.interceptors.response.use(function (response) {
// 2xx ๋ฒ์์ ์๋ ์ํ ์ฝ๋๋ ์ด ํจ์๋ฅผ ํธ๋ฆฌ๊ฑฐ ํฉ๋๋ค.
// ์๋ต ๋ฐ์ดํฐ๊ฐ ์๋ ์์
์ํ
return response;
}, function (error) {
// 2xx ์ธ์ ๋ฒ์์ ์๋ ์ํ ์ฝ๋๋ ์ด ํจ์๋ฅผ ํธ๋ฆฌ๊ฑฐ ํฉ๋๋ค.
// ์๋ต ์ค๋ฅ๊ฐ ์๋ ์์
์ํ
return Promise.reject(error);
});
- ์ธํฐ์ ํฐ ์ ๊ฑฐ
const myInterceptor = axios.interceptors.request.use(function () {/*...*/});
axios.interceptors.request.eject(myInterceptor);
- ์ปค์คํ ์ธ์คํด์ค์์๋ ์ธํฐ์ ํฐ ์ถ๊ฐ
const instance = axios.create();
instance.interceptors.request.use(function () {/*...*/});
์๋ฌ ํธ๋ค๋ง
axios.get('/user/12345')
.catch(function (error) {
if (error.response) {
// ์์ฒญ์ด ์ ์ก๋์๊ณ , ์๋ฒ๋ 2xx ์ธ์ ์ํ ์ฝ๋๋ก ์๋ตํ์ต๋๋ค.
console.log(error.response.data);
console.log(error.response.status);
console.log(error.response.headers);
} else if (error.request) {
// ์์ฒญ์ด ์ ์ก๋์์ง๋ง, ์๋ต์ด ์์ ๋์ง ์์์ต๋๋ค.
// 'error.request'๋ ๋ธ๋ผ์ฐ์ ์์ XMLHtpRequest ์ธ์คํด์ค์ด๊ณ ,
// node.js์์๋ http.ClientRequest ์ธ์คํด์ค์
๋๋ค.
console.log(error.request);
} else {
// ์ค๋ฅ๊ฐ ๋ฐ์ํ ์์ฒญ์ ์ค์ ํ๋ ๋์ ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ต๋๋ค.
console.log('Error', error.message);
}
console.log(error.config);
});
- validateStatus config ์ต์ ์ ์ฌ์ฉํ๋ฉด, ์ค๋ฅ๋ฅผ ๋ฐ์์ํค๋ HTTP ์ฝ๋๋ฅผ ์ ์ํ ์ ์์
axios.get('/user/12345', {
validateStatus: function (status) {
return status < 500; // ์ํ ์ฝ๋๊ฐ 500 ๋ฏธ๋ง์ธ ๊ฒฝ์ฐ์๋ง ํด๊ฒฐ
}
})
- toJSON์ ์ฌ์ฉํ๋ฉด, HTTP ์๋ฌ์ ๋ํ ๋ ๋ง์ ์ ๋ณด๋ฅผ ๊ฐ์ฒด ํ์์ผ๋ก ๊ฐ์ ธ์ด
axios.get('/user/12345')
.catch(function (error) {
console.log(error.toJSON());
});
Reference
'Development' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฐ๋ฒจ(Babel) (0) | 2023.06.15 |
---|---|
์นํฉ(Webpack) (0) | 2023.06.15 |
[axios] axios 1 (0) | 2023.01.21 |
[npm].npmrc (0) | 2023.01.19 |
๋๊ธ