Giter Club home page Giter Club logo

fetchttp's Introduction

fetchttp

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.

Class Properties:

  • #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.

Constructor:

Takes a URL as a parameter and initializes the url property.

HTTP Methods:

  • get(), post(), patch(), update(), delete(): Set the HTTP method and return the instance to enable method chaining.

Configuration Methods:

  • 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.

Request Building:

  • buildRequest(): Constructs the complete request object based on the chosen HTTP method, headers, body, and options.

Request Execution:

  • call(): Alias for buildRequest(); returns the result of the request execution.

Request Execution and Response Handling:

  • resquest(): Uses the fetch API to execute the request and parse the JSON response.

Usage

const wrapper = new Wrapper("URL HERE");

const responseData = await wrapper
  .get()
  .headers({ Authorization: 'Bearer TOKEN' })
  .call();

console.log(responseData);

Make a post request

const postData = { key: 'value' };

const responseData = await wrapper
  .post()
  .headers({ Authorization: 'Bearer TOKEN' })
  .body(postData)
  .call();

console.log(responseData);

Additional Configuration

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);

Instalation

  npm i fastwrapper

Usage

const fetchttp = require("fastwrapper");

fetchttp's People

Contributors

toniswx avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.