Universal/Isomorphic HTTP(S) Request for Catberry Framework
npm install catberry-uhr 
``bash`
npm install catberry-uhr
It has the same interface and different implementations at the server and in a browser.
At the server it uses node's http.request
or https.request
(depends on the specified protocol in URL).
In a browser it uses a native XmlHttpRequest.
This module has been developed using HTTP/1.1v2 RFC 2616.
It supports:
* gzip and deflate request/response content encodingsapplication/json
* and application/x-www-form-urlencoded
request/response content types
* Request timeout
* Auto stringify/parse request/response data
* HTTP/HTTPS
* Any additional HTTP headers you set
UHR has following methods:
`javascript
class UHRBase {
/**
* Does a GET request to the HTTP server.
* @param {string} url URL to request.
* @param {Object?} parameters The request parameters.
* @param {Object?} parameters.headers The HTTP headers to send.
* @param {(string|Object)?} parameters.data The data to send.
* @param {number?} parameters.timeout The request timeout.
* @param {boolean?} parameters.unsafeHTTPS If true then requests to servers with
* invalid HTTPS certificates are allowed.
* @returns {Promise
/**
* Does a POST request to the HTTP server.
* @param {string} url URL to request.
* @param {Object?} parameters The request parameters.
* @param {Object?} parameters.headers The HTTP headers to send.
* @param {(string|Object)?} parameters.data The data to send.
* @param {number?} parameters.timeout The request timeout.
* @param {boolean?} parameters.unsafeHTTPS If true then requests to servers with
* invalid HTTPS certificates are allowed.
* @returns {Promise
/**
* Does a PUT request to the HTTP server.
* @param {string} url URL to request.
* @param {Object?} parameters The request parameters.
* @param {Object?} parameters.headers The HTTP headers to send.
* @param {(string|Object)?} parameters.data The data to send.
* @param {number?} parameters.timeout The request timeout.
* @param {boolean?} parameters.unsafeHTTPS If true then requests to servers with
* invalid HTTPS certificates are allowed.
* @returns {Promise
/**
* Does a PATCH request to the HTTP server.
* @param {string} url URL to request.
* @param {Object?} parameters The request parameters.
* @param {Object?} parameters.headers The HTTP headers to send.
* @param {(string|Object)?} parameters.data The data to send.
* @param {number?} parameters.timeout The request timeout.
* @param {boolean?} parameters.unsafeHTTPS If true then requests to servers with
* invalid HTTPS certificates are allowed.
* @returns {Promise
/**
* Does a DELETE request to the HTTP server.
* @param {string} url URL to request.
* @param {Object?} parameters The request parameters.
* @param {Object?} parameters.headers The HTTP headers to send.
* @param {(string|Object)?} parameters.data The data to send.
* @param {number?} parameters.timeout The request timeout.
* @param {boolean?} parameters.unsafeHTTPS If true then requests to servers with
* invalid HTTPS certificates are allowed.
* @returns {Promise
/**
* Does a request to the HTTP server.
* @param {string} url URL to request.
* @param {Object?} parameters The request parameters.
* @param {string?} parameters.method The HTTP method for the request.
* @param {string?} parameters.url The URL for the request.
* @param {Object?} parameters.headers The HTTP headers to send.
* @param {(string|Object)?} parameters.data The data to send.
* @param {number?} parameters.timeout The request timeout.
* @param {boolean?} parameters.unsafeHTTPS If true then requests to servers with
* invalid HTTPS certificates are allowed.
* @returns {Promise
`javascript`
{
method: 'GET',
timeout: 30000,
// sets value to XMLHttpRequest.withCredentials, works only in a browser
withCredentials: false,
unsafeHTTPS: false, // requires valid certificate by default
headers: {
Cookie: 'name=value'
},
data: {
parameter: 'value' // all parameters will be URL encoded
}
}
In case you're doing POST/PUT/PATCH requests, data object willapplication/x-www-form-urlencoded
be passed as via request stream.Content-Type
If you set a header to application/json then object will
be sent as JSON.
If data value is not an object then its string representation will be senttext/plain
as to the server.
Also, if you put anything to data object and useapplication/x-www-form-urlencoded then this data will be
automatically percent-encoded.
Request result consists of following:
* The status object with HTTP status code, status text and response headerscontent
* Response as a plain text or an objectContent-Type
(depends on in response headers)
For example, request result can be an object like this:
`javascript`
{
status: {
code: 200,
text: 'OK',
headers: {
'cache-control': 'no-cache',
'content-length': '1',
'content-type': 'text/html; charset=utf-8',
'date': 'Tue, 08 Apr 2014 05:16:19 GMT'
}
},
content: 'some content from server'
}
All header names are always in a lower-case like they are in node.js.
`javascript
const cat = catberry.create();
const uhr = require('catberry-uhr');
uhr.register(cat.locator);
`
Then you can just resolve uhr from the locator:
`javascript``
class Store {
constructor(locator) {
this._uhr = locator.resolve('uhr');
}
load() {
const options = {
timeout: 3000,
data: {
username: 'some'
},
headers: {
Authorization: 'Bearer somecrazytoken'
}
};
return this._uhr.get('http://localhost/api/user', options)
.then(result => result.content);
}
}
There are a lot of ways to contribute:
* Give it a star
* Join the Gitter room and leave a feedback or help with answering users' questions
* Submit a bug or a feature request
* Submit a PR
Denis Rechkunov