@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/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { t as __name } from "./chunk--u3MIqq1.js";
2
- import { A as UserPluginWithLifeCycle, C as Printer, D as UserConfig, E as UnknownUserPlugin, F as linters, I as logLevel, L as AsyncEventEmitter, M as PluginManager, N as getMode, O as UserLogger, P as formatters, S as PluginWithLifeCycle, T as ResolvePathParams, _ as PluginFactoryOptions, a as Config, b as PluginLifecycleHooks, c as Group, d as Logger, f as LoggerContext, g as PluginContext, h as Plugin, i as BarrelType, j as KubbEvents, k as UserPlugin, l as InputData, m as Output, n as AdapterFactoryOptions, o as DevtoolsOptions, p as LoggerOptions, r as AdapterSource, s as GetPluginFactoryOptions, t as Adapter, u as InputPath, v as PluginKey, w as ResolveNameParams, x as PluginParameter, y as PluginLifecycle } from "./types-7DgxNmCG.js";
2
+ import { A as UserPlugin, B as AsyncEventEmitter, C as Printer, D as UnknownUserPlugin, E as ResolvePathParams, F as DefineStorage, I as defineStorage, L as formatters, M as KubbEvents, N as PluginManager, O as UserConfig, P as getMode, R as linters, S as PluginWithLifeCycle, T as ResolveNameParams, _ as PluginFactoryOptions, a as Config, b as PluginLifecycleHooks, c as Group, d as Logger, f as LoggerContext, g as PluginContext, h as Plugin, i as BarrelType, j as UserPluginWithLifeCycle, k as UserLogger, l as InputData, m as Output, n as AdapterFactoryOptions, o as DevtoolsOptions, p as LoggerOptions, r as AdapterSource, s as GetPluginFactoryOptions, t as Adapter, u as InputPath, v as PluginKey, w as PrinterFactoryOptions, x as PluginParameter, y as PluginLifecycle, z as logLevel } from "./types-D-MpaA4Z.js";
3
3
  import { definePrinter } from "@kubb/ast";
4
4
  import { Fabric } from "@kubb/react-fabric";
5
5
  import { KubbFile } from "@kubb/fabric-core/types";
@@ -158,6 +158,52 @@ declare class PromiseManager<TState = unknown> {
158
158
  }): TOutput;
159
159
  }
160
160
  //#endregion
161
+ //#region src/storages/fsStorage.d.ts
162
+ /**
163
+ * Built-in filesystem storage driver.
164
+ *
165
+ * This is the default storage when no `storage` option is configured in `output`.
166
+ * Keys are resolved against `process.cwd()`, so root-relative paths such as
167
+ * `src/gen/api/getPets.ts` are written to the correct location without extra configuration.
168
+ *
169
+ * Internally uses the `write` utility from `@internals/utils`, which:
170
+ * - trims leading/trailing whitespace before writing
171
+ * - skips the write when file content is already identical (deduplication)
172
+ * - creates missing parent directories automatically
173
+ * - supports Bun's native file API when running under Bun
174
+ *
175
+ * @example
176
+ * ```ts
177
+ * import { defineConfig, fsStorage } from '@kubb/core'
178
+ *
179
+ * export default defineConfig({
180
+ * input: { path: './petStore.yaml' },
181
+ * output: { path: './src/gen', storage: fsStorage() },
182
+ * })
183
+ * ```
184
+ */
185
+ declare const fsStorage: (options?: Record<string, never> | undefined) => DefineStorage;
186
+ //#endregion
187
+ //#region src/storages/memoryStorage.d.ts
188
+ /**
189
+ * In-memory storage driver. Useful for testing and dry-run scenarios where
190
+ * generated output should be captured without touching the filesystem.
191
+ *
192
+ * All data lives in a `Map` scoped to the storage instance and is discarded
193
+ * when the instance is garbage-collected.
194
+ *
195
+ * @example
196
+ * ```ts
197
+ * import { defineConfig, memoryStorage } from '@kubb/core'
198
+ *
199
+ * export default defineConfig({
200
+ * input: { path: './petStore.yaml' },
201
+ * output: { path: './src/gen', storage: memoryStorage() },
202
+ * })
203
+ * ```
204
+ */
205
+ declare const memoryStorage: (options?: Record<string, never> | undefined) => DefineStorage;
206
+ //#endregion
161
207
  //#region src/utils/FunctionParams.d.ts
162
208
  type FunctionParamsASTWithoutType = {
163
209
  name?: string;
@@ -265,5 +311,5 @@ declare function getConfigs(config: ConfigInput | UserConfig, args: CLIOptions):
265
311
  type Linter = keyof typeof linters;
266
312
  declare function detectLinter(): Promise<Linter | undefined>;
267
313
  //#endregion
268
- export { Adapter, AdapterFactoryOptions, AdapterSource, AsyncEventEmitter, BarrelType, type CLIOptions, Config, type ConfigInput, DevtoolsOptions, type FileMetaBase, FunctionParams, type FunctionParamsAST, GetPluginFactoryOptions, Group, InputData, InputPath, KubbEvents, Logger, LoggerContext, LoggerOptions, Output, PackageManager, Plugin, PluginContext, PluginFactoryOptions, PluginKey, PluginLifecycle, PluginLifecycleHooks, PluginManager, PluginParameter, PluginWithLifeCycle, Printer, PromiseManager, ResolveNameParams, ResolvePathParams, UnknownUserPlugin, UserConfig, UserLogger, UserPlugin, UserPluginWithLifeCycle, build, build as default, defineAdapter, defineConfig, defineLogger, definePlugin, definePrinter, detectFormatter, detectLinter, formatters, getBarrelFiles, getConfigs, getMode, isInputPath, linters, logLevel, safeBuild, setup };
314
+ export { Adapter, AdapterFactoryOptions, AdapterSource, AsyncEventEmitter, BarrelType, type CLIOptions, Config, type ConfigInput, DefineStorage, DevtoolsOptions, type FileMetaBase, FunctionParams, type FunctionParamsAST, GetPluginFactoryOptions, Group, InputData, InputPath, KubbEvents, Logger, LoggerContext, LoggerOptions, Output, PackageManager, Plugin, PluginContext, PluginFactoryOptions, PluginKey, PluginLifecycle, PluginLifecycleHooks, PluginManager, PluginParameter, PluginWithLifeCycle, Printer, PrinterFactoryOptions, PromiseManager, ResolveNameParams, ResolvePathParams, UnknownUserPlugin, UserConfig, UserLogger, UserPlugin, UserPluginWithLifeCycle, build, build as default, defineAdapter, defineConfig, defineLogger, definePlugin, definePrinter, defineStorage, detectFormatter, detectLinter, formatters, fsStorage, getBarrelFiles, getConfigs, getMode, isInputPath, linters, logLevel, memoryStorage, safeBuild, setup };
269
315
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -3,8 +3,8 @@ import mod from "node:module";
3
3
  import { EventEmitter } from "node:events";
4
4
  import { parseArgs, styleText } from "node:util";
5
5
  import { readFileSync } from "node:fs";
6
- import { access, mkdir, readFile, rm, writeFile } from "node:fs/promises";
7
- import path, { dirname, join, posix, resolve } from "node:path";
6
+ import { access, mkdir, readFile, readdir, rm, writeFile } from "node:fs/promises";
7
+ import path, { dirname, join, posix, relative, resolve } from "node:path";
8
8
  import { definePrinter } from "@kubb/ast";
9
9
  import { createFabric } from "@kubb/react-fabric";
10
10
  import { typescriptParser } from "@kubb/react-fabric/parsers";
@@ -401,7 +401,7 @@ function readSync(path) {
401
401
  * identical to what is already on disk.
402
402
  * Creates any missing parent directories automatically.
403
403
  * When `sanity` is `true`, re-reads the file after writing and throws if the
404
- * content does not match — useful for catching write failures on unreliable file systems.
404
+ * content does not match.
405
405
  */
406
406
  async function write(path, data, options = {}) {
407
407
  const trimmed = data.trim();
@@ -1359,8 +1359,109 @@ var PluginManager = class {
1359
1359
  }
1360
1360
  };
1361
1361
  //#endregion
1362
+ //#region src/defineStorage.ts
1363
+ /**
1364
+ * Wraps a storage builder so the `options` argument is optional, following the
1365
+ * same factory pattern as `definePlugin`, `defineLogger`, and `defineAdapter`.
1366
+ *
1367
+ * The builder receives the resolved options object and must return a
1368
+ * `DefineStorage`-compatible object that includes a `name` string.
1369
+ *
1370
+ * @example
1371
+ * ```ts
1372
+ * import { defineStorage } from '@kubb/core'
1373
+ *
1374
+ * export const memoryStorage = defineStorage((_options) => {
1375
+ * const store = new Map<string, string>()
1376
+ * return {
1377
+ * name: 'memory',
1378
+ * async hasItem(key) { return store.has(key) },
1379
+ * async getItem(key) { return store.get(key) ?? null },
1380
+ * async setItem(key, value) { store.set(key, value) },
1381
+ * async removeItem(key) { store.delete(key) },
1382
+ * async getKeys() { return [...store.keys()] },
1383
+ * async clear() { store.clear() },
1384
+ * }
1385
+ * })
1386
+ * ```
1387
+ */
1388
+ function defineStorage(build) {
1389
+ return (options) => build(options ?? {});
1390
+ }
1391
+ //#endregion
1392
+ //#region src/storages/fsStorage.ts
1393
+ /**
1394
+ * Built-in filesystem storage driver.
1395
+ *
1396
+ * This is the default storage when no `storage` option is configured in `output`.
1397
+ * Keys are resolved against `process.cwd()`, so root-relative paths such as
1398
+ * `src/gen/api/getPets.ts` are written to the correct location without extra configuration.
1399
+ *
1400
+ * Internally uses the `write` utility from `@internals/utils`, which:
1401
+ * - trims leading/trailing whitespace before writing
1402
+ * - skips the write when file content is already identical (deduplication)
1403
+ * - creates missing parent directories automatically
1404
+ * - supports Bun's native file API when running under Bun
1405
+ *
1406
+ * @example
1407
+ * ```ts
1408
+ * import { defineConfig, fsStorage } from '@kubb/core'
1409
+ *
1410
+ * export default defineConfig({
1411
+ * input: { path: './petStore.yaml' },
1412
+ * output: { path: './src/gen', storage: fsStorage() },
1413
+ * })
1414
+ * ```
1415
+ */
1416
+ const fsStorage = defineStorage(() => ({
1417
+ name: "fs",
1418
+ async hasItem(key) {
1419
+ try {
1420
+ await access(resolve(key));
1421
+ return true;
1422
+ } catch {
1423
+ return false;
1424
+ }
1425
+ },
1426
+ async getItem(key) {
1427
+ try {
1428
+ return await readFile(resolve(key), "utf8");
1429
+ } catch {
1430
+ return null;
1431
+ }
1432
+ },
1433
+ async setItem(key, value) {
1434
+ await write(resolve(key), value, { sanity: false });
1435
+ },
1436
+ async removeItem(key) {
1437
+ await rm(resolve(key), { force: true });
1438
+ },
1439
+ async getKeys(base) {
1440
+ const keys = [];
1441
+ async function walk(dir, prefix) {
1442
+ let entries;
1443
+ try {
1444
+ entries = await readdir(dir, { withFileTypes: true });
1445
+ } catch {
1446
+ return;
1447
+ }
1448
+ for (const entry of entries) {
1449
+ const rel = prefix ? `${prefix}/${entry.name}` : entry.name;
1450
+ if (entry.isDirectory()) await walk(join(dir, entry.name), rel);
1451
+ else keys.push(rel);
1452
+ }
1453
+ }
1454
+ await walk(resolve(base ?? process.cwd()), "");
1455
+ return keys;
1456
+ },
1457
+ async clear(base) {
1458
+ if (!base) return;
1459
+ await clean(resolve(base));
1460
+ }
1461
+ }));
1462
+ //#endregion
1362
1463
  //#region package.json
1363
- var version$1 = "4.35.0";
1464
+ var version$1 = "4.36.0";
1364
1465
  //#endregion
1365
1466
  //#region src/utils/diagnostics.ts
1366
1467
  /**
@@ -1391,7 +1492,7 @@ async function setup(options) {
1391
1492
  ` • Output: ${userConfig.output?.path || "not specified"}`,
1392
1493
  ` • Plugins: ${userConfig.plugins?.length || 0}`,
1393
1494
  "Output Settings:",
1394
- ` • Write: ${userConfig.output?.write !== false ? "enabled" : "disabled"}`,
1495
+ ` • Storage: ${userConfig.output?.storage ? `custom(${userConfig.output.storage.name})` : userConfig.output?.write === false ? "disabled" : "filesystem (default)"}`,
1395
1496
  ` • Formatter: ${userConfig.output?.format || "none"}`,
1396
1497
  ` • Linter: ${userConfig.output?.lint || "none"}`,
1397
1498
  "Environment:",
@@ -1428,12 +1529,13 @@ async function setup(options) {
1428
1529
  } : void 0,
1429
1530
  plugins: userConfig.plugins
1430
1531
  };
1532
+ const storage = definedConfig.output.write === false ? null : definedConfig.output.storage ?? fsStorage();
1431
1533
  if (definedConfig.output.clean) {
1432
1534
  await events.emit("debug", {
1433
1535
  date: /* @__PURE__ */ new Date(),
1434
1536
  logs: ["Cleaning output directories", ` • Output: ${definedConfig.output.path}`]
1435
1537
  });
1436
- await clean(definedConfig.output.path);
1538
+ await storage?.clear(resolve(definedConfig.root, definedConfig.output.path));
1437
1539
  }
1438
1540
  const fabric = createFabric();
1439
1541
  fabric.use(fsPlugin);
@@ -1453,7 +1555,8 @@ async function setup(options) {
1453
1555
  source
1454
1556
  });
1455
1557
  if (source) {
1456
- if (definedConfig.output.write) await write(file.path, source, { sanity: false });
1558
+ const key = relative(resolve(definedConfig.root), file.path);
1559
+ await storage?.setItem(key, source);
1457
1560
  sources.set(file.path, source);
1458
1561
  }
1459
1562
  });
@@ -1468,7 +1571,7 @@ async function setup(options) {
1468
1571
  date: /* @__PURE__ */ new Date(),
1469
1572
  logs: [
1470
1573
  "✓ Fabric initialized",
1471
- ` • File writing: ${definedConfig.output.write ? "enabled" : "disabled (dry-run)"}`,
1574
+ ` • Storage: ${storage ? storage.name : "disabled (dry-run)"}`,
1472
1575
  ` • Barrel type: ${definedConfig.output.barrelType || "none"}`
1473
1576
  ]
1474
1577
  });
@@ -1786,6 +1889,54 @@ var PackageManager = class PackageManager {
1786
1889
  }
1787
1890
  };
1788
1891
  //#endregion
1892
+ //#region src/storages/memoryStorage.ts
1893
+ /**
1894
+ * In-memory storage driver. Useful for testing and dry-run scenarios where
1895
+ * generated output should be captured without touching the filesystem.
1896
+ *
1897
+ * All data lives in a `Map` scoped to the storage instance and is discarded
1898
+ * when the instance is garbage-collected.
1899
+ *
1900
+ * @example
1901
+ * ```ts
1902
+ * import { defineConfig, memoryStorage } from '@kubb/core'
1903
+ *
1904
+ * export default defineConfig({
1905
+ * input: { path: './petStore.yaml' },
1906
+ * output: { path: './src/gen', storage: memoryStorage() },
1907
+ * })
1908
+ * ```
1909
+ */
1910
+ const memoryStorage = defineStorage(() => {
1911
+ const store = /* @__PURE__ */ new Map();
1912
+ return {
1913
+ name: "memory",
1914
+ async hasItem(key) {
1915
+ return store.has(key);
1916
+ },
1917
+ async getItem(key) {
1918
+ return store.get(key) ?? null;
1919
+ },
1920
+ async setItem(key, value) {
1921
+ store.set(key, value);
1922
+ },
1923
+ async removeItem(key) {
1924
+ store.delete(key);
1925
+ },
1926
+ async getKeys(base) {
1927
+ const keys = [...store.keys()];
1928
+ return base ? keys.filter((k) => k.startsWith(base)) : keys;
1929
+ },
1930
+ async clear(base) {
1931
+ if (!base) {
1932
+ store.clear();
1933
+ return;
1934
+ }
1935
+ for (const key of store.keys()) if (key.startsWith(base)) store.delete(key);
1936
+ }
1937
+ };
1938
+ });
1939
+ //#endregion
1789
1940
  //#region src/utils/FunctionParams.ts
1790
1941
  /**
1791
1942
  * @deprecated
@@ -2155,6 +2306,6 @@ async function detectLinter() {
2155
2306
  ]) if (await isLinterAvailable(linter)) return linter;
2156
2307
  }
2157
2308
  //#endregion
2158
- export { AsyncEventEmitter, FunctionParams, PackageManager, PluginManager, PromiseManager, build, build as default, defineAdapter, defineConfig, defineLogger, definePlugin, definePrinter, detectFormatter, detectLinter, formatters, getBarrelFiles, getConfigs, getMode, isInputPath, linters, logLevel, safeBuild, setup };
2309
+ export { AsyncEventEmitter, FunctionParams, PackageManager, PluginManager, PromiseManager, build, build as default, defineAdapter, defineConfig, defineLogger, definePlugin, definePrinter, defineStorage, detectFormatter, detectLinter, formatters, fsStorage, getBarrelFiles, getConfigs, getMode, isInputPath, linters, logLevel, memoryStorage, safeBuild, setup };
2159
2310
 
2160
2311
  //# sourceMappingURL=index.js.map