-
Notifications
You must be signed in to change notification settings - Fork 1
/
decorator.js
47 lines (41 loc) · 994 Bytes
/
decorator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const nock = require('nock')
const theon = require('..')
// Set up mock
nock('http://my.api.com')
.get('/api/users/123')
.matchHeader('Version', '1.0')
.reply(200, [{
id: 123,
username: 'foo'
}])
const client = theon('http://my.api.com')
.set('Version', '1.0')
.basePath('/api')
.format('json')
const users = client
.collection('users')
.basePath('/users')
// Decorate entity contructor
.decorate(function (delegate) {
delegate.root.set('Content-Type', 'application/json')
return delegate
})
users
.resource('get')
.path('/:id')
// Decorate resource constructor
.decorate(function (delegate) {
return function decorator (id) {
const delegation = delegate()
delegation.param('id', id)
return delegation
}
})
// Render the API
const api = client.render()
api.users
// Path param in now supported in the constructor via decorator
.get('123')
.end(function (err, res) {
console.log('Body:', res.body)
})