open-request-js

1.0.35 • Public • Published

Open Request JS 📣

npm version Maintainability

Simply send http requests.

Install

npm install open-request-js

Usage

var Request = require('open-request-js'); // To be used from node.
var Request = require('open-request-js/dist'); // To be used from browser.
 
var request = new Request('/users/:id?lang=en');
 
request.url.path.id = 4;
request.url.parameters.lang = 'fr';
request.url.string;
// => "/users/4?lang=fr"
 
request.send().then(response => console.log(response.json));

Why does the require statement depend on wether it's meant to be use from node or from browser?

Because from node, Request needs to require XMLHttpRequest while it already exists in the browser. By the way, you will not be able to use html and xml HttpResponse accessors from node neither because (at the moment) they depend on browser-exclusive APIs.

Request

Kind: global class

new Request([url], [method], [data])

Creates a Request object.

Param Type Default Description
[url] string | Url The url of the request.
[method] string "get" The request method.
[data] Object | Parameters The data to send.

request.method : string

The method of the request. When set it will be checked for being included in 'get', 'post', 'put', 'patch' and 'delete'. If not it will be replaced by 'get'.

Kind: instance property of Request

request.actualMethod : string

The actual method that will be used to send the request (wether 'get' or 'post').

Kind: instance property of Request
Read only: true

request.url : Url | string

The request url.

Kind: instance property of Request

request.data : Parameters | Object | string

The request data. When set it will be parsed to a Parameters object.

Kind: instance property of Request

request.send(data) ⇒ Promise

Sends the request, merging the given data with the instance data to a new object.

Kind: instance method of Request
Returns: Promise - A promise to resolved when the request finished and succeed, or to be rejected if any error occurs (including http errors). A HttpResponse object is always passed.

Param Type Description
data Parameters | Object | string The data to send, in addition to the instance data.

Request.send(url, [method], [data]) ⇒ Promise

Sends a request.

Kind: static method of Request
Returns: Promise - A promise to resolved when the request finished and succeed, or to be rejected if any error occurs (including http errors). A HttpResponse object is always passed.

Param Type Default Description
url string | Url The url of the request.
[method] string "get" The request method.
[data] Object | Parameters The data to send.

Request.get(url, [data]) ⇒ Promise

Sends a get request.

Kind: static method of Request
Returns: Promise - A promise to resolved when the request finished and succeed, or to be rejected if any error occurs (including http errors). A HttpResponse object is always passed.

Param Type Description
url string | Url The url of the request.
[data] Object | Parameters The data to send.

Request.post(url, [data]) ⇒ Promise

Sends a post request.

Kind: static method of Request
Returns: Promise - A promise to resolved when the request finished and succeed, or to be rejected if any error occurs (including http errors). A HttpResponse object is always passed.

Param Type Description
url string | Url The url of the request.
[data] Object | Parameters The data to send.

Request.put(url, [data]) ⇒ Promise

Sends put get request.

Kind: static method of Request
Returns: Promise - A promise to resolved when the request finished and succeed, or to be rejected if any error occurs (including http errors). A HttpResponse object is always passed.

Param Type Description
url string | Url The url of the request.
[data] Object | Parameters The data to send.

Request.patch(url, [data]) ⇒ Promise

Sends patch get request.

Kind: static method of Request
Returns: Promise - A promise to resolved when the request finished and succeed, or to be rejected if any error occurs (including http errors). A HttpResponse object is always passed.

Param Type Description
url string | Url The url of the request.
[data] Object | Parameters The data to send.

Request.delete(url, [data]) ⇒ Promise

Sends a delete request.

Kind: static method of Request
Returns: Promise - A promise to resolved when the request finished and succeed, or to be rejected if any error occurs (including http errors). A HttpResponse object is always passed.

Param Type Description
url string | Url The url of the request.
[data] Object | Parameters | Parameters The data to send.

HttpResponse

Kind: global class

new HttpResponse([data], [status])

Creates a HttpResponse object.

Param Type Description
[data] string The data of the response.
[status] Status | Object The status of the response.

httpResponse.status : Status | Object

The response status. When set, it will be parsed to a Status object

Kind: instance property of HttpResponse

httpResponse.text : string

The response in text format.

Kind: instance property of HttpResponse

httpResponse.json : Object

The response in json format.

Kind: instance property of HttpResponse

httpResponse.xml : Element

The response in XML format.

Kind: instance property of HttpResponse

httpResponse.html : Element

The response in HTML format.

Kind: instance property of HttpResponse

Status

Kind: global class

new Status([code], [text])

Creates a Status object.

Param Type Description
[code] number | string The code of the response.
[text] string The text of the response.

status.code : number | string

The code of the response. When set, it will be parsed to an integer, and set to 0 if it's invalid.

Kind: instance property of Status

status.text : string

The text of the response.

Kind: instance property of Status

status.info : boolean

Tells if this is an info.

Kind: instance property of Status

status.success : boolean

Tells if this is a success.

Kind: instance property of Status

status.redirection : boolean

Tells if this is a redirection.

Kind: instance property of Status

status.error : boolean

Tells if this is an error.

Kind: instance property of Status

status.set([code], [text])

Set the status

Kind: instance method of Status

Param Type Description
[code] number | string The code of the response.
[text] string The text of the response.

Classes

Request
HttpResponse
Status

Readme

Keywords

Package Sidebar

Install

npm i open-request-js

Weekly Downloads

0

Version

1.0.35

License

MIT

Unpacked Size

46.3 kB

Total Files

12

Last publish

Collaborators

  • juliendargelos