Sometimes you may want to modify the request data globally or to customize the response.
To accomplish this, the SDK provides 2 function hooks:
beforeSend
- triggered right before sending the fetch request, allowing you to inspect/modify the request config.
const xg = new XGenClient({/* params */});
xg.beforeSend = function (url, options) {
// For list of the possible request options properties check
// https://developer.mozilla.org/en-US/docs/Web/API/fetch#options
options.headers = Object.assign({}, options.headers, {
'X-Custom-Header': 'example',
});
return { url, options };
};
afterSend
- triggered after successfully sending the fetch request, allowing you to inspect/modify the response object and its parsed data.
const xg = new XGenClient({/* params */});
xg.afterSend = function (response, data) {
// do something with the response state
console.log(response.status);
return Object.assign(data, {
// extend the data...
"newField": "This is added to the data of the response",
});
};