From c2792a80897146593f0861909c60db52af40fe3b Mon Sep 17 00:00:00 2001 From: Pablo Palacios Date: Sun, 21 Jul 2024 13:40:30 +0200 Subject: [PATCH] feat: make Request a thenable object In this way we don't need to call .end() to trigger the request when using promises. --- libs/fetcher.client.js | 22 ++++++++++++++++++++++ libs/fetcher.js | 24 ++++++++++++++++++++++++ tests/util/testCrud.js | 10 ---------- 3 files changed, 46 insertions(+), 10 deletions(-) diff --git a/libs/fetcher.client.js b/libs/fetcher.client.js index 6dbb876..d522900 100644 --- a/libs/fetcher.client.js +++ b/libs/fetcher.client.js @@ -104,6 +104,24 @@ Request.prototype._captureMetaAndStats = function (err, result) { } }; +Request.prototype.then = function (resolve, reject) { + return this.end(function (err, data, meta) { + if (err) { + reject(err); + } else { + resolve({ data, meta }); + } + }); +}; + +Request.prototype.catch = function (reject) { + return this.end(function (err) { + if (err) { + reject(err); + } + }); +}; + /** * Execute this fetcher request and call callback. * @method end @@ -112,6 +130,10 @@ Request.prototype._captureMetaAndStats = function (err, result) { * @async */ Request.prototype.end = function (callback) { + console.warn( + 'You called .end() without a callback. This will become an error in the future. Use .then() instead.', + ); + var self = this; self._startTime = Date.now(); diff --git a/libs/fetcher.js b/libs/fetcher.js index 13df16a..3fb2c27 100644 --- a/libs/fetcher.js +++ b/libs/fetcher.js @@ -226,6 +226,12 @@ class Request { * is complete. */ end(callback) { + if (!callback) { + console.warn( + 'You called .end() without a callback. This will become an error in the future. Use .then() instead.', + ); + } + this._startTime = Date.now(); const promise = new Promise((resolve, reject) => { @@ -254,6 +260,24 @@ class Request { return promise; } } + + then(resolve, reject) { + return this.end((err, data, meta) => { + if (err) { + reject(err); + } else { + resolve({ data, meta }); + } + }); + } + + catch(reject) { + return this.end((err) => { + if (err) { + reject(err); + } + }); + } } /** diff --git a/tests/util/testCrud.js b/tests/util/testCrud.js index 23a6bba..66fefaa 100644 --- a/tests/util/testCrud.js +++ b/tests/util/testCrud.js @@ -70,7 +70,6 @@ module.exports = function testCrud( .params(params) .body(body) .clientConfig(config) - .end() .then( resolve(operation, done), reject(operation, done), @@ -81,7 +80,6 @@ module.exports = function testCrud( this.fetcher[operation](resource) .params(params) .clientConfig(config) - .end() .then( resolve(operation, done), reject(operation, done), @@ -93,7 +91,6 @@ module.exports = function testCrud( .params(params) .body(body) .clientConfig(config) - .end() .then( resolve(operation, done), reject(operation, done), @@ -104,7 +101,6 @@ module.exports = function testCrud( this.fetcher[operation](resource) .params(params) .clientConfig(config) - .end() .then( resolve(operation, done), reject(operation, done), @@ -128,7 +124,6 @@ module.exports = function testCrud( .params(params) .body(body) .clientConfig(config) - .end() .then(denySuccess(done), allowFailure(done)); }); it('should reject a READ promise on invalid resource', function (done) { @@ -136,7 +131,6 @@ module.exports = function testCrud( this.fetcher[operation](invalidResource) .params(params) .clientConfig(config) - .end() .then(denySuccess(done), allowFailure(done)); }); it('should reject a UPDATE promise on invalid resource', function (done) { @@ -145,7 +139,6 @@ module.exports = function testCrud( .params(params) .body(body) .clientConfig(config) - .end() .then(denySuccess(done), allowFailure(done)); }); it('should reject a DELETE promise on invalid resource', function (done) { @@ -153,7 +146,6 @@ module.exports = function testCrud( this.fetcher[operation](invalidResource) .params(params) .clientConfig(config) - .end() .then(denySuccess(done), allowFailure(done)); }); it('should throw if no resource is given', function () { @@ -377,7 +369,6 @@ module.exports = function testCrud( meta: { headers: { 'x-foo': 'foo' } }, }) .clientConfig(config) - .end() .catch(function (err) { if (err) { var serviceMeta = fetcher.getServiceMeta(); @@ -414,7 +405,6 @@ module.exports = function testCrud( fetcher .read(mockNoopService.resource) .clientConfig(config) - .end() .catch(function (err) { expect(err.name).to.equal('FetchrError'); expect(err.message).to.contain(