@vulkano/core 1.10.0 → 1.11.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.
@@ -369,7 +369,7 @@ module.exports = function loadServer() {
369
369
  express: vulkano
370
370
  };
371
371
 
372
- const envNunjucks = nunjucks.configure(views.path, nunjucksSettings);
372
+ const envNunjucks = nunjucks.configure([views.path, CORE_PATH], nunjucksSettings);
373
373
 
374
374
  app.server.views._engine = envNunjucks;
375
375
 
@@ -545,35 +545,124 @@ module.exports = function loadServer() {
545
545
 
546
546
  const server = await vulkano.listen(expressConfig.port);
547
547
 
548
+ // Routes registered
549
+ const routesRegistered = vulkano._router.stack // registered routes
550
+ .filter((r) => r.route && r.route.path !== '*') // take out all the middleware
551
+ .map((r) => {
552
+ return {
553
+ method: Object.keys(r.route.methods)[0].toUpperCase(),
554
+ path: r.route.path
555
+ };
556
+ });
557
+
548
558
  // ---------------
549
559
  // ERROR 404
550
560
  // ---------------
551
561
  vulkano.use((req, res) => {
562
+
552
563
  if (+res.statusCode >= 500 && +res.statusCode < 600) {
553
564
  throw new Error();
554
565
  }
555
- res.status(404).render(`${views.path}/_shared/errors/404.html`);
566
+
567
+ res.status(404);
568
+
569
+ if (app.PRODUCTION) {
570
+ res.render(`${views.path}/_shared/errors/404.html`);
571
+ return;
572
+ }
573
+
574
+ // Verify if the error is a controller
575
+ const isController = routesRegistered.filter( (r) => {
576
+
577
+ const {
578
+ path: routePath
579
+ } = r || {};
580
+
581
+ const routerControllerToCheck = Filter.get(req.path, 'trim', '/').split('/')[0];
582
+
583
+ return routePath.startsWith(`/${routerControllerToCheck}`);
584
+
585
+ });
586
+
587
+ if (isController.length === 0) {
588
+
589
+ res.render(`${CORE_PATH}/views/errors/no_controller.html`, {
590
+ method: req.method,
591
+ controller: req.path.split('/')[1]
592
+ });
593
+
594
+ return;
595
+
596
+ }
597
+
598
+ // Show the action name error
599
+ res.render(`${CORE_PATH}/views/errors/no_action.html`, {
600
+ method: req.method,
601
+ controller: req.path.split('/')[1],
602
+ action: req.path.split('/')[2]
603
+ });
604
+
556
605
  });
557
606
 
558
607
  // ---------------
559
608
  // ERROR 5XX
560
609
  // ---------------
561
- vulkano.use((err, req, res) => {
562
- const status = err.status || res.statusCode || 500;
610
+ vulkano.use((err, req, res, next) => {
611
+
612
+ if (res.headersSent) {
613
+ next(err);
614
+ return;
615
+ }
616
+
617
+ const status = err ? (err.status || 500) : (res.statusCode || 500);
618
+
563
619
  res.status(status);
564
- if (!res.xhr) {
565
- if (+status > 400 && +status < 500) {
566
- res.render(`${vulkano.get('views')}/_shared/errors/404.html`, { content: err.stack });
567
- } else {
568
- res.render(`${vulkano.get('views')}/_shared/errors/500.html`, { content: err.stack });
569
- }
570
- } else {
620
+
621
+ // AJAX Response
622
+ if (req.xhr) {
623
+
571
624
  res.jsonp({
572
625
  success: false,
573
- error: err.message || err.error || err.invalidAttributes || err.toString() || 'Object Not Found',
574
- data: (app.PRODUCTION) ? {} : (err.stack || {})
626
+ statusCode: status,
627
+ error: {
628
+ detail: err.message || err.error || err.invalidAttributes || err.toString() || 'Object Not Found',
629
+ stack: (app.PRODUCTION) ? {} : (err.stack || {})
630
+ }
575
631
  });
632
+
633
+ return;
634
+
576
635
  }
636
+
637
+ if (app.PRODUCTION) {
638
+
639
+ if (+status > 400 && +status < 500) {
640
+ res.render(`${vulkano.get('views')}/_shared/errors/404.html`);
641
+ } else {
642
+ res.render(`${vulkano.get('views')}/_shared/errors/500.html`);
643
+ }
644
+
645
+ return;
646
+
647
+ }
648
+
649
+ let errorViewToShow = `${CORE_PATH}/views/errors/exception.html`;
650
+
651
+ if (err.stack.indexOf('template not found') >= 0) {
652
+ errorViewToShow = `${CORE_PATH}/views/errors/no_view.html`;
653
+ }
654
+
655
+ res.render(errorViewToShow, {
656
+ statusCode: status,
657
+ method: req.method,
658
+ controller: req.path.split('/')[1],
659
+ action: req.path.split('/')[2],
660
+ view: err.stack.indexOf('template not found') >= 0
661
+ ? String( `${(err.stack.split('not found:')[1]).split('.')[0]}.html` ).trim()
662
+ : '',
663
+ stack: err.stack
664
+ });
665
+
577
666
  });
578
667
 
579
668
  app.vulkano = vulkano;
@@ -1,5 +1,12 @@
1
1
  const path = require('path');
2
2
 
3
+ // Include all filters
4
+ const coreFilters = require('include-all')({
5
+ dirname: path.join(CORE_PATH, 'views/filters'),
6
+ filter: /(.+)\.js$/,
7
+ optional: true
8
+ });
9
+
3
10
  // Include all filters
4
11
  const appFilters = require('include-all')({
5
12
  dirname: path.join(APP_PATH, 'config/views/filters'),
@@ -20,7 +27,10 @@ module.exports = {
20
27
 
21
28
  engine: 'nunjucks',
22
29
 
23
- filters: [appFilters || {}],
30
+ filters: [
31
+ coreFilters || {},
32
+ appFilters || {}
33
+ ],
24
34
 
25
35
  helpers: [appHelpers || {}]
26
36
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vulkano/core",
3
- "version": "1.10.0",
3
+ "version": "1.11.0",
4
4
  "description": "A MVC framework using Express 4",
5
5
  "license": "MIT",
6
6
  "author": "Vulkano Team",
@@ -0,0 +1,21 @@
1
+ {% extends "../templates/exception.html" %}
2
+
3
+ {% block content %}
4
+
5
+ <div class="flash error">
6
+ <h1>Internal Error</h1>
7
+ <p>
8
+ <strong>Error: {{ statusCode }} </strong>
9
+ </p>
10
+ </div>
11
+
12
+ {% if stack %}
13
+ <div class="flash stack">
14
+ <h2>Stack</h2>
15
+ <p>
16
+ {{ stack }}
17
+ </p>
18
+ </div>
19
+ {% endif %}
20
+
21
+ {% endblock %}
@@ -0,0 +1,49 @@
1
+ {% extends "../templates/error.html" %}
2
+
3
+ {% block content %}
4
+
5
+ <div class="flash error">
6
+ <h1>Action Not Found</h1>
7
+ <p>
8
+ <strong>Error: </strong>
9
+ The Action <b><em>{{ action }}</em></b> does not exist or could not be found.
10
+ </p>
11
+ </div>
12
+
13
+ <div class="flash valid">
14
+ <h2>How To Fix:</h2>
15
+ <h4>
16
+ <strong>
17
+ Create the method <em>{{ action }}</em> into the <em>{{ controller | vCamelCase }}Controller</em> to make it work correctly :
18
+ </strong>
19
+ </h4>
20
+ <pre>
21
+ <code>
22
+ /**
23
+ * Controller:
24
+ * {{ controller | vCamelCase }}Controller.js
25
+ */
26
+ module.exports = {
27
+
28
+ // Action to run
29
+ '{{ action if action else method | vLowercase }}': (req, res) => {
30
+
31
+ // Render your view
32
+ res.render('{{ controller | vLowercase }}/index.html');
33
+ }
34
+
35
+ };
36
+ </code>
37
+ </pre>
38
+ </div>
39
+
40
+ {% if stack %}
41
+ <div class="flash stack">
42
+ <h2>Stack</h2>
43
+ <p>
44
+ {{ stack }}
45
+ </p>
46
+ </div>
47
+ {% endif %}
48
+
49
+ {% endblock %}
@@ -0,0 +1,51 @@
1
+ {% extends "../templates/error.html" %}
2
+
3
+ {% block content %}
4
+
5
+ <div class="flash error">
6
+ <h1>Controller Not Found</h1>
7
+ <p>
8
+ <strong>Error: </strong>
9
+ <em>{{ controller | vCamelCase }}Controller</em> not exist or could not be found.
10
+ </p>
11
+ </div>
12
+
13
+ <div class="flash valid">
14
+ <h2>How To Fix:</h2>
15
+ <h4>
16
+ <strong>
17
+ Create the file <em>{{ controller | vCamelCase }}Controller</em> in the path :
18
+ <br>
19
+ <em class="file">/app/controllers/{{ controller | vLowercase }}_controller.js</em>
20
+ </strong>
21
+ </h4>
22
+ <pre>
23
+ <code>
24
+ /**
25
+ * Controller:
26
+ * {{ controller | vCamelCase }}Controller.js
27
+ */
28
+ module.exports = {
29
+
30
+ // Action to run - {{ controller | vLowercase }}/hello
31
+ 'hello': (req, res) => {
32
+
33
+ // Render your view
34
+ res.render('{{ controller | vLowercase }}/index.html');
35
+ }
36
+
37
+ };
38
+ </code>
39
+ </pre>
40
+ </div>
41
+
42
+ {% if stack %}
43
+ <div class="flash stack">
44
+ <h2>Stack</h2>
45
+ <p>
46
+ {{ stack }}
47
+ </p>
48
+ </div>
49
+ {% endif %}
50
+
51
+ {% endblock %}
@@ -0,0 +1,31 @@
1
+ {% extends "../templates/error.html" %}
2
+
3
+ {% block content %}
4
+
5
+ <div class="flash error">
6
+ <h1>View Not Found</h1>
7
+ <p>
8
+ <strong>Error: </strong>
9
+ The template <b><em>{{ view }}</em></b> does not exist or could not be found.
10
+ </p>
11
+ </div>
12
+
13
+ <div class="flash valid">
14
+ <h2>How To Fix:</h2>
15
+ <h4>
16
+ <strong>
17
+ Create the view <em>{{ view }}</em> into the <em>{{ APP_PATH }}/views</em> to make it work correctly :
18
+ </strong>
19
+ </h4>
20
+ </div>
21
+
22
+ {% if stack %}
23
+ <div class="flash stack">
24
+ <h2>Stack</h2>
25
+ <p>
26
+ {{ stack }}
27
+ </p>
28
+ </div>
29
+ {% endif %}
30
+
31
+ {% endblock %}
@@ -0,0 +1,10 @@
1
+ module.exports = (str) => {
2
+
3
+ return String(str || '')
4
+ .split('_')
5
+ .map( (t) => {
6
+ return `${t.charAt(0).toUpperCase()}${t.slice(1).toLowerCase()}`;
7
+ })
8
+ .join('');
9
+
10
+ };
@@ -0,0 +1,8 @@
1
+ module.exports = (str) => {
2
+
3
+ return String(str || '')
4
+ .split(' ')
5
+ .join('_')
6
+ .toLowerCase();
7
+
8
+ };
@@ -0,0 +1,83 @@
1
+ <!DOCTYPE HTML>
2
+ <html lang="en">
3
+ <head>
4
+ <title>Oops! something was wrong :( - VulkanoJS</title>
5
+ <meta charset=UTF-8 >
6
+ <meta http-equiv="X-UA-Compatible" content="IE=edge">
7
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
8
+
9
+ <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/atom-one-dark.min.css">
10
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
11
+
12
+ <style>
13
+
14
+ * { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; }
15
+ html{ font-family:sans-serif;-webkit-text-size-adjust:100%;-ms-text-size-adjust:100% }
16
+ html, body { margin: 0; padding: 0; }
17
+
18
+ .flash {
19
+ margin: 0;
20
+ min-height: 32px;
21
+ padding:3px 50px;
22
+ background-repeat: no-repeat;
23
+ background-position: 10px center;
24
+ line-height: 1.7;
25
+ border-radius: 2px;
26
+ }
27
+ .error {
28
+ color: #D8000C;
29
+ background-color: #FFBABA;
30
+ }
31
+ .info {
32
+ color: #00529B;
33
+ background-color: #BDE5F8;
34
+ }
35
+ .valid {
36
+ color: #4F8A10;
37
+ background-color: #DFF2BF;
38
+ }
39
+ .warning {
40
+ color: #9F6000;
41
+ background-color: #FEEFB3;
42
+ }
43
+
44
+ pre {
45
+ tab-size: 2;
46
+ }
47
+
48
+ body {
49
+ background: #f2f2f2;
50
+ }
51
+
52
+ .file{ word-break: break-word; }
53
+
54
+ </style>
55
+ </head>
56
+ <body>
57
+
58
+
59
+ <div id="vulkano-error">
60
+ {% block content %} {% endblock %}
61
+ </div>
62
+
63
+ <footer>
64
+ <div class="flash info">
65
+ <p>
66
+ <b>In PRODUCTION this error does not appear :), it shows the app error template:</b>
67
+ </p>
68
+ <p>
69
+ /app/views/_shared/errors/404.html
70
+ </p>
71
+ <p>
72
+ <span style="font-weight: normal">You can edit it as you want!</span>
73
+ </p>
74
+ </div>
75
+ </footer>
76
+
77
+
78
+ <!-- and it's easy to individually load additional languages -->
79
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/javascript.min.js"></script>
80
+
81
+ <script>hljs.highlightAll();</script>
82
+ </body>
83
+ </html>
@@ -0,0 +1,83 @@
1
+ <!DOCTYPE HTML>
2
+ <html lang="en">
3
+ <head>
4
+ <title>Oops! something was wrong :( - VulkanoJS</title>
5
+ <meta charset=UTF-8 >
6
+ <meta http-equiv="X-UA-Compatible" content="IE=edge">
7
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
8
+
9
+ <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/atom-one-dark.min.css">
10
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
11
+
12
+ <style>
13
+
14
+ * { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; }
15
+ html{ font-family:sans-serif;-webkit-text-size-adjust:100%;-ms-text-size-adjust:100% }
16
+ html, body { margin: 0; padding: 0; }
17
+
18
+ .flash {
19
+ margin: 0;
20
+ min-height: 32px;
21
+ padding:3px 50px;
22
+ background-repeat: no-repeat;
23
+ background-position: 10px center;
24
+ line-height: 1.7;
25
+ border-radius: 2px;
26
+ }
27
+ .error {
28
+ color: #D8000C;
29
+ background-color: #FFBABA;
30
+ }
31
+ .info {
32
+ color: #00529B;
33
+ background-color: #BDE5F8;
34
+ }
35
+ .valid {
36
+ color: #4F8A10;
37
+ background-color: #DFF2BF;
38
+ }
39
+ .warning {
40
+ color: #9F6000;
41
+ background-color: #FEEFB3;
42
+ }
43
+
44
+ pre {
45
+ tab-size: 2;
46
+ }
47
+
48
+ body {
49
+ background: #f2f2f2;
50
+ }
51
+
52
+ .file{ word-break: break-word; }
53
+
54
+ </style>
55
+ </head>
56
+ <body>
57
+
58
+
59
+ <div id="vulkano-error">
60
+ {% block content %} {% endblock %}
61
+ </div>
62
+
63
+ <footer>
64
+ <div class="flash info">
65
+ <p>
66
+ <b>In PRODUCTION this error does not appear :), it shows the app error template:</b>
67
+ </p>
68
+ <p>
69
+ /app/views/_shared/errors/500.html
70
+ </p>
71
+ <p>
72
+ <span style="font-weight: normal">You can edit it as you want!</span>
73
+ </p>
74
+ </div>
75
+ </footer>
76
+
77
+
78
+ <!-- and it's easy to individually load additional languages -->
79
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/javascript.min.js"></script>
80
+
81
+ <script>hljs.highlightAll();</script>
82
+ </body>
83
+ </html>