@blaasvaer/frmwrk 0.1.17 → 0.2.0

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
@@ -49,6 +49,8 @@ async function handleRequest( req, res ) {
49
49
  * Find route in Router based on url
50
50
  */
51
51
  const route = findRoute( request_url );
52
+ // console.log("request_url", request_url);
53
+ // console.log("route", route);
52
54
  /**
53
55
  * Check for complete route
54
56
  * @param {[type]} route [description]
@@ -58,41 +60,28 @@ async function handleRequest( req, res ) {
58
60
  /**
59
61
  * Create new controller from route
60
62
  */
61
- const controller = new Controller( route, req, res );
63
+ const controller = new Controller( route, req, res, request_url );
64
+
65
+ /**
66
+ * AUTHORIZATION
67
+ * Check for authorization parameters.
68
+ */
69
+ if ( route?.params !== null ) {
70
+ // console.log("Params not null", route.params);
71
+ let _redirect = (route.params?.redirect) ? route.params.redirect : '/login';
72
+
73
+ if ( route?.params?.authorize === true ) {
74
+ console.log("This route should be authorized!");
75
+ res.writeHead(301, { "Location": "http://" + req.headers['host'] + _redirect });
76
+ return res.end();
77
+ }
78
+ }
79
+
80
+ let response_output = `Nothing here …`;
62
81
 
63
- let response_output = 'Nothing here …';
64
-
65
82
  switch ( method ) {
66
83
  case 'GET':
67
- if ( ! controller.route.params ) {
68
- /**
69
- * If the route has no parameters, then just run the route method and serve whatever it may …
70
- */
71
- response_output = await controller.view();
72
- } else {
73
- /**
74
- * Authorization
75
- */
76
- if ( Authorize( controller.route ) ) {
77
- response_output = await controller.view();
78
- } else {
79
- /**
80
- * Redirect the user to login if auth fails
81
- */
82
- if ( controller.route.params.access.redirect ) {
83
- status_code = 302;
84
- res.setHeader( 'Location', controller.route.params.access.redirect );
85
- }
86
- // console.log("NO USER", controller.route.params.redirect);
87
- // response_output = redirect.action.call();
88
- // route.params.redirect.call();
89
- // response_output = 'Access denied';
90
- }
91
-
92
- if ( controller.route.params.type ) {
93
- content_type = controller.route.params.type;
94
- }
95
- }
84
+ response_output = await controller.view();
96
85
 
97
86
  res.statusCode = status_code;
98
87
  res.setHeader( 'Content-Type', content_types[content_type] );
package/index.js CHANGED
@@ -60,12 +60,11 @@ FW = function ( config ) {
60
60
  * @param {object} req The incomming request object
61
61
  * @param {object} res The response object
62
62
  */
63
- Controller = function Controller ( route, req, res ) {
64
- // console.log("Controller in index (req.method):\n", req.method);
65
- // console.log("Controller in index (req):\n", req);
63
+ Controller = function Controller ( route, req, res, url ) {
66
64
  this.route = route;
67
65
  this.req = req;
68
66
  this.res = res;
67
+ this.url = url;
69
68
  this.route.method = req.method;
70
69
 
71
70
  /**
@@ -75,21 +74,24 @@ FW = function ( config ) {
75
74
  route.data = data;
76
75
  route.req = this.req;
77
76
  route.res = this.res;
77
+ route.url = this.url;
78
+
79
+ /**
80
+ * Add search params to route
81
+ */
82
+ let params = {};
83
+
84
+ for ( let pair of url.searchParams) {
85
+ params[pair[0]] = pair[1];
86
+ }
87
+
88
+ route.searchParams = params;
89
+
78
90
  let output = await route.action( route );
79
91
 
80
92
  return output;
81
93
  }
82
94
 
83
- /**
84
- * Check for authorization parameters.
85
- */
86
- if ( route.params !== null ) {
87
- // console.log("Params not null", route.params);
88
- if ( route.params.access !== undefined ) {
89
- console.log("This route should be authorized!");
90
- }
91
- }
92
-
93
95
  return this;
94
96
  }
95
97
 
@@ -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.1.17",
3
+ "version": "0.2.0",
4
4
  "description": "My personal Node framework",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/router.js CHANGED
@@ -30,55 +30,47 @@ global.ROUTE = function ( route, action, params = null ) {
30
30
  global.routes.push( r );
31
31
  };
32
32
 
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('/');
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
38
 
39
- // Remove first empty element
40
- url_array.shift();
39
+ // // Remove first empty element
40
+ // url_array.shift();
41
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];
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
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);
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
56
 
57
- return this;
58
- };
57
+ // return this;
58
+ // };
59
59
 
60
60
  function findRoute ( url ) {
61
61
  let pathname = url.pathname;
62
- // console.log( "ROUTE, url", url );
63
62
 
64
- // console.log("THIS", this);
65
- // Check if route exist
66
63
  let route = global.routes.find( ( obj ) => obj.route === FW.utils.removeTrailingSlashes( pathname ) );
67
64
 
68
65
  if ( route ) {
69
- // console.log("Route defined url:", url);
70
- // console.log("Route defined route:", route);
71
- // Execute action for route
72
66
  return route;
73
67
  } else {
74
- // console.log("Route NOT defined", url);
75
68
  const url_array = pathname.split('/').filter( e => e );
76
69
 
77
70
  // Check if route is an API call
78
71
  let isAPICall = pathname.split('/')[1] === 'api' ? true : false;
79
72
 
80
73
  if ( isAPICall ) {
81
- // console.log("Route NOT defined (API)", url, url.split('/')[1]);
82
74
  /**
83
75
  * If checking for UUID use:
84
76
  * UUID v1:
@@ -122,6 +114,6 @@ function findRoute ( url ) {
122
114
  }
123
115
 
124
116
  module.exports = {
125
- Router,
117
+ // Router,
126
118
  findRoute
127
119
  }
@@ -1,22 +0,0 @@
1
- {
2
- "workbench.colorCustomizations": {
3
- "activityBar.activeBackground": "#2f7c47",
4
- "activityBar.activeBorder": "#422c74",
5
- "activityBar.background": "#2f7c47",
6
- "activityBar.foreground": "#e7e7e7",
7
- "activityBar.inactiveForeground": "#e7e7e799",
8
- "activityBarBadge.background": "#422c74",
9
- "activityBarBadge.foreground": "#e7e7e7",
10
- "statusBar.background": "#215732",
11
- "statusBar.foreground": "#e7e7e7",
12
- "statusBarItem.hoverBackground": "#2f7c47",
13
- "titleBar.activeBackground": "#215732",
14
- "titleBar.activeForeground": "#e7e7e7",
15
- "titleBar.inactiveBackground": "#21573299",
16
- "titleBar.inactiveForeground": "#e7e7e799",
17
- "sash.hoverBorder": "#2f7c47",
18
- "statusBarItem.remoteBackground": "#215732",
19
- "statusBarItem.remoteForeground": "#e7e7e7"
20
- },
21
- "peacock.color": "#215732"
22
- }