🎩 You're Invited:Meet the Socket team at Black Hat in Las Vegas, August 3-6.RSVP
Sign In

@wujue0115/fetch-factory

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@wujue0115/fetch-factory

A fetch wrapper for browser and nodejs.

latest
Source
npmnpm
Version
0.1.5
Version published
Maintainers
1
Created
Source

fetch-factory

The fetch-factory is a JavaScript library for simplified HTTP requests. It provides a convenient way to create and send HTTP requests with a customizable configuration.

Installation

Install fetch-factory using a package manager such as npm, yarn, or pnpm.

npm

npm install @wujue0115/fetch-factory

yarn

yarn add @wujue0115/fetch-factory

pnpm

pnpm install @wujue0115/fetch-factory

Usage

Basic Usage

Create an instance of FetchFactory by providing a base URL and default configuration:

import FetchFactory from "@wujue0115/fetch-factory";


const myAPI = new FetchFactory({
  baseUrl: "http://...",
  baseConfig: {
    headers: {
      ...
    }
  }
});

Use the fetch method for making HTTP requests, such as a GET request:

async function fetch() {
  const { data, error } = await myAPI.fetch("/example...", "get");

  data && console.log(data);
  error && console.error(error);
}

Similarly, you can use the get method to send an HTTP GET request.

async function getRequest() {
  const { data, error } = await myAPI.get("/example...");

  data && console.log(data);
  error && console.error(error);
}

Flexible Configuration for the fetch Method

The fetch method in FetchFactory supports multiple configurations, providing flexibility for various use cases. Each argument enables you to customize the URL, HTTP method, request data, headers, and additional options. Here are the different configurations:

myAPI.fetch(/* options */);
myAPI.fetch(/* url */, /* options */);
myAPI.fetch(/* url */, /* method */, /* options */);
myAPI.fetch(/* url */, /* method */, /* data */, /* options */);
myAPI.fetch(/* url */, /* method */, /* data */, /* headers */, /* options */);

Flexible Configuration for the get, post, put, delete Method

For get, post, put, and delete methods, the example below presents the support for multiple parameters.

myAPI.get(/* options */);
myAPI.get(/* url */, /* options */);
myAPI.get(/* url */, /* params */, /* options */);
myAPI.get(/* url */, /* params */, /* headers */, /* options */);

myAPI.post(/* options */);
myAPI.post(/* url */, /* options */);
myAPI.post(/* url */, /* data */, /* options */);
myAPI.post(/* url */, /* data */, /* headers */, /* options */);

The options argument is equivalent to the axios request config.

License

MIT

Keywords

fetch-factory

FAQs

Package last updated on 27 Nov 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts