@blaasvaer/frmwrk 0.2.1 → 0.2.3

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/index.js CHANGED
@@ -121,7 +121,7 @@ console.log("This route should be authorized!");
121
121
  /**
122
122
  * Run post install processes here …
123
123
  */
124
- // console.log("Install result:", routes);
124
+
125
125
  })
126
126
  .catch(function( err ) {
127
127
  console.log("Install Error:", err);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blaasvaer/frmwrk",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
4
4
  "description": "My personal Node framework",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/router.js CHANGED
@@ -10,63 +10,37 @@ const url = require('url');
10
10
  global.routes = [];
11
11
 
12
12
  global.ROUTE = function ( route, action, params = null ) {
13
+ let _route = route.split('/');
14
+
13
15
  let r = {
14
- "controller": route.split('/')[1] == '' ? 'index' : route.split('/')[1],
15
- "resource": route.split('/')[2] == '' ? 'index' : route.split('/')[2],
16
- "resource_id": route.split('/')[3] == '' ? 'index' : route.split('/')[3],
16
+ "controller": _route[1] == '' ? 'index' : _route[1],
17
+ "resource": _route[2] == '' ? '' : _route[2],
18
+ "resource_id": _route[3] == '' ? '' : _route[3],
17
19
  "route": route,
18
20
  "action": action,
19
21
  "params": params,
20
22
  }
21
- // console.log("Router params:", params?.type);
22
- // console.log("----------------------");
23
- // console.log("Router route:", route);
24
- // console.log("Router controller:", r.controller);
25
- // console.log("Router resource:", r.resource);
26
- // console.log("Router resource_id:", r.resource_id);
27
- // console.log("Router route:", r.route);
28
- // console.log("Router action:", r.action);
29
- // console.log("Router params:", r.params);
23
+
30
24
  global.routes.push( r );
31
25
  };
32
26
 
33
- // function Router ( req, url ) {
34
- // const method = req.method;
35
- // // console.log("Router url:", url);
36
- // // Split url into array
37
- // let url_array = url.pathname.split('/');
38
-
39
- // // Remove first empty element
40
- // url_array.shift();
41
-
42
- // // Set controller, resource, resource_id, search, searchParams
43
- // this.controller = url_array[0];
44
- // this.resource = url_array[1];
45
- // this.resource_id = url_array[2];
46
- // this.search = url_array[3];
47
- // this.searchParams = url_array[4];
48
-
49
- // // console.log("referer", referer);
50
- // // console.log("url", url);
51
- // // console.log("this.controller", this.controller);
52
- // // console.log("this.resource", this.resource);
53
- // // console.log("this.resource_id", this.resource_id);
54
- // // console.log("this.search", this.search);
55
- // // console.log("this.searchParams", this.searchParams);
56
-
57
- // return this;
58
- // };
59
-
27
+ /**
28
+ * A route can be either static or dynamic.
29
+ * A static route is defined in the controllers and dynamic routes will be resolved automatically.
30
+ * In order to figure out if we have a static route, we check the pathname and match it against the global.routes object.
31
+ * If we do not find any match we asume the route is dynamic and therefore pass '/api/:resource' on.
32
+ * @param {string} url The url to find
33
+ * @returns object
34
+ */
60
35
  function findRoute ( url ) {
61
- let pathname = url.pathname;
36
+ let pathname = url.pathname,
37
+ url_array = pathname.split('/').filter( e => e );
62
38
 
63
39
  let route = global.routes.find( ( obj ) => obj.route === FW.utils.removeTrailingSlashes( pathname ) );
64
40
 
65
41
  if ( route ) {
66
42
  return route;
67
43
  } else {
68
- const url_array = pathname.split('/').filter( e => e );
69
-
70
44
  // Check if route is an API call
71
45
  let isAPICall = pathname.split('/')[1] === 'api' ? true : false;
72
46
 
@@ -93,8 +67,19 @@ function findRoute ( url ) {
93
67
  route = global.routes.find( ( obj ) => obj.route === '/api/:resources' );
94
68
  break;
95
69
  case 3:
96
- regex = /api\/(?<resource>[a-z_]+)\/(?<id>[0-9]+)/; // match /api/<resource>/<id>
97
- route = global.routes.find( ( obj ) => obj.route === '/api/:resources/:id' );
70
+ // Check for static route with dynamic resource_id
71
+ let static_route_path = '/' + url_array[0] + '/' + url_array[1] + '/:id';
72
+ let static_route = global.routes.find( ( obj ) => obj.route === static_route_path );
73
+
74
+ // If we don't find a static route we pass on a dynamic
75
+ if ( ! static_route ) {
76
+ regex = /api\/(?<resource>[a-z_]+)\/(?<id>[0-9]+)/; // match /api/<resource>/<id>
77
+ route = global.routes.find( ( obj ) => obj.route === '/api/:resources/:id' );
78
+ } else {
79
+ let resource = url_array[1];
80
+ regex = new RegExp(`\/api\/${resource}\/(?<id>[0-9]+)`); // match /api/resource/<id>
81
+ route = global.routes.find( ( obj ) => obj.route === '/api/' + url_array[1] + '/:id' );
82
+ }
98
83
  break;
99
84
  }
100
85
 
@@ -114,6 +99,5 @@ function findRoute ( url ) {
114
99
  }
115
100
 
116
101
  module.exports = {
117
- // Router,
118
102
  findRoute
119
103
  }
package/utils.js CHANGED
@@ -71,6 +71,15 @@ class Utils {
71
71
  removeTrailingSlashes( url ) {
72
72
  return url.replace(/\/+$/, '');
73
73
  }
74
+
75
+ /**
76
+ * Removes all starting and trailing slashes from URL
77
+ * @param {string} url
78
+ * @return {string} Return the string without trailing slashes.
79
+ */
80
+ removeStartingTrailingSlashes( url ) {
81
+ return url.replace(/^\/+|\/+$/g, '')
82
+ }
74
83
  }
75
84
 
76
85
  module.exports = new Utils();