Skip to content

Commit b6907d8

Browse files
MentthewdragoscirjanDragos CirjanMariusVincent Petry
authored
Allow Promise return type for onBeforeRequest and onAfterResponse. (#212)
* Allow Promise return type for onBeforeRequest and onAfterResponse. * Allow Promise return type for onBeforeRequest and onAfterResponse: documentation and test case. * Allow to specify options for Node's request method (#203) * adding nodejs HttpStack request options * fix * fix * fix * visual fix * proposal for request options * disabling browser * fix * Clarify how to import using ESM syntax * Added onShouldRetry callback for controlling retries (#198) * Added onShouldRetry callback for controlling retry Whenever the library is about to retry an upload due to an error, the new optional callback onShouldRetry will be called when defined. Its return value will tell the library whether to actually retry the upload or fail with an error, for example based on status code checks. This makes it possible to customize the behavior like reacting on specific status codes. * Move onShouldRetry example to usage.md * Refactored shouldRetry logic - isOnline is now excluded from the check, so the onShouldRetry callback should manually add an online check if desired - removed inline function and made the conditions more readable - clarified inline comments about status code check * Add test assertion for onShouldRetry arguments * Update usage.md * Update test-common.js * Update api.md Co-authored-by: Marius <[email protected]> * making property 'private', adding small test for 'insecure' request * fixes ? * fix: import * Update js-base64 to a version which does not use eval() anymore Closes #147 * PR comments fixes * fixed exports on node, addex HttpStack export on browser * added new test case - node 14 * test fixes * test fixes * fix tests * Bump lodash from 4.17.14 to 4.17.19 Bumps [lodash](https://github.com/lodash/lodash) from 4.17.14 to 4.17.19. - [Release notes](https://github.com/lodash/lodash/releases) - [Commits](lodash/lodash@4.17.14...4.17.19) Signed-off-by: dependabot[bot] <[email protected]> * Clean up request initialization and add proper test Co-authored-by: Dragos Cirjan <[email protected]> Co-authored-by: Dragos Cirjan <[email protected]> Co-authored-by: Marius <[email protected]> Co-authored-by: Vincent Petry <[email protected]> Co-authored-by: Marius <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Kevin van Zonneveld <[email protected]> * Bump elliptic from 6.3.1 to 6.5.3 (#214) Bumps [elliptic](https://github.com/indutny/elliptic) from 6.3.1 to 6.5.3. - [Release notes](https://github.com/indutny/elliptic/releases) - [Commits](indutny/elliptic@v6.3.1...v6.5.3) Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Update dependencies using 'npm audit fix' * Do not pass URL as separate parameter This is not supported in Node.js 8 and 9. * Downgrade tsd to support Node.js 8 and 9 See https://github.com/SamVerschueren/tsd/releases/tag/v0.12.1 * Correct type definition for Upload#terminate (#217) inconsistency between typescript typing and function definition * Minor cleanups Co-authored-by: Dragos Cirjan <[email protected]> Co-authored-by: Dragos Cirjan <[email protected]> Co-authored-by: Dragos Cirjan <[email protected]> Co-authored-by: Marius <[email protected]> Co-authored-by: Vincent Petry <[email protected]> Co-authored-by: Marius <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Kevin van Zonneveld <[email protected]> Co-authored-by: kabaliserv <[email protected]>
1 parent 46d7471 commit b6907d8

File tree

3 files changed

+86
-12
lines changed

3 files changed

+86
-12
lines changed

docs/api.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,18 @@ onBeforeRequest: function (req) {
227227
}
228228
```
229229

230+
You can also return a Promise if you need to perform some calculations before the request is sent:
231+
232+
```js
233+
onBeforeRequest: function (req) {
234+
return new Promise(resolve => {
235+
var xhr = req.getUnderlyingObect()
236+
xhr.withCredentials = true
237+
resolve()
238+
})
239+
}
240+
```
241+
230242
#### onAfterResponse
231243

232244
*Default value:* `null`
@@ -241,6 +253,19 @@ onAfterResponse: function (req, res) {
241253
}
242254
```
243255

256+
You can also return a Promise if you need to perform some calculations before tus-js-client processes the response:
257+
258+
```js
259+
onAfterResponse: function (req, res) {
260+
return new Promise(resolve => {
261+
var url = req.getURL()
262+
var value = res.getHeader("X-My-Header")
263+
console.log(`Request for ${url} responded with ${value}`)
264+
resolve()
265+
})
266+
}
267+
```
268+
244269
#### httpStack
245270

246271
*Default value:* Environment-specific implementation

lib/upload.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -834,18 +834,20 @@ class BaseUpload {
834834
* @api private
835835
*/
836836
_sendRequest(req, body = null) {
837-
if (typeof this.options.onBeforeRequest === "function") {
838-
this.options.onBeforeRequest(req);
839-
}
840-
841-
return req.send(body)
842-
.then((res) => {
843-
if (typeof this.options.onAfterResponse === "function") {
844-
this.options.onAfterResponse(req, res);
845-
}
846-
847-
return res;
848-
});
837+
const onBeforeRequestPromise = (typeof this.options.onBeforeRequest === "function")
838+
? Promise.resolve(this.options.onBeforeRequest(req))
839+
: Promise.resolve();
840+
841+
return onBeforeRequestPromise.then(() => {
842+
return req.send(body)
843+
.then((res) => {
844+
const onAfterResponsePromise = (typeof this.options.onAfterResponse === "function")
845+
? Promise.resolve(this.options.onAfterResponse(req, res))
846+
: Promise.resolve();
847+
848+
return onAfterResponsePromise.then(() => res);
849+
});
850+
});
849851
}
850852
}
851853

test/spec/test-common.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,5 +1248,52 @@ describe("tus", function () {
12481248
expect(options.onError).not.toHaveBeenCalled();
12491249
expect(options.onSuccess).toHaveBeenCalled();
12501250
});
1251+
1252+
it("should invoke the request and response Promises", async function () {
1253+
const testStack = new TestHttpStack();
1254+
var file = getBlob("hello world");
1255+
var options = {
1256+
httpStack: testStack,
1257+
uploadUrl: "http://tus.io/uploads/foo",
1258+
onBeforeRequest: function (req) {
1259+
return new Promise(resolve => {
1260+
expect(req.getURL()).toBe("http://tus.io/uploads/foo");
1261+
expect(req.getMethod()).toBe("HEAD");
1262+
resolve();
1263+
});
1264+
},
1265+
onAfterResponse: function (req, res) {
1266+
return new Promise(resolve => {
1267+
expect(req.getURL()).toBe("http://tus.io/uploads/foo");
1268+
expect(req.getMethod()).toBe("HEAD");
1269+
expect(res.getStatus()).toBe(204);
1270+
expect(res.getHeader("Upload-Offset")).toBe(11);
1271+
resolve();
1272+
});
1273+
},
1274+
onSuccess: waitableFunction("onSuccess")
1275+
};
1276+
spyOn(options, "onBeforeRequest");
1277+
spyOn(options, "onAfterResponse");
1278+
1279+
var upload = new tus.Upload(file, options);
1280+
upload.start();
1281+
1282+
var req = await testStack.nextRequest();
1283+
expect(req.url).toBe("http://tus.io/uploads/foo");
1284+
expect(req.method).toBe("HEAD");
1285+
1286+
req.respondWith({
1287+
status: 204,
1288+
responseHeaders: {
1289+
"Upload-Offset": 11,
1290+
"Upload-Length": 11
1291+
}
1292+
});
1293+
1294+
await options.onSuccess.toBeCalled;
1295+
expect(options.onBeforeRequest).toHaveBeenCalled();
1296+
expect(options.onAfterResponse).toHaveBeenCalled();
1297+
});
12511298
});
12521299
});

0 commit comments

Comments
 (0)