First, we need to create a new API wrapper to be used in this recipe. This will be the main file we will use on all the HTTP methods.
Let's create the base wrapper by following these steps:
- Create a new file called baseFetch.js in the src/http folder.
- We will create an asynchronous function that will receive as an argument the three variables of url, method, and options. This will be a currying function, which the second function will receive as an argument, type:
export default async (url, method, options = {}) => {
let httpRequest;
if (method.toUpperCase() === 'GET') {
httpRequest = await fetch(url, {
cache: 'reload',
...options,
});
} else {
httpRequest = fetch(url, {
method: method.toUpperCase(),
cache: 'reload',
...options,
});
}
return (type) => {
switch (type.toLocaleLowerCase()) {
case 'json':
return httpRequest.json();
case 'blob':
...