binhend 2.3.8 → 2.3.9

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/index.js CHANGED
@@ -11,7 +11,7 @@ const path = require('path');
11
11
 
12
12
  //___ Run scripts ___//
13
13
  const moduleAlias = require('./packages/module-alias'); // path alias '@' for requiring modules: require('@/any/path') - use jsconfig.json
14
- moduleAlias.path('@binhend/*', [ path.join(__dirname, './packages') ]); // set path alias '@binhend' for all packages to call each others when 'npm install' in another project
14
+ moduleAlias.path('@binhend/*', [path.join(__dirname, './packages')]); // set path alias '@binhend' for all packages to call each others when 'npm install' in another project
15
15
 
16
16
 
17
17
  //___ Load packages ___//
@@ -30,7 +30,7 @@ const csrf = require('./packages/middlewares/src/csrf');
30
30
  const trycatch = require('./packages/middlewares/src/trycatch');
31
31
  const { Auth } = require('./packages/middlewares/src/authorize');
32
32
 
33
- const { config, ConfigLoader } = require('./packages/config/src/configuration');
33
+ const { config, ConfigLoader, env } = require('./packages/config');
34
34
 
35
35
  const CSD = require('./packages/csd/src/csd');
36
36
  const CinCSD = require('./packages/csd/src/controller');
@@ -53,7 +53,7 @@ module.exports = {
53
53
 
54
54
  cors, csrf, trycatch, Auth,
55
55
 
56
- config, ConfigLoader,
56
+ env, config, ConfigLoader,
57
57
 
58
58
  CSD, CinCSD, SinCSD, DinCSD, // only use CSD, others for exposing methods (via IDE auto-suggestion)
59
59
 
package/jsconfig.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "compilerOptions": {
3
3
  "forceConsistentCasingInFileNames": true,
4
4
  "moduleResolution": "node",
5
- "target": "ES2017",
5
+ "target": "ES2022",
6
6
  "checkJs": true,
7
7
  "allowJs": true,
8
8
  "noEmit": true,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "binhend",
3
- "version": "2.3.8",
3
+ "version": "2.3.9",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "author": "Nguyen Duc Binh",
@@ -1,3 +1,4 @@
1
1
  module.exports = {
2
2
  ...require('./src/configuration'),
3
+ ...require('./src/process.env')
3
4
  };
@@ -0,0 +1,32 @@
1
+
2
+ const path = require('path');
3
+ const { cli } = require('./configuration').ConfigLoader;
4
+ const { String } = require('@binhend/validation');
5
+
6
+ function env(env = String(), rootPath = String()) {
7
+ rootPath = String(rootPath, { default: require.main.path });
8
+
9
+ const envExtension = typeof env === 'string' ? `.${env}` : '';
10
+ const configModuleFileName = `./env${envExtension}`;
11
+ const configModuleFilePath = path.resolve(rootPath, configModuleFileName);
12
+
13
+ try {
14
+ const localConfigs = require(configModuleFilePath);
15
+
16
+ for (var key in localConfigs) {
17
+ const value = localConfigs[key];
18
+ localConfigs[key] = typeof value === 'string' ? value : JSON.stringify(value);
19
+ }
20
+
21
+ return {
22
+ ...localConfigs,
23
+ ...process.env,
24
+ ...cli()
25
+ };
26
+ }
27
+ catch (error) {
28
+ throw new Error(`[BINHEND][CONFIG] Failed loading config from: ${configModuleFilePath}`, { cause: error });
29
+ }
30
+ };
31
+
32
+ module.exports = { env };
@@ -1,7 +1,7 @@
1
1
 
2
2
  class HttpError extends Error {
3
- constructor(httpCode, message) {
4
- super(message);
3
+ constructor(httpCode, message, options = {}) {
4
+ super(message, options);
5
5
  this.name = 'HttpError';
6
6
  this.httpCode = httpCode;
7
7
  }