Skip to content
This repository was archived by the owner on Feb 8, 2024. It is now read-only.

Commit 84efff6

Browse files
committed
Add resource creator
1 parent 34f142b commit 84efff6

5 files changed

Lines changed: 78 additions & 3 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Declare your routes
55

66
## Warning!
77

8-
> That middleware now in development! Do not use it in production (only before v1 release)
8+
> Now in development! Do not use it in production (only before v1 release)
99
1010

1111
## Usage example

examples/test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const {
99
scope,
1010
childs,
1111
resources,
12+
resource,
1213
member,
1314
} = require('../dist/index')
1415

@@ -37,7 +38,8 @@ const router = createRest({}, childs(
3738
get('foo'),
3839
member(
3940
get('bar', noop),
40-
post('baz', [authByToken, noop])
41+
post('baz', [authByToken, noop]),
42+
resource('like', { before: [authByToken], after: [noop] }, Foo)
4143
)
4244
))
4345
))

lib/creators/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export put from './put'
55
export patch from './patch'
66
export destroy from './destroy'
77
export resources from './resources'
8-
// export resource from './resource'
8+
export resource from './resource'
99
export scope from './scope'
1010
export member from './member'
1111
export childs from './childs'

lib/creators/resource.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import pluralize from 'pluralize'
2+
import { addRoute, createContext, addChildContext } from '../context'
3+
4+
5+
/**
6+
* @typedef {Object} ResourceConfig
7+
* @property {string[]} [only] Create only that routes
8+
* @property {string[]} [except] That routes that will not be created
9+
* @example
10+
* { only: ['index', 'create'] }
11+
* { except: ['update', 'destroy'] }
12+
*/
13+
14+
15+
16+
const routes = [
17+
['index', 'GET'],
18+
['create', 'POST'],
19+
['update', 'PUT'],
20+
['destroy', 'DELETE'],
21+
]
22+
23+
const defaultConfig = {
24+
}
25+
26+
/**
27+
* Creates a set of routes that describe single resource
28+
* @param {string} name Base name in lowercase. From name a path will be created.
29+
* @param {ResourceConfig} configuration Setup for resource
30+
* @param {Object} controller Object with handlers
31+
* @param {Function} [createChilds] Childs will be added to resource root
32+
* @example
33+
* resource('profile', {}, ProfileController)
34+
*/
35+
export default function resource(name, configuration, controller, createChilds) {
36+
const config = Object.assign({}, defaultConfig, configuration)
37+
const insetContext = createContext(name)
38+
let targetRoutes = routes.concat([])
39+
40+
insetContext.controller = controller
41+
42+
if (config.only) {
43+
targetRoutes = routes.filter(route => config.only.includes(route[0]))
44+
}
45+
else if (config.except) {
46+
targetRoutes = routes.filter(route => !config.only.includes(route[0]))
47+
}
48+
49+
targetRoutes.forEach(([methodName, httpMethod]) => {
50+
const method = controller[methodName]
51+
let handlers = []
52+
53+
if (config.before) {
54+
handlers = handlers.concat(config.before)
55+
}
56+
57+
handlers.push(method)
58+
59+
if (config.after) {
60+
handlers = handlers.concat(config.after)
61+
}
62+
63+
addRoute(httpMethod, '', handlers)(insetContext)
64+
})
65+
66+
if (createChilds) {
67+
createChilds(insetContext)
68+
}
69+
70+
return addChildContext(insetContext)
71+
}

lib/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createContext } from './context'
22

33
/**
4+
* Defines your routes
45
*
56
* @param {Object} config Root configuration
67
* @param {Function} applyRoot Main childs for root `childs()`
@@ -27,5 +28,6 @@ export {
2728
destroy,
2829
scope,
2930
resources,
31+
resource,
3032
member,
3133
} from './creators'

0 commit comments

Comments
 (0)