-
Notifications
You must be signed in to change notification settings - Fork 298
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
HEAD call useless without resolveWithFullResponse #58
Comments
Good point @zcei ! Indeed, I do prefer having the same default settings for all convenience functions because it is less confusing for the users. Nonetheless could you rephrase your proposed change? I don't understand what you mean by "having res.headers as the default parameter passed to .head() calls". Right away you may make use of the var rp = require('request-promise');
var rpWithFullResp = rp.defaults({ resolveWithFullResponse: true });
rpWithFullResp.head(...);
rp.get(...); |
Uh, saw the The proposed change would be one .then(function extractHeader(res) {
return res.headers;
}) |
I like your idea. This improves convenience and doesn't really introduce inconsistency in this case. |
Would it be sufficient to add another if (isFunction(self._rp_options.transform)) {
try {
self._rp_resolve(self._rp_options.transform(body, response));
} catch (e) {
self._rp_reject(e);
}
} else if (self._rp_options.resolveWithFullResponse) {
self._rp_resolve(response);
+ } else if (response.request.method === 'HEAD') {
+ self._rp_resolve(response.headers);
} else {
self._rp_resolve(body);
} or would you directly want to implement something like |
I already started the implementation and chose to add it directly because that should be much more performant: if (isFunction(self._rp_options.transform)) {
try {
self._rp_resolve(self._rp_options.transform(body, response));
} catch (e) {
self._rp_reject(e);
}
} else if (self._rp_options.resolveWithFullResponse) {
self._rp_resolve(response);
} else {
- self._rp_resolve(body);
+ self._rp_resolve(self._rp_options.method && self._rp_options.method.toUpperCase() === 'HEAD' ? response.headers : body);
} However, an interceptor might be a very useful idea. Please have a look at the transform function and give me feedback whether this already covering everything you imagine or if we should extend its capabilities or if a new interceptor mechanism would allow to cover new use cases. Thanks a lot! |
Transform seems fine for me. if (isString(options.method)) {
options.method = options.method.toUpperCase();
+ options.transform = options.transform || defaultTransformations[options.method];
} Obviously with an var defaultTransformations = {
HEAD: extractHeaders
}; Then the transformation stays |
Thanks @zcei that is a really clean solution! |
I just released version 1.0.0 which fixes this issue. Thanks for providing the solution in your last comment @zcei which I just had to apply. |
As a
HEAD
call is aGET
call without response body, it's pretty useless without theresolveWithFullResponse
option, as you get an empty string all the time.I personally would vote for having
res.headers
as the default parameter passed to.head()
calls, or to at least setresolveWithFullResponse
totrue
by default.Or is it intended to preserve the same behavior in all convenience functions, even if it's nonsense to a particular function?
The text was updated successfully, but these errors were encountered: