-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.js
44 lines (33 loc) · 887 Bytes
/
user.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
import Datastore from 'nedb'
import { error } from 'quiver/error'
import {
async, promisifyMethods
} from 'quiver/promise'
import {
argsBuilderFilter,
simpleHandlerBuilder
} from 'quiver/component'
import {
databaseMiddleware
} from './database'
export const userHandler = simpleHandlerBuilder(
config => {
const { db } = config
return async(function*(args) {
const { username } = args
const user = yield db.findOne({ username })
if(!user) throw error(404, 'user not found')
return user
})
}, 'void', 'json')
.middleware(databaseMiddleware)
export const getUserFilter = argsBuilderFilter(
config => {
const { getUser } = config
return async(function*(args) {
if(args.user) return
const { username } = args
args.user = yield getUser({ username })
})
})
.inputHandler(userHandler, 'getUser')