This repository has been archived by the owner on Mar 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
route.js
143 lines (123 loc) · 2.99 KB
/
route.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
const Base = require('./base')
const middleware = require('./middleware')
module.exports = Route
/**
* Route implements an HTTP route behavior and logic
*
* @param {String} path
* @class Route
* @extend Base
* @constructor
*/
function Route (path) {
Base.call(this)
this.path = path
this.unregistered = false
}
Route.prototype = Object.create(Base.prototype)
/**
* Defines a host header field for the current route
*
* @param {String} host
* @return {Route}
* @method host
*/
Route.prototype.host = function (host) {
this.use(middleware.host(host))
return this
}
/**
* Overwrites the HTTP path for the current request, optionally
* using a custom path params.
*
* @param {String} path
* @param {Object} params
* @return {Route}
* @method toPath
*/
Route.prototype.toPath = function (path, params) {
this.use(middleware.toPath(path, params))
return this
}
/**
* Replies an incoming request with a custom HTTP code
* and optionally with additional headers and/or body.
*
* @param {Number} code
* @param {Object} headers
* @param {String/Buffer} body
* @return {Route}
* @method reply
*/
Route.prototype.reply = function (code, headers, body) {
this.use(middleware.reply(code, headers, body))
return this
}
/**
* Redirects and incoming request to a target URL.
*
* @param {String} url
* @return {Route}
* @method redirect
*/
Route.prototype.redirect = function (url) {
this.use(middleware.redirect(url))
return this
}
/**
* Unregister the current route, meaning it won't be used
* by the router in future incoming traffic.
*
* @method unregister
* @return {Route}
*/
Route.prototype.unregister = function () {
this.unregistered = true
return this
}
/**
* Attaches a middleware function to the incoming
* phase middleware for the current route.
*
* @param {Function} middleware
* @return {Route}
* @method use
* @alias useIncoming
*/
Base.prototype.use =
Base.prototype.useIncoming = function () {
this.useFor('global', arguments)
return this
}
/**
* Attaches a transformer function to the incoming
* phase for the current route, optinally defining a filter
* in order to determine if the transformer should be applied or not.
*
* @param {Function} transformer
* @param {Function} filter
* @return {Route}
* @method transformResponse
* @alias transformResponseBody
*/
Route.prototype.transformResponse =
Route.prototype.transformResponseBody = function (transformer, filter) {
this.use(middleware.responseBody(transformer, filter))
return this
}
/**
* Attaches a transformer function to the outgoing
* phase for the current route, optinally defining a filter
* in order to determine if the transformer should be applied or not.
*
* @param {Function} transformer
* @param {Function} filter
* @return {Route}
* @method transformRequest
* @alias transformRequestBody
*/
Route.prototype.transformRequest =
Route.prototype.transformRequestBody = function (transformer, filter) {
this.use(middleware.requestBody(transformer, filter))
return this
}