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

Commit dcddef7

Browse files
committed
Refactor examples
1 parent 22a105d commit dcddef7

6 files changed

Lines changed: 95 additions & 140 deletions

File tree

examples/all-in-use.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
const { createRest, flattenRoutes, printRoutes } = require('../dist')
2+
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()') }
13+
14+
const ExampleController = {
15+
beforeEach() { console.log('Call before each handler') },
16+
afterEach() {},
17+
read() {},
18+
create() {},
19+
update() {},
20+
destroy () {},
21+
}
22+
23+
const BooksController = {
24+
beforeEach() { console.log('Call before each handler') },
25+
afterEach() {},
26+
index() {},
27+
create() {},
28+
read() {},
29+
update() {},
30+
patch() {},
31+
destroy () {},
32+
}
33+
34+
const routes = createRest(root => {
35+
root.beforeEach(before1)
36+
root.afterEach(after1)
37+
38+
root.post('/', post1)
39+
40+
root.scope('demo', demoRoute => {
41+
demoRoute.beforeEach(before2)
42+
demoRoute.afterEach(after2)
43+
44+
demoRoute.get('/', get1)
45+
demoRoute.get('/foo', get2)
46+
47+
demoRoute.scope('bar', barRoute => {
48+
barRoute.beforeEach(before3)
49+
barRoute.afterEach(after3)
50+
51+
barRoute.put('/', put3)
52+
53+
barRoute.crud('example', ExampleController)
54+
})
55+
})
56+
57+
root.resources('books', BooksController)
58+
})
59+
60+
printRoutes(routes)

examples/old-full-example.js

Lines changed: 0 additions & 105 deletions
This file was deleted.
Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -80,25 +80,23 @@ const routes = createRest(root => {
8080
root.beforeEach(before1)
8181
root.afterEach(after1)
8282

83-
84-
85-
// root.post('/', post1)
83+
root.post('/', post1)
8684

8785
root.scope('demo', demo => {
88-
// demo.beforeEach(before2)
89-
// demo.afterEach(after2)
86+
demo.beforeEach(before2)
87+
demo.afterEach(after2)
9088

91-
// demo.get('/', get1)
92-
// demo.get('/foo', get2)
89+
demo.get('/', get1)
90+
demo.get('/foo', get2)
9391

94-
// demo.scope('bar', bar => {
95-
// bar.beforeEach(before3)
96-
// bar.afterEach(after3)
92+
demo.scope('bar', bar => {
93+
bar.beforeEach(before3)
94+
bar.afterEach(after3)
9795

98-
// bar.put('/', put3)
99-
// })
96+
bar.put('/', put3)
97+
})
10098
})
101-
root.crud('tests', TestsController)
99+
root.resources('tests', TestsController)
102100
})
103101

104102
const strf = (data, indent = ' ') => stringify(data, {
@@ -125,9 +123,12 @@ Object.keys(flat).forEach(path => {
125123
Object.keys(mt).forEach(method => {
126124
console.log(
127125
chalk.green(`${method} ${path}`),
128-
mt[method].map(fn => `${fn.name}()`).join(', ')
126+
' >> ',
127+
mt[method].map(fn => `${chalk.yellow(fn.name)}()`).join(', ')
129128
)
130129
})
131130
})
132131

132+
console.log('\n---\n')
133+
133134
printRoutes(routes)

examples/resource.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "REST routes constructor for express and koa",
55
"main": "dist/index.js",
66
"scripts": {
7-
"dev": "nodemon -w lib -w examples -x \"npm run build && node examples/test.js\"",
7+
"dev": "nodemon -w lib -w examples -x \"npm run build && node examples/printers.js\"",
88
"clean": "rimraf ./dist ./docs ./.nyc_output ./coverage",
99
"build": "babel -d ./dist ./lib",
1010
"test": "npm run test:lint && npm run test:code",

readme.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@ const ExampleController = {
4242
destroy () {},
4343
}
4444

45+
const BooksController = {
46+
beforeEach() { console.log('Call before each handler') },
47+
afterEach() {},
48+
index() {},
49+
create() {},
50+
read() {},
51+
update() {},
52+
patch() {},
53+
destroy () {},
54+
}
55+
4556
const routes = createRest(root => {
4657
root.beforeEach(before1)
4758
root.afterEach(after1)
@@ -64,6 +75,8 @@ const routes = createRest(root => {
6475
barRoute.crud('example', ExampleController)
6576
})
6677
})
78+
79+
root.resources('books', BooksController)
6780
})
6881

6982
module.exports = routes
@@ -119,4 +132,10 @@ GET /demo/bar/example/ -> before1(), before2(), before3(), beforeEach(), read(),
119132
POST /demo/bar/example/ -> before1(), before2(), before3(), beforeEach(), create(), afterEach(), after3(), after2(), after1()
120133
PUT /demo/bar/example/ -> before1(), before2(), before3(), beforeEach(), update(), afterEach(), after3(), after2(), after1()
121134
DELETE /demo/bar/example/ -> before1(), before2(), before3(), beforeEach(), destroy(), afterEach(), after3(), after2(), after1()
135+
GET /books/ -> before1(), beforeEach(), index(), afterEach(), after1()
136+
POST /books/ -> before1(), beforeEach(), create(), afterEach(), after1()
137+
GET /books/:bookId/ -> before1(), beforeEach(), read(), afterEach(), after1()
138+
PUT /books/:bookId/ -> before1(), beforeEach(), update(), afterEach(), after1()
139+
PATCH /books/:bookId/ -> before1(), beforeEach(), patch(), afterEach(), after1()
140+
DELETE /books/:bookId/ -> before1(), beforeEach(), destroy(), afterEach(), after1()
122141
```

0 commit comments

Comments
 (0)