Skip to content

Commit

Permalink
feat: make Request a thenable object
Browse files Browse the repository at this point in the history
In this way we don't need to call .end() to trigger the request when
using promises.
  • Loading branch information
pablopalacios committed Jul 21, 2024
1 parent 8668e3c commit 3470325
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
24 changes: 24 additions & 0 deletions libs/fetcher.client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -112,6 +130,12 @@ Request.prototype._captureMetaAndStats = function (err, result) {
* @async
*/
Request.prototype.end = function (callback) {
if (!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();

Expand Down
24 changes: 24 additions & 0 deletions libs/fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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);
}
});
}
}

/**
Expand Down
10 changes: 0 additions & 10 deletions tests/util/testCrud.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ module.exports = function testCrud(
.params(params)
.body(body)
.clientConfig(config)
.end()
.then(
resolve(operation, done),
reject(operation, done),
Expand All @@ -81,7 +80,6 @@ module.exports = function testCrud(
this.fetcher[operation](resource)
.params(params)
.clientConfig(config)
.end()
.then(
resolve(operation, done),
reject(operation, done),
Expand All @@ -93,7 +91,6 @@ module.exports = function testCrud(
.params(params)
.body(body)
.clientConfig(config)
.end()
.then(
resolve(operation, done),
reject(operation, done),
Expand All @@ -104,7 +101,6 @@ module.exports = function testCrud(
this.fetcher[operation](resource)
.params(params)
.clientConfig(config)
.end()
.then(
resolve(operation, done),
reject(operation, done),
Expand All @@ -128,15 +124,13 @@ 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) {
var operation = 'read';
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) {
Expand All @@ -145,15 +139,13 @@ 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) {
var operation = 'delete';
this.fetcher[operation](invalidResource)
.params(params)
.clientConfig(config)
.end()
.then(denySuccess(done), allowFailure(done));
});
it('should throw if no resource is given', function () {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 3470325

Please sign in to comment.