nfkit 1.0.27 → 1.0.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/dist/index.cjs +156 -0
- package/dist/index.cjs.map +3 -3
- package/dist/index.d.ts +1 -0
- package/dist/index.mjs +154 -0
- package/dist/index.mjs.map +3 -3
- package/dist/src/configurer/configurer.d.ts +23 -0
- package/dist/src/configurer/index.d.ts +1 -0
- package/index.ts +2 -0
- package/package.json +1 -1
- package/tsconfig.json +2 -1
package/dist/index.cjs
CHANGED
|
@@ -22,6 +22,8 @@ __export(index_exports, {
|
|
|
22
22
|
AbortedError: () => AbortedError,
|
|
23
23
|
AppContextCore: () => AppContextCore,
|
|
24
24
|
AppServiceBase: () => AppServiceBase,
|
|
25
|
+
Configurer: () => Configurer,
|
|
26
|
+
ConfigurerInstance: () => ConfigurerInstance,
|
|
25
27
|
DUAL_PENDING: () => DUAL_PENDING,
|
|
26
28
|
DynamicMiddlewareDispatcher: () => DynamicMiddlewareDispatcher,
|
|
27
29
|
I18n: () => I18n,
|
|
@@ -1456,11 +1458,165 @@ var AppServiceBase = class {
|
|
|
1456
1458
|
async init() {
|
|
1457
1459
|
}
|
|
1458
1460
|
};
|
|
1461
|
+
|
|
1462
|
+
// src/configurer/configurer.ts
|
|
1463
|
+
var ConfigurerInstance = class {
|
|
1464
|
+
constructor(config, defaultConfig) {
|
|
1465
|
+
this.config = config;
|
|
1466
|
+
this.defaultConfig = defaultConfig;
|
|
1467
|
+
}
|
|
1468
|
+
getString(key) {
|
|
1469
|
+
return this.config[key] || this.defaultConfig[key];
|
|
1470
|
+
}
|
|
1471
|
+
getInt(key) {
|
|
1472
|
+
return parseInt(this.getString(key));
|
|
1473
|
+
}
|
|
1474
|
+
getFloat(key) {
|
|
1475
|
+
return parseFloat(this.getString(key));
|
|
1476
|
+
}
|
|
1477
|
+
getBoolean(key) {
|
|
1478
|
+
const defaultBoolean = parseConfigBoolean(this.defaultConfig[key], false);
|
|
1479
|
+
return parseConfigBoolean(this.getString(key), defaultBoolean);
|
|
1480
|
+
}
|
|
1481
|
+
getStringArray(key) {
|
|
1482
|
+
return convertStringArray(this.getString(key));
|
|
1483
|
+
}
|
|
1484
|
+
getIntArray(key) {
|
|
1485
|
+
return convertIntArray(this.getString(key));
|
|
1486
|
+
}
|
|
1487
|
+
getFloatArray(key) {
|
|
1488
|
+
return convertFloatArray(this.getString(key));
|
|
1489
|
+
}
|
|
1490
|
+
getBooleanArray(key) {
|
|
1491
|
+
const defaultBoolean = parseConfigBoolean(this.defaultConfig[key], false);
|
|
1492
|
+
return convertBooleanArray(this.getString(key), defaultBoolean);
|
|
1493
|
+
}
|
|
1494
|
+
};
|
|
1495
|
+
var Configurer = class {
|
|
1496
|
+
constructor(defaultConfig) {
|
|
1497
|
+
this.defaultConfig = defaultConfig;
|
|
1498
|
+
}
|
|
1499
|
+
loadConfig(options = {}) {
|
|
1500
|
+
const readConfig = options?.obj && typeof options.obj === "object" ? options.obj : {};
|
|
1501
|
+
const normalizedConfig = normalizeConfigByDefaultKeys(
|
|
1502
|
+
readConfig,
|
|
1503
|
+
this.defaultConfig
|
|
1504
|
+
);
|
|
1505
|
+
return new ConfigurerInstance(
|
|
1506
|
+
{
|
|
1507
|
+
...this.defaultConfig,
|
|
1508
|
+
...normalizedConfig,
|
|
1509
|
+
...options?.env || {}
|
|
1510
|
+
},
|
|
1511
|
+
this.defaultConfig
|
|
1512
|
+
);
|
|
1513
|
+
}
|
|
1514
|
+
generateExampleObject() {
|
|
1515
|
+
return Object.fromEntries(
|
|
1516
|
+
Object.entries(this.defaultConfig).map(([key, value]) => {
|
|
1517
|
+
if (value.includes(",")) {
|
|
1518
|
+
return [
|
|
1519
|
+
toCamelCaseKey(key),
|
|
1520
|
+
value.split(",").map((v) => toTypedValue(v))
|
|
1521
|
+
];
|
|
1522
|
+
}
|
|
1523
|
+
return [toCamelCaseKey(key), toTypedValue(value)];
|
|
1524
|
+
})
|
|
1525
|
+
);
|
|
1526
|
+
}
|
|
1527
|
+
};
|
|
1528
|
+
function toCamelCaseKey(key) {
|
|
1529
|
+
const lower = key.toLowerCase();
|
|
1530
|
+
return lower.replace(/_([a-z0-9])/g, (_, ch) => ch.toUpperCase());
|
|
1531
|
+
}
|
|
1532
|
+
function normalizeConfigValue(value) {
|
|
1533
|
+
if (value == null) {
|
|
1534
|
+
return void 0;
|
|
1535
|
+
}
|
|
1536
|
+
if (typeof value === "string") {
|
|
1537
|
+
return value;
|
|
1538
|
+
}
|
|
1539
|
+
if (typeof value === "number") {
|
|
1540
|
+
return value.toString();
|
|
1541
|
+
}
|
|
1542
|
+
if (typeof value === "boolean") {
|
|
1543
|
+
return value ? "1" : "0";
|
|
1544
|
+
}
|
|
1545
|
+
if (Array.isArray(value)) {
|
|
1546
|
+
return value.map((item) => normalizeArrayItem(item)).join(",");
|
|
1547
|
+
}
|
|
1548
|
+
return String(value);
|
|
1549
|
+
}
|
|
1550
|
+
function normalizeArrayItem(value) {
|
|
1551
|
+
if (typeof value === "string") {
|
|
1552
|
+
return value;
|
|
1553
|
+
}
|
|
1554
|
+
if (typeof value === "number") {
|
|
1555
|
+
return value.toString();
|
|
1556
|
+
}
|
|
1557
|
+
if (typeof value === "boolean") {
|
|
1558
|
+
return value ? "1" : "0";
|
|
1559
|
+
}
|
|
1560
|
+
return String(value);
|
|
1561
|
+
}
|
|
1562
|
+
function normalizeConfigByDefaultKeys(readConfig, defaultConfig) {
|
|
1563
|
+
const normalizedConfig = {};
|
|
1564
|
+
for (const key of Object.keys(defaultConfig)) {
|
|
1565
|
+
const rawKey = key;
|
|
1566
|
+
const camelKey = toCamelCaseKey(rawKey);
|
|
1567
|
+
const value = readConfig[camelKey] !== void 0 ? readConfig[camelKey] : readConfig[rawKey];
|
|
1568
|
+
const normalized = normalizeConfigValue(value);
|
|
1569
|
+
if (normalized !== void 0) {
|
|
1570
|
+
normalizedConfig[key] = normalized;
|
|
1571
|
+
}
|
|
1572
|
+
}
|
|
1573
|
+
return normalizedConfig;
|
|
1574
|
+
}
|
|
1575
|
+
function parseConfigBoolean(value, defaultValue = false) {
|
|
1576
|
+
if (typeof value === "boolean") {
|
|
1577
|
+
return value;
|
|
1578
|
+
}
|
|
1579
|
+
if (typeof value === "number") {
|
|
1580
|
+
return value !== 0;
|
|
1581
|
+
}
|
|
1582
|
+
if (typeof value === "string") {
|
|
1583
|
+
const normalized = value.trim().toLowerCase();
|
|
1584
|
+
if (defaultValue) {
|
|
1585
|
+
return !(normalized === "0" || normalized === "false" || normalized === "null");
|
|
1586
|
+
}
|
|
1587
|
+
return !(normalized === "" || normalized === "0" || normalized === "false" || normalized === "null");
|
|
1588
|
+
}
|
|
1589
|
+
if (value == null) {
|
|
1590
|
+
return defaultValue;
|
|
1591
|
+
}
|
|
1592
|
+
return Boolean(value);
|
|
1593
|
+
}
|
|
1594
|
+
function convertStringArray(str) {
|
|
1595
|
+
return str?.split(",").map((s) => s.trim()).filter((s) => s) || [];
|
|
1596
|
+
}
|
|
1597
|
+
function convertIntArray(str) {
|
|
1598
|
+
return str?.split(",").map((s) => parseInt(s.trim())).filter((n) => !isNaN(n)) || [];
|
|
1599
|
+
}
|
|
1600
|
+
function convertFloatArray(str) {
|
|
1601
|
+
return str?.split(",").map((s) => parseFloat(s.trim())).filter((n) => !isNaN(n)) || [];
|
|
1602
|
+
}
|
|
1603
|
+
function convertBooleanArray(str, defaultValue = false) {
|
|
1604
|
+
return str?.split(",").map((s) => parseConfigBoolean(s.trim(), defaultValue)).filter((item) => typeof item === "boolean") || [];
|
|
1605
|
+
}
|
|
1606
|
+
function toTypedValue(value) {
|
|
1607
|
+
const trimmed = value.trim();
|
|
1608
|
+
if (/^\d+$/.test(trimmed)) {
|
|
1609
|
+
return Number.parseInt(trimmed, 10);
|
|
1610
|
+
}
|
|
1611
|
+
return trimmed;
|
|
1612
|
+
}
|
|
1459
1613
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1460
1614
|
0 && (module.exports = {
|
|
1461
1615
|
AbortedError,
|
|
1462
1616
|
AppContextCore,
|
|
1463
1617
|
AppServiceBase,
|
|
1618
|
+
Configurer,
|
|
1619
|
+
ConfigurerInstance,
|
|
1464
1620
|
DUAL_PENDING,
|
|
1465
1621
|
DynamicMiddlewareDispatcher,
|
|
1466
1622
|
I18n,
|