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.mjs CHANGED
@@ -1240,7 +1240,6 @@ var makeArray = (value) => {
1240
1240
  };
1241
1241
 
1242
1242
  // src/app-context/app-context.ts
1243
- var ProvidePrefix = "provide:";
1244
1243
  var getMethodDescriptor = (cls, key) => {
1245
1244
  let proto = cls.prototype;
1246
1245
  while (proto && proto !== Object.prototype) {
@@ -1254,7 +1253,6 @@ var AppContextCore = class {
1254
1253
  constructor() {
1255
1254
  this.provideRecords = [];
1256
1255
  this.registry = /* @__PURE__ */ new Map();
1257
- this.loadSeq = [];
1258
1256
  this.objectSteps = [];
1259
1257
  this.started = false;
1260
1258
  }
@@ -1264,12 +1262,10 @@ var AppContextCore = class {
1264
1262
  const options = hasOptions ? last : void 0;
1265
1263
  const _args = hasOptions ? args.slice(0, -1) : args;
1266
1264
  const classRef = cls;
1267
- const provideKey = options?.provide ? ProvidePrefix + String(options.provide) : void 0;
1268
- const factory = () => options?.useValue ?? (options?.useFactory ? options.useFactory(this, ..._args) : new (options?.useClass ?? cls)(this, ..._args));
1265
+ const factory = (ctx) => options?.useValue ?? (options?.useFactory ? options.useFactory(ctx, ..._args) : new (options?.useClass ?? cls)(ctx, ..._args));
1269
1266
  this.provideRecords.push({
1270
1267
  classRef,
1271
- factory,
1272
- provideKey
1268
+ factory
1273
1269
  });
1274
1270
  if (options?.provide) {
1275
1271
  const prop = options.provide;
@@ -1344,6 +1340,11 @@ var AppContextCore = class {
1344
1340
  use(...ctxes) {
1345
1341
  for (const ctx of ctxes) {
1346
1342
  const other = ctx;
1343
+ if (this.started && !other?.started) {
1344
+ throw new Error(
1345
+ "Cannot use an unstarted context into a started context."
1346
+ );
1347
+ }
1347
1348
  if (Array.isArray(other?.provideRecords)) {
1348
1349
  this.provideRecords.push(...other.provideRecords);
1349
1350
  }
@@ -1354,9 +1355,6 @@ var AppContextCore = class {
1354
1355
  }
1355
1356
  }
1356
1357
  if (other?.started) {
1357
- if (Array.isArray(other?.loadSeq)) {
1358
- this.loadSeq.push(...other.loadSeq);
1359
- }
1360
1358
  if (other?.registry instanceof Map) {
1361
1359
  for (const [key, value] of other.registry.entries()) {
1362
1360
  this.registry.set(key, value);
@@ -1373,24 +1371,26 @@ var AppContextCore = class {
1373
1371
  if (this.started) {
1374
1372
  return this;
1375
1373
  }
1374
+ const startedEntries = [];
1375
+ const preloadedKeys = new Set(this.registry.keys());
1376
1376
  for (const record of this.provideRecords) {
1377
- const inst = record.factory();
1377
+ if (preloadedKeys.has(record.classRef)) {
1378
+ continue;
1379
+ }
1380
+ const inst = record.factory(this);
1378
1381
  const entry = {
1379
1382
  classRef: record.classRef,
1380
1383
  inst
1381
1384
  };
1382
1385
  this.registry.set(record.classRef, entry);
1383
- if (record.provideKey) {
1384
- this.registry.set(record.provideKey, entry);
1385
- }
1386
- this.loadSeq.push(entry);
1386
+ startedEntries.push(entry);
1387
1387
  }
1388
- for (const entry of this.loadSeq) {
1388
+ for (const entry of startedEntries) {
1389
1389
  if (entry.inst && typeof entry.inst.then === "function") {
1390
1390
  entry.inst = await entry.inst;
1391
1391
  }
1392
1392
  }
1393
- for (const entry of this.loadSeq) {
1393
+ for (const entry of startedEntries) {
1394
1394
  const inst = entry.inst;
1395
1395
  if (inst && typeof inst.init === "function") {
1396
1396
  await inst.init();
@@ -1410,10 +1410,164 @@ var AppServiceBase = class {
1410
1410
  async init() {
1411
1411
  }
1412
1412
  };
1413
+
1414
+ // src/configurer/configurer.ts
1415
+ var ConfigurerInstance = class {
1416
+ constructor(config, defaultConfig) {
1417
+ this.config = config;
1418
+ this.defaultConfig = defaultConfig;
1419
+ }
1420
+ getString(key) {
1421
+ return this.config[key] || this.defaultConfig[key];
1422
+ }
1423
+ getInt(key) {
1424
+ return parseInt(this.getString(key));
1425
+ }
1426
+ getFloat(key) {
1427
+ return parseFloat(this.getString(key));
1428
+ }
1429
+ getBoolean(key) {
1430
+ const defaultBoolean = parseConfigBoolean(this.defaultConfig[key], false);
1431
+ return parseConfigBoolean(this.getString(key), defaultBoolean);
1432
+ }
1433
+ getStringArray(key) {
1434
+ return convertStringArray(this.getString(key));
1435
+ }
1436
+ getIntArray(key) {
1437
+ return convertIntArray(this.getString(key));
1438
+ }
1439
+ getFloatArray(key) {
1440
+ return convertFloatArray(this.getString(key));
1441
+ }
1442
+ getBooleanArray(key) {
1443
+ const defaultBoolean = parseConfigBoolean(this.defaultConfig[key], false);
1444
+ return convertBooleanArray(this.getString(key), defaultBoolean);
1445
+ }
1446
+ };
1447
+ var Configurer = class {
1448
+ constructor(defaultConfig) {
1449
+ this.defaultConfig = defaultConfig;
1450
+ }
1451
+ loadConfig(options = {}) {
1452
+ const readConfig = options?.obj && typeof options.obj === "object" ? options.obj : {};
1453
+ const normalizedConfig = normalizeConfigByDefaultKeys(
1454
+ readConfig,
1455
+ this.defaultConfig
1456
+ );
1457
+ return new ConfigurerInstance(
1458
+ {
1459
+ ...this.defaultConfig,
1460
+ ...normalizedConfig,
1461
+ ...options?.env || {}
1462
+ },
1463
+ this.defaultConfig
1464
+ );
1465
+ }
1466
+ generateExampleObject() {
1467
+ return Object.fromEntries(
1468
+ Object.entries(this.defaultConfig).map(([key, value]) => {
1469
+ if (value.includes(",")) {
1470
+ return [
1471
+ toCamelCaseKey(key),
1472
+ value.split(",").map((v) => toTypedValue(v))
1473
+ ];
1474
+ }
1475
+ return [toCamelCaseKey(key), toTypedValue(value)];
1476
+ })
1477
+ );
1478
+ }
1479
+ };
1480
+ function toCamelCaseKey(key) {
1481
+ const lower = key.toLowerCase();
1482
+ return lower.replace(/_([a-z0-9])/g, (_, ch) => ch.toUpperCase());
1483
+ }
1484
+ function normalizeConfigValue(value) {
1485
+ if (value == null) {
1486
+ return void 0;
1487
+ }
1488
+ if (typeof value === "string") {
1489
+ return value;
1490
+ }
1491
+ if (typeof value === "number") {
1492
+ return value.toString();
1493
+ }
1494
+ if (typeof value === "boolean") {
1495
+ return value ? "1" : "0";
1496
+ }
1497
+ if (Array.isArray(value)) {
1498
+ return value.map((item) => normalizeArrayItem(item)).join(",");
1499
+ }
1500
+ return String(value);
1501
+ }
1502
+ function normalizeArrayItem(value) {
1503
+ if (typeof value === "string") {
1504
+ return value;
1505
+ }
1506
+ if (typeof value === "number") {
1507
+ return value.toString();
1508
+ }
1509
+ if (typeof value === "boolean") {
1510
+ return value ? "1" : "0";
1511
+ }
1512
+ return String(value);
1513
+ }
1514
+ function normalizeConfigByDefaultKeys(readConfig, defaultConfig) {
1515
+ const normalizedConfig = {};
1516
+ for (const key of Object.keys(defaultConfig)) {
1517
+ const rawKey = key;
1518
+ const camelKey = toCamelCaseKey(rawKey);
1519
+ const value = readConfig[camelKey] !== void 0 ? readConfig[camelKey] : readConfig[rawKey];
1520
+ const normalized = normalizeConfigValue(value);
1521
+ if (normalized !== void 0) {
1522
+ normalizedConfig[key] = normalized;
1523
+ }
1524
+ }
1525
+ return normalizedConfig;
1526
+ }
1527
+ function parseConfigBoolean(value, defaultValue = false) {
1528
+ if (typeof value === "boolean") {
1529
+ return value;
1530
+ }
1531
+ if (typeof value === "number") {
1532
+ return value !== 0;
1533
+ }
1534
+ if (typeof value === "string") {
1535
+ const normalized = value.trim().toLowerCase();
1536
+ if (defaultValue) {
1537
+ return !(normalized === "0" || normalized === "false" || normalized === "null");
1538
+ }
1539
+ return !(normalized === "" || normalized === "0" || normalized === "false" || normalized === "null");
1540
+ }
1541
+ if (value == null) {
1542
+ return defaultValue;
1543
+ }
1544
+ return Boolean(value);
1545
+ }
1546
+ function convertStringArray(str) {
1547
+ return str?.split(",").map((s) => s.trim()).filter((s) => s) || [];
1548
+ }
1549
+ function convertIntArray(str) {
1550
+ return str?.split(",").map((s) => parseInt(s.trim())).filter((n) => !isNaN(n)) || [];
1551
+ }
1552
+ function convertFloatArray(str) {
1553
+ return str?.split(",").map((s) => parseFloat(s.trim())).filter((n) => !isNaN(n)) || [];
1554
+ }
1555
+ function convertBooleanArray(str, defaultValue = false) {
1556
+ return str?.split(",").map((s) => parseConfigBoolean(s.trim(), defaultValue)).filter((item) => typeof item === "boolean") || [];
1557
+ }
1558
+ function toTypedValue(value) {
1559
+ const trimmed = value.trim();
1560
+ if (/^\d+$/.test(trimmed)) {
1561
+ return Number.parseInt(trimmed, 10);
1562
+ }
1563
+ return trimmed;
1564
+ }
1413
1565
  export {
1414
1566
  AbortedError,
1415
1567
  AppContextCore,
1416
1568
  AppServiceBase,
1569
+ Configurer,
1570
+ ConfigurerInstance,
1417
1571
  DUAL_PENDING,
1418
1572
  DynamicMiddlewareDispatcher,
1419
1573
  I18n,