1.0.35 • Published 6 years ago

open-request-js v1.0.35

Weekly downloads
2
License
MIT
Repository
-
Last release
6 years ago

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.

ParamTypeDefaultDescription
urlstring | UrlThe url of the request.
methodstring"get"The request method.
dataObject | ParametersThe 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.

ParamTypeDescription
dataParameters | Object | stringThe 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.

ParamTypeDefaultDescription
urlstring | UrlThe url of the request.
methodstring"get"The request method.
dataObject | ParametersThe 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.

ParamTypeDescription
urlstring | UrlThe url of the request.
dataObject | ParametersThe 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.

ParamTypeDescription
urlstring | UrlThe url of the request.
dataObject | ParametersThe 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.

ParamTypeDescription
urlstring | UrlThe url of the request.
dataObject | ParametersThe 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.

ParamTypeDescription
urlstring | UrlThe url of the request.
dataObject | ParametersThe 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.

ParamTypeDescription
urlstring | UrlThe url of the request.
dataObject | Parameters | ParametersThe data to send.

HttpResponse

Kind: global class

new HttpResponse(data, status)

Creates a HttpResponse object.

ParamTypeDescription
datastringThe data of the response.
statusStatus | ObjectThe 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.

ParamTypeDescription
codenumber | stringThe code of the response.
textstringThe 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

ParamTypeDescription
codenumber | stringThe code of the response.
textstringThe text of the response.

Classes

1.0.35

6 years ago

1.0.34

6 years ago

1.0.33

6 years ago

1.0.32

6 years ago

1.0.31

6 years ago

1.0.30

6 years ago

1.0.29

6 years ago

1.0.28

6 years ago

1.0.27

6 years ago

1.0.26

6 years ago

1.0.25

6 years ago

1.0.24

6 years ago

1.0.23

6 years ago

1.0.22

6 years ago

1.0.21

6 years ago

1.0.20

6 years ago

1.0.19

6 years ago

1.0.18

6 years ago

1.0.17

6 years ago

1.0.16

6 years ago

1.0.15

6 years ago

1.0.14

6 years ago

1.0.13

6 years ago

1.0.12

6 years ago

1.0.11

6 years ago

1.0.10

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago