Provides functions for making requests to the server.
These functions will handle any generic error handling, such as server down, or session timed out, for you.
NOTE: This module is highly dependent on the version of jQuery that is being used. We may upgrade jQuery at any time,
and this will affect the behavior of this module.
Web Resource: com.atlassian.bitbucket.server.bitbucket-web-api:server
- Source:
Methods
-
<static> ajax(options) → {jqXHR}
-
This function closely resembles the jQuery.ajax() function with a few notable exceptions.
First, it only accepts the options signature - all options including
urlmust be included on the options object.Second, it adds default error handling for all HTTP error codes and for cases where logged in state changes.
Third, it overrides the
statusCodeoption to allow you to specify your own error handling per-HTTP code.Parameters:
Name Type Description optionsObject A map of option values. All options accepted by jQuery.ajax are accepted here.
- Source:
Returns:
- A jQuery XHR object.
- Type
- jqXHR
Example
require('bitbucket/util/server').ajax({ url : '/plugins/servlet/my-plugin' statusCode : { 400 : false, // do not do any default handling for HTTP 400 404 : function(xhr, textStatus, errorThrown, dominantError) { // return false; // do not handle this by default // return myDeferred.promise(); // resolve the request with my custom promise return { shouldReload : true }; // open a dialog requesting the user to reload the page. } } }); -
<static> poll(options) → {Promise}
-
This function builds on
restby adding polling. You can use it to make a request repeatedly, until
a "finished" response is returned. This is useful, for example, when waiting for the server to complete a background task
like deleting a repository or waiting for maintenance to complete.Parameters:
Name Type Description optionsObject See
bitbucket/util/server.restfor the options accepted.Properties
Name Type Argument Default Description pollTimeoutnumber | boolean <optional>
60000 The number of milliseconds to poll before ending the poll
as a failure. May passfalseto indicate no timeout.intervalnumber <optional>
500 The number of milliseconds between each AJAX response and subsequent request.
delaynumber <optional>
0 The number of milliseconds before the first AJAX call.
tickfunction A function to call with each AJAX response's callback parameters.
It should returntruthyto end polling successfully,undefined
to continue polling, orfalsyto end polling as a failure.- Source:
Returns:
A jQuery Promise with added pause() and resume() methods.
- Type
- Promise
Example
require('bitbucket/util/server').poll({ url : '/plugin/servlet/expensive-task-checker', tick : function(data, textStatus, xhr) { if (data.expensiveTaskComplete) { return true; // success } if (data.expensiveTaskAborted) { return false; // failure } // return undefined; // keep polling. return undefined is implied. } }); -
<static> rest(options) → {jqXHR}
-
This function builds on
bitbucket/util/server.ajaxto add some defaults. It will:- Default the content type and Accept header to JSON (but this can be overridden).
- Add X-AUSERNAME and X-AUSERID headers for the current user, so the server knows which user we are attempting to make the request as.
- Stringify any JSON objects passed through the
dataoption. - Adds a fourth argument to the success handler that contains the JSON object parsed from the response body.
- Adds a fourth argument to the error handler that contains a description of how the error would have been handled by default.
Parameters:
Name Type Description optionsObject See
bitbucket/util/server.ajaxfor a description of the accepted options.- Source:
Returns:
- A jQuery XHR object.
- Type
- jqXHR
Example
require('bitbucket/util/server').rest({ type : 'DELETE', url : '/rest/my-plugin/latest/things/1' statusCode : { 404 : function() { return $.Deferred().resolve('Already deleted.'); } } });