-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
156 lines (131 loc) · 3.63 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
* Axel Boberg © 2019
*/
const HttpError = require('./lib/error/http')
const objects = require('./lib/objects')
const url = require('./lib/url')
const resolvers = require('./lib/resolvers')
function defaults (target, definition) {
const mirror = Object.assign({}, definition)
return Object.assign(mirror, target)
}
function Router (routes) {
if (typeof routes !== 'object' || Array.isArray(routes)) {
throw new TypeError('Routes must be a non-array object')
}
let _routes = routes || {}
/**
* Allow the routes to be modified
* after the creation of the router
*/
this.routes = _routes
/**
* Make copy of resolvers in order
* to be able to add custom ones
* without modifying the original
* array
*/
let _resolvers = [].concat(resolvers)
/**
* Helper to populate a payload
* object for a path
*/
function createPayload (path, payload = {}) {
payload = objects.payload.ensure(payload)
const info = url.info(path)
// Assign the url info to the payload object
payload.path = info.path
payload.pathname = info.pathname
payload.query = Object.assign(payload.query || {}, info.query)
payload.searchPath = payload.path.concat([ payload.method ])
return payload
}
/**
* Resolve a path with payload
*/
this.resolve = function (path, req = {}) {
req = createPayload(path, req)
return resolve(_routes, req.searchPath, { resolvers: _resolvers, req: req })
}
/**
* Execute a route with a request object as payload
*/
this.execute = function (path, req = { method: 'GET' }, ...args) {
const endpoint = this.resolve(path, req)
return execute(endpoint, req, ...args)
}
/**
* Add a resolver to the front of the stack
*/
this.addResolverBefore = function (resolver) {
if (typeof resolver !== 'function') {
throw new TypeError('Resolver needs to be a function')
}
_resolvers.unshift(resolver)
}
/**
* Add a resolver to the back of the stack
*/
this.addResolverAfter = function (resolver) {
if (typeof resolver !== 'function') {
throw new TypeError('Resolver needs to be a function')
}
_resolvers.push(resolver)
}
}
/**
* Resolve a path in the object,
* will add any path parameters to opts.req.params
*
* @param { Object } obj
* @param { String } path The path to execute
* @param { Object? } opts
* @returns { Any? }
*//**
* Resolve a path in the object,
* will add any path parameters to opts.req.params
*
* @param { Object } obj
* @param { String } path The path to execute
* @param { Object? } opts
* @returns { Any? }
*/
function resolve (obj, path, opts = {}) {
opts = defaults(opts, {
resolvers: [],
delimiter: '/'
})
if (typeof path === 'string') path = path.split(opts.delimiter)
if (path.length === 0) return obj
let resolved
const _resolve = (...args) => {
path.shift()
const [ _obj, _path, _opts ] = args
resolved = resolve(_obj, _path || path, _opts || opts)
}
for (let resolver of opts.resolvers) {
resolver(obj, path, opts, _resolve)
if (resolved) break
}
return resolved || obj
}
/**
* Execute an endpoint
* @param { Function? } endpoint
* @param { Object? } req
* @param { Array<Any?>? } args Additional arguments to pass to the endpoint
*/
function execute (endpoint, req = {}, ...args) {
return new Promise (resolve => {
// 404
if (endpoint === null) {
throw new HttpError(404)
}
if (typeof endpoint !== 'function') {
throw new TypeError(`Endpoint is not a function`)
}
resolve(endpoint(req, ...args))
})
}
module.exports = Router
module.exports.HttpError = HttpError