binhend 2.1.28 → 2.1.30

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.28",
3
+ "version": "2.1.30",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "author": "Nguyen Duc Binh",
@@ -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, isString } = 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
  };
@@ -35,24 +27,6 @@ function ConfigLoader(configObject, { rootPath } = {}) {
35
27
  return this.object(cli(), options);
36
28
  };
37
29
 
38
- this.json = (filepath, options = {}) => {
39
- let filePath = path.resolve(rootPath, filepath);
40
- 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
- return this.object(jsonObject, options);
54
- };
55
-
56
30
  this.file = (filepath, options) => {
57
31
  let filePath = path.resolve(rootPath, filepath);
58
32
  return this.object(file(filePath, options?.encoding), options);
@@ -60,7 +34,6 @@ function ConfigLoader(configObject, { rootPath } = {}) {
60
34
  }
61
35
 
62
36
  ConfigLoader.cli = cli;
63
- ConfigLoader.json = json;
64
37
  ConfigLoader.file = file;
65
38
  ConfigLoader.require = require.main.require;
66
39
 
@@ -78,33 +51,60 @@ function filter(object, filterKeys) {
78
51
  return filtered;
79
52
  };
80
53
 
81
- function json(path) {
82
- try {
83
- // return require.main.require(path);
84
- return ConfigLoader.require(path);
54
+ function assign(config, filtered, overwrite) {
55
+ if (overwrite !== false) {
56
+ Object.assign(config, filtered);
85
57
  }
86
- catch (e) {
87
- return failed(e, path);
58
+ else for (var key in filtered) {
59
+ if (isNullish(config[key])) {
60
+ config[key] = filtered[key];
61
+ }
88
62
  }
89
- }
63
+ return config;
64
+ };
65
+
66
+ function remap(object, use) {
67
+ if (!isString(use)) return object;
68
+
69
+ for (var key in object) {
70
+ const value = object[key];
71
+ const hasValue = isObject(value) && value.hasOwnProperty(use);
72
+ if (hasValue) object[key] = value[use];
73
+ // else if value is not an object or has no "env" key, then the desired value is itself => no new assignment
74
+ }
75
+
76
+ return object;
77
+ };
90
78
 
91
79
  function cli() {
92
80
  return processKeyValueStrings(process.argv);
93
81
  }
94
82
 
95
- function file(path, encoding) {
83
+ function file(filepath, encoding) {
96
84
  encoding = encoding || 'utf8';
85
+
97
86
  try {
98
- var source = fs.readFileSync(path, { encoding });
99
- // @ts-ignore
100
- source = source.replace(/\r\n?/mg, '\n');
101
- // @ts-ignore
102
- var lines = source.split('\n');
87
+ var fileExtension = path.parse(filepath).ext;
103
88
 
104
- return processKeyValueStrings(lines);
89
+ switch (fileExtension) {
90
+ case '.js':
91
+ case '.json':
92
+ return ConfigLoader.require(filepath);
93
+
94
+ default:
95
+ var source = fs.readFileSync(filepath, { encoding });
96
+ // @ts-ignore
97
+ source = source.replace(/\r\n?/mg, '\n');
98
+ // @ts-ignore
99
+ var lines = source.split('\n');
100
+
101
+ return processKeyValueStrings(lines);
102
+ }
105
103
  }
106
- catch (e) {
107
- return failed(e, path);
104
+ catch (error) {
105
+ console.log(`[BINHEND] Failed loading config from: ${filepath}`);
106
+ console.log('[BINHEND] Details:', error);
107
+ return {};
108
108
  }
109
109
  }
110
110
 
@@ -141,10 +141,4 @@ function parseKeyValue(string) {
141
141
  return { key, value, success: true };
142
142
  }
143
143
 
144
- function failed(error, source) {
145
- console.log(`[BINHEND] Failed loading config from: ${source}`);
146
- console.log('[BINHEND] Details:', error);
147
- return {};
148
- };
149
-
150
144
  module.exports = { ConfigLoader, config: {} };
@@ -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
  });