nfkit 1.0.26 → 1.0.28

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 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,
@@ -1286,7 +1288,6 @@ var makeArray = (value) => {
1286
1288
  };
1287
1289
 
1288
1290
  // src/app-context/app-context.ts
1289
- var ProvidePrefix = "provide:";
1290
1291
  var getMethodDescriptor = (cls, key) => {
1291
1292
  let proto = cls.prototype;
1292
1293
  while (proto && proto !== Object.prototype) {
@@ -1300,7 +1301,6 @@ var AppContextCore = class {
1300
1301
  constructor() {
1301
1302
  this.provideRecords = [];
1302
1303
  this.registry = /* @__PURE__ */ new Map();
1303
- this.loadSeq = [];
1304
1304
  this.objectSteps = [];
1305
1305
  this.started = false;
1306
1306
  }
@@ -1310,12 +1310,10 @@ var AppContextCore = class {
1310
1310
  const options = hasOptions ? last : void 0;
1311
1311
  const _args = hasOptions ? args.slice(0, -1) : args;
1312
1312
  const classRef = cls;
1313
- const provideKey = options?.provide ? ProvidePrefix + String(options.provide) : void 0;
1314
- const factory = () => options?.useValue ?? (options?.useFactory ? options.useFactory(this, ..._args) : new (options?.useClass ?? cls)(this, ..._args));
1313
+ const factory = (ctx) => options?.useValue ?? (options?.useFactory ? options.useFactory(ctx, ..._args) : new (options?.useClass ?? cls)(ctx, ..._args));
1315
1314
  this.provideRecords.push({
1316
1315
  classRef,
1317
- factory,
1318
- provideKey
1316
+ factory
1319
1317
  });
1320
1318
  if (options?.provide) {
1321
1319
  const prop = options.provide;
@@ -1390,6 +1388,11 @@ var AppContextCore = class {
1390
1388
  use(...ctxes) {
1391
1389
  for (const ctx of ctxes) {
1392
1390
  const other = ctx;
1391
+ if (this.started && !other?.started) {
1392
+ throw new Error(
1393
+ "Cannot use an unstarted context into a started context."
1394
+ );
1395
+ }
1393
1396
  if (Array.isArray(other?.provideRecords)) {
1394
1397
  this.provideRecords.push(...other.provideRecords);
1395
1398
  }
@@ -1400,9 +1403,6 @@ var AppContextCore = class {
1400
1403
  }
1401
1404
  }
1402
1405
  if (other?.started) {
1403
- if (Array.isArray(other?.loadSeq)) {
1404
- this.loadSeq.push(...other.loadSeq);
1405
- }
1406
1406
  if (other?.registry instanceof Map) {
1407
1407
  for (const [key, value] of other.registry.entries()) {
1408
1408
  this.registry.set(key, value);
@@ -1419,24 +1419,26 @@ var AppContextCore = class {
1419
1419
  if (this.started) {
1420
1420
  return this;
1421
1421
  }
1422
+ const startedEntries = [];
1423
+ const preloadedKeys = new Set(this.registry.keys());
1422
1424
  for (const record of this.provideRecords) {
1423
- const inst = record.factory();
1425
+ if (preloadedKeys.has(record.classRef)) {
1426
+ continue;
1427
+ }
1428
+ const inst = record.factory(this);
1424
1429
  const entry = {
1425
1430
  classRef: record.classRef,
1426
1431
  inst
1427
1432
  };
1428
1433
  this.registry.set(record.classRef, entry);
1429
- if (record.provideKey) {
1430
- this.registry.set(record.provideKey, entry);
1431
- }
1432
- this.loadSeq.push(entry);
1434
+ startedEntries.push(entry);
1433
1435
  }
1434
- for (const entry of this.loadSeq) {
1436
+ for (const entry of startedEntries) {
1435
1437
  if (entry.inst && typeof entry.inst.then === "function") {
1436
1438
  entry.inst = await entry.inst;
1437
1439
  }
1438
1440
  }
1439
- for (const entry of this.loadSeq) {
1441
+ for (const entry of startedEntries) {
1440
1442
  const inst = entry.inst;
1441
1443
  if (inst && typeof inst.init === "function") {
1442
1444
  await inst.init();
@@ -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,