(easy) Fetch API
Easy to use and lightweight wrapper for the Fetch API.
- Provides CRUD methods
- Each method returns a Promise, therefore works with async/await
- Automatically sets required headers (for POST, PUT and PATCH it sets
Accept
andContent-Type
headers toapplication/json
) - Provides method for easily posting form data
- Pre-request and post-request callbacks for an easier integration with store-based architectures like Redux - see example
This library does not provide a polyfill for the Fetch API.
Installation
npm install --save easy-fetch-api
Usage
; Api; await Api; // Set headers on each requestApi; // Or set headers globally and they are automatically passed on each requestApi; console // { json: 'json', blob: 'blob', text: 'text' }
More detailed usage examples below in the docs of each method:
SET BASE URL
Set a base URL (if needed).
This will be used to prefix all later URLs
Api;
GET
Performs a HTTP Get request.
Api
- url (String, required) - URL to make request to
- headers (Object, optional) HTTP Headers object in the form of
{ headerKey: headerValue }
- query (Object, optional) Query object in the form of
{ queryKey: queryValue }
- callback (Function, optional) Function called after the server responds, with resulting data
- responseType (String, optional) one of the RESPONSE_TYPES allowed values
- returns Promise
POST (json)
Performs a HTTP Post request.
Api
- url (String, required) - URL to make request to
- data (Object, required) - Object body to be posted
- headers (Object, optional) HTTP Headers object in the form of
{ headerKey: headerValue }
. Note that there are two preset heders:{ Accept: 'application/json', 'Content-Type': 'application/json' }
. You can override them using this parameter - callback (Function, optional) Function called after the server responds, with resulting data
- responseType (String, optional) one of the RESPONSE_TYPES allowed values
- returns Promise
POST FORM
Performs a HTTP Post request with form data.
Api
- url (String, required) - URL to make request to
- data (String, required) - Form data to be posted
- headers (Object, optional) HTTP Headers object in the form of
{ headerKey: headerValue }
. Note that there are two preset heders:{ Accept: 'application/json', 'Content-Type': 'application/json' }
. You can override them using this parameter - callback (Function, optional) Function called after the server responds, with resulting data
- returns Promise
PUT
Performs a HTTP Put request.
Api
- url (String, required) - URL to make request to
- data (Object, required) - Object body to be updated
- headers (Object, optional) HTTP Headers object in the form of
{ headerKey: headerValue }
. Note that there are two preset heders:{ Accept: 'application/json', 'Content-Type': 'application/json' }
. You can override them using this parameter - callback (Function, optional) Function called after the server responds, with resulting data
- responseType (String, optional) one of the RESPONSE_TYPES allowed values
- returns Promise
PATCH
Performs a HTTP Patch request.
Api
- url (String, required) - URL to make request to
- data (Object, required) - Object body to be updated
- headers (Object, optional) HTTP Headers object in the form of
{ headerKey: headerValue }
. Note that there are two preset heders:{ Accept: 'application/json', 'Content-Type': 'application/json' }
. You can override them using this parameter - callback (Function, optional) Function called after the server responds, with resulting data
- responseType (String, optional) one of the RESPONSE_TYPES allowed values
- returns Promise
DELETE
Performs a HTTP Delete request.
Api
- url (String, required) - URL to make request to
- headers (Object, optional) HTTP Headers object in the form of
{ headerKey: headerValue }
- query (Object, optional) Query object in the form of
{ queryKey: queryValue }
- callback (Function, optional) Function called after the server responds, with resulting data
- returns Promise
CallBefore and Callback
Functions to be called before the request is fired and after the server responds, respectively. Facilitates integration with store-based architectures like Redux.
Api
Licence
The code is open-source and available under the MIT Licence. More details in the LICENCE.md file.