The main objective of this project is eliminate all of the boilerplate that comes with the fetch api.
A HTTP wrapper build to provide a much easy dev experience for those who use the standart fetch API. is designed to simplify and streamline the process of making HTTP requests in JavaScript. It provides a fluent interface for configuring and executing HTTP requests with various methods such as GET, POST, PATCH, UPDATE, and DELETE.
The class abstracts away the complexities of constructing requests and handling responses, providing a cleaner and more readable syntax for making HTTP requests in your applications.
#method
: Private field to store the HTTP method (e.g., "get", "post", "patch", etc.).#body
: Private field to store the request body for methods like POST, PATCH, etc.#headers
: Private field to store default headers with the Content-Type set to "application/json".#requestOption
: Private field to store default request options, such as mode, cache, credentials, etc.#fullreq
: Private field to store the complete request object.
Takes a URL as a parameter and initializes the url
property.
get()
,post()
,patch()
,update()
,delete()
: Set the HTTP method and return the instance to enable method chaining.
headers(custom)
: Set custom headers for the request.body(custom)
: Set the request body by converting the provided object to a JSON string.options(custom)
: Set custom request options by merging with the default options.
buildRequest()
: Constructs the complete request object based on the chosen HTTP method, headers, body, and options.
call()
: Alias forbuildRequest()
; returns the result of the request execution.
resquest()
: Uses thefetch
API to execute the request and parse the JSON response.
const wrapper = new Wrapper("URL HERE");
const responseData = await wrapper
.get()
.headers({ Authorization: 'Bearer TOKEN' })
.call();
console.log(responseData);
const postData = { key: 'value' };
const responseData = await wrapper
.post()
.headers({ Authorization: 'Bearer TOKEN' })
.body(postData)
.call();
console.log(responseData);
Customize headers, request body, and options using available methods:
const responseData = await wrapper
.get()
.headers({ Authorization: 'Bearer TOKEN', 'Custom-Header': 'Value' })
.options({ credentials: 'include' })
.call();
console.log(responseData);
npm i fastwrapper
Usage
const fetchttp = require("fastwrapper");