-
Notifications
You must be signed in to change notification settings - Fork 6
/
todos.js
53 lines (40 loc) · 1.22 KB
/
todos.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// NOTICE: THIS IS AN EXAMPLE SERVICE
import { Todo } from '../models'
import Service from '../../boilerplate/builders/service'
export const Todos = new Service({
model: Todo,
endpoint: '/todos',
// NOTE: hooks allow you to do things over default or custom methods
hooks: {
// arrow function
'before delete': async () => console.log( 'OH NO!!!!11' ),
// async method
async 'before update write' ( req ) {
if ( ! req.me ) {
throw new Error()
}
},
// using next callback
'after create' ( req, res, next ) {
console.log( 'created a new TODO!' )
next()
},
// calling custom hook
// check http://localhost:8888/todos/
async 'after sunbath' () {
console.log( 'see? I appear here because of the first custom above' )
},
},
// NOTE: custom object allows you to replace default methods or insert new custom ones
custom: {
// check http://localhost:8888/todos/
async 'GET /' () {
const { hook } = this
await hook( 'after sunbath' )
return { json: 'response' }
},
// check http://localhost:8888/todos/arrow
'GET /arrow': async () => 'just responded from an arrow method!! YOLO',
}
})
export default Todos