This class enhances the Fetch API by rejecting on HTTP error status, even for an HTTP 404
or 500.
The Fetch API provides an interface for fetching resources (including across the network). It will seem familiar to anyone who has used XMLHttpRequest, but the new API provides a more powerful and flexible feature set.
Problem with Fetch API: The Promise returned from fetch() will not reject
on HTTP error status even if the response is an HTTP 404 or 500. Instead, it will resolve normally
(with ok status set to false), and it will only reject on network failure or if anything prevented the
request from completing.
- Since:
- 4.7.0
Methods
(static) poll(fn, validateFn, intervalopt, maxAttemptsopt) → {Promise}
Polling function inspired from Polling in JavaScript blog post.
The function is a higher-order function that returns a function, executePoll. The executePoll
function returns a promise and will run recursively until a stopping condition is met.
Example Usage:
mindsmine.Http.poll(
() => {
// Function to be polled
},
(result) => {
// Function that validates the result of the above function
}
).then((result) => {
// Do something with the result
}).catch((response) => {
console.error(`HTTP error code = ${response.status}`);
console.error(`HTTP error code = ${response.body.message}`);
});
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
fn |
function | The function to be polled. |
||
validateFn |
function | The function that validates the result. |
||
interval |
Number |
<optional> |
10000 | The interval (in milliseconds) in between polling the function. |
maxAttempts |
Number |
<optional> |
6 | The maximum times the function should be polled. |
- Since:
- 4.8.0
Throws:
-
If invalid arguments.
- Type
- TypeError
Returns:
- Type
- Promise
(static) request(url, optionsopt) → {Promise}
Sends an HTTP request to a remote server and returns a Promise object. This function enhances the global
fetch function by rejecting on HTTP error status, even for an HTTP 404 or
500.
The promise resolves to the Response object representing the response to the request.
Problem with fetch function: A fetch() promise only rejects when a network error is encountered
(which is usually when there is a permissions issue or similar). A fetch() promise does not reject on HTTP
errors (404, etc.). Instead, a then() handler must check the
Response.ok and/or
Response.status properties.
Example Usage:
mindsmine.Http.request(
"valid URI",
{
headers: {
"Accept" : "some value",
"Content-Type" : "some value",
"Authorization" : "some value"
}
}
).then((response) => {
console.log(`HTTP status code = ${response.status}`);
response.json().then(data => {
console.log(data);
});
}).catch((response) => {
console.log(`HTTP error code = ${response.status}`);
}).finally(() => {
console.log("This function is called regardless of success or failure.");
});
Parameters:
| Name | Type | Attributes | Description | |||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
url |
String | The URL to which to send the request. |
||||||||||||||||||||||||||||||||||||
options |
Object |
<optional> |
An object containing any custom settings to apply to the request. The possible options are: Properties
|
- Since:
- 4.7.0
Throws:
-
If invalid arguments.
- Type
- TypeError
Returns:
The promise that resolves to the Response object representing the response to the request.
- Type
- Promise