npm.io
0.8.0 • Published 12h ago

hahoorequest

Licence
MIT
Version
0.8.0
Deps
2
Size
42 kB
Vulns
0
Weekly
0
Stars
2

hahoorequest

A HTTP request abstract library, use other http request libraries in one way. Isomorphic request to work in Node or in the browser. For example React isomorphism, the same code for the server and the browser.

What's new in 0.8.0

  • Added Axios support for both browsers and Node.js.
  • Added SSR-ready axios-fetch, fetch-axios, reqwest-axios, and superagent-axios combinations. Application code imports one entry point and the bundler selects the browser or server implementation automatically.
  • Axios requests use the same options and normalized response/error shape as the existing adapters.
  • Axios, reqwest, and SuperAgent are now optional peer dependencies, keeping the default fetch installation lightweight.
  • Removed the deprecated request dependency; historical *-request entry points remain compatibility aliases backed by node-fetch.

Why

  • Different libraries use different syntax, the replacement library will lead to a large number of changes
  • The browser and the server can use the same API with different implementations, or both use fetch.

Install

npm install hahoorequest --save

The default fetch adapter works without installing another request library. Install an optional adapter only when you use one of its entry points:

# Axios and any *-axios entry
npm install axios

# reqwest and any reqwest-* entry
npm install reqwest

# SuperAgent and any superagent* entry
npm install superagent

Axios, reqwest, and SuperAgent are optional peer dependencies. They are not installed automatically with hahoorequest; importing an adapter without its peer installed causes a module resolution error during build or startup.

Import library

Select the library you want to use

Default

The package root uses node-fetch in Node.js and whatwg-fetch in browser-aware bundlers:

import request from 'hahoorequest';
Browser & Server
Browser with Axios, Server with Axios
import request from 'hahoorequest/lib/axios';

Requires npm install axios.

Browser with Axios, Server with fetch
import request from 'hahoorequest/lib/axios-fetch';

Requires npm install axios for the browser build.

Browser with fetch, Server with fetch
import request from 'hahoorequest/lib/fetch';
Browser with fetch, Server with Axios
import request from 'hahoorequest/lib/fetch-axios';

Requires npm install axios for the server build.

Browser with reqwest, Server with fetch
import request from 'hahoorequest/lib/reqwest-fetch';

Requires npm install reqwest.

Browser with reqwest, Server with Axios
import request from 'hahoorequest/lib/reqwest-axios';

Requires npm install reqwest axios.

Browser with superagent, Server with superagent
import request from 'hahoorequest/lib/superagent';

Requires npm install superagent.

Browser with superagent, Server with fetch
import request from 'hahoorequest/lib/superagent-fetch';

Requires npm install superagent for the browser build.

Browser with superagent, Server with Axios
import request from 'hahoorequest/lib/superagent-axios';

Requires npm install superagent axios.

Browser
fetch
import request from 'hahoorequest/lib/whatwg-fetch';
reqwest
import request from 'hahoorequest/lib/reqwest';

Requires npm install reqwest.

superagent
import request from 'hahoorequest/lib/superagent';

Requires npm install superagent.

Server
Axios
import request from 'hahoorequest/lib/axios';

Requires npm install axios.

node-fetch
import request from 'hahoorequest/lib/node-fetch';
Migration from request

The deprecated request dependency has been removed. Historical node-request and *-request import paths remain compatibility aliases backed by node-fetch, but are no longer recommended for new code.

Node.js compatibility

Node.js 14.18 or newer is supported. The published output remains CommonJS, so existing require() users and server-side rendering builds do not need to move to native ES modules.

Usage

Return Promises

request({
  url: 'https://raw.githubusercontent.com/hahoocn/hahoorequest/master/test/test.json',
  method: 'GET',
  type: 'json'
})
.then((res) => {
  console.log(res.body);
});

Options

  • url a fully qualified uri
  • method http method (default: GET)
  • headers http headers
  • body entity body for PATCH, POST and PUT requests. Must be a query String or JSON object
  • type a string enum. html, xml, json, form, png... (default: json)
  • qs object containing querystring values to be appended to the url
  • credentials Sending cookies. fetch set credentials option. if credentials is not undefined, reqwest and superagent will add withCredentials = true

Response

  • response.body Content of response
  • response.status http status code

Errors

  • response.errcode Code of errors
  • response.errmsg Content of errors

Keywords