@kubb/core 4.35.0 → 4.36.0

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/hooks.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { t as __name } from "./chunk--u3MIqq1.js";
2
- import { M as PluginManager, _ as PluginFactoryOptions, h as Plugin } from "./types-7DgxNmCG.js";
2
+ import { N as PluginManager, _ as PluginFactoryOptions, h as Plugin } from "./types-D-MpaA4Z.js";
3
3
  import { KubbFile } from "@kubb/fabric-core/types";
4
4
 
5
5
  //#region src/hooks/useMode.d.ts
package/dist/index.cjs CHANGED
@@ -409,7 +409,7 @@ function readSync(path) {
409
409
  * identical to what is already on disk.
410
410
  * Creates any missing parent directories automatically.
411
411
  * When `sanity` is `true`, re-reads the file after writing and throws if the
412
- * content does not match — useful for catching write failures on unreliable file systems.
412
+ * content does not match.
413
413
  */
414
414
  async function write(path, data, options = {}) {
415
415
  const trimmed = data.trim();
@@ -1367,8 +1367,109 @@ var PluginManager = class {
1367
1367
  }
1368
1368
  };
1369
1369
  //#endregion
1370
+ //#region src/defineStorage.ts
1371
+ /**
1372
+ * Wraps a storage builder so the `options` argument is optional, following the
1373
+ * same factory pattern as `definePlugin`, `defineLogger`, and `defineAdapter`.
1374
+ *
1375
+ * The builder receives the resolved options object and must return a
1376
+ * `DefineStorage`-compatible object that includes a `name` string.
1377
+ *
1378
+ * @example
1379
+ * ```ts
1380
+ * import { defineStorage } from '@kubb/core'
1381
+ *
1382
+ * export const memoryStorage = defineStorage((_options) => {
1383
+ * const store = new Map<string, string>()
1384
+ * return {
1385
+ * name: 'memory',
1386
+ * async hasItem(key) { return store.has(key) },
1387
+ * async getItem(key) { return store.get(key) ?? null },
1388
+ * async setItem(key, value) { store.set(key, value) },
1389
+ * async removeItem(key) { store.delete(key) },
1390
+ * async getKeys() { return [...store.keys()] },
1391
+ * async clear() { store.clear() },
1392
+ * }
1393
+ * })
1394
+ * ```
1395
+ */
1396
+ function defineStorage(build) {
1397
+ return (options) => build(options ?? {});
1398
+ }
1399
+ //#endregion
1400
+ //#region src/storages/fsStorage.ts
1401
+ /**
1402
+ * Built-in filesystem storage driver.
1403
+ *
1404
+ * This is the default storage when no `storage` option is configured in `output`.
1405
+ * Keys are resolved against `process.cwd()`, so root-relative paths such as
1406
+ * `src/gen/api/getPets.ts` are written to the correct location without extra configuration.
1407
+ *
1408
+ * Internally uses the `write` utility from `@internals/utils`, which:
1409
+ * - trims leading/trailing whitespace before writing
1410
+ * - skips the write when file content is already identical (deduplication)
1411
+ * - creates missing parent directories automatically
1412
+ * - supports Bun's native file API when running under Bun
1413
+ *
1414
+ * @example
1415
+ * ```ts
1416
+ * import { defineConfig, fsStorage } from '@kubb/core'
1417
+ *
1418
+ * export default defineConfig({
1419
+ * input: { path: './petStore.yaml' },
1420
+ * output: { path: './src/gen', storage: fsStorage() },
1421
+ * })
1422
+ * ```
1423
+ */
1424
+ const fsStorage = defineStorage(() => ({
1425
+ name: "fs",
1426
+ async hasItem(key) {
1427
+ try {
1428
+ await (0, node_fs_promises.access)((0, node_path.resolve)(key));
1429
+ return true;
1430
+ } catch {
1431
+ return false;
1432
+ }
1433
+ },
1434
+ async getItem(key) {
1435
+ try {
1436
+ return await (0, node_fs_promises.readFile)((0, node_path.resolve)(key), "utf8");
1437
+ } catch {
1438
+ return null;
1439
+ }
1440
+ },
1441
+ async setItem(key, value) {
1442
+ await write((0, node_path.resolve)(key), value, { sanity: false });
1443
+ },
1444
+ async removeItem(key) {
1445
+ await (0, node_fs_promises.rm)((0, node_path.resolve)(key), { force: true });
1446
+ },
1447
+ async getKeys(base) {
1448
+ const keys = [];
1449
+ async function walk(dir, prefix) {
1450
+ let entries;
1451
+ try {
1452
+ entries = await (0, node_fs_promises.readdir)(dir, { withFileTypes: true });
1453
+ } catch {
1454
+ return;
1455
+ }
1456
+ for (const entry of entries) {
1457
+ const rel = prefix ? `${prefix}/${entry.name}` : entry.name;
1458
+ if (entry.isDirectory()) await walk((0, node_path.join)(dir, entry.name), rel);
1459
+ else keys.push(rel);
1460
+ }
1461
+ }
1462
+ await walk((0, node_path.resolve)(base ?? process.cwd()), "");
1463
+ return keys;
1464
+ },
1465
+ async clear(base) {
1466
+ if (!base) return;
1467
+ await clean((0, node_path.resolve)(base));
1468
+ }
1469
+ }));
1470
+ //#endregion
1370
1471
  //#region package.json
1371
- var version = "4.35.0";
1472
+ var version = "4.36.0";
1372
1473
  //#endregion
1373
1474
  //#region src/utils/diagnostics.ts
1374
1475
  /**
@@ -1399,7 +1500,7 @@ async function setup(options) {
1399
1500
  ` • Output: ${userConfig.output?.path || "not specified"}`,
1400
1501
  ` • Plugins: ${userConfig.plugins?.length || 0}`,
1401
1502
  "Output Settings:",
1402
- ` • Write: ${userConfig.output?.write !== false ? "enabled" : "disabled"}`,
1503
+ ` • Storage: ${userConfig.output?.storage ? `custom(${userConfig.output.storage.name})` : userConfig.output?.write === false ? "disabled" : "filesystem (default)"}`,
1403
1504
  ` • Formatter: ${userConfig.output?.format || "none"}`,
1404
1505
  ` • Linter: ${userConfig.output?.lint || "none"}`,
1405
1506
  "Environment:",
@@ -1436,12 +1537,13 @@ async function setup(options) {
1436
1537
  } : void 0,
1437
1538
  plugins: userConfig.plugins
1438
1539
  };
1540
+ const storage = definedConfig.output.write === false ? null : definedConfig.output.storage ?? fsStorage();
1439
1541
  if (definedConfig.output.clean) {
1440
1542
  await events.emit("debug", {
1441
1543
  date: /* @__PURE__ */ new Date(),
1442
1544
  logs: ["Cleaning output directories", ` • Output: ${definedConfig.output.path}`]
1443
1545
  });
1444
- await clean(definedConfig.output.path);
1546
+ await storage?.clear((0, node_path.resolve)(definedConfig.root, definedConfig.output.path));
1445
1547
  }
1446
1548
  const fabric = (0, _kubb_react_fabric.createFabric)();
1447
1549
  fabric.use(_kubb_react_fabric_plugins.fsPlugin);
@@ -1461,7 +1563,8 @@ async function setup(options) {
1461
1563
  source
1462
1564
  });
1463
1565
  if (source) {
1464
- if (definedConfig.output.write) await write(file.path, source, { sanity: false });
1566
+ const key = (0, node_path.relative)((0, node_path.resolve)(definedConfig.root), file.path);
1567
+ await storage?.setItem(key, source);
1465
1568
  sources.set(file.path, source);
1466
1569
  }
1467
1570
  });
@@ -1476,7 +1579,7 @@ async function setup(options) {
1476
1579
  date: /* @__PURE__ */ new Date(),
1477
1580
  logs: [
1478
1581
  "✓ Fabric initialized",
1479
- ` • File writing: ${definedConfig.output.write ? "enabled" : "disabled (dry-run)"}`,
1582
+ ` • Storage: ${storage ? storage.name : "disabled (dry-run)"}`,
1480
1583
  ` • Barrel type: ${definedConfig.output.barrelType || "none"}`
1481
1584
  ]
1482
1585
  });
@@ -1794,6 +1897,54 @@ var PackageManager = class PackageManager {
1794
1897
  }
1795
1898
  };
1796
1899
  //#endregion
1900
+ //#region src/storages/memoryStorage.ts
1901
+ /**
1902
+ * In-memory storage driver. Useful for testing and dry-run scenarios where
1903
+ * generated output should be captured without touching the filesystem.
1904
+ *
1905
+ * All data lives in a `Map` scoped to the storage instance and is discarded
1906
+ * when the instance is garbage-collected.
1907
+ *
1908
+ * @example
1909
+ * ```ts
1910
+ * import { defineConfig, memoryStorage } from '@kubb/core'
1911
+ *
1912
+ * export default defineConfig({
1913
+ * input: { path: './petStore.yaml' },
1914
+ * output: { path: './src/gen', storage: memoryStorage() },
1915
+ * })
1916
+ * ```
1917
+ */
1918
+ const memoryStorage = defineStorage(() => {
1919
+ const store = /* @__PURE__ */ new Map();
1920
+ return {
1921
+ name: "memory",
1922
+ async hasItem(key) {
1923
+ return store.has(key);
1924
+ },
1925
+ async getItem(key) {
1926
+ return store.get(key) ?? null;
1927
+ },
1928
+ async setItem(key, value) {
1929
+ store.set(key, value);
1930
+ },
1931
+ async removeItem(key) {
1932
+ store.delete(key);
1933
+ },
1934
+ async getKeys(base) {
1935
+ const keys = [...store.keys()];
1936
+ return base ? keys.filter((k) => k.startsWith(base)) : keys;
1937
+ },
1938
+ async clear(base) {
1939
+ if (!base) {
1940
+ store.clear();
1941
+ return;
1942
+ }
1943
+ for (const key of store.keys()) if (key.startsWith(base)) store.delete(key);
1944
+ }
1945
+ };
1946
+ });
1947
+ //#endregion
1797
1948
  //#region src/utils/FunctionParams.ts
1798
1949
  /**
1799
1950
  * @deprecated
@@ -2180,15 +2331,18 @@ Object.defineProperty(exports, "definePrinter", {
2180
2331
  return _kubb_ast.definePrinter;
2181
2332
  }
2182
2333
  });
2334
+ exports.defineStorage = defineStorage;
2183
2335
  exports.detectFormatter = detectFormatter;
2184
2336
  exports.detectLinter = detectLinter;
2185
2337
  exports.formatters = formatters;
2338
+ exports.fsStorage = fsStorage;
2186
2339
  exports.getBarrelFiles = getBarrelFiles;
2187
2340
  exports.getConfigs = getConfigs;
2188
2341
  exports.getMode = getMode;
2189
2342
  exports.isInputPath = isInputPath;
2190
2343
  exports.linters = linters;
2191
2344
  exports.logLevel = logLevel;
2345
+ exports.memoryStorage = memoryStorage;
2192
2346
  exports.safeBuild = safeBuild;
2193
2347
  exports.setup = setup;
2194
2348