Simple wrapper over fetch api

This is a simple and easy to use wrapper over Javascript fetch api

sendRequest method accepts url(String), method: GET, POST, PUT, DELETE(String), useCredentials(boolean) which is if you want the server to read the session cookie(it sets credentials=’include’), body, headers and simple queryParams object.

By default the above wrapper sets ‘content-type’ key in the headers to ‘application/json’. You can provide your own headers and they will overwrite the exisiting ones.

You can use the above wrapper this way:

  sendRequest({
url,
method: 'GET',
useCredentials: true,
queryParams: {
offset,
limit
}
})
.then((res) => alert("Got my feed!!!"))
.catch((err) => alert(`status: ${err.status} ${err.message}`));

I have found that this wrapper is sufficient in small to medium projects. Please let me know in the comments section if you found this wrapper useful or if there are any mistakes or if you would like to see any changes to make this wrapper better.

--

--

Full Stack Web Developer. Loves to build stuff.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store