0.7.0 • Published 4 years ago
hahoorequest v0.7.0
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 --saveImport 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
fetch
import request from 'hahoorequest/lib/whatwg-fetch';reqwest
import request from 'hahoorequest/lib/reqwest';superagent
import request from 'hahoorequest/lib/superagent';Server
node-fetch
import request from 'hahoorequest/lib/node-fetch';request
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
urla fully qualified urimethodhttp method (default:GET)headershttp headersbodyentity body forPATCH,POSTandPUTrequests. Must be a queryStringorJSONobjecttypea string enum.html,xml,json,form,png... (default:json)qsobject containing querystring values to be appended to the urlcredentialsSending cookies. fetch set credentials option. if credentials is not undefined, reqwest and superagent will addwithCredentials = true
Response
response.bodyContent of responseresponse.statushttp status code
Errors
response.errcodeCode of errorsresponse.errmsgContent of errors