The issue you linked to isn't linked to any commits, so all we can do is speculate on whether or not that feature was implemented and then removed, or never implemented in the first place.
If you read through the src, you will see that use
is only ever defined on the prototype of the Request
constructor, meaning it can only ever be used after you've began constructing a request, as shown in the readme.
In other words, the issue seems to be talking about a feature that either has been removed, or never existed. You should instead use the syntax mentioned in the readme.
var request = require('superagent');
request
.get('/some-url')
.use(bearer) // affects **only** this request
.end(function(err, res){
// Do something
});
function bearer ( request ){
// "config" is a global var where token and other stuff resides
if ( config.token ) {
request.set( 'Authorization', 'Bearer ' + config.token );
}
}
You could of course create your own wrapper so that you don't have to do it for every request.
var superagent = require('superagent');
function request(method, url) {
// callback
if ('function' == typeof url) {
return new superagent.Request('GET', method).end(url).use(bearer);
}
// url first
if (1 == arguments.length) {
return new superagent.Request('GET', method).use(bearer);
}
return new superagent.Request(method, url).use(bearer);
}
// re-implement the .get and .post helpers if you feel they're important..
function bearer ( request ){
// "config" is a global var where token and other stuff resides
if ( config.token ) {
request.set( 'Authorization', 'Bearer ' + config.token );
}
}
request('GET', '/some-url')
.end(function(err, res){
// Do something
});
request
function that is exported by superagent does not have ause
method. however, the instance ofRequest
that is returned by executingrequest
does. Whether or not this is a bug, or can be fixed without a pull request is unclear. – Roving