Skip to content

Commit db0fd2d

Browse files
author
Steffan
committed
add uploadProgress, downloadProgress callbacks
1 parent a254833 commit db0fd2d

File tree

4 files changed

+24
-17
lines changed

4 files changed

+24
-17
lines changed

docs/http.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,10 @@ params | `Object` | Parameters object to be sent as URL parameters
5151
method | `string` | HTTP method (e.g. GET, POST, ...)
5252
responseType | `string` | Type of the response body (e.g. text, blob, json, ...)
5353
timeout | `number` | Request timeout in milliseconds (`0` means no timeout)
54-
before | `function(request)` | Callback function to modify the request options before it is sent
55-
progress | `function(event)` | Callback function to handle the [ProgressEvent](https://developer.mozilla.org/en-US/docs/Web/API/ProgressEvent) of uploads
5654
credentials | `boolean` | Indicates whether or not cross-site Access-Control requests should be made using credentials
55+
before | `function(request)` | Callback function to modify the request options before it is sent
56+
uploadProgress | `function(event)` | Callback function to handle the [ProgressEvent](https://developer.mozilla.org/en-US/docs/Web/API/ProgressEvent) of uploads
57+
downloadProgress | `function(event)` | Callback function to handle the [ProgressEvent](https://developer.mozilla.org/en-US/docs/Web/API/ProgressEvent) of downloads
5758
emulateHTTP | `boolean` | Send PUT, PATCH and DELETE requests with a HTTP POST and set the `X-HTTP-Method-Override` header
5859
emulateJSON | `boolean` | Send request body as `application/x-www-form-urlencoded` content type
5960

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"eslint": "^4.17.0",
4242
"generate-release": "^0.14.0",
4343
"jasmine": "^3.0.0",
44-
"jasmine-core": "^2.99.1",
44+
"jasmine-core": "^3.1.0",
4545
"jest": "^22.2.2",
4646
"karma": "^2.0.0",
4747
"karma-chrome-launcher": "^2.2.0",

src/http/client/xhr.js

+19-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44

55
import Promise from '../../promise';
6-
import {each, trim} from '../../util';
6+
import {each, trim, isFunction} from '../../util';
77

88
export default function (request) {
99
return new Promise(resolve => {
@@ -25,14 +25,6 @@ export default function (request) {
2525

2626
request.abort = () => xhr.abort();
2727

28-
if (request.progress) {
29-
if (request.method === 'GET') {
30-
xhr.addEventListener('progress', request.progress);
31-
} else if (/^(POST|PUT)$/i.test(request.method)) {
32-
xhr.upload.addEventListener('progress', request.progress);
33-
}
34-
}
35-
3628
xhr.open(request.method, request.getUrl(), true);
3729

3830
if (request.timeout) {
@@ -51,6 +43,24 @@ export default function (request) {
5143
request.headers.set('X-Requested-With', 'XMLHttpRequest');
5244
}
5345

46+
// deprecated use downloadProgress
47+
if (isFunction(request.progress) && request.method === 'GET') {
48+
xhr.addEventListener('progress', request.progress);
49+
}
50+
51+
if (isFunction(request.downloadProgress)) {
52+
xhr.addEventListener('progress', request.downloadProgress);
53+
}
54+
55+
// deprecated use uploadProgress
56+
if (isFunction(request.progress) && /^(POST|PUT)$/i.test(request.method)) {
57+
xhr.upload.addEventListener('progress', request.progress);
58+
}
59+
60+
if (isFunction(request.uploadProgress) && xhr.upload) {
61+
xhr.upload.addEventListener('progress', request.uploadProgress);
62+
}
63+
5464
request.headers.forEach((value, name) => {
5565
xhr.setRequestHeader(name, value);
5666
});

yarn.lock

+1-5
Original file line numberDiff line numberDiff line change
@@ -3208,11 +3208,7 @@ isurl@^1.0.0-alpha5:
32083208
has-to-string-tag-x "^1.2.0"
32093209
is-object "^1.0.1"
32103210

3211-
jasmine-core@^2.99.1:
3212-
version "2.99.1"
3213-
resolved "https://registry.yarnpkg.com/jasmine-core/-/jasmine-core-2.99.1.tgz#e6400df1e6b56e130b61c4bcd093daa7f6e8ca15"
3214-
3215-
jasmine-core@~3.1.0:
3211+
jasmine-core@^3.1.0, jasmine-core@~3.1.0:
32163212
version "3.1.0"
32173213
resolved "https://registry.yarnpkg.com/jasmine-core/-/jasmine-core-3.1.0.tgz#a4785e135d5df65024dfc9224953df585bd2766c"
32183214

0 commit comments

Comments
 (0)