binhend 2.0.1 → 2.0.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "binhend",
3
- "version": "2.0.1",
3
+ "version": "2.0.3",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "author": "Nguyen Duc Binh",
@@ -0,0 +1,10 @@
1
+
2
+ class HttpError extends Error {
3
+ constructor(httpCode, message) {
4
+ super(message);
5
+ this.name = 'HttpError';
6
+ this.httpCode = httpCode;
7
+ }
8
+ }
9
+
10
+ module.exports = HttpError;
@@ -0,0 +1,55 @@
1
+
2
+ const { Module } = require('module');
3
+ const { ServerResponse } = require('http');
4
+ const { isFunction } = require('../utils/typeOf');
5
+ const responseErrorByDefault = require('./responseErrorByDefault');
6
+ const trycatch = require('./trycatch');
7
+ const express = require('express');
8
+
9
+
10
+ function Router(moduleInstance) {
11
+ var router = express.Router();
12
+
13
+ var output = { router };
14
+
15
+ for (var key in router) {
16
+ let method = router[key]; // use 'let' to not lose reference, 'var' will override 'method' reference in this function scope => error on run-time
17
+
18
+ if (!isFunction(method)) continue;
19
+
20
+ output[key] = function() {
21
+ let args = [];
22
+
23
+ Array.from(arguments).forEach((arg) => {
24
+ if (!isFunction(arg)) return args.push(arg);
25
+ args.push(createErrorHandler(router, arg));
26
+ });
27
+
28
+ return method.apply(router, args);
29
+ };
30
+ }
31
+
32
+ if (moduleInstance instanceof Module) {
33
+ moduleInstance.exports = router;
34
+ }
35
+
36
+ output.trycatch = trycatch;
37
+
38
+ return output;
39
+ }
40
+
41
+ function createErrorHandler(router, callback) {
42
+ if (callback.isAppliedTryCatch) return callback;
43
+
44
+ return async function(request, response, next) {
45
+ try {
46
+ return await callback.apply(router, arguments);
47
+ }
48
+ catch (error) {
49
+ if (arguments.length < 3 || !(response instanceof ServerResponse)) throw error;
50
+ responseErrorByDefault(error, response, next);
51
+ }
52
+ }
53
+ }
54
+
55
+ module.exports = Router;
@@ -0,0 +1,9 @@
1
+
2
+ const { loadRouter, loadRoutes, mapRoutes } = require('./loader');
3
+ const HttpError = require('./HttpError');
4
+ const Router = require('./Router');
5
+ const trycatch = require('./trycatch');
6
+
7
+ module.exports = {
8
+ loadRouter, loadRoutes, mapRoutes, trycatch, Router, HttpError
9
+ };
@@ -0,0 +1,57 @@
1
+ const { readdirSync, statSync } = require('fs');
2
+ const { join, parse, resolve } = require('path');
3
+ const { isUndefined } = require('../utils/typeOf');
4
+ const { server } = require('../server');
5
+ const express = require('express');
6
+ const parseBasicAuthToken = require('../middleware/parseBasicAuthToken');
7
+
8
+
9
+ function mapRoutes(router, dirpath) {
10
+ readdirSync(dirpath).forEach(function(direntName) {
11
+ var path = join(dirpath, direntName),
12
+ route = parse(direntName).name;
13
+
14
+ var stat = statSync(path);
15
+ var childRouter;
16
+
17
+ if (stat.isFile()) {
18
+ childRouter = require(path);
19
+ }
20
+ else if (stat.isDirectory()) {
21
+ childRouter = express.Router();
22
+ mapRoutes(childRouter, path);
23
+ }
24
+
25
+ router.use(`/${route}`, childRouter);
26
+ console.log('[BINHEND] Mapping routes from:', path);
27
+ });
28
+ }
29
+
30
+ function loadRouter(dirPath) {
31
+ if (isUndefined(dirPath)) {
32
+ console.error(`[BINHEND] Error missing directory path: ${dirPath}.\n`);
33
+ throw new Error('Require directory path to load routes.');
34
+ }
35
+
36
+ const router = express.Router();
37
+ router.use(express.urlencoded({ extended: false }));
38
+ router.use(express.json());
39
+ router.use(parseBasicAuthToken);
40
+
41
+ try {
42
+ mapRoutes(router, resolve(dirPath));
43
+ }
44
+ catch (error) {
45
+ console.error(`[BINHEND] Error mapping routes from: ${resolve(dirPath)}.\n` + error);
46
+ throw error;
47
+ }
48
+
49
+ return router;
50
+ }
51
+
52
+ function loadRoutes(dirPath) {
53
+ const router = loadRouter(dirPath);
54
+ server.use(router);
55
+ }
56
+
57
+ module.exports = { loadRouter, loadRoutes, mapRoutes };
@@ -0,0 +1,9 @@
1
+
2
+ const HttpError = require('./HttpError');
3
+
4
+ function responseErrorByDefault(error, response, next) {
5
+ if (!(error instanceof HttpError)) return next(error);
6
+ response.status(error.httpCode || 500).json({ error: error.message || 'Internal Server Error' });
7
+ }
8
+
9
+ module.exports = responseErrorByDefault;
@@ -0,0 +1,21 @@
1
+
2
+ const responseErrorByDefault = require('./responseErrorByDefault');
3
+
4
+ function trycatch(callback) {
5
+ if (callback?.isAppliedTryCatch) return callback;
6
+
7
+ var trycatchCallback = async function(request, response, next) {
8
+ try {
9
+ return await callback(request, response, next);
10
+ }
11
+ catch (error) {
12
+ responseErrorByDefault(error, response, next);
13
+ }
14
+ };
15
+
16
+ trycatchCallback.isAppliedTryCatch = true;
17
+
18
+ return trycatchCallback;
19
+ }
20
+
21
+ module.exports = trycatch;
package/src/binh.js CHANGED
@@ -2,7 +2,7 @@
2
2
  const typeOf = require('./utils/typeOf');
3
3
  const security = require('./security');
4
4
  const { server } = require('./server');
5
- const { loadRoutes, mapRoutes, trycatch, HttpError, Router } = require('./api');
5
+ const { loadRouter, loadRoutes, mapRoutes, trycatch, Router, HttpError } = require('./api');
6
6
  const { ConfigLoader } = require('./configuration');
7
7
  const { WebBuilder } = require('./web');
8
8
  const { binh } = require('./web/component.method');
@@ -14,7 +14,7 @@ module.exports = {
14
14
  typeOf,
15
15
  security,
16
16
  server,
17
- loadRoutes, mapRoutes, trycatch, HttpError, Router,
17
+ loadRouter, loadRoutes, mapRoutes, trycatch, Router, HttpError,
18
18
  ConfigLoader,
19
19
  WebBuilder,
20
20
  binh,
File without changes