Can I contribute to this client script?
rfg-anthony-lim opened this issue · 3 comments
Currently, RESTClient.go() method uses callbacks for completed calls. It's a smart implementation, and I would like to submit an asynchronous method for a modernized approach.
Using the asynchronous approach, we can utilize the then() and catch() methods for successful and failed REST calls, respectively.
Yes, we always appreciate contributions.
We build our client libraries using a template and a JSON DSL, so I can't easily accept a PR to the client code but I'd be happy to review any code examples or suggested improvements.
Or if you'd like to review our submit a PR to the template that may work.
If the change will be to the RESTClient code that will be easier to accept PR.
In any case feel free to offer some examples code or I can prototype this change and see if you think this will work better for you.
I modified the go() method to return a promise. I have tested this in my application.
It is backward compatible with the current code:
- Since go() currently doesn't return anything, and FusionAuthClient currently returns all the go() method calls, consuming this will be straightforward.
- Callbacks will still be invoked when it is present, and the REST call is completed.
/**
* Creates the request to the REST API and returns a Promise object.
* If the callBack argument is set, it will be invoked with argument ClientResponse object
*
* Some browsers don't support Promise API natively.
* You can add polyfills to support this.
* https://cdn.polyfill.io/v2/polyfill.min.js
*
* @param {Function} callBack
* @returns {Promise} of {ClientResponse}
*/
go: function(callBack) {
if (this.parameters) {
this.restUrl = this.restUrl + this._getQueryString();
}
var clientResponse = new ClientResponse();
var xhr = new XMLHttpRequest();
const self = this;
const promise = new Promise(function(resolve, reject){
xhr.onreadystatechange = function() {
// readyState is DONE
if(xhr.readyState === 4) {
clientResponse.statusCode = xhr.status;
var json = xhr.responseText;
try {
json = JSON.parse(xhr.responseText);
} catch (err) {
}
if (clientResponse.wasSuccessful()) {
clientResponse.successResponse = json;
resolve(clientResponse);
} else {
clientResponse.errorResponse = json;
reject(clientResponse);
}
}
};
xhr.open(self.method, self.restUrl, true);
for (var header in self.headers) {
xhr.setRequestHeader(header, self.headers[header]);
}
xhr.send(self.body);
});
if (typeof callBack === 'function') {
promise.then(callBack).catch(callBack);
}
return promise;
},
Happy Thanksgiving to you!
Hi @rfg-anthony-lim - sorry for such a long delay. If you want to use a promise, would the node client work for you?
https://github.com/FusionAuth/fusionauth-node-client/blob/master/lib/RESTClient.js