1 | 'use strict'
|
2 |
|
3 | const path = require('path')
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | const _route = function(route) {
|
13 |
|
14 |
|
15 | route = Object.assign({}, route)
|
16 |
|
17 |
|
18 |
|
19 | if (route.name==null || route.name==='') throw new Error('Missing name property in route')
|
20 |
|
21 |
|
22 | if (route.path==null) throw new Error(`Missing path property in route '${ route.name }'`)
|
23 | if (route.handler==null) throw new Error(`Missing handler property in route '${ route.name }'`)
|
24 |
|
25 |
|
26 | if (path.isAbsolute(route.path)===true) throw new Error(`Path in route '${ route.name }' must be relative`)
|
27 |
|
28 |
|
29 | if (typeof route.handler==='string') route.handler = require(route.handler)
|
30 | if (typeof route.handler!=='function') throw new Error(`Handler in route '${ route.name }' is not a function nor a string`)
|
31 |
|
32 |
|
33 | if (route.opts==null) route.opts = {}
|
34 |
|
35 | return route
|
36 |
|
37 | }
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | const _path = function(filePath) {
|
46 |
|
47 |
|
48 | filePath = path.resolve(filePath)
|
49 |
|
50 | return filePath
|
51 |
|
52 | }
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | const _opts = function(opts = {}) {
|
61 |
|
62 |
|
63 | return {
|
64 | ignore: (Array.isArray(opts.ignore)===true ? opts.ignore : []),
|
65 | static: (Array.isArray(opts.static)===true ? opts.static : []),
|
66 | verbose: (opts.verbose===true ? true : false),
|
67 | open: (typeof opts.open==='string' || opts.open===true ? true : false),
|
68 | path: (typeof opts.open==='string' ? opts.open : null)
|
69 | }
|
70 |
|
71 | }
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | const _next = function(next) {
|
80 |
|
81 |
|
82 | if (typeof next!=='function') next = (err) => {
|
83 |
|
84 | if (err!=null) throw err
|
85 |
|
86 | }
|
87 |
|
88 | return next
|
89 |
|
90 | }
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | module.exports = {
|
96 | route: _route,
|
97 | path: _path,
|
98 | opts: _opts,
|
99 | next: _next
|
100 | } |
\ | No newline at end of file |