-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
37 lines (33 loc) · 1.07 KB
/
index.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
const querystring = require('querystring')
const fmw = require('find-my-way')
function router (options) {
return function (...routes) {
const router = fmw(options)
routes.forEach(rt => router.on(...rt))
return (req, res) => router.lookup(req, res)
}
}
function enhancer (fn) {
return function (req, res, params, store) {
req.params = params
req.query = querystring.parse(new URL(req.url, `http://${req.headers.host}`).searchParams.toString())
return fn(req, res, store)
}
}
const get = (path, fn, store) => ['GET', path, enhancer(fn), store]
const put = (path, fn, store) => ['PUT', path, enhancer(fn), store]
const del = (path, fn, store) => ['DELETE', path, enhancer(fn), store]
const post = (path, fn, store) => ['POST', path, enhancer(fn), store]
const head = (path, fn, store) => ['HEAD', path, enhancer(fn), store]
const patch = (path, fn, store) => ['PATCH', path, enhancer(fn), store]
const options = (path, fn, store) => ['OPTIONS', path, enhancer(fn), store]
module.exports = {
router,
get,
put,
del,
post,
head,
patch,
options
}