vaderjs 1.2.3 → 1.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/readme.md +17 -14
- package/vader.js +16 -11
- package/vaderRouter.js +178 -437
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -65,24 +65,27 @@ or
|
|
|
65
65
|
### Declarative Routing
|
|
66
66
|
|
|
67
67
|
```javascript
|
|
68
|
-
import
|
|
69
|
-
import {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
app.use('/') // use the route
|
|
73
|
-
|
|
74
|
-
app.get('/', async (req, res) => {
|
|
75
|
-
res.render('#app', await MyComponent.render())
|
|
76
|
-
})
|
|
68
|
+
import VaderRouter from "../dist/vader/vaderRouter.js";
|
|
69
|
+
import { Mycomponent} from "../src/pages/Home.js";
|
|
70
|
+
|
|
71
|
+
const app = new VaderRouter('/');
|
|
77
72
|
|
|
78
|
-
app.
|
|
79
|
-
res.
|
|
73
|
+
app.get("/", async (req, res)=>{
|
|
74
|
+
res.send('#root', await new Home().render())
|
|
80
75
|
})
|
|
81
|
-
app.get('/:
|
|
82
|
-
|
|
76
|
+
app.get('/docs/:page/*', async (req, res)=>{
|
|
77
|
+
// page and asterisk route use req.params for params and req.params[0] to get the asterisk
|
|
78
|
+
// you can get queries from the url using req.query!
|
|
83
79
|
})
|
|
80
|
+
const middleware = (req, res)=>{
|
|
81
|
+
req.time = Date.now()
|
|
82
|
+
}
|
|
83
|
+
app.use(middleware) // use middlewares
|
|
84
84
|
|
|
85
|
-
app.
|
|
85
|
+
app.listen(3000, ()=>{
|
|
86
|
+
console.log('listening on port 3000')
|
|
87
|
+
})
|
|
88
|
+
|
|
86
89
|
```
|
|
87
90
|
|
|
88
91
|
|
package/vader.js
CHANGED
|
@@ -17,13 +17,16 @@ function markdown(content) {
|
|
|
17
17
|
let link = line.match(/\[(.*?)\]\((.*?)\)/g);
|
|
18
18
|
let ul = line.match(/^\-\s/);
|
|
19
19
|
let ol = line.match(/^\d\.\s/);
|
|
20
|
+
let li = line.match(/^\s/);
|
|
20
21
|
let hr = line.match(/^\-\-\-\s/);
|
|
21
22
|
let blockquote = line.match(/^\>\s/);
|
|
22
23
|
let image = line.match(/\!\[(.*?)\]\((.*?)\)/g);
|
|
23
24
|
|
|
25
|
+
|
|
24
26
|
let codeBlock = line.match(/\`\`\`/g);
|
|
25
27
|
let codeBlockEnd = line.match(/\`\`\`/g);
|
|
26
28
|
|
|
29
|
+
|
|
27
30
|
if (heading) {
|
|
28
31
|
// @ts-ignore
|
|
29
32
|
let headingLevel = heading[0].match(/#/g).length;
|
|
@@ -81,11 +84,15 @@ function markdown(content) {
|
|
|
81
84
|
});
|
|
82
85
|
}
|
|
83
86
|
if(codeBlock){
|
|
84
|
-
line = line.replace(codeBlock[0], `<
|
|
87
|
+
line = line.replace(codeBlock[0], `<code>`);
|
|
88
|
+
|
|
85
89
|
}
|
|
86
90
|
if(codeBlockEnd){
|
|
87
|
-
line = line.replace(codeBlockEnd[0], `</code
|
|
91
|
+
line = line.replace(codeBlockEnd[0], `</code>`);
|
|
92
|
+
// remove spaces
|
|
93
|
+
line = line.replace(/^\s+/g, '');
|
|
88
94
|
}
|
|
95
|
+
|
|
89
96
|
|
|
90
97
|
|
|
91
98
|
result += `${line}\n`;
|
|
@@ -826,8 +833,9 @@ export class Component {
|
|
|
826
833
|
"You should wrap your html in a body tag, vader may not grab all html!"
|
|
827
834
|
);
|
|
828
835
|
}
|
|
829
|
-
|
|
830
|
-
|
|
836
|
+
|
|
837
|
+
const dom = new DOMParser().parseFromString(result, "text/html");
|
|
838
|
+
const elements = dom.documentElement.querySelectorAll("*");
|
|
831
839
|
|
|
832
840
|
elements.forEach((element) => {
|
|
833
841
|
switch (element.nodeName) {
|
|
@@ -861,11 +869,8 @@ export class Component {
|
|
|
861
869
|
let prevurl = element.getAttribute("src");
|
|
862
870
|
element.setAttribute("aria-hidden", "true");
|
|
863
871
|
element.setAttribute("hidden", "true");
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
window.location.pathname +
|
|
867
|
-
"/public/" +
|
|
868
|
-
element.getAttribute("src");
|
|
872
|
+
// if window.lcoation.pathname includes a html file remove it and only use the path
|
|
873
|
+
let url = window.location.origin + window.location.pathname.replace(/\/[^\/]*$/, '') + '/public/' + element.getAttribute("src");
|
|
869
874
|
let image = new Image();
|
|
870
875
|
image.src = url;
|
|
871
876
|
image.onerror = () => {
|
|
@@ -891,7 +896,7 @@ export class Component {
|
|
|
891
896
|
dom[element.getAttribute("ref")] = element;
|
|
892
897
|
}
|
|
893
898
|
if(element.nodeName === "MARKDOWN"){
|
|
894
|
-
element.innerHTML = markdown(element.innerHTML.trim())
|
|
899
|
+
element.innerHTML = markdown(element.innerHTML.replace(/\\n/g, '\n').trim())
|
|
895
900
|
}
|
|
896
901
|
|
|
897
902
|
if (element.hasAttribute("class")) {
|
|
@@ -1119,4 +1124,4 @@ export const include = async (path) => {
|
|
|
1119
1124
|
});
|
|
1120
1125
|
};
|
|
1121
1126
|
|
|
1122
|
-
export default Vader
|
|
1127
|
+
export default Vader
|
package/vaderRouter.js
CHANGED
|
@@ -1,477 +1,218 @@
|
|
|
1
|
-
// @ts-ignore
|
|
2
|
-
window.$URL_PARAMS = {};
|
|
3
|
-
// @ts-ignore
|
|
4
|
-
window.$URL_QUERY = {};
|
|
5
1
|
|
|
6
2
|
/**
|
|
7
|
-
* @
|
|
8
|
-
* @
|
|
9
|
-
* @
|
|
10
|
-
* @
|
|
3
|
+
* @class VaderRouter
|
|
4
|
+
* @description - creates an instance of Vader Express Router
|
|
5
|
+
* @param {String} path
|
|
6
|
+
* @param {Function} handler
|
|
7
|
+
* @param {object} req request object
|
|
8
|
+
* @param {object} res response object
|
|
9
|
+
* @returns {Object} Express
|
|
10
|
+
*
|
|
11
11
|
*/
|
|
12
|
-
class VaderRouter
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Object to store route information.
|
|
20
|
-
* @type {Object}
|
|
21
|
-
*/
|
|
22
|
-
this.routes = {};
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* The current URL being navigated.
|
|
26
|
-
* @type {string}
|
|
27
|
-
*/
|
|
28
|
-
this.currentUrl = "";
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Listener function for hash change events.
|
|
32
|
-
* @type {Function}
|
|
33
|
-
*/
|
|
34
|
-
//@ts-ignore
|
|
35
|
-
this.hashChangeListener = null;
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Error handlers for different error types.
|
|
39
|
-
* @type {Object}
|
|
40
|
-
*/
|
|
41
|
-
this.errorHandlers = {};
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Flag indicating if custom error handling is enabled.
|
|
45
|
-
* @type {boolean}
|
|
46
|
-
*/
|
|
47
|
-
//@ts-ignore
|
|
48
|
-
this.customerror = null;
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Flag indicating if the router is currently handling a route.
|
|
52
|
-
* @type {boolean}
|
|
53
|
-
*/
|
|
54
|
-
this.hooked = false;
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* The starting URL for the router.
|
|
58
|
-
* @type {string}
|
|
59
|
-
*/
|
|
60
|
-
this.starturl = starturl;
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Array to store stored routes.
|
|
64
|
-
* @type {string[]}
|
|
65
|
-
*/
|
|
66
|
-
this.storedroutes = [];
|
|
12
|
+
class VaderRouter{
|
|
13
|
+
constructor() {
|
|
14
|
+
this.routes = [];
|
|
15
|
+
this.middlewares = [];
|
|
16
|
+
this.errorMiddlewares = [];
|
|
17
|
+
this.listeners = [];
|
|
67
18
|
}
|
|
68
19
|
|
|
69
20
|
/**
|
|
70
|
-
*
|
|
21
|
+
* @method get
|
|
22
|
+
* @param {String} path
|
|
23
|
+
* @param {Function} handler
|
|
24
|
+
* @description This method is used to register a get route
|
|
25
|
+
* @returns {void}
|
|
26
|
+
* @memberof Express
|
|
71
27
|
*/
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
window.addEventListener("hashchange", () => {
|
|
79
|
-
this.handleRoute("POST");
|
|
28
|
+
get(path, handler) {
|
|
29
|
+
|
|
30
|
+
this.routes.push({
|
|
31
|
+
path,
|
|
32
|
+
handler,
|
|
33
|
+
method: 'get',
|
|
80
34
|
});
|
|
81
35
|
}
|
|
82
|
-
|
|
83
36
|
/**
|
|
84
|
-
* @
|
|
85
|
-
* @
|
|
86
|
-
* @param {
|
|
87
|
-
* @returns {void}
|
|
88
|
-
* @memberof VaderRouter
|
|
89
|
-
* @description Handles errors for the router.
|
|
90
|
-
* @example
|
|
91
|
-
* router.handleErrors('404', (err) => {
|
|
92
|
-
* // do something with err
|
|
93
|
-
* });
|
|
37
|
+
* @method use
|
|
38
|
+
* @description This method allows you to use middlewares
|
|
39
|
+
* @param {Function} middleware
|
|
94
40
|
*/
|
|
95
|
-
handleErrors(type, callback) {
|
|
96
|
-
this.errorHandlers[type] = callback;
|
|
97
|
-
this.customerror = true;
|
|
98
|
-
}
|
|
99
|
-
handleRoute(method) {
|
|
100
|
-
|
|
101
|
-
let route = window.location.hash.substring(1);
|
|
102
|
-
// @ts-ignore
|
|
103
|
-
window.$CURRENT_URL = route;
|
|
104
41
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
route = route.split("?")[0];
|
|
108
|
-
}
|
|
109
|
-
route = route.split("/")[0] + "/" + route.split("/")[1];
|
|
110
|
-
if (this.routes[route]) {
|
|
111
|
-
|
|
112
|
-
this.storedroutes.push(route);
|
|
113
|
-
const req = {
|
|
114
|
-
// @ts-ignore
|
|
115
|
-
params: $URL_PARAMS ? $URL_PARAMS : {},
|
|
116
|
-
// @ts-ignore
|
|
117
|
-
query: $URL_QUERY ? $URL_QUERY : {},
|
|
118
|
-
url: route,
|
|
119
|
-
method: method ? method : "GET",
|
|
120
|
-
};
|
|
121
|
-
const res = {
|
|
122
|
-
return: function (data) {
|
|
123
|
-
this.hooked = false;
|
|
124
|
-
},
|
|
125
|
-
render: function (selector, data) {
|
|
126
|
-
document.querySelector(selector).innerHTML = data;
|
|
127
|
-
},
|
|
128
|
-
};
|
|
129
|
-
if(typeof this.routes[route] === 'function'){
|
|
130
|
-
this.routes[route](req, res);
|
|
131
|
-
}
|
|
132
|
-
} else {
|
|
133
|
-
if (this.customerror) {
|
|
134
|
-
//@ts-ignore
|
|
135
|
-
this.handleError("404", route);
|
|
136
|
-
console.error("404: Route not found");
|
|
137
|
-
} else {
|
|
138
|
-
console.error("404: Route not found");
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
}
|
|
42
|
+
use(/* path, */ middleware) {
|
|
43
|
+
this.middlewares.push(middleware);
|
|
143
44
|
}
|
|
45
|
+
|
|
144
46
|
/**
|
|
145
|
-
*
|
|
146
|
-
* @param {
|
|
147
|
-
* @param {
|
|
47
|
+
* @method listen
|
|
48
|
+
* @param {String} port - unique id for the listener
|
|
49
|
+
* @param {Function} callback - callback function
|
|
50
|
+
* @description This method is used to start listening to the routes
|
|
148
51
|
* @returns {void}
|
|
149
|
-
*
|
|
150
|
-
* @description used by start() to handle errors.
|
|
52
|
+
*
|
|
151
53
|
*/
|
|
152
54
|
|
|
153
|
-
|
|
154
|
-
if
|
|
155
|
-
|
|
156
|
-
}
|
|
157
|
-
|
|
55
|
+
listen(port, callback) {
|
|
56
|
+
if(!port){
|
|
57
|
+
port = Math.random().toString(36).substring(7);
|
|
58
|
+
}
|
|
59
|
+
this.listeners.push(port);
|
|
60
|
+
callback();
|
|
61
|
+
if (this.listeners.length === 1) {
|
|
62
|
+
this.handleRoute(window.location.hash);
|
|
63
|
+
}else{
|
|
64
|
+
this.listeners.pop();
|
|
65
|
+
}
|
|
66
|
+
if (callback) {
|
|
67
|
+
callback();
|
|
68
|
+
}
|
|
69
|
+
window.onhashchange = () => {
|
|
70
|
+
this.handleRoute(window.location.hash);
|
|
158
71
|
}
|
|
159
72
|
}
|
|
160
73
|
/**
|
|
161
|
-
* @
|
|
162
|
-
* @
|
|
163
|
-
* @param {
|
|
164
|
-
* @
|
|
165
|
-
* @
|
|
166
|
-
* @
|
|
74
|
+
* @method extractParams
|
|
75
|
+
* @description This method is used to extract parameters from the route path
|
|
76
|
+
* @param {*} routePath
|
|
77
|
+
* @param {*} hash
|
|
78
|
+
* @returns {Object} params
|
|
79
|
+
* @memberof Express
|
|
167
80
|
*/
|
|
168
|
-
get(path, callback) {
|
|
169
|
-
const paramNames = [];
|
|
170
|
-
const queryNames = [];
|
|
171
|
-
const parsedPath = path
|
|
172
|
-
.split("/")
|
|
173
|
-
.map((part) => {
|
|
174
|
-
if (part.startsWith(":")) {
|
|
175
|
-
paramNames.push(part.substring(1));
|
|
176
|
-
return "([^/]+)";
|
|
177
|
-
}
|
|
178
|
-
if (part.startsWith("*")) {
|
|
179
|
-
paramNames.push(part.substring(1));
|
|
180
|
-
return "(.*)";
|
|
181
|
-
}
|
|
182
|
-
if (part.startsWith("?")) {
|
|
183
|
-
queryNames.push(part.substring(1));
|
|
184
|
-
return "([^/]+)";
|
|
185
|
-
}
|
|
186
|
-
return part;
|
|
187
|
-
})
|
|
188
|
-
.join("/");
|
|
189
|
-
const regex = new RegExp("^" + parsedPath + "(\\?(.*))?$");
|
|
190
|
-
|
|
191
|
-
if (window.location.hash.substring(1).match(regex)) {
|
|
192
|
-
this.storedroutes.push(window.location.hash.substring(1));
|
|
193
|
-
const matches = window.location.hash.substring(1).match(regex);
|
|
194
|
-
const params = {};
|
|
195
81
|
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
82
|
+
extractParams(routePath, hash) {
|
|
83
|
+
const routeParts = routePath.split('/');
|
|
84
|
+
const hashParts = hash.split('/');
|
|
85
|
+
const params = {};
|
|
86
|
+
routeParts.forEach((part, index) => {
|
|
87
|
+
if (part.startsWith(':')) {
|
|
88
|
+
const paramName = part.slice(1);
|
|
89
|
+
params[paramName] = hashParts[index];
|
|
90
|
+
}else if(part.startsWith('*')){
|
|
91
|
+
// remove queries from this par
|
|
92
|
+
params[0] = hashParts.slice(index).join('/').split('?')[0];
|
|
199
93
|
}
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
window.location.hash.substring(1).split("?")[1]
|
|
203
|
-
) {
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
return false;
|
|
207
|
-
}
|
|
208
|
-
const query = {};
|
|
209
|
-
|
|
210
|
-
const queryString = window.location.hash.substring(1).split("?")[1];
|
|
211
|
-
if (queryString) {
|
|
212
|
-
const queryParts = queryString.split("&");
|
|
213
|
-
for (let i = 0; i < queryParts.length; i++) {
|
|
214
|
-
const queryParam = queryParts[i].split("=");
|
|
215
|
-
query[queryParam[0]] = queryParam[1];
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
/**
|
|
219
|
-
* @alias req
|
|
220
|
-
* @type {Object}
|
|
221
|
-
* @property {Object} params - The params object.
|
|
222
|
-
* @returns {Object} current url params
|
|
223
|
-
*/
|
|
224
|
-
|
|
225
|
-
const req = {
|
|
226
|
-
params: params,
|
|
227
|
-
query: query,
|
|
228
|
-
url: window.location.hash.substring(1),
|
|
229
|
-
method: "GET",
|
|
230
|
-
};
|
|
231
|
-
// @ts-ignore
|
|
232
|
-
window.$URL_PARAMS = params;
|
|
233
|
-
// @ts-ignore
|
|
234
|
-
window.$URL_QUERY = query;
|
|
235
|
-
// @ts-ignore
|
|
236
|
-
window.$CURRENT_URL = window.location.hash.substring(1);
|
|
237
|
-
/**
|
|
238
|
-
* @alias render
|
|
239
|
-
* @param {*} selector
|
|
240
|
-
* @param {*} data
|
|
241
|
-
* @returns {void}
|
|
242
|
-
* @memberof VaderRouter
|
|
243
|
-
* @description Allows you to perform actions when the currentRoute changes.
|
|
244
|
-
*/
|
|
245
|
-
const res = {
|
|
246
|
-
return: function (data) {
|
|
247
|
-
this.hooked = false;
|
|
248
|
-
},
|
|
249
|
-
render: function (selector, data) {
|
|
250
|
-
document.querySelector(selector).innerHTML = data;
|
|
251
|
-
},
|
|
252
|
-
};
|
|
253
|
-
|
|
254
|
-
callback(req, res);
|
|
255
|
-
// @ts-ignore
|
|
256
|
-
return true;
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
this.hooked = false;
|
|
260
|
-
return false;
|
|
94
|
+
});
|
|
95
|
+
return params;
|
|
261
96
|
}
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
97
|
+
extractQueryParams(hash){
|
|
98
|
+
|
|
99
|
+
const queryParams = hash.split('?')[1];
|
|
100
|
+
if(!queryParams){
|
|
101
|
+
return {};
|
|
266
102
|
}
|
|
103
|
+
const params = {};
|
|
104
|
+
queryParams.split('&').forEach((param)=>{
|
|
105
|
+
const [key, value] = param.split('=');
|
|
106
|
+
params[key] = value;
|
|
107
|
+
})
|
|
108
|
+
return params;
|
|
267
109
|
}
|
|
110
|
+
|
|
268
111
|
/**
|
|
269
|
-
* @
|
|
270
|
-
* @param {String}
|
|
271
|
-
* @
|
|
272
|
-
* @returns {void}
|
|
273
|
-
* @memberof VaderRouter
|
|
274
|
-
* @description Allows you to set routes to be used throughout your spa.
|
|
112
|
+
* @method handleRoute
|
|
113
|
+
* @param {String} hash
|
|
114
|
+
* @description This method is used to handle the route
|
|
275
115
|
*/
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
// Get params
|
|
286
|
-
const match = window.location.hash.substring(1).match(regex);
|
|
287
|
-
|
|
288
|
-
if (match) {
|
|
289
|
-
this.storedroutes.push(window.location.hash.substring(1));
|
|
290
|
-
const matches = match.slice(1); // Extract matched groups
|
|
291
|
-
|
|
292
|
-
// Extract named params from the pattern
|
|
293
|
-
const paramNames = pattern.match(/:[^/]+/g) || [];
|
|
294
|
-
for (let i = 0; i < paramNames.length; i++) {
|
|
295
|
-
const paramName = paramNames[i].substring(1); // Remove the leading ":"
|
|
296
|
-
params[paramName] = matches[i];
|
|
116
|
+
|
|
117
|
+
handleRoute(hash) {
|
|
118
|
+
hash = hash.slice(1);
|
|
119
|
+
let status = 200;
|
|
120
|
+
let route = this.routes.find((route) => {
|
|
121
|
+
|
|
122
|
+
if (route.path === hash) {
|
|
123
|
+
return true;
|
|
297
124
|
}
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
const queryParam = queryParts[i].split("=");
|
|
306
|
-
query[queryParam[0]] = queryParam[1];
|
|
307
|
-
}
|
|
125
|
+
const routePathParts = route.path.split('/');
|
|
126
|
+
const hashParts = hash.split('/');
|
|
127
|
+
// if asterisk is present in route path then it will be the last part
|
|
128
|
+
if(routePathParts[routePathParts.length-1].startsWith('*')){
|
|
129
|
+
return true;
|
|
130
|
+
}else if (routePathParts.length !== hashParts.length) {
|
|
131
|
+
return false;
|
|
308
132
|
}
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
window.$URL_QUERY = query;
|
|
314
|
-
//@ts-ignore
|
|
315
|
-
if (callback) {
|
|
316
|
-
this.routes[pattern] = callback;
|
|
317
|
-
} else {
|
|
318
|
-
this.routes[pattern] = true;
|
|
319
|
-
this.storedroutes.push(window.location.hash.substring(1));
|
|
320
|
-
}
|
|
321
|
-
}
|
|
322
|
-
|
|
133
|
+
|
|
134
|
+
const params = this.extractParams( route.path, hash);
|
|
135
|
+
return Object.keys(params).length > 0;
|
|
136
|
+
});
|
|
323
137
|
|
|
138
|
+
if (!route) {
|
|
139
|
+
route = this.routes.find((route) => {
|
|
140
|
+
return route.path === '/404';
|
|
141
|
+
});
|
|
324
142
|
|
|
325
|
-
|
|
326
|
-
// await dom to be done make sure no new elements are added
|
|
327
|
-
if (
|
|
328
|
-
document.readyState === "complete" ||
|
|
329
|
-
// @ts-ignore
|
|
330
|
-
document.readyState === "loaded" ||
|
|
331
|
-
document.readyState === "interactive"
|
|
332
|
-
) {
|
|
333
|
-
callback();
|
|
143
|
+
status = 404;
|
|
334
144
|
}
|
|
335
|
-
}
|
|
336
145
|
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
? window.location.hash.split("#")[1]
|
|
372
|
-
: window.location.hash;
|
|
373
|
-
|
|
374
|
-
let basePath = "";
|
|
375
|
-
if (hash.length > 1) {
|
|
376
|
-
basePath = hash.split("/")[0] + "/" + hash.split("/")[1];
|
|
377
|
-
} else {
|
|
378
|
-
basePath = hash[0];
|
|
379
|
-
}
|
|
380
|
-
const route = basePath;
|
|
381
|
-
this.currentUrl = route;
|
|
382
|
-
// @ts-ignore
|
|
383
|
-
window.$CURRENT_URL = route;
|
|
384
|
-
// @ts-ignore
|
|
385
|
-
window.$URL_PARAMS = {};
|
|
386
|
-
if (
|
|
387
|
-
window.location.hash.substring(1).match(regex) &&
|
|
388
|
-
// @ts-ignore
|
|
389
|
-
this.routes[window.$CURRENT_URL]
|
|
390
|
-
) {
|
|
391
|
-
this.storedroutes.push(window.location.hash.substring(1));
|
|
392
|
-
const matches = window.location.hash.substring(1).match(regex);
|
|
393
|
-
const params = {};
|
|
394
|
-
|
|
395
|
-
for (let i = 0; i < paramNames.length; i++) {
|
|
396
|
-
//@ts-ignore
|
|
397
|
-
params[paramNames[i]] = matches[i + 1];
|
|
146
|
+
|
|
147
|
+
const queryParams = this.extractQueryParams(hash);
|
|
148
|
+
const params = route && route.path ? this.extractParams(route.path, hash) : {};
|
|
149
|
+
const req = {
|
|
150
|
+
headers: {},
|
|
151
|
+
params: params,
|
|
152
|
+
query: queryParams,
|
|
153
|
+
path: hash,
|
|
154
|
+
method: route ? route.method : 'get',
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
// @ts-ignore
|
|
158
|
+
window.$CURRENT_URL = req.path
|
|
159
|
+
|
|
160
|
+
// @ts-ignore
|
|
161
|
+
window.$FULL_URL = window.location.href.replace('#', '')
|
|
162
|
+
|
|
163
|
+
const res = {
|
|
164
|
+
status: status,
|
|
165
|
+
/**
|
|
166
|
+
* @method log
|
|
167
|
+
* @param {String} type
|
|
168
|
+
* @description This method is used to log the request and response
|
|
169
|
+
*/
|
|
170
|
+
log: (type) => {
|
|
171
|
+
if(type === undefined){
|
|
172
|
+
console.log(`${req.path} ${req.method} ${res.status} ${req.timestamp}`);
|
|
173
|
+
}else{
|
|
174
|
+
console.table({
|
|
175
|
+
'Request Path': req.path,
|
|
176
|
+
'Request Method': route.method,
|
|
177
|
+
'Response Status': res.status,
|
|
178
|
+
'Request Timestamp': req.timestamp,
|
|
179
|
+
});
|
|
398
180
|
}
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
return false;
|
|
181
|
+
},
|
|
182
|
+
send: (selector, data) => {
|
|
183
|
+
if(typeof selector === 'string'){
|
|
184
|
+
// @ts-ignore
|
|
185
|
+
document.querySelector(selector).innerHTML = data;
|
|
186
|
+
}else if(typeof selector === 'number'){
|
|
187
|
+
res.status = selector;
|
|
188
|
+
}else if(typeof selector === 'object'){
|
|
189
|
+
res.status = selector.status;
|
|
409
190
|
}
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
if
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
191
|
+
},
|
|
192
|
+
json: (selector, data) => {
|
|
193
|
+
|
|
194
|
+
if(typeof selector === 'string'){
|
|
195
|
+
// @ts-ignore
|
|
196
|
+
let obj = document.createElement('object');
|
|
197
|
+
// data url
|
|
198
|
+
obj.data = URL.createObjectURL(new Blob([JSON.stringify(data)], {type: 'application/json'}));
|
|
199
|
+
// @ts-ignore
|
|
200
|
+
document.querySelector(selector).appendChild(obj);
|
|
201
|
+
}else{
|
|
202
|
+
throw new Error('Selector must be a string');
|
|
419
203
|
}
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
method: "POST",
|
|
425
|
-
};
|
|
426
|
-
const res = {
|
|
427
|
-
return: function (data) {
|
|
428
|
-
this.hooked = false;
|
|
429
|
-
},
|
|
430
|
-
/**
|
|
431
|
-
* @alias send
|
|
432
|
-
* @param {String} selector
|
|
433
|
-
* @param {String} data
|
|
434
|
-
* @returns {void}
|
|
435
|
-
* @memberof VaderRouter
|
|
436
|
-
* @description Allows you to perform actions when the currentRoute changes.
|
|
437
|
-
* @example
|
|
438
|
-
* res.send('#root', '<h1>Hello World</h1>');
|
|
439
|
-
* */
|
|
440
|
-
send: function (selector, data) {
|
|
441
|
-
//@ts-ignore
|
|
442
|
-
document.querySelector(selector).innerHTML = data;
|
|
443
|
-
},
|
|
444
|
-
/**
|
|
445
|
-
* @alias render
|
|
446
|
-
* @param {String} selector
|
|
447
|
-
* @param {String} data
|
|
448
|
-
* @returns {void}
|
|
449
|
-
* @memberof VaderRouter
|
|
450
|
-
* @description Allows you to perform actions when the currentRoute changes.
|
|
451
|
-
*/
|
|
452
|
-
render: function (selector, data) {
|
|
453
|
-
//@ts-ignore
|
|
454
|
-
document.querySelector(selector).innerHTML = data;
|
|
455
|
-
},
|
|
456
|
-
};
|
|
457
|
-
// @ts-ignore
|
|
458
|
-
window.$URL_QUERY = query;
|
|
459
|
-
// @ts-ignore
|
|
460
|
-
window.$URL_PARAMS = params;
|
|
461
|
-
|
|
462
|
-
/**
|
|
463
|
-
* @alias callback
|
|
464
|
-
* @type {function}
|
|
465
|
-
* @param {Object} req - The request object.
|
|
466
|
-
* @returns {void}
|
|
467
|
-
* @memberof VaderRouter
|
|
468
|
-
* @description Allows you to perform actions when the currentRoute changes.
|
|
469
|
-
*/
|
|
470
|
-
callback(req, res);
|
|
471
|
-
}else{
|
|
472
|
-
console.log('no route')
|
|
473
|
-
}
|
|
204
|
+
},
|
|
205
|
+
};
|
|
206
|
+
this.middlewares.forEach((middleware) => {
|
|
207
|
+
middleware(req, res);
|
|
474
208
|
});
|
|
209
|
+
|
|
210
|
+
route ? route.handler(req, res) : null;
|
|
211
|
+
|
|
475
212
|
}
|
|
213
|
+
|
|
476
214
|
}
|
|
477
|
-
|
|
215
|
+
|
|
216
|
+
export default VaderRouter;
|
|
217
|
+
|
|
218
|
+
|