This repository has been archived by the owner on Jan 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathosprey-router.js
153 lines (130 loc) · 3.55 KB
/
osprey-router.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
const ramlPathMatch = require('raml-path-match')
const Engine = require('router/engine')
const methods = require('methods')
const flatten = require('array-flatten').flatten
const slice = Array.prototype.slice
/**
* Expose `router`.
*/
module.exports = router
/**
* Initialize router instance.
*
* @param {Object} options
* @return {Function}
*/
function router (options) {
return new Router(options)
}
/**
* Constructs a router instance.
*
* @param {Object} options Following options are supported:
* ramlUriParameters. Array.<webapi-parser.Parameter>
* @return {Engine}
*/
function Router (options) {
const router = Engine.call(this, options)
// Construct with default URI parameters.
router.ramlUriParameters = options
? options.ramlUriParameters
: []
return router
}
/**
* Inherits from the router engine.
*/
Router.prototype = Object.create(Engine.prototype)
/**
* Creates a `raml-path-match` compatible `.use`.
*
* When uri parameters schema is passed as a second parameter,
* it must be an array of `webapi-parser.Parameter`.
*/
Router.prototype.use = function use () {
let offset = 0
let path = '/'
let uriParams
if (!isMiddleware(arguments[0])) {
path = arguments[0]
offset = 1
if (!isMiddleware(arguments[1])) {
uriParams = arguments[1]
offset = 2
}
}
const callbacks = flatten(slice.call(arguments, offset))
uriParams = extendParams(this.ramlUriParameters, uriParams)
const match = ramlPathMatch(path, uriParams, {
sensitive: this.caseSensitive,
strict: this.strict,
end: false
})
this.ramlUriParameters = uriParams
return Engine.prototype.use.call(this, path, match, callbacks)
}
/**
* Creates a `raml-path-match` compatible route.
*
* @param {String} path
* @param {Array.<webapi-parser.Parameter>} uriParams
*/
Router.prototype.route = function route (path, uriParams) {
uriParams = extendParams(this.ramlUriParameters, uriParams)
const match = ramlPathMatch(path, uriParams, {
sensitive: this.caseSensitive,
strict: this.strict,
end: true
})
this.ramlUriParameters = uriParams
return Engine.prototype.route.call(this, path, match)
}
//
/**
* Create Router#VERB functions.
*
* Callbacks' params are as follows:
* @param {String} path
* @param {Array.<webapi-parser.Parameter>} uriParams
*/
methods.concat('all').forEach(function (methodName) {
Router.prototype[methodName] = function (path, uriParams) {
const hasUriParams = !isMiddleware(uriParams)
const route = this.route(path, hasUriParams ? uriParams : null)
route[methodName].apply(route, slice.call(arguments, hasUriParams ? 2 : 1))
return this
}
})
/**
* Check if a value is possible middleware.
*
* @param {*} value
* @return {Boolean}
*/
function isMiddleware (value) {
const isFunction = typeof value === 'function'
const firstElementIsFunction = (
Array.isArray(value) && typeof value[0] === 'function')
return isFunction || firstElementIsFunction
}
/**
* Extends target list of Parameters with a source one.
* Parameters from source override parameters from target with
* the same IDs.
*
* @param {Array.<webapi-parser.Parameter>} target
* @param {Array.<webapi-parser.Parameter>} source
* @return {Boolean}
*/
function extendParams (target, source) {
target = target || []
source = source || []
const params = [...source]
const sourceIds = source.map(p => p.id)
target.forEach(param => {
if (!sourceIds.includes(param.id)) {
params.push(param)
}
})
return params
}