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

Commit 3fecc44

Browse files
committed
Add creator for resources. #11
1 parent a2948d0 commit 3fecc44

5 files changed

Lines changed: 214 additions & 36 deletions

File tree

packages/createrest/examples/all-in-use.js

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,74 @@
11
const { createRest, flattenRoutes, printRoutes } = require('../dist')
22

3-
const before1 = () => { console.log('before1()') }
4-
const before2 = () => { console.log('before2()') }
5-
const before3 = () => { console.log('before3()') }
6-
const after1 = () => { console.log('after1()') }
7-
const after2 = () => { console.log('after2()') }
8-
const after3 = () => { console.log('after3()') }
9-
const post1 = () => { console.log('post1()') }
10-
const get1 = () => { console.log('get1()') }
11-
const get2 = () => { console.log('get2()') }
12-
const put3 = () => { console.log('put3()') }
3+
const before1 = () => {
4+
console.log('before1()')
5+
}
6+
const before2 = () => {
7+
console.log('before2()')
8+
}
9+
const before3 = () => {
10+
console.log('before3()')
11+
}
12+
const after1 = () => {
13+
console.log('after1()')
14+
}
15+
const after2 = () => {
16+
console.log('after2()')
17+
}
18+
const after3 = () => {
19+
console.log('after3()')
20+
}
21+
const post1 = () => {
22+
console.log('post1()')
23+
}
24+
const get1 = () => {
25+
console.log('get1()')
26+
}
27+
const get2 = () => {
28+
console.log('get2()')
29+
}
30+
const put3 = () => {
31+
console.log('put3()')
32+
}
1333

1434
const ExampleController = {
15-
beforeEach() { console.log('Call before each handler') },
35+
beforeEach() {
36+
console.log('Call before each handler')
37+
},
1638
afterEach() {},
1739
read() {},
1840
create() {},
1941
update() {},
20-
destroy () {},
42+
destroy() {},
2143
}
2244

2345
const BooksController = {
24-
beforeEach() { console.log('Call before each handler') },
46+
beforeEach() {
47+
console.log('Call before each handler')
48+
},
2549
afterEach() {},
2650
index() {},
2751
create() {},
2852
read() {},
2953
update() {},
3054
patch() {},
31-
destroy () {},
55+
destroy() {},
3256
}
3357

34-
const routes = createRest(root => {
58+
const routes = createRest((root) => {
3559
root.beforeEach(before1)
3660
root.afterEach(after1)
3761

3862
root.post('/', post1)
3963

40-
root.scope('demo', demoRoute => {
64+
root.scope('demo', (demoRoute) => {
4165
demoRoute.beforeEach(before2)
4266
demoRoute.afterEach(after2)
4367

4468
demoRoute.get('/', get1)
4569
demoRoute.get('/foo', get2)
4670

47-
demoRoute.scope('bar', barRoute => {
71+
demoRoute.scope('bar', (barRoute) => {
4872
barRoute.beforeEach(before3)
4973
barRoute.afterEach(after3)
5074

packages/createrest/examples/printers.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,13 @@ const put3 = function put3() {
8080
console.log('put3()')
8181
}
8282

83-
const routes = createRest(root => {
83+
const routes = createRest((root) => {
8484
root.beforeEach(before1)
8585
root.afterEach(after1)
8686

8787
root.post('/', post1)
8888

89-
root.scope('demo', demo => {
89+
root.scope('demo', (demo) => {
9090
demo.beforeEach(before2)
9191
demo.afterEach(after2)
9292

@@ -99,7 +99,7 @@ const routes = createRest(root => {
9999

100100
// bar.put('/', put3)
101101
// })
102-
demo.crud('bar', TestsController, {}, bar => {
102+
demo.crud('bar', TestsController, {}, (bar) => {
103103
bar.get('baz', get3)
104104
})
105105
})
@@ -114,7 +114,7 @@ const strf = (data, indent = ' ') => stringify(data, {
114114
original
115115
.replace(/\n+/mg, '')
116116
.replace(/\s+/mg, ' ')
117-
.replace(/^\w+\s+(\w+)\(\).*/mg, `$1()`)
117+
.replace(/^\w+\s+(\w+)\(\).*/mg, '$1()')
118118
)
119119
}
120120

@@ -125,9 +125,9 @@ const strf = (data, indent = ' ') => stringify(data, {
125125
console.log(strf(routes))
126126

127127
const flat = flattenRoutes(routes)
128-
Object.keys(flat).forEach(path => {
128+
Object.keys(flat).forEach((path) => {
129129
const mt = flat[path]
130-
Object.keys(mt).forEach(method => {
130+
Object.keys(mt).forEach((method) => {
131131
console.log(
132132
chalk.green(`${method} ${path}`),
133133
' >> ',

packages/createrest/examples/resources.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,22 +80,28 @@ const put3 = function put3() {
8080
console.log('put3()')
8181
}
8282

83-
const routes = createRest(root => {
83+
const routes = createRest((root) => {
8484
// root.beforeEach(before1)
8585
// root.afterEach(after1)
8686

87-
root.post('/', post1)
87+
// root.post('/', post1)
8888

89-
root.scope('demo', demo => {
89+
// root.scope('demo', (demo) => {
9090
// demo.crud('bar', TestsController, {}, bar => {
9191
// bar.get('baz', get3)
9292
// })
93-
demo.get('bar', get3)
94-
})
95-
root.scope('demo', demo => {
96-
demo.get('baz', get2)
93+
// demo.get('bar', get3)
94+
// })
95+
// root.scope('demo', (demo) => {
96+
// demo.get('baz', get2)
97+
// })
98+
root.resources('tests', TestsController, (tests) => {
99+
tests.get('/status', get1)
100+
101+
tests.scope(':testId', (testId) => {
102+
testId.get('description', get2)
103+
})
97104
})
98-
// root.resources('tests', TestsController)
99105
})
100106

101107
const strf = (data, indent = ' ') => stringify(data, {
@@ -106,7 +112,7 @@ const strf = (data, indent = ' ') => stringify(data, {
106112
original
107113
.replace(/\n+/mg, '')
108114
.replace(/\s+/mg, ' ')
109-
.replace(/^\w+\s+(\w+)\(\).*/mg, `$1()`)
115+
.replace(/^\w+\s+(\w+)\(\).*/mg, '$1()')
110116
)
111117
}
112118

@@ -117,9 +123,9 @@ const strf = (data, indent = ' ') => stringify(data, {
117123
console.log(strf(routes))
118124

119125
const flat = flattenRoutes(routes)
120-
Object.keys(flat).forEach(path => {
126+
Object.keys(flat).forEach((path) => {
121127
const mt = flat[path]
122-
Object.keys(mt).forEach(method => {
128+
Object.keys(mt).forEach((method) => {
123129
console.log(
124130
chalk.green(`${method} ${path}`),
125131
' >> ',

packages/createrest/lib/index.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ export class Maker {
180180
/**
181181
* Add scoped address, before/after handlers and simple handlers.<br/>
182182
* Before/After handlers is inherits from parent scope.<br/>
183-
* Scopes don't merging. Use only one scope for unique path.
183+
* Scopes with the same name will be merged
184184
*
185185
* @param {string} name Name of the scope
186186
* @param {function(scope: Maker): void} creator
@@ -589,6 +589,7 @@ export class Maker {
589589
* @param {string} name Name of the resources. Path created from. Example: `books`
590590
* @param {ResourcesController} controller Object with methods
591591
* @param {resourcesOptions} [options={}] Options for resources
592+
* @param {function(scope: Maker): void} [creator=null] Scoped creator function
592593
* @return {void}
593594
* @throws {Error} "Resources should be named"
594595
* @throws {Error} "You can't use 'except' and 'only' options at the same time"
@@ -605,7 +606,7 @@ export class Maker {
605606
* root.resources('users', UsersController)
606607
* })
607608
*/
608-
resources(name, controller, options = {}) {
609+
resources(name, controller, options = {}, creator = null) {
609610
/**
610611
* index : get /
611612
* create : post /
@@ -622,6 +623,13 @@ export class Maker {
622623
throw new TypeError('Controller should be object')
623624
}
624625

626+
if (typeof options === 'function') {
627+
/* eslint-disable no-param-reassign */
628+
creator = options
629+
options = {}
630+
/* eslint-enable no-param-reassign */
631+
}
632+
625633
if (options.only && options.except) {
626634
throw new Error('You can\'t use \'except\' and \'only\' options at the same time')
627635
}
@@ -678,6 +686,10 @@ export class Maker {
678686
member.delete('/', controller.destroy)
679687
}
680688
})
689+
690+
if (creator) {
691+
creator(scope)
692+
}
681693
})
682694
}
683695
}

packages/createrest/test/index.test.js

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,3 +656,139 @@ avaTest('Resources cannot use only and expect at the same time', (test) => {
656656
})
657657
}, /You can't use/, 'except and only not throws')
658658
})
659+
660+
avaTest('Resources with shallow creator with empty options', (test) => {
661+
test.deepEqual(
662+
createRest((root) => {
663+
root.resources('books', DefaultController, {}, (books) => {
664+
books.get('status', get)
665+
})
666+
}),
667+
make([], [], {}, {
668+
books: make(
669+
[DefaultController.beforeEach],
670+
[DefaultController.afterEach],
671+
{
672+
GET: [DefaultController.index], POST: [DefaultController.create],
673+
},
674+
{
675+
':bookId': make([], [], {
676+
GET: [DefaultController.read],
677+
PUT: [DefaultController.update],
678+
PATCH: [DefaultController.patch],
679+
DELETE: [DefaultController.destroy],
680+
}),
681+
status: make([], [], {
682+
GET: [get],
683+
}),
684+
}
685+
),
686+
})
687+
)
688+
})
689+
690+
avaTest('Resources with shallow creator instead of options', (test) => {
691+
test.deepEqual(
692+
createRest((root) => {
693+
root.resources('books', DefaultController, (books) => {
694+
books.get('status', get)
695+
})
696+
}),
697+
make([], [], {}, {
698+
books: make(
699+
[DefaultController.beforeEach],
700+
[DefaultController.afterEach],
701+
{
702+
GET: [DefaultController.index], POST: [DefaultController.create],
703+
},
704+
{
705+
':bookId': make([], [], {
706+
GET: [DefaultController.read],
707+
PUT: [DefaultController.update],
708+
PATCH: [DefaultController.patch],
709+
DELETE: [DefaultController.destroy],
710+
}),
711+
status: make([], [], {
712+
GET: [get],
713+
}),
714+
}
715+
),
716+
})
717+
)
718+
})
719+
720+
avaTest('Resources with scope:memberId creator', (test) => {
721+
test.deepEqual(
722+
createRest((root) => {
723+
root.resources('books', DefaultController, {}, (books) => {
724+
books.get('status', get)
725+
books.scope(':bookId', (bookId) => {
726+
bookId.get('details', get)
727+
})
728+
})
729+
}),
730+
make([], [], {}, {
731+
books: make(
732+
[DefaultController.beforeEach],
733+
[DefaultController.afterEach],
734+
{
735+
GET: [DefaultController.index], POST: [DefaultController.create],
736+
},
737+
{
738+
':bookId': make([], [],
739+
{
740+
GET: [DefaultController.read],
741+
PUT: [DefaultController.update],
742+
PATCH: [DefaultController.patch],
743+
DELETE: [DefaultController.destroy],
744+
},
745+
{
746+
details: make([], [], { GET: [get] }),
747+
}
748+
),
749+
status: make([], [], {
750+
GET: [get],
751+
}),
752+
}
753+
),
754+
})
755+
)
756+
})
757+
758+
avaTest('Resources with memberId creator with renamed memberId', (test) => {
759+
test.deepEqual(
760+
createRest((root) => {
761+
root.resources('books', DefaultController, { memberId: 'demoId' }, (books) => {
762+
books.get('status', get)
763+
books.scope(':demoId', (bookId) => {
764+
bookId.get('details', get)
765+
})
766+
})
767+
}),
768+
make([], [], {}, {
769+
books: make(
770+
[DefaultController.beforeEach],
771+
[DefaultController.afterEach],
772+
{
773+
GET: [DefaultController.index], POST: [DefaultController.create],
774+
},
775+
{
776+
':demoId': make([], [],
777+
{
778+
GET: [DefaultController.read],
779+
PUT: [DefaultController.update],
780+
PATCH: [DefaultController.patch],
781+
DELETE: [DefaultController.destroy],
782+
},
783+
{
784+
details: make([], [], { GET: [get] }),
785+
}
786+
),
787+
status: make([], [], {
788+
GET: [get],
789+
}),
790+
}
791+
),
792+
})
793+
)
794+
})

0 commit comments

Comments
 (0)