binhend 2.1.27 → 2.1.29

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.1.27",
3
+ "version": "2.1.29",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "author": "Nguyen Duc Binh",
package/src/api/routes.js CHANGED
@@ -71,9 +71,9 @@ async function buildRoutes(dirPath) {
71
71
  * @param {Object} corsOptions - CORS configuration options
72
72
  */
73
73
  async function loadRoutes(dirPath, corsOptions) {
74
- server.use(cors(corsOptions));
74
+ server().use(cors(corsOptions));
75
75
  const router = await buildRoutes(dirPath);
76
- server.use(router);
76
+ server().use(router);
77
77
  }
78
78
 
79
79
  const routes = {
@@ -1,6 +1,6 @@
1
1
  const fs = require('fs');
2
2
  const path = require('path');
3
- const { isEmptyArray, isArray, isObject, mustString, isNullish } = require('./utils/types');
3
+ const { isEmptyArray, isArray, isObject, mustString, isNullish, isEmptyString } = require('./utils/types');
4
4
 
5
5
  // @ts-ignore
6
6
  function ConfigLoader(configObject, { rootPath } = {}) {
@@ -13,20 +13,12 @@ function ConfigLoader(configObject, { rootPath } = {}) {
13
13
  };
14
14
 
15
15
  // @ts-ignore
16
- this.object = (object, { key, filter: filters, overwrite } = {}) => {
16
+ this.object = (object, { key, filter: filters, overwrite, use } = {}) => {
17
17
  let config = getConfigPosition(key);
18
18
  let filtered = isArray(filters) ? filter(object, filters) : object;
19
19
 
20
- if (overwrite === false) {
21
- for (let key in filtered) {
22
- if (isNullish(config[key])) {
23
- config[key] = filtered[key];
24
- }
25
- }
26
- }
27
- else {
28
- Object.assign(config, filtered);
29
- }
20
+ remap(object, use);
21
+ assign(config, filtered, overwrite);
30
22
 
31
23
  return this;
32
24
  };
@@ -38,18 +30,6 @@ function ConfigLoader(configObject, { rootPath } = {}) {
38
30
  this.json = (filepath, options = {}) => {
39
31
  let filePath = path.resolve(rootPath, filepath);
40
32
  let jsonObject = json(filePath);
41
-
42
- if (options.hasOwnProperty('env')) {
43
- let env = options.env;
44
- for (let key in jsonObject) {
45
- let value = jsonObject[key];
46
- if (isObject(value) && value.hasOwnProperty(env)) {
47
- // if object has no "env" key, it means the intended value is the whole object => no new assignment
48
- jsonObject[key] = value[env];
49
- }
50
- }
51
- }
52
-
53
33
  return this.object(jsonObject, options);
54
34
  };
55
35
 
@@ -78,6 +58,31 @@ function filter(object, filterKeys) {
78
58
  return filtered;
79
59
  };
80
60
 
61
+ function assign(config, filtered, overwrite) {
62
+ if (overwrite !== false) {
63
+ Object.assign(config, filtered);
64
+ }
65
+ else for (var key in filtered) {
66
+ if (isNullish(config[key])) {
67
+ config[key] = filtered[key];
68
+ }
69
+ }
70
+ return config;
71
+ };
72
+
73
+ function remap(object, use) {
74
+ if (isEmptyString(use)) return object;
75
+
76
+ for (var key in object) {
77
+ const value = object[key];
78
+ const hasValue = isObject(value) && value.hasOwnProperty(use);
79
+ if (hasValue) object[key] = value[use];
80
+ // else if value is not an object or has no "env" key, then the desired value is itself => no new assignment
81
+ }
82
+
83
+ return object;
84
+ };
85
+
81
86
  function json(path) {
82
87
  try {
83
88
  // return require.main.require(path);
@@ -3,7 +3,7 @@ module.exports = (request, response, next) => {
3
3
  const header = request.headers.cookie;
4
4
  const cookies = {};
5
5
 
6
- header.split(';').forEach(cookie => {
6
+ if (header) header.split(';').forEach(cookie => {
7
7
  const [key, ...val] = cookie.trim().split('=');
8
8
  cookies[key] = decodeURIComponent(val.join('='));
9
9
  });
package/src/web/index.js CHANGED
@@ -45,8 +45,8 @@ function WebBuilder(source, { output, external } = {}) {
45
45
  };
46
46
 
47
47
  setTimeout(() => {
48
- server.use(express.static(web));
49
- server.get('/*', (req, res) => res.sendFile('index.html', { root: web }));
48
+ server().use(express.static(web));
49
+ server().get('/*', (req, res) => res.sendFile('index.html', { root: web }));
50
50
  });
51
51
 
52
52
  binh.webModulePath = module;