Skip to content

Instantly share code, notes, and snippets.

@moreta
Last active September 5, 2023 15:27
Show Gist options
  • Star 26 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save moreta/fb2625c59aa788009b1f7ce8e44ac559 to your computer and use it in GitHub Desktop.
Save moreta/fb2625c59aa788009b1f7ce8e44ac559 to your computer and use it in GitHub Desktop.
axios interceptor sample code
/**
* src/api/http.js
*/
import axios from 'axios'
import qs from 'qs'
/**
*
* parse error response
*/
function parseError (messages) {
// error
if (messages) {
if (messages instanceof Array) {
return Promise.reject({ messages: messages })
} else {
return Promise.reject({ messages: [messages] })
}
} else {
return Promise.reject({ messages: ['エラーが発生しました'] })
}
}
/**
* parse response
*/
function parseBody (response) {
// if (response.status === 200 && response.data.status.code === 200) { // - if use custom status code
if (response.status === 200) {
return response.data.result
} else {
return this.parseError(response.data.messages)
}
}
/**
* axios instance
*/
let instance = axios.create({
baseURL: `/your/host/api/v1`,
paramsSerializer: function (params) {
return qs.stringify(params, { indices: false })
}
})
// request header
instance.interceptors.request.use((config) => {
// Do something before request is sent
// api tokenなどを利用してheaderに載せる場合
// const apiToken = sessionStorage.getItem('token')
// config.headers = { 'Custom-Header-IF-Exist': apiToken }
return config
}, error => {
return Promise.reject(error)
})
// response parse
instance.interceptors.response.use((response) => {
return parseBody(response)
}, error => {
console.warn('Error status', error.response.status)
// return Promise.reject(error)
if (error.response) {
return parseError(error.response.data)
} else {
return Promise.reject(error)
}
})
export const http = instance
/**
* src/api/test.js
*/
import { http } from './http'
export default {
find (query) {
return http.get(`/test`, {
params: query
})
},
create (params) {
return http.post(`/test`, { test: params })
},
update (params) {
return http.put(`/test`, { test: params })
}
}
@nayefmhmd
Copy link

owesome

@anujsachan1990
Copy link

good one

@saidbouhmouch
Copy link

thanks

@yohanelly95
Copy link

What if I want to send data to a 3rd part site on my current axios setup. I have an axios interceptor that is configured for my server. Can I bypass it or customize it in some way ?

@moreta
Copy link
Author

moreta commented Mar 16, 2020

@yohanelly95

What if I want to send data to a 3rd part site on my current axios setup. I have an axios interceptor that is configured for my server. Can I bypass it or customize it in some way ?

You can create another axios instance.
And define interceptors.

https://github.com/axios/axios#axioscreateconfig

let myInstance = axios.create({})
myInstance.interceptors  ...

let 3rdPartyInstance = axios.create({})
3rdPartyInstance.interceptors ...

You can write factory function that create axios instance for shared logic.

@ramon-perdomo-em
Copy link

thank you so much~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment