Minimal SuperAgent customizations for working with Indaba Music API
- Use standard SuperAgent API unmodified
- Use
agent.inGet,agent.inGetAll,agent.inPost, andrequest.inEndfor extra convenience with Indaba API. - Set
agent._endpointto use relative paths - Set
agent._tokento include Authorization header in each request - Use
inEndinstead ofendto parse jSend response and provides error first callback
$ component install indabaui/agent
var agent = require('agent');
// path + callback
agent.inGet('/opportunities', function(err, data) {
console.log(data);
});
// path + query + callback
agent.inGet('/opportunities', {limit: 10}, function(err, data) {
console.log(data);
});
// superagent chaining style
agent.inGet('/opportunities')
.query({limit: 10})
.query({offset: 80})
.inEnd(function(err, data) {
console.log(data);
});
// inGetAll
agent.inGetAll('/opportunities', function(err, data) {
console.log(data);
});
// one off authenticated request
agent.inGet('/whoami')
.query({access_token: 'xxxx'})
.inEnd(function(err, data) {
console.log(data);
});
// set `agent._token` and you can omit access_token on subsequent requests
agent._token = 'xxxx';
agent.inGet('/whoami')
.inEnd(function(err, data) {
console.log(data);
});Do not set agent._token on the server. There is only one instance of agent, so this token is not unique to any context.
Instead, you should explicitly include a user's token when using agent server side:
// one off authenticated request
agent.inGet('/whoami')
.query({access_token: 'xxxx'})
.inEnd(function(err, data) {
console.log(data);
});MIT