-
Notifications
You must be signed in to change notification settings - Fork 0
/
route.js
152 lines (135 loc) · 2.91 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
144
145
146
147
148
149
150
151
152
"use strict";
/**
* Route class to create a single route instance and the have the ability to
* modify the instance to alter some data
*/
class Route {
/**
* Create a new route instance
*
* @param {string} app application namespace
* @param {string} namespace route namespace
* @param {string} path route path
* @param {string} handler route handler in {Controller}.{Mehtod} format
*/
constructor(app, namespace, path, handler) {
this._app = app;
this._namespace = namespace;
this._path = path;
this._handler = handler;
this._middlewares = [];
}
/**
* Update route namespace
*
* @param {string} namespace
*
* @returns void
*/
updateNamespace(namespace) {
this._namespace = namespace;
}
/**
* Update route path
*
* @param {string} path
*
* @returns void
*/
updatePath(path) {
this._path = path;
}
/**
* Update route middlewares. This method will not reset them rater add
* the new ones with the old ones as well as the group middlewares
*
* @param {string} middlewares
*
* @returns void
*/
updateMiddlewares(middlewares) {
this._middlewares = this._middlewares.concat(middlewares);
}
/**
* Update route prefix
*
* @param {string} prefix
*
* @returns void
*/
updateHandler(handler) {
this._handler = handler;
}
/**
* get route path
*
* @returns {string} route path
*/
get path() {
const path = `${this._app}/${this._namespace}/${this._path}`;
return path.replace(new RegExp(/\/+/, "g"), "/");
}
/**
* get route handler
*
* @returns {String} handler
*/
get handler() {
return this._handler;
}
/**
* get route middlewares
*
* @returns {Array} middlewares
*/
get middlewares() {
return this._middlewares;
}
/**
* Append middleware to the list
*
* @param {Array} middleware
*
* @returns {Route} route
*/
middleware(middleware) {
if (middleware) {
const middlewares = Array.isArray(middleware) ? middleware : [middleware];
this._middlewares = this._middlewares.concat(middlewares);
}
return this;
}
/**
* Update all the properties of this Route
*
* @param {*} props
*
* @returns {Route} route
*/
updateProps(props) {
if (props.namespace) {
this.updateNamespace(props.namespace);
}
if (Array.isArray(props.middlewares) && props.middlewares.length) {
this.updateMiddlewares(props.middlewares);
}
return this;
}
/**
* Update properties from the paramters of a route group
*
* @param {RouteGroup} group
*
* @returns {Route} route
*/
updateGroupProps(group) {
if (group.namespace) {
this.updateNamespace(group.namespace);
}
if (Array.isArray(group.middlewares) && group.middlewares.length) {
this.updateMiddlewares(group.middlewares);
}
return this;
}
}
module.exports = Route;