1
1
/**
2
- * basicRouter .ts
2
+ * router .ts
3
3
* Copyright(c) 2021 Aaron Hedges <[email protected] >
4
4
* MIT Licensed
5
5
*
6
- * This is a basic router middleware for roads.
6
+ * This is a router middleware for roads.
7
7
* It allows you to easily attach functionality to HTTP methods and paths.
8
8
*/
9
9
@@ -15,7 +15,7 @@ import { NextCallback, RequestChain } from '../core/requestChain';
15
15
16
16
17
17
export interface Route < ContextType extends Context > {
18
- ( this : ContextType , method : string , path : BasicRouterURL , body : string ,
18
+ ( this : ContextType , method : string , path : RouterURL , body : string ,
19
19
headers : IncomingHeaders , next : NextCallback ) : Promise < Response >
20
20
}
21
21
@@ -25,14 +25,14 @@ interface RouteDetails {
25
25
method : string
26
26
}
27
27
28
- export interface BasicRouterURL extends ReturnType < typeof parse > {
28
+ export interface RouterURL extends ReturnType < typeof parse > {
29
29
args ?: Record < string , string | number >
30
30
}
31
31
/**
32
- * This is a basic router middleware for roads.
33
- * You can assign functions to url paths, and those paths can have some very basic variable templating
32
+ * This is a router middleware for roads.
33
+ * You can assign functions to url paths, and those paths can have variable templating
34
34
*
35
- * Templating is basic . Each URI is considered to be a series of "path parts" separated by slashes.
35
+ * There are only a couple of template options . Each URI is considered to be a series of "path parts" separated by slashes.
36
36
* If a path part starts with a #, it is assumed to be a numeric variable. Non-numbers will not match this route
37
37
* If a path part starts with a $, it is considered to be an alphanumeric variabe.
38
38
* All non-slash values will match this route.
@@ -43,13 +43,13 @@ export interface BasicRouterURL extends ReturnType<typeof parse> {
43
43
* /users/#user_id will match /users/12345, not /users/abcde. If a request is made to /users/12345
44
44
* the route's requestUrl object will contain { args: {user_id: 12345}} along with all other url object values
45
45
*
46
- * @name BasicRouter
46
+ * @name Router
47
47
*/
48
- export class BasicRouter {
48
+ export class Router < RouterContextType extends Context > {
49
49
protected _routes : RouteDetails [ ] ;
50
50
51
51
/**
52
- * @param {Road } [road] - The road that will receive the BasicRouter middleware
52
+ * @param {Road } [road] - The road that will receive the Router middleware
53
53
*/
54
54
constructor ( road ?: Road ) {
55
55
this . _routes = [ ] ;
@@ -63,15 +63,15 @@ export class BasicRouter {
63
63
* If you don't provide a road to the SimpleRouter constructor, your routes will not be executed.
64
64
* If you have reason not to assign the road off the bat, you can assign it later with this function.
65
65
*
66
- * @param {Road } road - The road that will receive the BasicRouter middleware
66
+ * @param {Road } road - The road that will receive the Router middleware
67
67
*/
68
68
applyMiddleware ( road : Road ) : void {
69
69
// We need to alias because "this" for the middleware function must
70
- // be the this applied by road.use, not the BasicRouter
70
+ // be the this applied by road.use, not the Router
71
71
// eslint-disable-next-line @typescript-eslint/no-this-alias
72
72
const _self = this ;
73
73
74
- // We do this to ensure we have access to the BasicRouter once we lose this due to road's context
74
+ // We do this to ensure we have access to the Router once we lose this due to road's context
75
75
road . use ( ( function ( request_method , request_url , request_body , request_headers , next ) {
76
76
return _self . _middleware . call ( this , _self . _routes , request_method , request_url ,
77
77
request_body , request_headers , next ) ;
@@ -82,7 +82,7 @@ export class BasicRouter {
82
82
* This is where you want to write the majority of your webservice. The `fn` parameter should contain
83
83
* the actions you want to perform when a certain `path` and HTTP `method` are accessed via the `road` object.
84
84
*
85
- * The path supports a very basic templating system. The values inbetween each slash can be interpreted
85
+ * The path supports a templating system. The values inbetween each slash can be interpreted
86
86
* in one of three ways
87
87
* - If a path part starts with a #, it is assumed to be a numeric variable. Non-numbers will not match this route
88
88
* - If a path part starts with a $, it is considered to be an alphanumeric variabe. All non-slash values
@@ -99,8 +99,10 @@ export class BasicRouter {
99
99
* @param {(string|array) } paths - One or many URL paths that will trigger the provided function
100
100
* @param {function } fn - The function containing all of your route logic
101
101
*/
102
- addRoute < ContextType extends Context > (
103
- method : string , paths : string | string [ ] , fn : Route < ContextType > | Route < ContextType > [ ]
102
+ addRoute < RouteContextType extends Context > (
103
+ method : string ,
104
+ paths : string | string [ ] ,
105
+ fn : Route < RouterContextType & RouteContextType > | Route < RouterContextType & RouteContextType > [ ]
104
106
) : void {
105
107
if ( ! Array . isArray ( paths ) ) {
106
108
paths = [ paths ] ;
@@ -201,7 +203,7 @@ export class BasicRouter {
201
203
/**
202
204
* Checks to see if the route matches the request, and if true assigns any applicable url variables and returns the route
203
205
*
204
- * @param {object } route - Route object from this basic router class
206
+ * @param {object } route - Route object from this router class
205
207
* @param {object } route.method - HTTP method associated with this route
206
208
* @param {object } route.path - HTTP path associated with this route
207
209
* @param {object } request_url - Parsed HTTP request url
@@ -240,14 +242,14 @@ function compareRouteAndApplyArgs (route: {method: string, path: string}, reques
240
242
}
241
243
242
244
// TODO: get rid of this `as`
243
- applyArg ( request_url as BasicRouterURL , template_part . substring ( 1 ) , Number ( actual_part ) ) ;
245
+ applyArg ( request_url as RouterURL , template_part . substring ( 1 ) , Number ( actual_part ) ) ;
244
246
continue ;
245
247
}
246
248
247
249
if ( template_part [ 0 ] === '$' ) {
248
250
// $ templates accept any non-slash alphanumeric character
249
251
// TODO: get rid of this `as`
250
- applyArg ( request_url as BasicRouterURL , template_part . substring ( 1 ) , String ( actual_part ) ) ;
252
+ applyArg ( request_url as RouterURL , template_part . substring ( 1 ) , String ( actual_part ) ) ;
251
253
// Continue so that
252
254
continue ;
253
255
}
@@ -270,7 +272,7 @@ function compareRouteAndApplyArgs (route: {method: string, path: string}, reques
270
272
* @param {string } template_part - The template variable
271
273
* @param {* } actual_part - The url value
272
274
*/
273
- function applyArg ( request_url : BasicRouterURL , template_part : string , actual_part : string | number ) : void {
275
+ function applyArg ( request_url : RouterURL , template_part : string , actual_part : string | number ) : void {
274
276
if ( typeof ( request_url . args ) === 'undefined' ) {
275
277
request_url . args = { } ;
276
278
}
0 commit comments