Overrides the built-in defaults for network handling.
import { initializeNetworkHelpers } from '@brigad/redux-rest-easy';
const networkHelpers = {
// Mandatory if you don't override the others
getToken: () => localStorage.getItem('token'),
// Override to add custom headers
requestGET() {
return {
method: 'GET',
headers: {
Accept: 'application/json',
Authorization: `Bearer ${this.getToken()}`,
},
};
},
requestPATCH(body) {
return {
method: 'PATCH',
headers: {
Accept: 'application/json',
Authorization: `Bearer ${this.getToken()}`,
'Content-Type': 'application/json',
},
body,
};
},
requestPUT(body) {
return {
method: 'PUT',
headers: {
Accept: 'application/json',
Authorization: `Bearer ${this.getToken()}`,
'Content-Type': 'application/json',
},
body,
};
},
requestPOST(body) {
return {
method: 'POST',
headers: {
Accept: 'application/json',
Authorization: `Bearer ${this.getToken()}`,
'Content-Type': 'application/json',
},
body,
};
},
requestDELETE() {
return {
method: 'DELETE',
headers: {
Accept: 'application/json',
Authorization: `Bearer ${this.getToken()}`,
},
};
},
// Override to change status code handling / error handling
handleStatusCode: response => {
if (response) {
if (response.status >= 200 && response.status < 300) {
return response;
}
const error = new Error(response.statusText);
error.response = response;
throw error;
}
return null;
},
// eslint-disable-next-line no-unused-vars
handleError: async (err, dispatch) => {
try {
if (err && err.response) {
const error = await err.response.json();
// dispatch some action to warn the user about the error
// eslint-disable-next-line no-console
console.error(error);
} else {
// eslint-disable-next-line no-console
console.error(err);
}
} catch (e) {
// eslint-disable-next-line no-console
console.error(e);
}
},
};
initializeNetworkHelpers(networkHelpers);