@anjianshi/utils 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.
@@ -38,7 +38,9 @@ export declare class EnvReader {
38
38
  batchGet<Defs extends Record<string, EnvValue>>(definitions: Defs): Defs;
39
39
  /**
40
40
  * 同 envReader.getByType(),只不过是通过对象指定各 env 的类型来批量获取。
41
- * 若 required 为 true,要求所有 env 都必须有值,否则会抛出异常;默认为 false。
41
+ *
42
+ * - required=false(默认)时,不存在或值为 undefined 的 env 不会出现在返回对象里,以保证 { ...defaults, ...envReader.batchGetByType(...) } 的用法能正常保留默认值。
43
+ * - required=true 时要求所有 env 都必须有值,否则会抛出异常
42
44
  *
43
45
  * envReader.batchGetByType({
44
46
  * port: 'number',
@@ -68,9 +68,10 @@ export class EnvReader {
68
68
  const result = {};
69
69
  for (const [key, def] of Object.entries(definitions)) {
70
70
  const value = typeof def === 'string' ? this.getByType(key, def) : this.getByType(key, 'json');
71
- if (value === undefined && required)
71
+ if (value !== undefined)
72
+ result[key] = value;
73
+ else if (required)
72
74
  throw new Error(`env ${key} needs a value`);
73
- result[key] = value;
74
75
  }
75
76
  return result;
76
77
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anjianshi/utils",
3
- "version": "2.3.8",
3
+ "version": "2.3.9",
4
4
  "description": "Common JavaScript Utils",
5
5
  "homepage": "https://github.com/anjianshi/js-packages/utils",
6
6
  "bugs": {
@@ -26,11 +26,11 @@
26
26
  "dotenv": "^16.4.5",
27
27
  "typescript": "^5.5.4",
28
28
  "vconsole": "^3.15.1",
29
- "@anjianshi/presets-eslint-node": "4.0.8",
30
- "@anjianshi/presets-eslint-react": "4.0.7",
31
29
  "@anjianshi/presets-eslint-typescript": "5.0.5",
30
+ "@anjianshi/presets-eslint-node": "4.0.8",
32
31
  "@anjianshi/presets-prettier": "3.0.1",
33
- "@anjianshi/presets-typescript": "3.2.2"
32
+ "@anjianshi/presets-typescript": "3.2.2",
33
+ "@anjianshi/presets-eslint-react": "4.0.7"
34
34
  },
35
35
  "peerDependencies": {
36
36
  "@emotion/react": "^11.13.3",