Skip to content

Instantly share code, notes, and snippets.

@alexlawrence
Created March 13, 2015 09:03
Show Gist options
  • Save alexlawrence/dbac7feae0917cecc484 to your computer and use it in GitHub Desktop.
Save alexlawrence/dbac7feae0917cecc484 to your computer and use it in GitHub Desktop.
middlewearable v2
makeFunctionWithMiddleware = (originalFunction) ->
_allFunctions = [originalFunction]
middleWearableFunction = ->
_allFunctions[0].apply null, arguments
middleWearableFunction.use = ([criteria]..., middleware) ->
nextFunction = _allFunctions[0]
decoratedMiddleWare = (args...) ->
if criteria and not criteria.apply(null, args)
nextFunction.apply null, args
else
middleware.apply null, args.concat nextFunction
_allFunctions.unshift decoratedMiddleWare
middleWearableFunction
fakeHttpFunction = (request, response) ->
console.log 'got request', request.path
fakeHttpFunction = makeFunctionWithMiddleware fakeHttpFunction
fakeHttpFunction.use(
(request) ->
request.path.indexOf('/profile') > -1
(request, response, next) ->
if request.header is 'authorized'
next request, response
else
console.log 'unauthorized'
)
fakeHttpFunction {path: '/'}, {}
fakeHttpFunction {path: '/profile'}, {}
fakeHttpFunction {path: '/profile', header: 'authorized'}, {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment