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

Commit 29ec64e

Browse files
committed
Add forgotten filess
1 parent 8dc05b0 commit 29ec64e

7 files changed

Lines changed: 226 additions & 0 deletions

File tree

.babelrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"presets": ["latest"],
3+
"plugins": ["transform-export-extensions", "syntax-trailing-function-commas"]
4+
}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,4 @@ Temporary Items
115115
.nfs*
116116

117117
# End of https://www.gitignore.io/api/node,windows,osx,linux
118+
dist

examples/test.fp.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
2+
function createRest() {}
3+
function resources() {}
4+
function resource() {}
5+
function member() {}
6+
function childs() {}
7+
function scope() {}
8+
function root() {}
9+
function post() {}
10+
function get() {}
11+
function destroy() {}
12+
function put() {}
13+
function patch() {}
14+
15+
class SectionsController {}
16+
class BooksController {}
17+
class DemoController {}
18+
class LikeController {}
19+
class ProfileController {}
20+
class PagesController {}
21+
class BookmarksController {}
22+
23+
module.exports = createRest(
24+
root(
25+
resources('book', { validate: mySuperValidateFunction }, BooksController),
26+
resources('section', { only: ['index', 'read'] }, SectionsController),
27+
resources('demo', {}, DemoController, childs(
28+
member(
29+
get('status'),
30+
post('close'),
31+
post('open', { methodName: 'reopen' }),
32+
),
33+
resource('like', {}, LikeController),
34+
)),
35+
resource('profile', {}, ProfileController, childs(
36+
resources('bookmark', { memberId: 'id' }, BookmarksController, childs(
37+
member(
38+
resource('share_link', {}, BookmarksController.ShareLinkController),
39+
),
40+
)),
41+
)),
42+
scope('admin', childs(
43+
post('login'),
44+
post('logout'),
45+
resources('pages', {}, PagesController),
46+
scope('status', childs(
47+
get('database'),
48+
get('service'),
49+
get('redis'),
50+
)),
51+
)),
52+
),
53+
)
54+
55+
56+
const SimpleController = {
57+
read() {},
58+
create() {},
59+
update() {},
60+
destroy() {},
61+
}
62+
63+
const myValidations = (req, res, next) => {
64+
// validations
65+
next()
66+
}
67+
68+
createRest(
69+
resource('simple', { before: [myValidations] }, SimpleController),
70+
)
71+

examples/test.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const {
2+
createRest,
3+
printRoutes,
4+
get,
5+
post,
6+
put,
7+
patch,
8+
destroy,
9+
scope,
10+
childs,
11+
resources,
12+
member,
13+
} = require('../dist/index')
14+
15+
const noop = () => {}
16+
const Demo = { load() {} }
17+
const authByToken = () => {}
18+
const Foo = { index() {}, create() {}, read() {}, update() {}, patch() {}, destroy() {} }
19+
20+
const router = createRest({}, childs(
21+
get('example_get', noop),
22+
post('post_example', [noop, noop]),
23+
put('update_example', noop),
24+
patch('patching', [noop, noop, noop]),
25+
destroy('drop', noop),
26+
scope('first', childs(
27+
get('getter', noop),
28+
scope('another', childs(
29+
get('read', noop),
30+
post('create', noop),
31+
put('update', noop),
32+
patch('patch', [authByToken, Demo.load]),
33+
destroy('remove', [noop, noop])
34+
))
35+
)),
36+
resources('resource', {}, Foo, childs(
37+
get('foo'),
38+
member(
39+
get('bar', noop),
40+
post('baz', [authByToken, noop])
41+
)
42+
))
43+
))
44+
45+
console.log('\n\n', router, '\n\n')
46+
printRoutes(router)

examples/test.oop.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class SectionsController {}
2+
class BooksController {}
3+
class DemosController {}
4+
class LikeController {}
5+
class ProfileController {}
6+
class BookmarksController {}
7+
function createRest() {}
8+
9+
const router = createRest()
10+
module.exports = router
11+
12+
router.root(r => {
13+
r.resources('book', {}, BooksController)
14+
r.resources('section', { only: ['index', 'read'] }, SectionsController)
15+
16+
r.resources('demo', {}, DemosController, r => {
17+
r.member.post('close')
18+
r.member.post('open', { methodName: 'reopen' })
19+
20+
r.resource('like', {}, LikeController)
21+
})
22+
23+
r.resource('profile', {}, ProfileController, r => {
24+
r.resources('bookmark', { memberId: 'id' }, BookmarksController, r => {
25+
r.member({}, r => {
26+
r.resource('share_link', {}, BookmarksController.ShareLinkController)
27+
})
28+
})
29+
})
30+
31+
r.scope('admin', r => {
32+
33+
})
34+
})
35+
36+

lib/context.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
/**
3+
*
4+
* @param {String} path
5+
*/
6+
export function createContext(path = '') {
7+
return {
8+
path,
9+
before: [],
10+
after: [],
11+
routes: [],
12+
childs: [],
13+
}
14+
}
15+
16+
/**
17+
*
18+
* @param {String} method
19+
* @param {String} path
20+
* @param {(Function[]|Function)} handler
21+
*/
22+
export function addRoute(method, path, handler = []) {
23+
const handlers = Array.isArray(handler) ? handler : [handler]
24+
25+
return context => {
26+
context.routes.push({
27+
method,
28+
path,
29+
handlers,
30+
})
31+
32+
return context
33+
}
34+
}
35+
36+
export function addChildContext(childContext) {
37+
return context => {
38+
context.childs.push(childContext)
39+
}
40+
}
41+

lib/printer.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
*
3+
* @param {String[]} path
4+
* @param {String} method
5+
* @param {Function[]} handlers
6+
*/
7+
function print(path, method, handlers) {
8+
const handlersNames = handlers.map(handler => `${handler.name}()`).join(', ')
9+
10+
console.log(method.toUpperCase(), path.join('/'), '->', handlersNames)
11+
}
12+
13+
/**
14+
*
15+
* @param {Object} routes
16+
*/
17+
export function printRoutes(context, path = []) {
18+
const fullPath = path.concat(context.path)
19+
20+
context.routes.forEach(route =>
21+
print(fullPath.concat(route.path), route.method, route.handlers)
22+
)
23+
24+
context.childs.forEach(child =>
25+
printRoutes(child, fullPath)
26+
)
27+
}

0 commit comments

Comments
 (0)