AbstractXhrService

AbstractXhrService ⇐ Abstract

Kind: global abstract class
Extends: Abstract
Service:

AbstractXhrService()

A service to manage HTTP requests. Compatibility: IE10+

Features :

  • a flexible request method wrapping the native XMLHttpRequest object.
  • shortcuts for get, post, put, patch, delete, head, patch HTTP methods.
  • feature detection and partial polyfill for XMLHttpRequest.requestType
  • convenience methods to build RFC compliant requests (query-string, form-urlencoded, RFC3986, RFC5987)
  • can send binary files and even whole HTML Forms with multipart/form-data
  • wrappers around the API that default to whatever options passed to them. Allows multiple cascading defaults sets.
  • jQuery-style beforeSend callback to modify requests on-the-fly
  • fires a serverDown event
  • basic time statistics

Body options and defaults wrapper inspired by Node.JS request library.

mergeConfig(sources) ⇒ Object*

Deep-cloning helper. First arguments more important than last. Only headers are deep-cloned, not potential form data.

Kind: instance method of AbstractXhrService
Returns: Object - A javascript object holding the merged header objects.

ParamTypeDescription
sources*Object

Various objects passed as arguments that will be merged.

resetLoadingStats()

Reset the Loading information

Kind: instance method of AbstractXhrService

defaults(config) ⇒ Object

This method returns a wrapper around the normal API that defaults to whatever options you pass to it. Note: request.defaults() does not modify the global API; instead, it returns a wrapper that has your default settings applied to it. Note: You can call .defaults() on the wrapper that is returned to add/override defaults that were previously defaulted.

Kind: instance method of AbstractXhrService

ParamTypeDescription
configObject

the default config to use by this wrapper.

Example

var baseRequest = $XhrService.getInstance().defaults({ headers: { 'Content-Type': 'application/x-www-form-urlencoded' } });
  baseRequest.get('http://example.com')

_getUrl(url, query) ⇒ string

Appends query string to the all HTTP request URLs get(), post(), put(), patch(), head(), del() are calling this method

Kind: instance method of AbstractXhrService
Returns: string - New URL with query
Access: protected

ParamTypeDescription
urlstring

URL without query (?...) nor fragment (#...)

queryObject

URL query as an object

_requestConf([config], [method]) ⇒ Object

Configure request() config object for all HTTP methods Called by _postConf, head and get.

Kind: instance method of AbstractXhrService
Returns: Object - new request() method config
Access: protected

ParamTypeDescription
[config]Object

Source config

[method]string

HTTP method (will override config defaults)

_postConf([method], [jsonPayload], [config]) ⇒ Object

Configure request() config object for POST-like methods Called by post, put, patch and del are calling this method.

Send jsonPayload as “application/json”. Equivalent to post/put/patch(url, undefined, { json: jsonPayload }) To send as “x-www-form-urlencoded”, call post/put/patch(url, undefined, { form: payload }) To send as “multipart/formdata”, call post/put/patch(url, undefined, { formData: payload })

Kind: instance method of AbstractXhrService
Returns: Object - new request() method config
Access: protected

ParamTypeDescription
[method]string

HTTP method

[jsonPayload]*

HTTP request payload (sent as a application/json)

[config]Object

Source config

get(url, [query], [config]) ⇒ Promise.<(Object|*)>

Send a GET HTTP request

Kind: instance method of AbstractXhrService
Returns: Promise.<(Object|*)> - Returns similar XHR result object on success, error on failure (types depends on vendor impl.)
See: https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.3

Will invoke _requestConf and _getUrl.

ParamTypeDescription
urlstring

URL (w/o fragment (#...) nor query (?...) if query arg is defined)

[query]Object

URL query as an object

[config]Object

request() method config

post(url, [jsonPayload], [config], [query]) ⇒ Promise.<(Object|*)>

Send a POST HTTP request

Kind: instance method of AbstractXhrService
Returns: Promise.<(Object|*)> - Returns similar XHR result object on success, error on failure (depends on vendor impl.)
See: https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.5

Will invoke _postConf and _getUrl.

ParamTypeDescription
urlstring

URL (w/o fragment (#...) nor query (?...) if query arg is defined)

[jsonPayload]*

HTTP request payload (sent as a application/json)

[config]Object

request() method config

[query]Object

URL query as an object

put(url, [jsonPayload], [config], [query]) ⇒ Promise.<(Object|*)>

Send a PUT HTTP request

Kind: instance method of AbstractXhrService
Returns: Promise.<(Object|*)> - Returns similar XHR result object on success, error on failure (depends on vendor impl.)
See: https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.6

Will invoke _postConf and _getUrl.

ParamTypeDescription
urlstring

URL (w/o fragment (#...) nor query (?...) if query arg is defined)

[jsonPayload]*

HTTP request payload (sent as a application/json)

[config]Object

request() method config

[query]Object

URL query as an object

patch(url, [jsonPayload], [config], [query]) ⇒ Promise.<(Object|*)>

Send a PATCH HTTP request

Will invoke _postConf and _getUrl.

Kind: instance method of AbstractXhrService
Returns: Promise.<(Object|*)> - Returns similar XHR result object on success, error on failure (depends on vendor impl.)

ParamTypeDescription
urlstring

URL (w/o fragment (#...) nor query (?...) if query arg is defined)

[jsonPayload]*

HTTP request payload (sent as a application/json)

[config]Object

request() method config

[query]Object

URL query as an object

head(url, [query], [config]) ⇒ Promise.<(Object|*)>

Send a HEAD HTTP request

Kind: instance method of AbstractXhrService
Returns: Promise.<(Object|*)> - Returns similar XHR result object on success, error on failure (depends on vendor impl.)
See: https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.4

Will invoke _requestConf and _getUrl.

ParamTypeDescription
urlstring

URL (w/o fragment (#...) nor query (?...) if query arg is defined)

[query]Object

URL query as an object

[config]Object

request() method config

del(url, [query], [config]) ⇒ Promise.<(Object|*)>

Send a DELETE HTTP request

Kind: instance method of AbstractXhrService
Returns: Promise.<(Object|*)> - Returns similar XHR result object on success, error on failure (depends on vendor impl.)
See: https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.7

Will invoke _postConf and _getUrl.

ParamTypeDescription
urlstring

URL (w/o fragment (#...) nor query (?...) if query arg is defined)

[query]Object

URL query as an object

[config]Object

request() method config

request(url, conf) ⇒ Promise.<Object>

Generic request. This is were everything in this service happens.

Kind: instance method of AbstractXhrService
Returns: Promise.<Object> - The promise resolves with an object similar to the XHR object, or rejects with an error object.
See

  • https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest
  • https://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5
ParamTypeDefaultDescription
urlstring

complete URL with query string

confObject
conf.methodstring

GET, POST, PUT, DELETE, HEAD, OPTIONS, PATCH...

[conf.timeout]number0

request timeout in ms

[conf.withCredentials]booleanfalse

see XMLHttpRequest.withCredentials

[conf.responseType]string"''"

One of "arraybuffer", "blob", "document", "json", or "text". Default '' is the same as 'text'.

[conf.overrideMimeType]string

Overrides the MIME type returned by the server.

[conf.onprogress]function

progression callback

[conf.headers]Object

HTTP headers

[conf.body]string

entity body for PATCH, POST and PUT requests.

[conf.form]Object

data to send in the body. Forces Content-type: application/x-www-form-urlencoded

[conf.json]Object

data to send in the body. Forces Content-type: application/json; charset=utf-8

[conf.formData]Object

Data to pass for a multipart/form-data request. Use to send binary files. Requires the FormData API.

[conf.beforeSend]function

A function(url, xhr, conf) to be invoked (synchronously) before sending the request. Can modify the request, or cancel it by returning false or throwing an error.

stringifyPrimitive(v) ⇒ string

Code adapted from Node.js require('querystring').stringify()

Kind: instance method of AbstractXhrService
See: https://github.com/Gozala/querystring/blob/master/encode.js

ParamType
vstring | boolean | number

queryStringEncode(obj, sep, eq, name) ⇒ string

Code adapted from Node.js require('querystring').stringify()

Kind: instance method of AbstractXhrService
See: https://github.com/Gozala/querystring/blob/master/encode.js

ParamType
objObject
sepstring
eqstring
namestring

formURLEncode(str) ⇒ string

For “application/x-www-form-urlencoded”, spaces are to be replaced by ‘+’, so one may wish to follow a encodeURIComponent replacement with an additional replacement of “%20” with “+”.

Kind: instance method of AbstractXhrService
See: https://html.spec.whatwg.org/multipage/forms.html#application/x-www-form-urlencoded-encoding-algorithm

ParamType
strstring

_request(url, conf, body) ⇒ Promise.<Object>

Generic request. This is were everything in this service happens.

Kind: instance abstract method of AbstractXhrService
Returns: Promise.<Object> - The promise resolves with an object similar to the XHR object, or rejects with an error object.

ParamTypeDescription
urlstring

complete URL with query string

confObject

configuration object. @see AbstractXhrService#request

conf.headersArray.<string>

request headers, modified and prepared.

bodyObject

Body of the request

“serverDown”

This event is fired when an http error code 0 (no network) or 5xx (server error) is encountered.

Kind: event emitted by AbstractXhrService