Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 6603d4f

Browse files
committed
feat(route:resource): resource members & coll accepts callback
resource members and collections accepts callback to further chain methods to attach middleware or bind action
1 parent 6037bdb commit 6603d4f

File tree

4 files changed

+312
-10
lines changed

4 files changed

+312
-10
lines changed

src/Route/ResourceCollection.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
'use strict'
2+
3+
/**
4+
* adonis-framework
5+
*
6+
* (c) Harminder Virk <virk@adonisjs.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
const helpers = require('./helpers')
13+
const util = require('../../lib/util')
14+
15+
class ResourceCollection {
16+
17+
constructor (route) {
18+
this.route = route
19+
}
20+
21+
/**
22+
* binds action to the route, it will override
23+
* the old action
24+
*
25+
* @param {String|Function} action
26+
*
27+
* @return {Object}
28+
*
29+
* @public
30+
*/
31+
bindAction (action) {
32+
this.route.handler = action
33+
return this
34+
}
35+
36+
/**
37+
* @see this.middleware
38+
*/
39+
middlewares () {
40+
return this.middleware.apply(this, arguments)
41+
}
42+
43+
/**
44+
* appends middlewares to the route
45+
*
46+
* @return {Object}
47+
*
48+
* @public
49+
*/
50+
middleware () {
51+
helpers.appendMiddleware(
52+
this.route,
53+
util.spread.apply(this, arguments)
54+
)
55+
return this
56+
}
57+
58+
/**
59+
* assign name to the route
60+
*
61+
* @param {String} name
62+
*
63+
* @return {Object}
64+
*
65+
* @public
66+
*/
67+
as (name) {
68+
this.route.name = name
69+
return this
70+
}
71+
72+
/**
73+
* return json representation of the route
74+
*
75+
* @return {Object}
76+
*
77+
* @public
78+
*/
79+
toJSON () {
80+
return this.route
81+
}
82+
83+
}
84+
85+
module.exports = ResourceCollection

src/Route/ResourceMember.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
'use strict'
2+
3+
/**
4+
* adonis-framework
5+
*
6+
* (c) Harminder Virk <virk@adonisjs.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
const helpers = require('./helpers')
13+
const util = require('../../lib/util')
14+
15+
class ResourceMember {
16+
17+
constructor (route) {
18+
this.route = route
19+
}
20+
21+
/**
22+
* binds action to the route, it will override
23+
* the old action
24+
*
25+
* @param {String|Function} action
26+
*
27+
* @return {Object}
28+
*
29+
* @public
30+
*/
31+
bindAction (action) {
32+
this.route.handler = action
33+
return this
34+
}
35+
36+
/**
37+
* @see this.middleware
38+
*/
39+
middlewares () {
40+
return this.middleware.apply(this, arguments)
41+
}
42+
43+
/**
44+
* appends middlewares to the route
45+
*
46+
* @return {Object}
47+
*
48+
* @public
49+
*/
50+
middleware () {
51+
helpers.appendMiddleware(
52+
this.route,
53+
util.spread.apply(this, arguments)
54+
)
55+
return this
56+
}
57+
58+
/**
59+
* assign name to the route
60+
*
61+
* @param {String} name
62+
*
63+
* @return {Object}
64+
*
65+
* @public
66+
*/
67+
as (name) {
68+
this.route.name = name
69+
return this
70+
}
71+
72+
/**
73+
* return json representation of the route
74+
*
75+
* @return {Object}
76+
*
77+
* @public
78+
*/
79+
toJSON () {
80+
return this.route
81+
}
82+
83+
}
84+
85+
module.exports = ResourceMember

src/Route/resource.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const helpers = require('./helpers')
1111
const CatLog = require('cat-log')
1212
const logger = new CatLog('adonis:framework')
1313
const util = require('../../lib/util')
14+
const ResourceMember = require('./ResourceMember')
15+
const ResourceCollection = require('./ResourceCollection')
1416
const NE = require('node-exceptions')
1517

1618
/**
@@ -45,14 +47,17 @@ class Resource {
4547
* @param {String} route
4648
* @param {String} handler
4749
* @param {String} name
48-
* @return {void}
50+
*
51+
* @return {Object}
4952
*
5053
* @private
5154
*/
5255
_registerRoute (verb, route, handler, name) {
5356
const resourceName = (this.basename === '/' || !this.basename) ? name : `${this.basename}.${name}`
5457
this.RouteHelper.route(route, verb, `${handler}.${name}`).as(resourceName)
55-
this.routes.push(this.RouteHelper._lastRoute())
58+
const registeredRoute = this.RouteHelper._lastRoute()
59+
this.routes.push(registeredRoute)
60+
return registeredRoute
5661
}
5762

5863
/**
@@ -188,50 +193,56 @@ class Resource {
188193
/**
189194
* add a member route to the resource
190195
*
191-
* @param {String} action - the handle action method
192-
*
193196
* @param {String} route - Route and action to be added to the resource
194197
* @param {Mixed} [verbs=['GET', 'HEAD']] - An array of verbs
198+
* @param {Function} [callback]
199+
*
195200
* @return {Object} - reference to resource instance for chaining
196201
*
197202
* @example
198203
* Route.resource('...').addMember('completed')
199204
*
200205
* @public
201206
*/
202-
addMember (route, verbs) {
207+
addMember (route, verbs, callback) {
203208
if (_.isEmpty(route)) {
204209
throw new NE.InvalidArgumentException('action argument must be present')
205210
}
206211

207212
verbs = verbs || ['GET', 'HEAD']
208213
verbs = _.isArray(verbs) ? verbs : [verbs]
209-
this._registerRoute(verbs, `${this.pattern}/:id/${route}`, this.handler, route)
214+
const registeredRoute = this._registerRoute(verbs, `${this.pattern}/:id/${route}`, this.handler, route)
215+
if (typeof (callback) === 'function') {
216+
callback(new ResourceMember(registeredRoute))
217+
}
210218
return this
211219
}
212220

213221
/**
214222
* add a collection route to the resource
215223
*
216-
* @param {String} action - the handle action method
217-
*
218224
* @param {String} route - Route and action to be added to the resource
219225
* @param {Mixed} [verbs=['GET', 'HEAD']] - An array of verbs
226+
* @param {Function} [callback]
227+
*
220228
* @return {Object} - reference to resource instance for chaining
221229
*
222230
* @example
223231
* Route.resource('...').addCollection('completed')
224232
*
225233
* @public
226234
*/
227-
addCollection (route, verbs) {
235+
addCollection (route, verbs, callback) {
228236
if (_.isEmpty(route)) {
229237
throw new NE.InvalidArgumentException('action argument must be present')
230238
}
231239

232240
verbs = verbs || ['GET', 'HEAD']
233241
verbs = _.isArray(verbs) ? verbs : [verbs]
234-
this._registerRoute(verbs, `${this.pattern}/${route}`, this.handler, route)
242+
const registeredRoute = this._registerRoute(verbs, `${this.pattern}/${route}`, this.handler, route)
243+
if (typeof (callback) === 'function') {
244+
callback(new ResourceCollection(registeredRoute))
245+
}
235246
return this
236247
}
237248

0 commit comments

Comments
 (0)