Skip to content

Instantly share code, notes, and snippets.

@kwhitaker
Created February 3, 2015 19:57
Show Gist options
  • Save kwhitaker/e80296dd6016cf2a8df6 to your computer and use it in GitHub Desktop.
Save kwhitaker/e80296dd6016cf2a8df6 to your computer and use it in GitHub Desktop.
Decorator plugin for superagent.js, which lets you take actions on a response before the callback is fired.
# Use superagent's standard plugin architecture:
# superagent.get('/some/url').use(agentInterceptor).end(function(res){...});
agentInterceptor = (res) ->
Request = superagent.Request
oldCb = Request.prototype.callback
Request.prototype.callback = (err, r) ->
switch r.status
when 404
some404Func()
when 500
someErrorFunc()
else
oldCb.apply @, [err, r]
module.exports = agentInterceptor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment