Skip to content

A HTTP request abstract library, use other http request libraries in one way. Isomorphic request to work in Node or in the browser.

License

Notifications You must be signed in to change notification settings

hahoocn/hahoorequest

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

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 code for different libraries. For example: the browser with reqwest, the server with request. Or both the browser and the server are using fetch.

Install

npm install hahoorequest --save

Import library

Select the library you want to use

Browser & Server

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

Browser

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

Server

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

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

About

A HTTP request abstract library, use other http request libraries in one way. Isomorphic request to work in Node or in the browser.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published