@blaasvaer/frmwrk 0.2.1 → 0.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/handle-request.js CHANGED
@@ -23,6 +23,7 @@ async function handleRequest( req, res ) {
23
23
  // console.log("handleRequest, req", req );
24
24
  const request_url = new URL( req.url, `http://${req.headers.host}`);
25
25
  // console.log("handleRequest, request_url", request_url);
26
+ // console.log("handleRequest, request_url.searchParams", request_url.searchParams);
26
27
  /**
27
28
  * Set default status and header
28
29
  * @type {Number}
@@ -42,7 +43,24 @@ async function handleRequest( req, res ) {
42
43
  return;
43
44
  } else {
44
45
  return;
45
- }
46
+ }
47
+ }
48
+
49
+ /**
50
+ * Service Worker from root
51
+ */
52
+ if (request_url.pathname === '/service-worker.js') {
53
+ // TODO: Set theme dynamically
54
+ var favicon = path.join('', 'service-worker.js');
55
+ res.statusCode = 200;
56
+ res.setHeader('Content-Type', 'text/javascript');
57
+
58
+ if ( fs.existsSync( favicon ) ) {
59
+ fs.createReadStream( favicon ).pipe( res );
60
+ return;
61
+ } else {
62
+ return;
63
+ }
46
64
  }
47
65
 
48
66
  /**
@@ -65,7 +83,7 @@ async function handleRequest( req, res ) {
65
83
 
66
84
  switch ( method ) {
67
85
  case 'GET':
68
- if ( ! controller.route.params ) {
86
+ if ( ! controller.route.params?.authorize ) {
69
87
  /**
70
88
  * If the route has no parameters, then just run the route method and serve whatever it may …
71
89
  */
@@ -75,14 +93,17 @@ async function handleRequest( req, res ) {
75
93
  * Authorization
76
94
  */
77
95
  if ( Authorize( controller.route ) ) {
96
+ // console.log(`We're authorized!`, controller.route.params);
78
97
  response_output = await controller.view();
79
98
  } else {
99
+ // console.log(`We're NOT authorized!`, controller.route.params);
80
100
  /**
81
101
  * Redirect the user to login if auth fails
82
102
  */
83
- if ( controller.route.params.access.redirect ) {
103
+ if ( controller.route.params.authorize.redirect ) {
104
+ // console.log('controller.route.params.redirect', controller.route.params.redirect);
84
105
  status_code = 302;
85
- res.setHeader( 'Location', controller.route.params.access.redirect );
106
+ res.setHeader( 'Location', controller.route.params.authorize.redirect );
86
107
  }
87
108
  // console.log("NO USER", controller.route.params.redirect);
88
109
  // response_output = redirect.action.call();
package/index.js CHANGED
@@ -46,14 +46,6 @@ FW = function ( config ) {
46
46
  controllers = {};
47
47
  models = {};
48
48
 
49
- /**
50
- * Create and start the server
51
- * @return {function} Server instance
52
- */
53
- http.createServer( handleRequest ).listen(port, hostname, () => {
54
- console.log(`Server running at http://${hostname}:${port}`);
55
- });
56
-
57
49
  /**
58
50
  * Set the Controller
59
51
  * @param {string} name The name of the controller
@@ -75,7 +67,7 @@ FW = function ( config ) {
75
67
  route.req = this.req;
76
68
  route.res = this.res;
77
69
  route.url = this.url;
78
-
70
+
79
71
  /**
80
72
  * Add search params to route
81
73
  */
@@ -95,12 +87,13 @@ FW = function ( config ) {
95
87
  /**
96
88
  * Check for authorization parameters.
97
89
  */
98
- if ( route.params !== null ) {
90
+ // if ( route.params !== null ) {
99
91
  // console.log("Params not null", route.params);
100
- if ( route.params.access !== undefined ) {
101
- console.log("This route should be authorized!");
102
- }
103
- }
92
+ // if ( route.params.authorize !== undefined ) {
93
+ // console.log("This route should be authorized!");
94
+ // Authorize( route.params );
95
+ // }
96
+ // }
104
97
 
105
98
  return this;
106
99
  }
@@ -109,8 +102,9 @@ console.log("This route should be authorized!");
109
102
  * TODO: Authorize user
110
103
  */
111
104
  Authorize = function Authorize ( credentials ) {
112
- // console.log("Authorize - check credentials:", credentials);
113
- return true;
105
+ console.log("Authorize - check credentials:", credentials);
106
+ return false;
107
+ // return true;
114
108
  }
115
109
 
116
110
  /**
@@ -119,10 +113,17 @@ console.log("This route should be authorized!");
119
113
  Install( config )
120
114
  .then(( result ) => {
121
115
  /**
122
- * Run post install processes here …
116
+ * Run optional post install processes here …
117
+ */
118
+
119
+ /**
120
+ * Create and start the server
121
+ * @return {function} Server instance
123
122
  */
124
- // console.log("Install result:", routes);
125
- })
123
+ http.createServer( handleRequest ).listen(port, hostname, () => {
124
+ console.log(`Server running at http://${hostname}:${port}`);
125
+ });
126
+ })
126
127
  .catch(function( err ) {
127
128
  console.log("Install Error:", err);
128
129
  });;
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "@blaasvaer/frmwrk",
3
+ "version": "0.1.14",
4
+ "description": "My personal Node framework",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1",
8
+ "patch": "npm version patch --force && npm publish",
9
+ "major": "npm version major --force && npm publish",
10
+ "minor": "npm version minor --force && npm publish"
11
+ },
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+ssh://git@bitbucket.org/blaasvaer/frmwrk.git"
15
+ },
16
+ "keywords": [
17
+ "Node",
18
+ "Framework",
19
+ "Homebrew"
20
+ ],
21
+ "author": "Sam Blåsvær",
22
+ "license": "ISC",
23
+ "bugs": {
24
+ "url": "https://bitbucket.org/blaasvaer/frmwrk/issues"
25
+ },
26
+ "homepage": "https://bitbucket.org/blaasvaer/frmwrk#readme",
27
+ "dependencies": {
28
+ "serve-handler": "^6.1.3"
29
+ }
30
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blaasvaer/frmwrk",
3
- "version": "0.2.1",
3
+ "version": "0.2.4",
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();