Fetch API with middleware
fej exposes simple middleware API to manipulate request properties.
You can override middleware and initial data with each request: fej("/api/users", { headers: {"Accept": "application/xml"} })
npm install fej
See following usage examples
Set some static headers
import Fej from "fej";
Fej.setInit({
headers: {
"Accept": "application/json",
"Content-Type": "application/json"
}
});
Updating fetch
properties asynchronously
import Fej from "fej";
Fej.addAsyncMiddleware(async init => {
// get access token
const token = await authService.acquireTokenSilent();
// update Authorization header with new access token
return Promise.resolve({
headers: { Authorization: "Bearer " + token.accessToken }
});
});
import Fej from "fej";
Fej.addMiddleware(init => {
// Get current time
const currentDateTime = new Date().toISOString()
// update Authorization header with new access token
return {
headers: { "Z-CURRENTTIME": currentDateTime }
};
});