Skip to content

Instantly share code, notes, and snippets.

@paulsturgess
Last active March 15, 2026 18:46
Show Gist options
  • Select an option

  • Save paulsturgess/ebfae1d1ac1779f18487d3dee80d1258 to your computer and use it in GitHub Desktop.

Select an option

Save paulsturgess/ebfae1d1ac1779f18487d3dee80d1258 to your computer and use it in GitHub Desktop.
An example Service class wrapper for Axios
import axios from 'axios';
class Service {
constructor() {
let service = axios.create({
headers: {csrf: 'token'}
});
service.interceptors.response.use(this.handleSuccess, this.handleError);
this.service = service;
}
handleSuccess(response) {
return response;
}
handleError = (error) => {
switch (error.response.status) {
case 401:
this.redirectTo(document, '/')
break;
case 404:
this.redirectTo(document, '/404')
break;
default:
this.redirectTo(document, '/500')
break;
}
return Promise.reject(error)
}
redirectTo = (document, path) => {
document.location = path
}
get(path, callback) {
return this.service.get(path).then(
(response) => callback(response.status, response.data)
);
}
patch(path, payload, callback) {
return this.service.request({
method: 'PATCH',
url: path,
responseType: 'json',
data: payload
}).then((response) => callback(response.status, response.data));
}
post(path, payload, callback) {
return this.service.request({
method: 'POST',
url: path,
responseType: 'json',
data: payload
}).then((response) => callback(response.status, response.data));
}
}
export default new Service;
@fgblomqvist

fgblomqvist commented Oct 25, 2017

Copy link
Copy Markdown

error.response is not defined for me, any idea of why that could be? Other properties, such as error.config work fine.
(tested axios 0.16.2 and 0.17.0)

@sarvanios6

Copy link
Copy Markdown

how to handle cors domain api here?

@itsjeffro

itsjeffro commented Jul 3, 2018

Copy link
Copy Markdown

@sarvanious cors depends on the server youre making requests to

@kiritm-nimblechapps

Copy link
Copy Markdown

Can you write how to use it ?

@arunpkumar92

Copy link
Copy Markdown

Sample usage code needed.

@kiritm-nimblechapps

kiritm-nimblechapps commented Jul 8, 2019

Copy link
Copy Markdown

@arunpkumar92, Sample code to use. Import and use it.

  Service.get(
            "url"
            params,
            (response) => {
                console.log("****** response is *******", response);
            }
        )

@arunpkumar92

Copy link
Copy Markdown

@kiritnim Thanks for the help.

@mwlalata

mwlalata commented Feb 9, 2020

Copy link
Copy Markdown

Hi! what about handling auth headers?

@behnamazimi

behnamazimi commented Jun 24, 2020

Copy link
Copy Markdown

Thank you.
I code a dynamic version for this purpose and packed it under the name js-service-wrapper.
A promise based collective service wrapper with queue support which totally works in browser and/or Node.js environment

@ittroller

Copy link
Copy Markdown

how to handle call api refresh token with this class ? please send me some example

@Ankit9997

Copy link
Copy Markdown

serviceWrapper

@champion-054

Copy link
Copy Markdown

Is it possible to provide a parameterized constructor? And how exactly can we implement it?

@Sovai

Sovai commented Jan 29, 2022

Copy link
Copy Markdown

why do we need this wrapper service instead of direct axios?

@Darshan072

Copy link
Copy Markdown

@Sovai just to add all the common config at one place.

@deepaksemwal121

Copy link
Copy Markdown

@Sovai if any time you need to change the axios for XYZ reasons you don't have to do it manually in all of your requests you can change it in a single file. It will save your time and efforts

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