Skip to content

Commit

Permalink
Add @cache decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilya Radchenko committed Sep 16, 2015
1 parent 5470aa6 commit 40bce6d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,7 @@ Overall configuration setting if none of the other decorators are sufficient.

Add a validation object for the different types, except for the response.
`config` is an object, with keys for the different types, e.g. `payload`.

### `@cache(cacheConfig)`

Cache settings for the route config object.
14 changes: 14 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,20 @@ function validate (config) {

exports.validate = validate

function cache (cacheConfig) {
return function (target, key, descriptor) {
setRoute(target, key, {
config: {
cache: cacheConfig
}
})

return descriptor
}
}

exports.cache = cache

function setRoute (target, key, value) {
if (!target.rawRoutes) {
target.rawRoutes = []
Expand Down
23 changes: 17 additions & 6 deletions test/fixtures/default.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
var web = require('../../')
var controller = web.controller
var route = web.route
var cache = web.cache
var config = web.config
var validate = web.validate

@web.controller('/check')
@controller('/check')
class Check {
@web.route('get', '/in')
@web.validate({ payload: true })
@route('get', '/in')
@validate({ payload: true })
checkIn(request, reply) {
// intentionally empty
}

@web.route('get', '/out')
@web.config({ test: 'hello' })
@route('get', '/out')
@config({ test: 'hello' })
checkOut(request, reply) {


}

@route('get', '/')
@cache({ privacy: 'public' })
listAll(request, reply) {

}
}

Expand Down
11 changes: 10 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ test('instance generates routes array', function (t) {
let results = instance.routes()

t.ok(Array.isArray(results), 'results is an array')
t.equal(results.length, 2, 'Has right number of routes')
t.equal(results.length, 3, 'Has right number of routes')

let first = results[0]

Expand All @@ -44,6 +44,15 @@ test('validate sets up config correctly', function (t) {
t.end()
})

test('cache sets up config correctly', function (t) {
let instance = new Default()
let results = instance.routes()
let route = results[2]

t.same(route.config.cache, { privacy: 'public' }, 'cache config is valid')
t.end()
})

test('config sets up config correctly', function (t) {
let instance = new Default()
let results = instance.routes()
Expand Down

0 comments on commit 40bce6d

Please sign in to comment.