kubb 5.0.0-beta.3 → 5.0.0-beta.31
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/README.md +12 -16
- package/dist/index.cjs +41 -14
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +30 -10
- package/dist/index.js +41 -14
- package/dist/index.js.map +1 -1
- package/package.json +12 -17
- package/src/defineConfig.ts +37 -14
package/README.md
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
<div align="center">
|
|
2
|
+
<h1>kubb</h1>
|
|
2
3
|
<a href="https://kubb.dev" target="_blank" rel="noopener noreferrer">
|
|
3
4
|
<img width="180" src="https://raw.githubusercontent.com/kubb-labs/kubb/main/assets/logo.png" alt="Kubb logo">
|
|
4
5
|
</a>
|
|
@@ -11,7 +12,7 @@
|
|
|
11
12
|
|
|
12
13
|
### The meta framework for code generation
|
|
13
14
|
|
|
14
|
-
|
|
15
|
+
Point Kubb at an OpenAPI spec and it generates types, clients, hooks, validators, mocks, and more.
|
|
15
16
|
|
|
16
17
|
<h4>
|
|
17
18
|
<a href="https://kubb.dev" target="_blank">Documentation</a>
|
|
@@ -27,27 +28,22 @@
|
|
|
27
28
|
## Installation
|
|
28
29
|
|
|
29
30
|
```bash
|
|
30
|
-
|
|
31
|
+
bun add kubb
|
|
31
32
|
# or
|
|
32
33
|
pnpm add kubb
|
|
34
|
+
# or
|
|
35
|
+
npm install kubb
|
|
33
36
|
```
|
|
34
37
|
|
|
35
|
-
## Quick
|
|
38
|
+
## Quick start
|
|
36
39
|
|
|
37
|
-
|
|
40
|
+
Run the setup wizard to create a `kubb.config.ts`:
|
|
38
41
|
|
|
39
42
|
```bash
|
|
40
43
|
npx kubb init
|
|
41
44
|
```
|
|
42
45
|
|
|
43
|
-
The
|
|
44
|
-
|
|
45
|
-
- Create a `package.json` (if needed)
|
|
46
|
-
- Guide you through plugin selection
|
|
47
|
-
- Install packages automatically
|
|
48
|
-
- Generate `kubb.config.ts`
|
|
49
|
-
|
|
50
|
-
Then generate your code:
|
|
46
|
+
The wizard creates a `package.json` if needed, guides you through plugin selection, installs packages, and writes `kubb.config.ts`. Then generate your code:
|
|
51
47
|
|
|
52
48
|
```bash
|
|
53
49
|
npx kubb generate
|
|
@@ -58,12 +54,12 @@ See the [documentation](https://kubb.dev) for detailed usage and advanced featur
|
|
|
58
54
|
## Features
|
|
59
55
|
|
|
60
56
|
- Works with Node.js 22+ and TypeScript 6.
|
|
61
|
-
-
|
|
62
|
-
- Extensible plugin and middleware system
|
|
63
|
-
- CLI
|
|
57
|
+
- Converts Swagger 2.0, OpenAPI 3.0, and OpenAPI 3.1 into TypeScript types, API clients, and more via the [plugin ecosystem](https://github.com/kubb-labs/kubb-plugins).
|
|
58
|
+
- Extensible plugin and middleware system that you can compose for custom code generation pipelines.
|
|
59
|
+
- CLI with interactive setup, progress bar, and detailed logs.
|
|
64
60
|
- Model Context Protocol (MCP) server for AI assistants like [Claude](https://claude.ai), [Cursor](https://cursor.sh), and other MCP-compatible tools.
|
|
65
61
|
- JSX-based renderer (`@kubb/renderer-jsx`) for building custom plugin output.
|
|
66
|
-
- Barrel file generation via
|
|
62
|
+
- Barrel file generation via `@kubb/middleware-barrel`.
|
|
67
63
|
|
|
68
64
|
## Supporting Kubb
|
|
69
65
|
|
package/dist/index.cjs
CHANGED
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
|
3
3
|
let _kubb_adapter_oas = require("@kubb/adapter-oas");
|
|
4
4
|
let _kubb_middleware_barrel = require("@kubb/middleware-barrel");
|
|
5
5
|
let _kubb_parser_ts = require("@kubb/parser-ts");
|
|
6
|
+
let _kubb_parser_md = require("@kubb/parser-md");
|
|
6
7
|
//#region ../../internals/utils/src/promise.ts
|
|
7
8
|
/** Returns `true` when `result` is a thenable `Promise`.
|
|
8
9
|
*
|
|
@@ -18,15 +19,16 @@ function isPromise(result) {
|
|
|
18
19
|
//#endregion
|
|
19
20
|
//#region src/defineConfig.ts
|
|
20
21
|
/**
|
|
21
|
-
* Applies default adapter, parsers, middleware, `output.barrel`, `output.format`, and `output.lint` to a single user config when not set.
|
|
22
|
+
* Applies default `root`, adapter, parsers, middleware, `output.barrel`, `output.format`, and `output.lint` to a single user config when not set.
|
|
22
23
|
*
|
|
24
|
+
* - `root` defaults to `process.cwd()`
|
|
23
25
|
* - `adapter` defaults to `adapterOas()`
|
|
24
26
|
* - `parsers` defaults to `[parserTs, parserTsx]`
|
|
25
27
|
* - `middleware` defaults to `[middlewareBarrel()]`
|
|
26
28
|
* - `output.barrel` defaults to `{ type: 'named' }` **only when `middlewareBarrel` is part of `middleware`**.
|
|
27
29
|
* When the user provides a custom middleware list without `middlewareBarrel`, `barrel` is left untouched.
|
|
28
|
-
* - `output.format` defaults to `
|
|
29
|
-
* - `output.lint` defaults to `
|
|
30
|
+
* - `output.format` defaults to `false`
|
|
31
|
+
* - `output.lint` defaults to `false`
|
|
30
32
|
*/
|
|
31
33
|
function applyDefaults(config) {
|
|
32
34
|
const middleware = config.middleware?.length ? config.middleware : [(0, _kubb_middleware_barrel.middlewareBarrel)()];
|
|
@@ -37,8 +39,13 @@ function applyDefaults(config) {
|
|
|
37
39
|
if (output.lint === void 0) output.lint = false;
|
|
38
40
|
return {
|
|
39
41
|
...config,
|
|
42
|
+
root: config.root || process.cwd(),
|
|
40
43
|
adapter: config.adapter ?? (0, _kubb_adapter_oas.adapterOas)(),
|
|
41
|
-
parsers: config.parsers?.length ? config.parsers : [
|
|
44
|
+
parsers: config.parsers?.length ? config.parsers : [
|
|
45
|
+
_kubb_parser_ts.parserTs,
|
|
46
|
+
_kubb_parser_ts.parserTsx,
|
|
47
|
+
_kubb_parser_md.parserMd
|
|
48
|
+
],
|
|
42
49
|
middleware,
|
|
43
50
|
output
|
|
44
51
|
};
|
|
@@ -48,21 +55,41 @@ function normalizeConfig(config) {
|
|
|
48
55
|
return applyDefaults(config);
|
|
49
56
|
}
|
|
50
57
|
/**
|
|
51
|
-
*
|
|
58
|
+
* Defines a Kubb build configuration and applies sensible defaults so the
|
|
59
|
+
* minimal config stays small.
|
|
52
60
|
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
61
|
+
* Defaults applied when omitted:
|
|
62
|
+
* - `adapter` → `adapterOas()` (OpenAPI 2.0/3.0/3.1).
|
|
63
|
+
* - `parsers` → `[parserTs, parserTsx]`.
|
|
64
|
+
* - `middleware` → `[middlewareBarrel()]`.
|
|
65
|
+
* - `output.barrel` → `{ type: 'named' }` only when `middlewareBarrel` is
|
|
66
|
+
* in the middleware list.
|
|
67
|
+
* - `output.format` and `output.lint` → `false`.
|
|
55
68
|
*
|
|
56
|
-
* Accepts
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
* receiving the CLI options as argument
|
|
69
|
+
* Accepts a config object, an array of configs, a Promise resolving to one,
|
|
70
|
+
* or a function that receives the parsed CLI options and returns any of the
|
|
71
|
+
* above. The return type is preserved so async/array variants stay typed.
|
|
60
72
|
*
|
|
61
73
|
* @example
|
|
62
74
|
* ```ts
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
75
|
+
* import { defineConfig } from 'kubb'
|
|
76
|
+
* import { pluginTs } from '@kubb/plugin-ts'
|
|
77
|
+
*
|
|
78
|
+
* export default defineConfig({
|
|
79
|
+
* input: { path: './petStore.yaml' },
|
|
80
|
+
* output: { path: './src/gen' },
|
|
81
|
+
* plugins: [pluginTs()],
|
|
82
|
+
* })
|
|
83
|
+
* ```
|
|
84
|
+
*
|
|
85
|
+
* @example Function form with CLI options
|
|
86
|
+
* ```ts
|
|
87
|
+
* import { defineConfig } from 'kubb'
|
|
88
|
+
*
|
|
89
|
+
* export default defineConfig(({ input }) => ({
|
|
90
|
+
* input: { path: input ?? './petStore.yaml' },
|
|
91
|
+
* output: { path: './src/gen' },
|
|
92
|
+
* plugins: [],
|
|
66
93
|
* }))
|
|
67
94
|
* ```
|
|
68
95
|
*/
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["middlewareBarrelName","parserTs","parserTsx"],"sources":["../../../internals/utils/src/promise.ts","../src/defineConfig.ts"],"sourcesContent":["/** A value that may already be resolved or still pending.\n *\n * @example\n * ```ts\n * function load(id: string): PossiblePromise<string> {\n * return cache.get(id) ?? fetchRemote(id)\n * }\n * ```\n */\nexport type PossiblePromise<T> = Promise<T> | T\n\n/** Returns `true` when `result` is a thenable `Promise`.\n *\n * @example\n * ```ts\n * isPromise(Promise.resolve(1)) // true\n * isPromise(42) // false\n * ```\n */\nexport function isPromise<T>(result: PossiblePromise<T>): result is Promise<T> {\n return result !== null && result !== undefined && typeof (result as Record<string, unknown>)['then'] === 'function'\n}\n\n/** Returns `true` when `result` is a fulfilled `Promise.allSettled` result.\n *\n * @example\n * ```ts\n * const results = await Promise.allSettled([p1, p2])\n * results.filter(isPromiseFulfilledResult).map((r) => r.value)\n * ```\n */\nexport function isPromiseFulfilledResult<T = unknown>(result: PromiseSettledResult<unknown>): result is PromiseFulfilledResult<T> {\n return result.status === 'fulfilled'\n}\n\n/** Returns `true` when `result` is a rejected `Promise.allSettled` result with a typed `reason`.\n *\n * @example\n * ```ts\n * const results = await Promise.allSettled([p1, p2])\n * results.filter(isPromiseRejectedResult<Error>).map((r) => r.reason.message)\n * ```\n */\nexport function isPromiseRejectedResult<T>(result: PromiseSettledResult<unknown>): result is Omit<PromiseRejectedResult, 'reason'> & { reason: T } {\n return result.status === 'rejected'\n}\n","import { isPromise, type PossiblePromise } from '@internals/utils'\nimport { adapterOas } from '@kubb/adapter-oas'\nimport type { CLIOptions, UserConfig } from '@kubb/core'\nimport { middlewareBarrel, middlewareBarrelName } from '@kubb/middleware-barrel'\nimport { parserTs, parserTsx } from '@kubb/parser-ts'\n\ntype AnyConfigResult = UserConfig<any> | Array<UserConfig<any>>\ntype ConfigInput = AnyConfigResult | Promise<AnyConfigResult> | ((cli: CLIOptions) => PossiblePromise<AnyConfigResult>)\ntype NormalizeConfig<TConfig> =\n TConfig extends Array<UserConfig<infer TInput>> ? Array<UserConfig<TInput>> : TConfig extends UserConfig<infer TInput> ? UserConfig<TInput> : never\ntype DefinedConfig<TConfig extends ConfigInput> = TConfig extends (cli: CLIOptions) => PossiblePromise<infer TResult>\n ? (cli: CLIOptions) => Promise<NormalizeConfig<TResult>>\n : TConfig extends Promise<infer TResult>\n ? Promise<NormalizeConfig<TResult>>\n : NormalizeConfig<TConfig>\n\n/**\n * Applies default adapter, parsers, middleware, `output.barrel`, `output.format`, and `output.lint` to a single user config when not set.\n *\n * - `adapter` defaults to `adapterOas()`\n * - `parsers` defaults to `[parserTs, parserTsx]`\n * - `middleware` defaults to `[middlewareBarrel()]`\n * - `output.barrel` defaults to `{ type: 'named' }` **only when `middlewareBarrel` is part of `middleware`**.\n * When the user provides a custom middleware list without `middlewareBarrel`, `barrel` is left untouched.\n * - `output.format` defaults to `'auto'`\n * - `output.lint` defaults to `'auto'`\n */\nfunction applyDefaults<TInput>(config: UserConfig<TInput>): UserConfig<TInput> {\n const middleware = config.middleware?.length ? config.middleware : [middlewareBarrel()]\n const hasBarrelMiddleware = middleware.some((m) => m.name === middlewareBarrelName)\n\n const output = { ...config.output }\n if (hasBarrelMiddleware && output.barrel === undefined) {\n output.barrel = { type: 'named' }\n }\n if (output.format === undefined) {\n output.format = false\n }\n if (output.lint === undefined) {\n output.lint = false\n }\n\n return {\n ...config,\n adapter: config.adapter ?? adapterOas(),\n parsers: config.parsers?.length ? config.parsers : [parserTs, parserTsx],\n middleware,\n output,\n }\n}\n\nfunction normalizeConfig<TInput>(config: UserConfig<TInput> | Array<UserConfig<TInput>>): UserConfig<TInput> | Array<UserConfig<TInput>> {\n if (Array.isArray(config)) {\n return config.map(applyDefaults)\n }\n\n return applyDefaults(config)\n}\n\n/**\n * Helper for defining a Kubb configuration with built-in defaults.\n *\n * When no `adapter` is provided, `adapterOas()` is used automatically.\n * When no `parsers` are provided, `[parserTs, parserTsx]` is used automatically.\n *\n * Accepts either:\n * - A config object or array of configs\n * - A function returning the config(s), optionally async,\n * receiving the CLI options as argument\n *\n * @example\n * ```ts\n * export default defineConfig(({ logLevel }) => ({\n * root: 'src',\n * plugins: [myPlugin()],\n * }))\n * ```\n */\nexport function defineConfig<TConfig extends ConfigInput>(config: TConfig): DefinedConfig<TConfig> {\n if (typeof config === 'function') {\n return (async (cli: CLIOptions) => {\n return normalizeConfig(await config(cli))\n }) as DefinedConfig<TConfig>\n }\n\n if (isPromise(config)) {\n return config.then((resolved) => normalizeConfig(resolved)) as DefinedConfig<TConfig>\n }\n\n return normalizeConfig(config) as DefinedConfig<TConfig>\n}\n"],"mappings":";;;;;;;;;;;;;;AAmBA,SAAgB,UAAa,QAAkD;AAC7E,QAAO,WAAW,QAAQ,WAAW,KAAA,KAAa,OAAQ,OAAmC,YAAY;;;;;;;;;;;;;;;ACO3G,SAAS,cAAsB,QAAgD;CAC7E,MAAM,aAAa,OAAO,YAAY,SAAS,OAAO,aAAa,EAAA,GAAA,wBAAA,mBAAmB,CAAC;CACvF,MAAM,sBAAsB,WAAW,MAAM,MAAM,EAAE,SAASA,wBAAAA,qBAAqB;CAEnF,MAAM,SAAS,EAAE,GAAG,OAAO,QAAQ;AACnC,KAAI,uBAAuB,OAAO,WAAW,KAAA,EAC3C,QAAO,SAAS,EAAE,MAAM,SAAS;AAEnC,KAAI,OAAO,WAAW,KAAA,EACpB,QAAO,SAAS;AAElB,KAAI,OAAO,SAAS,KAAA,EAClB,QAAO,OAAO;AAGhB,QAAO;EACL,GAAG;EACH,SAAS,OAAO,YAAA,GAAA,kBAAA,aAAuB;EACvC,SAAS,OAAO,SAAS,SAAS,OAAO,UAAU,CAACC,gBAAAA,UAAUC,gBAAAA,UAAU;EACxE;EACA;EACD;;AAGH,SAAS,gBAAwB,QAAwG;AACvI,KAAI,MAAM,QAAQ,OAAO,CACvB,QAAO,OAAO,IAAI,cAAc;AAGlC,QAAO,cAAc,OAAO;;;;;;;;;;;;;;;;;;;;;AAsB9B,SAAgB,aAA0C,QAAyC;AACjG,KAAI,OAAO,WAAW,WACpB,SAAQ,OAAO,QAAoB;AACjC,SAAO,gBAAgB,MAAM,OAAO,IAAI,CAAC;;AAI7C,KAAI,UAAU,OAAO,CACnB,QAAO,OAAO,MAAM,aAAa,gBAAgB,SAAS,CAAC;AAG7D,QAAO,gBAAgB,OAAO"}
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["middlewareBarrelName","parserTs","parserTsx","parserMd"],"sources":["../../../internals/utils/src/promise.ts","../src/defineConfig.ts"],"sourcesContent":["function* chunks<T>(arr: readonly T[], size: number): Generator<T[]> {\n for (let i = 0; i < arr.length; i += size) {\n yield arr.slice(i, i + size)\n }\n}\n\nexport type ForBatchesOptions = {\n /**\n * Maximum batch size handed to `process`.\n * Parallel dispatch within a batch is the caller's responsibility\n * (typically via `Promise.all(batch.map(...))`).\n */\n concurrency: number\n /**\n * Called after every batch.\n *\n * Use a cheap, idempotent callback (e.g. one that short-circuits when there\n * is nothing new to do). The helper does not coalesce calls — if you need\n * throttling, do it inside `flush` itself.\n */\n flush?: () => Promise<void>\n}\n\n/**\n * Slices `source` into batches of `concurrency` items and awaits `process` for each batch.\n * Accepts both plain arrays (sync) and `AsyncIterable` (streaming).\n *\n * `process` controls whether items inside a batch run in parallel; this helper only\n * controls batch size and per-batch flushing.\n *\n * @example\n * ```ts\n * // parallel dispatch inside each batch\n * await forBatches(schemas, (batch) => Promise.all(batch.map(process)), { concurrency: 8 })\n *\n * // async iterable with a flush after every batch\n * await forBatches(stream.schemas, (batch) => dispatch(batch), { concurrency: 8, flush })\n * ```\n */\nexport async function forBatches<T>(\n source: readonly T[] | AsyncIterable<T>,\n process: (batch: T[]) => Promise<unknown>,\n options: ForBatchesOptions,\n): Promise<void> {\n const { concurrency, flush } = options\n\n if (Array.isArray(source)) {\n for (const batch of chunks(source, concurrency)) {\n await process(batch)\n if (flush) await flush()\n }\n return\n }\n\n const batch: T[] = []\n for await (const item of source) {\n batch.push(item)\n if (batch.length >= concurrency) {\n await process(batch.splice(0))\n\n if (flush) await flush()\n }\n }\n if (batch.length > 0) {\n await process(batch.splice(0))\n\n if (flush) await flush()\n }\n}\n\n/**\n * Runs `work`, passing `flush` as its periodic-flush callback, then calls\n * `flush` once more to drain any items that did not cross a flush boundary.\n *\n * @example\n * ```ts\n * await withDrain(\n * (flush) => processItems(items, { flush }),\n * () => writeRemainingFiles(),\n * )\n * ```\n */\nexport async function withDrain(work: (flush: () => Promise<void>) => Promise<void>, flush: () => Promise<void>): Promise<void> {\n await work(flush)\n await flush()\n}\n\n/** A value that may already be resolved or still pending.\n *\n * @example\n * ```ts\n * function load(id: string): PossiblePromise<string> {\n * return cache.get(id) ?? fetchRemote(id)\n * }\n * ```\n */\nexport type PossiblePromise<T> = Promise<T> | T\n\n/** Returns `true` when `result` is a thenable `Promise`.\n *\n * @example\n * ```ts\n * isPromise(Promise.resolve(1)) // true\n * isPromise(42) // false\n * ```\n */\nexport function isPromise<T>(result: PossiblePromise<T>): result is Promise<T> {\n return result !== null && result !== undefined && typeof (result as Record<string, unknown>)['then'] === 'function'\n}\n\n/** Returns `true` when `result` is a fulfilled `Promise.allSettled` result.\n *\n * @example\n * ```ts\n * const results = await Promise.allSettled([p1, p2])\n * results.filter(isPromiseFulfilledResult).map((r) => r.value)\n * ```\n */\nexport function isPromiseFulfilledResult<T = unknown>(result: PromiseSettledResult<unknown>): result is PromiseFulfilledResult<T> {\n return result.status === 'fulfilled'\n}\n\n/** Returns `true` when `result` is a rejected `Promise.allSettled` result with a typed `reason`.\n *\n * @example\n * ```ts\n * const results = await Promise.allSettled([p1, p2])\n * results.filter(isPromiseRejectedResult<Error>).map((r) => r.reason.message)\n * ```\n */\nexport function isPromiseRejectedResult<T>(result: PromiseSettledResult<unknown>): result is Omit<PromiseRejectedResult, 'reason'> & { reason: T } {\n return result.status === 'rejected'\n}\n\n/**\n * Returns a wrapper that caches the result of the first invocation and replays\n * it for every subsequent call, ignoring later arguments.\n *\n * Works for sync and async factories — for async, the cached value is the\n * promise itself, so concurrent callers share one in-flight execution and\n * cannot race each other.\n *\n * @example\n * ```ts\n * const loadDocument = once(async (path: string) => parse(await readFile(path)))\n * const a = loadDocument('./a.yaml') // parses\n * const b = loadDocument('./b.yaml') // returns the cached promise from the first call\n * ```\n */\nexport function once<TArgs extends unknown[], TReturn>(factory: (...args: TArgs) => TReturn): (...args: TArgs) => TReturn {\n let cache: { value: TReturn } | undefined\n return (...args: TArgs): TReturn => {\n if (!cache) cache = { value: factory(...args) }\n return cache.value\n }\n}\n\ntype Store<TKey, TValue> = {\n has(key: TKey): boolean\n get(key: TKey): TValue | undefined\n set(key: TKey, value: TValue): unknown\n}\n\n/**\n * Wraps `factory` with a keyed cache backed by the provided store.\n *\n * Pass a `WeakMap` for object keys (results are GC-eligible when the key is\n * collected) or a `Map` for primitive keys. For multi-argument functions,\n * nest two `memoize` calls — the outer keyed by the first argument, the\n * inner (created once per outer miss) keyed by the second.\n *\n * Because the cache is owned by the caller, it can be shared, inspected, or\n * cleared independently of the memoized function.\n *\n * @example Single WeakMap key\n * ```ts\n * const cache = new WeakMap<SchemaNode, Set<string>>()\n * const getRefs = memoize(cache, (node) => collectRefs(node))\n * ```\n *\n * @example Single Map key (primitive)\n * ```ts\n * const cache = new Map<string, Resolver>()\n * const getResolver = memoize(cache, (name) => buildResolver(name))\n * ```\n *\n * @example Two-level (object + primitive)\n * ```ts\n * const outer = new WeakMap<Params[], Map<string, Params[]>>()\n * const fn = memoize(outer, (params) => memoize(new Map(), (key) => transform(params, key)))\n * fn(params)('camelcase')\n * ```\n */\nexport function memoize<TKey, TValue>(store: Store<TKey, TValue>, factory: (key: TKey) => TValue): (key: TKey) => TValue {\n return (key: TKey): TValue => {\n if (store.has(key)) return store.get(key)!\n const value = factory(key)\n store.set(key, value)\n return value\n }\n}\n\n/**\n * Wraps a plain array in a reusable `AsyncIterable`.\n * Each `[Symbol.asyncIterator]()` call returns a fresh generator so the\n * iterable can be consumed multiple times (e.g. once per plugin pre-scan).\n *\n * @example\n * ```ts\n * const stream = arrayToAsyncIterable([1, 2, 3])\n * for await (const n of stream) console.log(n) // 1, 2, 3\n * ```\n */\nexport function arrayToAsyncIterable<T>(arr: readonly T[]): AsyncIterable<T> {\n return {\n [Symbol.asyncIterator]() {\n return (async function* () {\n yield* arr\n })()\n },\n }\n}\n","import { isPromise, type PossiblePromise } from '@internals/utils'\nimport { adapterOas } from '@kubb/adapter-oas'\nimport type { CLIOptions, UserConfig } from '@kubb/core'\nimport { middlewareBarrel, middlewareBarrelName } from '@kubb/middleware-barrel'\nimport { parserTs, parserTsx } from '@kubb/parser-ts'\nimport { parserMd } from '@kubb/parser-md'\n\ntype AnyConfigResult = UserConfig<any> | Array<UserConfig<any>>\ntype ConfigInput = AnyConfigResult | Promise<AnyConfigResult> | ((cli: CLIOptions) => PossiblePromise<AnyConfigResult>)\ntype NormalizeConfig<TConfig> =\n TConfig extends Array<UserConfig<infer TInput>> ? Array<UserConfig<TInput>> : TConfig extends UserConfig<infer TInput> ? UserConfig<TInput> : never\ntype DefinedConfig<TConfig extends ConfigInput> = TConfig extends (cli: CLIOptions) => PossiblePromise<infer TResult>\n ? (cli: CLIOptions) => Promise<NormalizeConfig<TResult>>\n : TConfig extends Promise<infer TResult>\n ? Promise<NormalizeConfig<TResult>>\n : NormalizeConfig<TConfig>\n\n/**\n * Applies default `root`, adapter, parsers, middleware, `output.barrel`, `output.format`, and `output.lint` to a single user config when not set.\n *\n * - `root` defaults to `process.cwd()`\n * - `adapter` defaults to `adapterOas()`\n * - `parsers` defaults to `[parserTs, parserTsx]`\n * - `middleware` defaults to `[middlewareBarrel()]`\n * - `output.barrel` defaults to `{ type: 'named' }` **only when `middlewareBarrel` is part of `middleware`**.\n * When the user provides a custom middleware list without `middlewareBarrel`, `barrel` is left untouched.\n * - `output.format` defaults to `false`\n * - `output.lint` defaults to `false`\n */\nfunction applyDefaults<TInput>(config: UserConfig<TInput>): UserConfig<TInput> {\n const middleware = config.middleware?.length ? config.middleware : [middlewareBarrel()]\n const hasBarrelMiddleware = middleware.some((m) => m.name === middlewareBarrelName)\n\n const output = { ...config.output }\n if (hasBarrelMiddleware && output.barrel === undefined) {\n output.barrel = { type: 'named' }\n }\n if (output.format === undefined) {\n output.format = false\n }\n if (output.lint === undefined) {\n output.lint = false\n }\n\n return {\n ...config,\n root: config.root || process.cwd(),\n adapter: config.adapter ?? adapterOas(),\n parsers: config.parsers?.length ? config.parsers : [parserTs, parserTsx, parserMd],\n middleware,\n output,\n }\n}\n\nfunction normalizeConfig<TInput>(config: UserConfig<TInput> | Array<UserConfig<TInput>>): UserConfig<TInput> | Array<UserConfig<TInput>> {\n if (Array.isArray(config)) {\n return config.map(applyDefaults)\n }\n\n return applyDefaults(config)\n}\n\n/**\n * Defines a Kubb build configuration and applies sensible defaults so the\n * minimal config stays small.\n *\n * Defaults applied when omitted:\n * - `adapter` → `adapterOas()` (OpenAPI 2.0/3.0/3.1).\n * - `parsers` → `[parserTs, parserTsx]`.\n * - `middleware` → `[middlewareBarrel()]`.\n * - `output.barrel` → `{ type: 'named' }` only when `middlewareBarrel` is\n * in the middleware list.\n * - `output.format` and `output.lint` → `false`.\n *\n * Accepts a config object, an array of configs, a Promise resolving to one,\n * or a function that receives the parsed CLI options and returns any of the\n * above. The return type is preserved so async/array variants stay typed.\n *\n * @example\n * ```ts\n * import { defineConfig } from 'kubb'\n * import { pluginTs } from '@kubb/plugin-ts'\n *\n * export default defineConfig({\n * input: { path: './petStore.yaml' },\n * output: { path: './src/gen' },\n * plugins: [pluginTs()],\n * })\n * ```\n *\n * @example Function form with CLI options\n * ```ts\n * import { defineConfig } from 'kubb'\n *\n * export default defineConfig(({ input }) => ({\n * input: { path: input ?? './petStore.yaml' },\n * output: { path: './src/gen' },\n * plugins: [],\n * }))\n * ```\n */\nexport function defineConfig<TConfig extends ConfigInput>(config: TConfig): DefinedConfig<TConfig> {\n if (typeof config === 'function') {\n return (async (cli: CLIOptions) => {\n return normalizeConfig(await config(cli))\n }) as DefinedConfig<TConfig>\n }\n\n if (isPromise(config)) {\n return config.then((resolved) => normalizeConfig(resolved)) as DefinedConfig<TConfig>\n }\n\n return normalizeConfig(config) as DefinedConfig<TConfig>\n}\n"],"mappings":";;;;;;;;;;;;;;;AA0GA,SAAgB,UAAa,QAAkD;CAC7E,OAAO,WAAW,QAAQ,WAAW,KAAA,KAAa,OAAQ,OAAmC,YAAY;;;;;;;;;;;;;;;;AC9E3G,SAAS,cAAsB,QAAgD;CAC7E,MAAM,aAAa,OAAO,YAAY,SAAS,OAAO,aAAa,EAAA,GAAA,wBAAA,mBAAmB,CAAC;CACvF,MAAM,sBAAsB,WAAW,MAAM,MAAM,EAAE,SAASA,wBAAAA,qBAAqB;CAEnF,MAAM,SAAS,EAAE,GAAG,OAAO,QAAQ;CACnC,IAAI,uBAAuB,OAAO,WAAW,KAAA,GAC3C,OAAO,SAAS,EAAE,MAAM,SAAS;CAEnC,IAAI,OAAO,WAAW,KAAA,GACpB,OAAO,SAAS;CAElB,IAAI,OAAO,SAAS,KAAA,GAClB,OAAO,OAAO;CAGhB,OAAO;EACL,GAAG;EACH,MAAM,OAAO,QAAQ,QAAQ,KAAK;EAClC,SAAS,OAAO,YAAA,GAAA,kBAAA,aAAuB;EACvC,SAAS,OAAO,SAAS,SAAS,OAAO,UAAU;GAACC,gBAAAA;GAAUC,gBAAAA;GAAWC,gBAAAA;GAAS;EAClF;EACA;EACD;;AAGH,SAAS,gBAAwB,QAAwG;CACvI,IAAI,MAAM,QAAQ,OAAO,EACvB,OAAO,OAAO,IAAI,cAAc;CAGlC,OAAO,cAAc,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0C9B,SAAgB,aAA0C,QAAyC;CACjG,IAAI,OAAO,WAAW,YACpB,QAAQ,OAAO,QAAoB;EACjC,OAAO,gBAAgB,MAAM,OAAO,IAAI,CAAC;;CAI7C,IAAI,UAAU,OAAO,EACnB,OAAO,OAAO,MAAM,aAAa,gBAAgB,SAAS,CAAC;CAG7D,OAAO,gBAAgB,OAAO"}
|
package/dist/index.d.ts
CHANGED
|
@@ -20,21 +20,41 @@ type ConfigInput = AnyConfigResult | Promise<AnyConfigResult> | ((cli: CLIOption
|
|
|
20
20
|
type NormalizeConfig<TConfig> = TConfig extends Array<UserConfig<infer TInput>> ? Array<UserConfig<TInput>> : TConfig extends UserConfig<infer TInput> ? UserConfig<TInput> : never;
|
|
21
21
|
type DefinedConfig<TConfig extends ConfigInput> = TConfig extends ((cli: CLIOptions) => PossiblePromise<infer TResult>) ? (cli: CLIOptions) => Promise<NormalizeConfig<TResult>> : TConfig extends Promise<infer TResult> ? Promise<NormalizeConfig<TResult>> : NormalizeConfig<TConfig>;
|
|
22
22
|
/**
|
|
23
|
-
*
|
|
23
|
+
* Defines a Kubb build configuration and applies sensible defaults so the
|
|
24
|
+
* minimal config stays small.
|
|
24
25
|
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
26
|
+
* Defaults applied when omitted:
|
|
27
|
+
* - `adapter` → `adapterOas()` (OpenAPI 2.0/3.0/3.1).
|
|
28
|
+
* - `parsers` → `[parserTs, parserTsx]`.
|
|
29
|
+
* - `middleware` → `[middlewareBarrel()]`.
|
|
30
|
+
* - `output.barrel` → `{ type: 'named' }` only when `middlewareBarrel` is
|
|
31
|
+
* in the middleware list.
|
|
32
|
+
* - `output.format` and `output.lint` → `false`.
|
|
27
33
|
*
|
|
28
|
-
* Accepts
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
* receiving the CLI options as argument
|
|
34
|
+
* Accepts a config object, an array of configs, a Promise resolving to one,
|
|
35
|
+
* or a function that receives the parsed CLI options and returns any of the
|
|
36
|
+
* above. The return type is preserved so async/array variants stay typed.
|
|
32
37
|
*
|
|
33
38
|
* @example
|
|
34
39
|
* ```ts
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
40
|
+
* import { defineConfig } from 'kubb'
|
|
41
|
+
* import { pluginTs } from '@kubb/plugin-ts'
|
|
42
|
+
*
|
|
43
|
+
* export default defineConfig({
|
|
44
|
+
* input: { path: './petStore.yaml' },
|
|
45
|
+
* output: { path: './src/gen' },
|
|
46
|
+
* plugins: [pluginTs()],
|
|
47
|
+
* })
|
|
48
|
+
* ```
|
|
49
|
+
*
|
|
50
|
+
* @example Function form with CLI options
|
|
51
|
+
* ```ts
|
|
52
|
+
* import { defineConfig } from 'kubb'
|
|
53
|
+
*
|
|
54
|
+
* export default defineConfig(({ input }) => ({
|
|
55
|
+
* input: { path: input ?? './petStore.yaml' },
|
|
56
|
+
* output: { path: './src/gen' },
|
|
57
|
+
* plugins: [],
|
|
38
58
|
* }))
|
|
39
59
|
* ```
|
|
40
60
|
*/
|
package/dist/index.js
CHANGED
|
@@ -2,6 +2,7 @@ import "./chunk--u3MIqq1.js";
|
|
|
2
2
|
import { adapterOas } from "@kubb/adapter-oas";
|
|
3
3
|
import { middlewareBarrel, middlewareBarrelName } from "@kubb/middleware-barrel";
|
|
4
4
|
import { parserTs, parserTsx } from "@kubb/parser-ts";
|
|
5
|
+
import { parserMd } from "@kubb/parser-md";
|
|
5
6
|
//#region ../../internals/utils/src/promise.ts
|
|
6
7
|
/** Returns `true` when `result` is a thenable `Promise`.
|
|
7
8
|
*
|
|
@@ -17,15 +18,16 @@ function isPromise(result) {
|
|
|
17
18
|
//#endregion
|
|
18
19
|
//#region src/defineConfig.ts
|
|
19
20
|
/**
|
|
20
|
-
* Applies default adapter, parsers, middleware, `output.barrel`, `output.format`, and `output.lint` to a single user config when not set.
|
|
21
|
+
* Applies default `root`, adapter, parsers, middleware, `output.barrel`, `output.format`, and `output.lint` to a single user config when not set.
|
|
21
22
|
*
|
|
23
|
+
* - `root` defaults to `process.cwd()`
|
|
22
24
|
* - `adapter` defaults to `adapterOas()`
|
|
23
25
|
* - `parsers` defaults to `[parserTs, parserTsx]`
|
|
24
26
|
* - `middleware` defaults to `[middlewareBarrel()]`
|
|
25
27
|
* - `output.barrel` defaults to `{ type: 'named' }` **only when `middlewareBarrel` is part of `middleware`**.
|
|
26
28
|
* When the user provides a custom middleware list without `middlewareBarrel`, `barrel` is left untouched.
|
|
27
|
-
* - `output.format` defaults to `
|
|
28
|
-
* - `output.lint` defaults to `
|
|
29
|
+
* - `output.format` defaults to `false`
|
|
30
|
+
* - `output.lint` defaults to `false`
|
|
29
31
|
*/
|
|
30
32
|
function applyDefaults(config) {
|
|
31
33
|
const middleware = config.middleware?.length ? config.middleware : [middlewareBarrel()];
|
|
@@ -36,8 +38,13 @@ function applyDefaults(config) {
|
|
|
36
38
|
if (output.lint === void 0) output.lint = false;
|
|
37
39
|
return {
|
|
38
40
|
...config,
|
|
41
|
+
root: config.root || process.cwd(),
|
|
39
42
|
adapter: config.adapter ?? adapterOas(),
|
|
40
|
-
parsers: config.parsers?.length ? config.parsers : [
|
|
43
|
+
parsers: config.parsers?.length ? config.parsers : [
|
|
44
|
+
parserTs,
|
|
45
|
+
parserTsx,
|
|
46
|
+
parserMd
|
|
47
|
+
],
|
|
41
48
|
middleware,
|
|
42
49
|
output
|
|
43
50
|
};
|
|
@@ -47,21 +54,41 @@ function normalizeConfig(config) {
|
|
|
47
54
|
return applyDefaults(config);
|
|
48
55
|
}
|
|
49
56
|
/**
|
|
50
|
-
*
|
|
57
|
+
* Defines a Kubb build configuration and applies sensible defaults so the
|
|
58
|
+
* minimal config stays small.
|
|
51
59
|
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
60
|
+
* Defaults applied when omitted:
|
|
61
|
+
* - `adapter` → `adapterOas()` (OpenAPI 2.0/3.0/3.1).
|
|
62
|
+
* - `parsers` → `[parserTs, parserTsx]`.
|
|
63
|
+
* - `middleware` → `[middlewareBarrel()]`.
|
|
64
|
+
* - `output.barrel` → `{ type: 'named' }` only when `middlewareBarrel` is
|
|
65
|
+
* in the middleware list.
|
|
66
|
+
* - `output.format` and `output.lint` → `false`.
|
|
54
67
|
*
|
|
55
|
-
* Accepts
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
* receiving the CLI options as argument
|
|
68
|
+
* Accepts a config object, an array of configs, a Promise resolving to one,
|
|
69
|
+
* or a function that receives the parsed CLI options and returns any of the
|
|
70
|
+
* above. The return type is preserved so async/array variants stay typed.
|
|
59
71
|
*
|
|
60
72
|
* @example
|
|
61
73
|
* ```ts
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
74
|
+
* import { defineConfig } from 'kubb'
|
|
75
|
+
* import { pluginTs } from '@kubb/plugin-ts'
|
|
76
|
+
*
|
|
77
|
+
* export default defineConfig({
|
|
78
|
+
* input: { path: './petStore.yaml' },
|
|
79
|
+
* output: { path: './src/gen' },
|
|
80
|
+
* plugins: [pluginTs()],
|
|
81
|
+
* })
|
|
82
|
+
* ```
|
|
83
|
+
*
|
|
84
|
+
* @example Function form with CLI options
|
|
85
|
+
* ```ts
|
|
86
|
+
* import { defineConfig } from 'kubb'
|
|
87
|
+
*
|
|
88
|
+
* export default defineConfig(({ input }) => ({
|
|
89
|
+
* input: { path: input ?? './petStore.yaml' },
|
|
90
|
+
* output: { path: './src/gen' },
|
|
91
|
+
* plugins: [],
|
|
65
92
|
* }))
|
|
66
93
|
* ```
|
|
67
94
|
*/
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../../../internals/utils/src/promise.ts","../src/defineConfig.ts"],"sourcesContent":["/** A value that may already be resolved or still pending.\n *\n * @example\n * ```ts\n * function load(id: string): PossiblePromise<string> {\n * return cache.get(id) ?? fetchRemote(id)\n * }\n * ```\n */\nexport type PossiblePromise<T> = Promise<T> | T\n\n/** Returns `true` when `result` is a thenable `Promise`.\n *\n * @example\n * ```ts\n * isPromise(Promise.resolve(1)) // true\n * isPromise(42) // false\n * ```\n */\nexport function isPromise<T>(result: PossiblePromise<T>): result is Promise<T> {\n return result !== null && result !== undefined && typeof (result as Record<string, unknown>)['then'] === 'function'\n}\n\n/** Returns `true` when `result` is a fulfilled `Promise.allSettled` result.\n *\n * @example\n * ```ts\n * const results = await Promise.allSettled([p1, p2])\n * results.filter(isPromiseFulfilledResult).map((r) => r.value)\n * ```\n */\nexport function isPromiseFulfilledResult<T = unknown>(result: PromiseSettledResult<unknown>): result is PromiseFulfilledResult<T> {\n return result.status === 'fulfilled'\n}\n\n/** Returns `true` when `result` is a rejected `Promise.allSettled` result with a typed `reason`.\n *\n * @example\n * ```ts\n * const results = await Promise.allSettled([p1, p2])\n * results.filter(isPromiseRejectedResult<Error>).map((r) => r.reason.message)\n * ```\n */\nexport function isPromiseRejectedResult<T>(result: PromiseSettledResult<unknown>): result is Omit<PromiseRejectedResult, 'reason'> & { reason: T } {\n return result.status === 'rejected'\n}\n","import { isPromise, type PossiblePromise } from '@internals/utils'\nimport { adapterOas } from '@kubb/adapter-oas'\nimport type { CLIOptions, UserConfig } from '@kubb/core'\nimport { middlewareBarrel, middlewareBarrelName } from '@kubb/middleware-barrel'\nimport { parserTs, parserTsx } from '@kubb/parser-ts'\n\ntype AnyConfigResult = UserConfig<any> | Array<UserConfig<any>>\ntype ConfigInput = AnyConfigResult | Promise<AnyConfigResult> | ((cli: CLIOptions) => PossiblePromise<AnyConfigResult>)\ntype NormalizeConfig<TConfig> =\n TConfig extends Array<UserConfig<infer TInput>> ? Array<UserConfig<TInput>> : TConfig extends UserConfig<infer TInput> ? UserConfig<TInput> : never\ntype DefinedConfig<TConfig extends ConfigInput> = TConfig extends (cli: CLIOptions) => PossiblePromise<infer TResult>\n ? (cli: CLIOptions) => Promise<NormalizeConfig<TResult>>\n : TConfig extends Promise<infer TResult>\n ? Promise<NormalizeConfig<TResult>>\n : NormalizeConfig<TConfig>\n\n/**\n * Applies default adapter, parsers, middleware, `output.barrel`, `output.format`, and `output.lint` to a single user config when not set.\n *\n * - `adapter` defaults to `adapterOas()`\n * - `parsers` defaults to `[parserTs, parserTsx]`\n * - `middleware` defaults to `[middlewareBarrel()]`\n * - `output.barrel` defaults to `{ type: 'named' }` **only when `middlewareBarrel` is part of `middleware`**.\n * When the user provides a custom middleware list without `middlewareBarrel`, `barrel` is left untouched.\n * - `output.format` defaults to `'auto'`\n * - `output.lint` defaults to `'auto'`\n */\nfunction applyDefaults<TInput>(config: UserConfig<TInput>): UserConfig<TInput> {\n const middleware = config.middleware?.length ? config.middleware : [middlewareBarrel()]\n const hasBarrelMiddleware = middleware.some((m) => m.name === middlewareBarrelName)\n\n const output = { ...config.output }\n if (hasBarrelMiddleware && output.barrel === undefined) {\n output.barrel = { type: 'named' }\n }\n if (output.format === undefined) {\n output.format = false\n }\n if (output.lint === undefined) {\n output.lint = false\n }\n\n return {\n ...config,\n adapter: config.adapter ?? adapterOas(),\n parsers: config.parsers?.length ? config.parsers : [parserTs, parserTsx],\n middleware,\n output,\n }\n}\n\nfunction normalizeConfig<TInput>(config: UserConfig<TInput> | Array<UserConfig<TInput>>): UserConfig<TInput> | Array<UserConfig<TInput>> {\n if (Array.isArray(config)) {\n return config.map(applyDefaults)\n }\n\n return applyDefaults(config)\n}\n\n/**\n * Helper for defining a Kubb configuration with built-in defaults.\n *\n * When no `adapter` is provided, `adapterOas()` is used automatically.\n * When no `parsers` are provided, `[parserTs, parserTsx]` is used automatically.\n *\n * Accepts either:\n * - A config object or array of configs\n * - A function returning the config(s), optionally async,\n * receiving the CLI options as argument\n *\n * @example\n * ```ts\n * export default defineConfig(({ logLevel }) => ({\n * root: 'src',\n * plugins: [myPlugin()],\n * }))\n * ```\n */\nexport function defineConfig<TConfig extends ConfigInput>(config: TConfig): DefinedConfig<TConfig> {\n if (typeof config === 'function') {\n return (async (cli: CLIOptions) => {\n return normalizeConfig(await config(cli))\n }) as DefinedConfig<TConfig>\n }\n\n if (isPromise(config)) {\n return config.then((resolved) => normalizeConfig(resolved)) as DefinedConfig<TConfig>\n }\n\n return normalizeConfig(config) as DefinedConfig<TConfig>\n}\n"],"mappings":";;;;;;;;;;;;;AAmBA,SAAgB,UAAa,QAAkD;AAC7E,QAAO,WAAW,QAAQ,WAAW,KAAA,KAAa,OAAQ,OAAmC,YAAY;;;;;;;;;;;;;;;ACO3G,SAAS,cAAsB,QAAgD;CAC7E,MAAM,aAAa,OAAO,YAAY,SAAS,OAAO,aAAa,CAAC,kBAAkB,CAAC;CACvF,MAAM,sBAAsB,WAAW,MAAM,MAAM,EAAE,SAAS,qBAAqB;CAEnF,MAAM,SAAS,EAAE,GAAG,OAAO,QAAQ;AACnC,KAAI,uBAAuB,OAAO,WAAW,KAAA,EAC3C,QAAO,SAAS,EAAE,MAAM,SAAS;AAEnC,KAAI,OAAO,WAAW,KAAA,EACpB,QAAO,SAAS;AAElB,KAAI,OAAO,SAAS,KAAA,EAClB,QAAO,OAAO;AAGhB,QAAO;EACL,GAAG;EACH,SAAS,OAAO,WAAW,YAAY;EACvC,SAAS,OAAO,SAAS,SAAS,OAAO,UAAU,CAAC,UAAU,UAAU;EACxE;EACA;EACD;;AAGH,SAAS,gBAAwB,QAAwG;AACvI,KAAI,MAAM,QAAQ,OAAO,CACvB,QAAO,OAAO,IAAI,cAAc;AAGlC,QAAO,cAAc,OAAO;;;;;;;;;;;;;;;;;;;;;AAsB9B,SAAgB,aAA0C,QAAyC;AACjG,KAAI,OAAO,WAAW,WACpB,SAAQ,OAAO,QAAoB;AACjC,SAAO,gBAAgB,MAAM,OAAO,IAAI,CAAC;;AAI7C,KAAI,UAAU,OAAO,CACnB,QAAO,OAAO,MAAM,aAAa,gBAAgB,SAAS,CAAC;AAG7D,QAAO,gBAAgB,OAAO"}
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../../internals/utils/src/promise.ts","../src/defineConfig.ts"],"sourcesContent":["function* chunks<T>(arr: readonly T[], size: number): Generator<T[]> {\n for (let i = 0; i < arr.length; i += size) {\n yield arr.slice(i, i + size)\n }\n}\n\nexport type ForBatchesOptions = {\n /**\n * Maximum batch size handed to `process`.\n * Parallel dispatch within a batch is the caller's responsibility\n * (typically via `Promise.all(batch.map(...))`).\n */\n concurrency: number\n /**\n * Called after every batch.\n *\n * Use a cheap, idempotent callback (e.g. one that short-circuits when there\n * is nothing new to do). The helper does not coalesce calls — if you need\n * throttling, do it inside `flush` itself.\n */\n flush?: () => Promise<void>\n}\n\n/**\n * Slices `source` into batches of `concurrency` items and awaits `process` for each batch.\n * Accepts both plain arrays (sync) and `AsyncIterable` (streaming).\n *\n * `process` controls whether items inside a batch run in parallel; this helper only\n * controls batch size and per-batch flushing.\n *\n * @example\n * ```ts\n * // parallel dispatch inside each batch\n * await forBatches(schemas, (batch) => Promise.all(batch.map(process)), { concurrency: 8 })\n *\n * // async iterable with a flush after every batch\n * await forBatches(stream.schemas, (batch) => dispatch(batch), { concurrency: 8, flush })\n * ```\n */\nexport async function forBatches<T>(\n source: readonly T[] | AsyncIterable<T>,\n process: (batch: T[]) => Promise<unknown>,\n options: ForBatchesOptions,\n): Promise<void> {\n const { concurrency, flush } = options\n\n if (Array.isArray(source)) {\n for (const batch of chunks(source, concurrency)) {\n await process(batch)\n if (flush) await flush()\n }\n return\n }\n\n const batch: T[] = []\n for await (const item of source) {\n batch.push(item)\n if (batch.length >= concurrency) {\n await process(batch.splice(0))\n\n if (flush) await flush()\n }\n }\n if (batch.length > 0) {\n await process(batch.splice(0))\n\n if (flush) await flush()\n }\n}\n\n/**\n * Runs `work`, passing `flush` as its periodic-flush callback, then calls\n * `flush` once more to drain any items that did not cross a flush boundary.\n *\n * @example\n * ```ts\n * await withDrain(\n * (flush) => processItems(items, { flush }),\n * () => writeRemainingFiles(),\n * )\n * ```\n */\nexport async function withDrain(work: (flush: () => Promise<void>) => Promise<void>, flush: () => Promise<void>): Promise<void> {\n await work(flush)\n await flush()\n}\n\n/** A value that may already be resolved or still pending.\n *\n * @example\n * ```ts\n * function load(id: string): PossiblePromise<string> {\n * return cache.get(id) ?? fetchRemote(id)\n * }\n * ```\n */\nexport type PossiblePromise<T> = Promise<T> | T\n\n/** Returns `true` when `result` is a thenable `Promise`.\n *\n * @example\n * ```ts\n * isPromise(Promise.resolve(1)) // true\n * isPromise(42) // false\n * ```\n */\nexport function isPromise<T>(result: PossiblePromise<T>): result is Promise<T> {\n return result !== null && result !== undefined && typeof (result as Record<string, unknown>)['then'] === 'function'\n}\n\n/** Returns `true` when `result` is a fulfilled `Promise.allSettled` result.\n *\n * @example\n * ```ts\n * const results = await Promise.allSettled([p1, p2])\n * results.filter(isPromiseFulfilledResult).map((r) => r.value)\n * ```\n */\nexport function isPromiseFulfilledResult<T = unknown>(result: PromiseSettledResult<unknown>): result is PromiseFulfilledResult<T> {\n return result.status === 'fulfilled'\n}\n\n/** Returns `true` when `result` is a rejected `Promise.allSettled` result with a typed `reason`.\n *\n * @example\n * ```ts\n * const results = await Promise.allSettled([p1, p2])\n * results.filter(isPromiseRejectedResult<Error>).map((r) => r.reason.message)\n * ```\n */\nexport function isPromiseRejectedResult<T>(result: PromiseSettledResult<unknown>): result is Omit<PromiseRejectedResult, 'reason'> & { reason: T } {\n return result.status === 'rejected'\n}\n\n/**\n * Returns a wrapper that caches the result of the first invocation and replays\n * it for every subsequent call, ignoring later arguments.\n *\n * Works for sync and async factories — for async, the cached value is the\n * promise itself, so concurrent callers share one in-flight execution and\n * cannot race each other.\n *\n * @example\n * ```ts\n * const loadDocument = once(async (path: string) => parse(await readFile(path)))\n * const a = loadDocument('./a.yaml') // parses\n * const b = loadDocument('./b.yaml') // returns the cached promise from the first call\n * ```\n */\nexport function once<TArgs extends unknown[], TReturn>(factory: (...args: TArgs) => TReturn): (...args: TArgs) => TReturn {\n let cache: { value: TReturn } | undefined\n return (...args: TArgs): TReturn => {\n if (!cache) cache = { value: factory(...args) }\n return cache.value\n }\n}\n\ntype Store<TKey, TValue> = {\n has(key: TKey): boolean\n get(key: TKey): TValue | undefined\n set(key: TKey, value: TValue): unknown\n}\n\n/**\n * Wraps `factory` with a keyed cache backed by the provided store.\n *\n * Pass a `WeakMap` for object keys (results are GC-eligible when the key is\n * collected) or a `Map` for primitive keys. For multi-argument functions,\n * nest two `memoize` calls — the outer keyed by the first argument, the\n * inner (created once per outer miss) keyed by the second.\n *\n * Because the cache is owned by the caller, it can be shared, inspected, or\n * cleared independently of the memoized function.\n *\n * @example Single WeakMap key\n * ```ts\n * const cache = new WeakMap<SchemaNode, Set<string>>()\n * const getRefs = memoize(cache, (node) => collectRefs(node))\n * ```\n *\n * @example Single Map key (primitive)\n * ```ts\n * const cache = new Map<string, Resolver>()\n * const getResolver = memoize(cache, (name) => buildResolver(name))\n * ```\n *\n * @example Two-level (object + primitive)\n * ```ts\n * const outer = new WeakMap<Params[], Map<string, Params[]>>()\n * const fn = memoize(outer, (params) => memoize(new Map(), (key) => transform(params, key)))\n * fn(params)('camelcase')\n * ```\n */\nexport function memoize<TKey, TValue>(store: Store<TKey, TValue>, factory: (key: TKey) => TValue): (key: TKey) => TValue {\n return (key: TKey): TValue => {\n if (store.has(key)) return store.get(key)!\n const value = factory(key)\n store.set(key, value)\n return value\n }\n}\n\n/**\n * Wraps a plain array in a reusable `AsyncIterable`.\n * Each `[Symbol.asyncIterator]()` call returns a fresh generator so the\n * iterable can be consumed multiple times (e.g. once per plugin pre-scan).\n *\n * @example\n * ```ts\n * const stream = arrayToAsyncIterable([1, 2, 3])\n * for await (const n of stream) console.log(n) // 1, 2, 3\n * ```\n */\nexport function arrayToAsyncIterable<T>(arr: readonly T[]): AsyncIterable<T> {\n return {\n [Symbol.asyncIterator]() {\n return (async function* () {\n yield* arr\n })()\n },\n }\n}\n","import { isPromise, type PossiblePromise } from '@internals/utils'\nimport { adapterOas } from '@kubb/adapter-oas'\nimport type { CLIOptions, UserConfig } from '@kubb/core'\nimport { middlewareBarrel, middlewareBarrelName } from '@kubb/middleware-barrel'\nimport { parserTs, parserTsx } from '@kubb/parser-ts'\nimport { parserMd } from '@kubb/parser-md'\n\ntype AnyConfigResult = UserConfig<any> | Array<UserConfig<any>>\ntype ConfigInput = AnyConfigResult | Promise<AnyConfigResult> | ((cli: CLIOptions) => PossiblePromise<AnyConfigResult>)\ntype NormalizeConfig<TConfig> =\n TConfig extends Array<UserConfig<infer TInput>> ? Array<UserConfig<TInput>> : TConfig extends UserConfig<infer TInput> ? UserConfig<TInput> : never\ntype DefinedConfig<TConfig extends ConfigInput> = TConfig extends (cli: CLIOptions) => PossiblePromise<infer TResult>\n ? (cli: CLIOptions) => Promise<NormalizeConfig<TResult>>\n : TConfig extends Promise<infer TResult>\n ? Promise<NormalizeConfig<TResult>>\n : NormalizeConfig<TConfig>\n\n/**\n * Applies default `root`, adapter, parsers, middleware, `output.barrel`, `output.format`, and `output.lint` to a single user config when not set.\n *\n * - `root` defaults to `process.cwd()`\n * - `adapter` defaults to `adapterOas()`\n * - `parsers` defaults to `[parserTs, parserTsx]`\n * - `middleware` defaults to `[middlewareBarrel()]`\n * - `output.barrel` defaults to `{ type: 'named' }` **only when `middlewareBarrel` is part of `middleware`**.\n * When the user provides a custom middleware list without `middlewareBarrel`, `barrel` is left untouched.\n * - `output.format` defaults to `false`\n * - `output.lint` defaults to `false`\n */\nfunction applyDefaults<TInput>(config: UserConfig<TInput>): UserConfig<TInput> {\n const middleware = config.middleware?.length ? config.middleware : [middlewareBarrel()]\n const hasBarrelMiddleware = middleware.some((m) => m.name === middlewareBarrelName)\n\n const output = { ...config.output }\n if (hasBarrelMiddleware && output.barrel === undefined) {\n output.barrel = { type: 'named' }\n }\n if (output.format === undefined) {\n output.format = false\n }\n if (output.lint === undefined) {\n output.lint = false\n }\n\n return {\n ...config,\n root: config.root || process.cwd(),\n adapter: config.adapter ?? adapterOas(),\n parsers: config.parsers?.length ? config.parsers : [parserTs, parserTsx, parserMd],\n middleware,\n output,\n }\n}\n\nfunction normalizeConfig<TInput>(config: UserConfig<TInput> | Array<UserConfig<TInput>>): UserConfig<TInput> | Array<UserConfig<TInput>> {\n if (Array.isArray(config)) {\n return config.map(applyDefaults)\n }\n\n return applyDefaults(config)\n}\n\n/**\n * Defines a Kubb build configuration and applies sensible defaults so the\n * minimal config stays small.\n *\n * Defaults applied when omitted:\n * - `adapter` → `adapterOas()` (OpenAPI 2.0/3.0/3.1).\n * - `parsers` → `[parserTs, parserTsx]`.\n * - `middleware` → `[middlewareBarrel()]`.\n * - `output.barrel` → `{ type: 'named' }` only when `middlewareBarrel` is\n * in the middleware list.\n * - `output.format` and `output.lint` → `false`.\n *\n * Accepts a config object, an array of configs, a Promise resolving to one,\n * or a function that receives the parsed CLI options and returns any of the\n * above. The return type is preserved so async/array variants stay typed.\n *\n * @example\n * ```ts\n * import { defineConfig } from 'kubb'\n * import { pluginTs } from '@kubb/plugin-ts'\n *\n * export default defineConfig({\n * input: { path: './petStore.yaml' },\n * output: { path: './src/gen' },\n * plugins: [pluginTs()],\n * })\n * ```\n *\n * @example Function form with CLI options\n * ```ts\n * import { defineConfig } from 'kubb'\n *\n * export default defineConfig(({ input }) => ({\n * input: { path: input ?? './petStore.yaml' },\n * output: { path: './src/gen' },\n * plugins: [],\n * }))\n * ```\n */\nexport function defineConfig<TConfig extends ConfigInput>(config: TConfig): DefinedConfig<TConfig> {\n if (typeof config === 'function') {\n return (async (cli: CLIOptions) => {\n return normalizeConfig(await config(cli))\n }) as DefinedConfig<TConfig>\n }\n\n if (isPromise(config)) {\n return config.then((resolved) => normalizeConfig(resolved)) as DefinedConfig<TConfig>\n }\n\n return normalizeConfig(config) as DefinedConfig<TConfig>\n}\n"],"mappings":";;;;;;;;;;;;;;AA0GA,SAAgB,UAAa,QAAkD;CAC7E,OAAO,WAAW,QAAQ,WAAW,KAAA,KAAa,OAAQ,OAAmC,YAAY;;;;;;;;;;;;;;;;AC9E3G,SAAS,cAAsB,QAAgD;CAC7E,MAAM,aAAa,OAAO,YAAY,SAAS,OAAO,aAAa,CAAC,kBAAkB,CAAC;CACvF,MAAM,sBAAsB,WAAW,MAAM,MAAM,EAAE,SAAS,qBAAqB;CAEnF,MAAM,SAAS,EAAE,GAAG,OAAO,QAAQ;CACnC,IAAI,uBAAuB,OAAO,WAAW,KAAA,GAC3C,OAAO,SAAS,EAAE,MAAM,SAAS;CAEnC,IAAI,OAAO,WAAW,KAAA,GACpB,OAAO,SAAS;CAElB,IAAI,OAAO,SAAS,KAAA,GAClB,OAAO,OAAO;CAGhB,OAAO;EACL,GAAG;EACH,MAAM,OAAO,QAAQ,QAAQ,KAAK;EAClC,SAAS,OAAO,WAAW,YAAY;EACvC,SAAS,OAAO,SAAS,SAAS,OAAO,UAAU;GAAC;GAAU;GAAW;GAAS;EAClF;EACA;EACD;;AAGH,SAAS,gBAAwB,QAAwG;CACvI,IAAI,MAAM,QAAQ,OAAO,EACvB,OAAO,OAAO,IAAI,cAAc;CAGlC,OAAO,cAAc,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0C9B,SAAgB,aAA0C,QAAyC;CACjG,IAAI,OAAO,WAAW,YACpB,QAAQ,OAAO,QAAoB;EACjC,OAAO,gBAAgB,MAAM,OAAO,IAAI,CAAC;;CAI7C,IAAI,UAAU,OAAO,EACnB,OAAO,OAAO,MAAM,aAAa,gBAAgB,SAAS,CAAC;CAG7D,OAAO,gBAAgB,OAAO"}
|
package/package.json
CHANGED
|
@@ -1,24 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "kubb",
|
|
3
|
-
"version": "5.0.0-beta.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "5.0.0-beta.31",
|
|
4
|
+
"description": "Meta-package and entry point for Kubb — a plugin-based code generation framework for OpenAPI. Includes defineConfig, all public APIs, and serves as the gateway to the entire Kubb ecosystem.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"api-client",
|
|
7
|
-
"cli",
|
|
8
7
|
"code-generator",
|
|
9
8
|
"codegen",
|
|
10
9
|
"faker",
|
|
11
10
|
"kubb",
|
|
11
|
+
"meta-framework",
|
|
12
12
|
"msw",
|
|
13
|
-
"oas",
|
|
14
13
|
"openapi",
|
|
15
14
|
"plugin-system",
|
|
16
|
-
"plugins",
|
|
17
15
|
"react-query",
|
|
18
16
|
"sdk-generator",
|
|
19
17
|
"swagger",
|
|
20
18
|
"tanstack-query",
|
|
21
|
-
"type-safe",
|
|
22
19
|
"typescript",
|
|
23
20
|
"zod"
|
|
24
21
|
],
|
|
@@ -57,27 +54,25 @@
|
|
|
57
54
|
"registry": "https://registry.npmjs.org/"
|
|
58
55
|
},
|
|
59
56
|
"dependencies": {
|
|
60
|
-
"@kubb/adapter-oas": "5.0.0-beta.
|
|
61
|
-
"@kubb/cli": "5.0.0-beta.
|
|
62
|
-
"@kubb/core": "5.0.0-beta.
|
|
63
|
-
"@kubb/
|
|
64
|
-
"@kubb/
|
|
65
|
-
"@kubb/
|
|
57
|
+
"@kubb/adapter-oas": "5.0.0-beta.31",
|
|
58
|
+
"@kubb/cli": "5.0.0-beta.31",
|
|
59
|
+
"@kubb/core": "5.0.0-beta.31",
|
|
60
|
+
"@kubb/mcp": "5.0.0-beta.31",
|
|
61
|
+
"@kubb/middleware-barrel": "5.0.0-beta.31",
|
|
62
|
+
"@kubb/parser-md": "5.0.0-beta.31",
|
|
63
|
+
"@kubb/parser-ts": "5.0.0-beta.31",
|
|
64
|
+
"@kubb/renderer-jsx": "5.0.0-beta.31"
|
|
66
65
|
},
|
|
67
66
|
"devDependencies": {
|
|
68
67
|
"typescript": "^6.0.3",
|
|
69
68
|
"@internals/utils": "0.0.0"
|
|
70
69
|
},
|
|
71
70
|
"peerDependencies": {
|
|
72
|
-
"@kubb/agent": "5.0.0-beta.
|
|
73
|
-
"@kubb/mcp": "5.0.0-beta.3"
|
|
71
|
+
"@kubb/agent": "5.0.0-beta.31"
|
|
74
72
|
},
|
|
75
73
|
"peerDependenciesMeta": {
|
|
76
74
|
"@kubb/agent": {
|
|
77
75
|
"optional": true
|
|
78
|
-
},
|
|
79
|
-
"@kubb/mcp": {
|
|
80
|
-
"optional": true
|
|
81
76
|
}
|
|
82
77
|
},
|
|
83
78
|
"size-limit": [
|
package/src/defineConfig.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { adapterOas } from '@kubb/adapter-oas'
|
|
|
3
3
|
import type { CLIOptions, UserConfig } from '@kubb/core'
|
|
4
4
|
import { middlewareBarrel, middlewareBarrelName } from '@kubb/middleware-barrel'
|
|
5
5
|
import { parserTs, parserTsx } from '@kubb/parser-ts'
|
|
6
|
+
import { parserMd } from '@kubb/parser-md'
|
|
6
7
|
|
|
7
8
|
type AnyConfigResult = UserConfig<any> | Array<UserConfig<any>>
|
|
8
9
|
type ConfigInput = AnyConfigResult | Promise<AnyConfigResult> | ((cli: CLIOptions) => PossiblePromise<AnyConfigResult>)
|
|
@@ -15,15 +16,16 @@ type DefinedConfig<TConfig extends ConfigInput> = TConfig extends (cli: CLIOptio
|
|
|
15
16
|
: NormalizeConfig<TConfig>
|
|
16
17
|
|
|
17
18
|
/**
|
|
18
|
-
* Applies default adapter, parsers, middleware, `output.barrel`, `output.format`, and `output.lint` to a single user config when not set.
|
|
19
|
+
* Applies default `root`, adapter, parsers, middleware, `output.barrel`, `output.format`, and `output.lint` to a single user config when not set.
|
|
19
20
|
*
|
|
21
|
+
* - `root` defaults to `process.cwd()`
|
|
20
22
|
* - `adapter` defaults to `adapterOas()`
|
|
21
23
|
* - `parsers` defaults to `[parserTs, parserTsx]`
|
|
22
24
|
* - `middleware` defaults to `[middlewareBarrel()]`
|
|
23
25
|
* - `output.barrel` defaults to `{ type: 'named' }` **only when `middlewareBarrel` is part of `middleware`**.
|
|
24
26
|
* When the user provides a custom middleware list without `middlewareBarrel`, `barrel` is left untouched.
|
|
25
|
-
* - `output.format` defaults to `
|
|
26
|
-
* - `output.lint` defaults to `
|
|
27
|
+
* - `output.format` defaults to `false`
|
|
28
|
+
* - `output.lint` defaults to `false`
|
|
27
29
|
*/
|
|
28
30
|
function applyDefaults<TInput>(config: UserConfig<TInput>): UserConfig<TInput> {
|
|
29
31
|
const middleware = config.middleware?.length ? config.middleware : [middlewareBarrel()]
|
|
@@ -42,8 +44,9 @@ function applyDefaults<TInput>(config: UserConfig<TInput>): UserConfig<TInput> {
|
|
|
42
44
|
|
|
43
45
|
return {
|
|
44
46
|
...config,
|
|
47
|
+
root: config.root || process.cwd(),
|
|
45
48
|
adapter: config.adapter ?? adapterOas(),
|
|
46
|
-
parsers: config.parsers?.length ? config.parsers : [parserTs, parserTsx],
|
|
49
|
+
parsers: config.parsers?.length ? config.parsers : [parserTs, parserTsx, parserMd],
|
|
47
50
|
middleware,
|
|
48
51
|
output,
|
|
49
52
|
}
|
|
@@ -58,21 +61,41 @@ function normalizeConfig<TInput>(config: UserConfig<TInput> | Array<UserConfig<T
|
|
|
58
61
|
}
|
|
59
62
|
|
|
60
63
|
/**
|
|
61
|
-
*
|
|
64
|
+
* Defines a Kubb build configuration and applies sensible defaults so the
|
|
65
|
+
* minimal config stays small.
|
|
62
66
|
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
67
|
+
* Defaults applied when omitted:
|
|
68
|
+
* - `adapter` → `adapterOas()` (OpenAPI 2.0/3.0/3.1).
|
|
69
|
+
* - `parsers` → `[parserTs, parserTsx]`.
|
|
70
|
+
* - `middleware` → `[middlewareBarrel()]`.
|
|
71
|
+
* - `output.barrel` → `{ type: 'named' }` only when `middlewareBarrel` is
|
|
72
|
+
* in the middleware list.
|
|
73
|
+
* - `output.format` and `output.lint` → `false`.
|
|
65
74
|
*
|
|
66
|
-
* Accepts
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
* receiving the CLI options as argument
|
|
75
|
+
* Accepts a config object, an array of configs, a Promise resolving to one,
|
|
76
|
+
* or a function that receives the parsed CLI options and returns any of the
|
|
77
|
+
* above. The return type is preserved so async/array variants stay typed.
|
|
70
78
|
*
|
|
71
79
|
* @example
|
|
72
80
|
* ```ts
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
81
|
+
* import { defineConfig } from 'kubb'
|
|
82
|
+
* import { pluginTs } from '@kubb/plugin-ts'
|
|
83
|
+
*
|
|
84
|
+
* export default defineConfig({
|
|
85
|
+
* input: { path: './petStore.yaml' },
|
|
86
|
+
* output: { path: './src/gen' },
|
|
87
|
+
* plugins: [pluginTs()],
|
|
88
|
+
* })
|
|
89
|
+
* ```
|
|
90
|
+
*
|
|
91
|
+
* @example Function form with CLI options
|
|
92
|
+
* ```ts
|
|
93
|
+
* import { defineConfig } from 'kubb'
|
|
94
|
+
*
|
|
95
|
+
* export default defineConfig(({ input }) => ({
|
|
96
|
+
* input: { path: input ?? './petStore.yaml' },
|
|
97
|
+
* output: { path: './src/gen' },
|
|
98
|
+
* plugins: [],
|
|
76
99
|
* }))
|
|
77
100
|
* ```
|
|
78
101
|
*/
|