-
Notifications
You must be signed in to change notification settings - Fork 0
Main API
io(url|options) takes a single argument, which can be a string, or an object. If it is an object, following properties can be processed:
-
urlis the only required property. It is a URL of an endpoint we deal with.
The rest of properties are all optional with reasonable defaults:
-
methodis an HTTP method (including PATCH) as a string. Default: 'GET'. -
queryis a query dictionary (a key/value hash), which is used to form a query part of URL after'?'. Values of such dictionary can be strings, or arrays of strings to form multiple values with the same key. If URL already contains a query part, it is added as is without checking for duplicates. Default: none. -
datais a data object to send. For GET method it is assumed to be a query object, ifqueryis not specified. For all other requests, it is assumed to be a payload. Ifdatais an object ofFormData,ArrayBuffer,Blob, orDocument, it is sent as is. Otherwise, ifContent-Typeisapplication/jsonor missing,datais assumed to be a JSON object and stringified. In all other cases it is assumed to be a preformatted value, and send as is. Default: none. -
headersis a dictionary (a key/value hash), which is used to set request headers. Values of such a dictionary can be strings, or arrays of strings to form multiple values with the same key. Default: none, but if there is noAcceptheader, it is set toapplication/json.
The next batch of properties is directly related to an underlying XHR request:
-
useris a user name as a string to be sent with the request. Default: not sent. -
passwordis a password as a string. It is used only ifuseris specified. Default:''. -
timeoutis a wait time for a request in milliseconds as a number. Default: not set. -
responseTypeis a requested response type as a string. It can be:'json','arraybuffer','blob','document','text', or''. Essentially it defines an automatic conversion of a received response inresponseproperty of XHR, which is used byio()to return a value. Default: not set. -
withCredentialsis a Boolean flag for cross-origin requests. Default: not set. -
mimeis a string used to override a returned MIME type inContent-Typeresponse header. Default: not set.
The next batch of properties used to replace default processing of received response:
-
returnXHRis a Boolean flag. Iftruean XHR object is returned instead of received data. It is useful, if we want to inspect headers, or raw data. Default:false. -
processSuccess(result)is a function that receives a result object for processing and returns either a data object, or a rejected promise. Default:io.processSuccess()(see below for more details). -
processFailure(error)is a function that received an error object for processing, and it should return either a data object, or a rejected promise. It will be called when I/O was unsuccessful, orprocessSuccess()returned an error. Default:io.processFailure()(see below for more details).
The next batch of properties is used for an advanced processing with plugins:
-
waitis a Boolean flag that indicates our interest in this I/O request, but we don't want to initiate it at this point. Requirestrackplugin. Default: false. -
bundleis a Boolean flag that allows to bundle this request. -
cacheis a Boolean flag that allows to use cache for this request. -
trackis a Boolean flag that allows to track this request (required for bundling). -
mockis a Boolean flag that allows to mock this request.
See respective plugins for more details on those properties, and their default values.
If a string is specified as the only argument of io(), it is assumed to be a URL. It will be treated as:
// io(url) =>
io({
url: url,
method: 'GET'
})io() returns a promise:
io('http://example.com').then(
function (data) {
console.log('We got data:', data);
},
function (error) {
console.log('We failed with', error.xhr.status, '=>', error.xhr.statusText);
}
);io.get() initiates a GET request. It takes two arguments:
-
urlis a URL as a string. -
optionsis an options object described above (seeio()). -
queryis an optional dictionary to form a query string (seedataabove inio()).
Either url or options should be specified.
The returned value is a promise (see io() for details).
io.head() initiates a HEAD request, and as such it returns no data. Otherwise it is identical to io.get().
io.post() initiates a POST request. It takes two arguments:
-
urlis a URL as a string. -
optionsis an options object described above (seeio()). -
datais an optional data object (seedataabove inio()).
Either url or options should be specified.
The returned value is a promise (see io() for details).
io.put() initiates a PUT request. Otherwise it is identical to io.post().
io.patch() initiates a PATCH request. Otherwise it is identical to io.post().
io.remove() initiates a DELETE request. Otherwise it is identical to io.post().
The other way to initiate a DELETE request is to call io['delete'](), which is less convenient than calling io.remove(),
because delete is a reserved word in JavaScript (not a problem nowadays). io.remove() is an alias of this function.
Following properties are available to customize all aspects of I/O handling.
It is an object constructor used to indicate an underlying I/O error. It takes three arguments:
-
xhris an underlying XHR object. It can benullin certain cases, e.g., for JSON-P calls. -
optionsis an originaloptionsobject, which initialized this request. -
eventis an event object, which finished the request, and possibly provides more information about the error. It can benull.
All those arguments are exposed as properties on an error object.
Objects of this type is used for generic I/O errors, when XHR has failed to start. It is exposed so user can construct its own errors,
or use it with instanceof operator.
io.FailedIO is used as a base class for other io errors.
It is a constructor based on io.FailedIO with the same arguments. It is used to indicate timeout errors.
It is a constructor based on io.FailedIO with the same arguments. It is used to indicate server status errors.
This function takes a key/value dictionary object and returns a correctly encoded query string. Values are assumed to be strings, or arrays of strings. The latter are used to form multiple values with the same key.
It is called when I/O itself was successful. It will be called even if a server returned an error. It is its job to detect such errors and respond accordingly.
A result object has three properties:
-
xhris an underlying XHR object to process. -
optionsis an originaloptionsobject, which initialized this request. -
eventis a load event object, which finished the request. It can benull.
This function is the first in the promise's then-chain. It performs following actions:
- If
statusis not in 2XX range, it returns a rejected promise with a valie ofio.BadStatus. That object will have all those three properties mentioned above. - If
responseTypewas set, it will returnresponseof an XHR object. - If
responseXMLof an XHR is set, it will be returned. - If the returned
Content-Typeisapplication/json, the received string will be parsed as a JSON object, which will be subsequently returned. - Otherwise
responseTextof an XHR will be returned.
This function can be overwritten for individual requests in io()'s options as processSuccess property.
By default an error object can be of three types:
-
io.FailedIO, when I/O failed to happen. -
io.TimedOut, whentimeoutwas specified, but I/O did not finish in a specified time. -
io.BadStatus, when everything was fine, but our response indicates a logical error, such as a bad status from a server.
An error object has the same three properties as the result object of io.processSuccess().
This function is the first in the promise's catch-chain right after io.processSuccess().
It returns a rejected promise resolved to an XHR object of the failed request.
If XHR is not available (e.g., for JSON-P requests), null is used.
This function can be overwritten for individual requests in io()'s options as processFailure property.
This function is called to transform io()'s options. Before calling this function, if options is a string,
it is assumed to be a URL, and new options object is formed:
{
url: url
}It ensures that the argument of io.processOPtions() is always an object.
The default implementation returns its argument as is.
This function is exposed solely to be replaced, or augmented (AOP-style) by users to accommodate unique requirements.
This procedure is used to configure an XHR object, and return a data object ready to be sent out. Arguments:
-
xhris an XHR object that will be used for a request. -
optionsis an originaloptionsobject, which initialized this request. -
datais an object fromoptions.dataornull.
The default implementation performs following actions:
- Checks headers for
Acceptheader. If it is not set, sets it toapplication/json. - Checks the method. If it is GET or missing (GET is assumed), returns
nullmeaning "no payload". - If
datais of following types:FormData,ArrayBuffer,Blob, orDocument, it returnsdataas is. - Checks headers for
Content-Type.- If it is
application/json,datais assumed to be JSON, it is stringified and returned. - If it is missing,
Content-Typeis set toapplication/json,datais assumed to be JSON, it is stringified and returned.
- If it is
- Otherwise
datais returned as is.
This function is exposed solely to be replaced, or augmented (AOP-style) by users to accommodate unique requirements.
This is the main function that actually starts an I/O request. It sets up an XHR object, forms a query string,
calls io.processData(), and controls a returned promise.
The only argument is options like in io() function. If it was a string (a URL), it is transformed to a minimal object as described above.
It returns a promise, which will be resolved with result object (see io.processSuccess() above for details), or error object
(see io.processFailure() for details). Both io.processSuccess() and io.processFailure() will be attached to this promise.
This function is exposed solely to be replaced, or augmented (AOP-style) by users to accommodate unique requirements.