-
Notifications
You must be signed in to change notification settings - Fork 6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JS code gen: Using Promises and non-jquery ajax #2014
Comments
@delenius are you generating the client with |
That looks pretty nice, except for the fact that it doesn't use standard promises. I'll give it a shot anyway. |
@wing328 do you mean suggest changes to this project or to the superagent project? |
I mean this project. |
I guess you could provide a // bunch of stuff
var handleResponse = null;
if (callback) {
handleResponse = function(error, data, response) {
callback(error, data, response);
};
}
return this.apiClient.callApi(
'/', 'GET',
pathParams, queryParams, headerParams, formParams, postBody,
contentTypes, accepts, handleResponse
); instead, you could do // bunch of stuff
return new Promise(function(resolve,reject) {
var handleResponse = function(error, data, response) {
error ? reject(error) : resolve(data);
};
this.apiClient.callApi(
'/', 'GET',
pathParams, queryParams, headerParams, formParams, postBody,
contentTypes, accepts, handleResponse
);
}); which would allow the caller to call the usual |
Thanks for the suggestion. Would you have cycle to submit a PR with the suggested change? |
@delenius Thanks for the suggestions. The reason I didn't use For controlled environments where // helper function
function makeCallback(resolve, reject) {
return function (error, data, response) {
if (error) {
reject(error);
} else {
resolve([data, response]);
}
};
}
// use the helper function
var p = new Promise((resolve, reject) => {
petApi.getPetById(1, makeCallback(resolve, reject));
});
p.then(([pet, response]) => {
console.log('Pet:', pet);
}); Another possible helper function: // PetApi.js
PetApi.prototype.callWithPromise = function (methodName, ...args) {
return new Promise((resolve, reject) => {
this[methodName](...args, makeCallback(resolve, reject));
});
};
// use the helper method
var p = petApi.callWithPromise('getPetById', 1);
p.then(([pet, response]) => {
console.log('Pet:', pet);
}); With that said, we could just generate additional functions to return var p = petApi.getPetByIdWithPromise(1); Any ideas? |
There are polyfills to support Promise on older browsers. It would be cleanest to not have to invoke a bunch of additional stuff just to get the promises. After all, the "online" JS generator has a "usePromise" parameter along these lines. |
For the "online" JS generator, do you mean https://editor.swagger.io? |
I think I got it: https://github.com/swagger-api/swagger-js/blob/master/lib/client.js#L130 |
@wing328 yes that one :) |
For the record, there is superagent-promise |
This is a couple of feature requests for the JavaScript client generator (not the runtime generator, I am generating form a swagger file offline):
Is there any way to make the generator produce a JS client produce a client that uses Promises? It looks like the online generator can do this, but not the offline one, but I could be wrong.
Is there any chance we could get rid of jQuery to do the ajax calls? It is unfortunate to have to pull in this heavy dependency just for that, when there are many lightweight ajax libraries out there. Ideally it should work both in a Web client and from Node, so how about something like isomorphic-fetch? This also uses Promises, so it helps with Adds the missing install-ivy build script #1 above.
The text was updated successfully, but these errors were encountered: