binhend 2.0.4 → 2.0.5
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 +1 -1
- package/src/web/component.method.js +3 -1
- package/src/web/index.js +1 -0
- package/src/z_api.js +0 -133
package/package.json
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
|
|
2
2
|
const path = require('path');
|
|
3
|
-
const
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const UglifyCSS = require('uglifycss');
|
|
4
5
|
const Component = require('./component');
|
|
6
|
+
const { isString } = require('../utils/typeOf');
|
|
5
7
|
|
|
6
8
|
const binh = {};
|
|
7
9
|
|
package/src/web/index.js
CHANGED
|
@@ -4,6 +4,7 @@ const { isEmptyString, isString } = require('../utils/typeOf');
|
|
|
4
4
|
const ComponentFormat = require('./component.format');
|
|
5
5
|
const ComponentBuild = require('./component.build');
|
|
6
6
|
const { server } = require('../server');
|
|
7
|
+
const express = require('express');
|
|
7
8
|
|
|
8
9
|
function WebBuilder(source, { output } = {}) {
|
|
9
10
|
|
package/src/z_api.js
DELETED
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
const { Module } = require('module');
|
|
2
|
-
const { readdirSync, statSync } = require('fs');
|
|
3
|
-
const { join, parse, resolve } = require('path');
|
|
4
|
-
const { isUndefined, isFunction } = require('./utils/typeOf');
|
|
5
|
-
const { ServerResponse } = require('http');
|
|
6
|
-
|
|
7
|
-
const express = require('express');
|
|
8
|
-
const { server } = require('./server');
|
|
9
|
-
const parseBasicAuthToken = require('./middleware/parseBasicAuthToken');
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
function mapRoutes(router, dirpath) {
|
|
13
|
-
readdirSync(dirpath).forEach(function(direntName) {
|
|
14
|
-
var path = join(dirpath, direntName),
|
|
15
|
-
route = parse(direntName).name;
|
|
16
|
-
|
|
17
|
-
var stat = statSync(path);
|
|
18
|
-
var childRouter;
|
|
19
|
-
|
|
20
|
-
if (stat.isFile()) {
|
|
21
|
-
childRouter = require(path);
|
|
22
|
-
}
|
|
23
|
-
else if (stat.isDirectory()) {
|
|
24
|
-
childRouter = Router().router;
|
|
25
|
-
mapRoutes(childRouter, path);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
router.use(`/${route}`, childRouter);
|
|
29
|
-
console.log('[BINHEND] Mapping routes from:', path);
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function loadRoutes(dirPath) {
|
|
34
|
-
if (isUndefined(dirPath)) {
|
|
35
|
-
console.error(`[BINHEND] Error missing directory path: ${dirPath}.\n`);
|
|
36
|
-
throw new Error('Require directory path to load routes.');
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
const router = express.Router();
|
|
40
|
-
router.use(express.urlencoded({ extended: false }));
|
|
41
|
-
router.use(express.json());
|
|
42
|
-
router.use(parseBasicAuthToken);
|
|
43
|
-
|
|
44
|
-
try {
|
|
45
|
-
mapRoutes(router, resolve(dirPath));
|
|
46
|
-
}
|
|
47
|
-
catch (error) {
|
|
48
|
-
console.error(`[BINHEND] Error mapping routes from: ${resolve(dirPath)}.\n` + error);
|
|
49
|
-
throw error;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
server.use(router);
|
|
53
|
-
|
|
54
|
-
return router;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
function Router(moduleInstance) {
|
|
58
|
-
var router = express.Router();
|
|
59
|
-
|
|
60
|
-
var output = { router };
|
|
61
|
-
|
|
62
|
-
for (var key in router) {
|
|
63
|
-
let method = router[key]; // use 'let' to not lose reference, 'var' will override 'method' reference in this function scope => error on run-time
|
|
64
|
-
|
|
65
|
-
if (!isFunction(method)) continue;
|
|
66
|
-
|
|
67
|
-
output[key] = function() {
|
|
68
|
-
let args = [];
|
|
69
|
-
|
|
70
|
-
Array.from(arguments).forEach((arg) => {
|
|
71
|
-
if (!isFunction(arg)) return args.push(arg);
|
|
72
|
-
args.push(createErrorHandler(router, arg));
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
return method.apply(router, args);
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
if (moduleInstance instanceof Module) {
|
|
80
|
-
moduleInstance.exports = router;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
output.trycatch = trycatch;
|
|
84
|
-
|
|
85
|
-
return output;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
function createErrorHandler(router, callback) {
|
|
89
|
-
if (callback.isAppliedTryCatch) return callback;
|
|
90
|
-
|
|
91
|
-
return async function(request, response, next) {
|
|
92
|
-
try {
|
|
93
|
-
return await callback.apply(router, arguments);
|
|
94
|
-
}
|
|
95
|
-
catch (error) {
|
|
96
|
-
if (arguments.length < 3 || !(response instanceof ServerResponse)) throw error;
|
|
97
|
-
responseErrorByDefault(error, response, next);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
function trycatch(callback) {
|
|
103
|
-
var trycatchCallback = async function(request, response, next) {
|
|
104
|
-
try {
|
|
105
|
-
return await callback(request, response, next);
|
|
106
|
-
}
|
|
107
|
-
catch (error) {
|
|
108
|
-
responseErrorByDefault(error, response, next);
|
|
109
|
-
}
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
trycatchCallback.isAppliedTryCatch = true;
|
|
113
|
-
|
|
114
|
-
return trycatchCallback;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
function responseErrorByDefault(error, response, next) {
|
|
118
|
-
if (!(error instanceof HttpError)) return next(error);
|
|
119
|
-
response.status(error.httpCode || 500).json({ error: error.message || 'Internal Server Error' });
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
class HttpError extends Error {
|
|
123
|
-
constructor(httpCode, message) {
|
|
124
|
-
super(message);
|
|
125
|
-
this.name = 'HttpError';
|
|
126
|
-
this.httpCode = httpCode;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
module.exports = {
|
|
131
|
-
loadRoutes, mapRoutes,
|
|
132
|
-
trycatch, HttpError, Router
|
|
133
|
-
};
|