kubb 5.0.0-alpha.39 → 5.0.0-alpha.40

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -29,18 +29,16 @@ function applyDefaults(config) {
29
29
  parsers: config.parsers?.length ? config.parsers : [_kubb_parser_ts.parserTs, _kubb_parser_ts.parserTsx]
30
30
  };
31
31
  }
32
+ function normalizeConfig(config) {
33
+ if (Array.isArray(config)) return config.map(applyDefaults);
34
+ return applyDefaults(config);
35
+ }
32
36
  function defineConfig(config) {
33
37
  if (typeof config === "function") return async (cli) => {
34
- const resolved = await config(cli);
35
- const result = (Array.isArray(resolved) ? resolved : [resolved]).map(applyDefaults);
36
- return result.length === 1 ? result[0] : result;
38
+ return normalizeConfig(await config(cli));
37
39
  };
38
- if (isPromise(config)) return config.then((resolved) => {
39
- const result = (Array.isArray(resolved) ? resolved : [resolved]).map(applyDefaults);
40
- return result.length === 1 ? result[0] : result;
41
- });
42
- const result = (Array.isArray(config) ? config : [config]).map(applyDefaults);
43
- return result.length === 1 ? result[0] : result;
40
+ if (isPromise(config)) return config.then((resolved) => normalizeConfig(resolved));
41
+ return normalizeConfig(config);
44
42
  }
45
43
  //#endregion
46
44
  exports.defineConfig = defineConfig;
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","names":["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 } from '@kubb/core'\nimport { parserTs, parserTsx } from '@kubb/parser-ts'\nimport type { ConfigInput, UserConfig } from './types.ts'\n\n/**\n * Applies default adapter and parsers to a single user config when not set.\n *\n * - `adapter` defaults to `adapterOas()`\n * - `parsers` defaults to `[parserTs, parserTsx]`\n */\nfunction applyDefaults(config: UserConfig): UserConfig {\n return {\n ...config,\n adapter: config.adapter ?? adapterOas(),\n parsers: config.parsers?.length ? config.parsers : [parserTs, parserTsx],\n }\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, `[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 * export default defineConfig(({ logLevel }) => ({\n * root: 'src',\n * plugins: [myPlugin()],\n * }))\n */\nexport function defineConfig(config: (cli: CLIOptions) => PossiblePromise<UserConfig | UserConfig[]>): (cli: CLIOptions) => Promise<UserConfig | UserConfig[]>\nexport function defineConfig(config: Promise<UserConfig | UserConfig[]>): Promise<UserConfig | UserConfig[]>\nexport function defineConfig(config: UserConfig | UserConfig[]): UserConfig | UserConfig[]\nexport function defineConfig(config: ConfigInput): ConfigInput {\n if (typeof config === 'function') {\n return async (cli: CLIOptions) => {\n const resolved = await config(cli)\n const configs = Array.isArray(resolved) ? resolved : [resolved]\n const result = configs.map(applyDefaults)\n\n return result.length === 1 ? result[0]! : result\n }\n }\n\n if (isPromise(config)) {\n return config.then((resolved) => {\n const configs = Array.isArray(resolved) ? resolved : [resolved]\n const result = configs.map(applyDefaults)\n\n return result.length === 1 ? result[0]! : result\n })\n }\n\n const configs = Array.isArray(config) ? config : [config]\n const result = configs.map(applyDefaults)\n\n return result.length === 1 ? result[0]! : result\n}\n"],"mappings":";;;;;;;;;;;;;AAmBA,SAAgB,UAAa,QAAkD;AAC7E,QAAO,WAAW,QAAQ,WAAW,KAAA,KAAa,OAAQ,OAAmC,YAAY;;;;;;;;;;ACR3G,SAAS,cAAc,QAAgC;AACrD,QAAO;EACL,GAAG;EACH,SAAS,OAAO,YAAA,GAAA,kBAAA,aAAuB;EACvC,SAAS,OAAO,SAAS,SAAS,OAAO,UAAU,CAACA,gBAAAA,UAAUC,gBAAAA,UAAU;EACzE;;AAuBH,SAAgB,aAAa,QAAkC;AAC7D,KAAI,OAAO,WAAW,WACpB,QAAO,OAAO,QAAoB;EAChC,MAAM,WAAW,MAAM,OAAO,IAAI;EAElC,MAAM,UADU,MAAM,QAAQ,SAAS,GAAG,WAAW,CAAC,SAAS,EACxC,IAAI,cAAc;AAEzC,SAAO,OAAO,WAAW,IAAI,OAAO,KAAM;;AAI9C,KAAI,UAAU,OAAO,CACnB,QAAO,OAAO,MAAM,aAAa;EAE/B,MAAM,UADU,MAAM,QAAQ,SAAS,GAAG,WAAW,CAAC,SAAS,EACxC,IAAI,cAAc;AAEzC,SAAO,OAAO,WAAW,IAAI,OAAO,KAAM;GAC1C;CAIJ,MAAM,UADU,MAAM,QAAQ,OAAO,GAAG,SAAS,CAAC,OAAO,EAClC,IAAI,cAAc;AAEzC,QAAO,OAAO,WAAW,IAAI,OAAO,KAAM"}
1
+ {"version":3,"file":"index.cjs","names":["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 { parserTs, parserTsx } from '@kubb/parser-ts'\n\ntype ConfigValue<TInput> = UserConfig<TInput>\ntype ConfigArray<TInput> = Array<ConfigValue<TInput>>\ntype ConfigResult<TInput> = ConfigValue<TInput> | ConfigArray<TInput>\ntype ConfigPromise<TInput> = Promise<ConfigResult<TInput>>\ntype ConfigFactory<TInput> = (cli: CLIOptions) => PossiblePromise<ConfigResult<TInput>>\ntype DefinedConfig<TInput> = ConfigResult<TInput> | ConfigPromise<TInput> | ((cli: CLIOptions) => Promise<ConfigResult<TInput>>)\n\n/**\n * Applies default adapter and parsers to a single user config when not set.\n *\n * - `adapter` defaults to `adapterOas()`\n * - `parsers` defaults to `[parserTs, parserTsx]`\n */\nfunction applyDefaults<TInput>(config: UserConfig<TInput>): UserConfig<TInput> {\n return {\n ...config,\n adapter: config.adapter ?? adapterOas(),\n parsers: config.parsers?.length ? config.parsers : [parserTs, parserTsx],\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, `[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 * export default defineConfig(({ logLevel }) => ({\n * root: 'src',\n * plugins: [myPlugin()],\n * }))\n */\nexport function defineConfig<TInput>(config: (cli: CLIOptions) => PossiblePromise<UserConfig<TInput>>): (cli: CLIOptions) => Promise<UserConfig<TInput>>\nexport function defineConfig<TInput>(\n config: (cli: CLIOptions) => PossiblePromise<Array<UserConfig<TInput>>>,\n): (cli: CLIOptions) => Promise<Array<UserConfig<TInput>>>\nexport function defineConfig<TInput>(config: Promise<UserConfig<TInput>>): Promise<UserConfig<TInput>>\nexport function defineConfig<TInput>(config: Promise<Array<UserConfig<TInput>>>): Promise<Array<UserConfig<TInput>>>\nexport function defineConfig<TInput>(config: UserConfig<TInput>): UserConfig<TInput>\nexport function defineConfig<TInput>(config: Array<UserConfig<TInput>>): Array<UserConfig<TInput>>\nexport function defineConfig<TInput>(config: ConfigFactory<TInput>): (cli: CLIOptions) => Promise<ConfigResult<TInput>>\nexport function defineConfig<TInput>(config: ConfigResult<TInput> | ConfigPromise<TInput> | ConfigFactory<TInput>): DefinedConfig<TInput> {\n if (typeof config === 'function') {\n return async (cli: CLIOptions) => {\n return normalizeConfig(await config(cli))\n }\n }\n\n if (isPromise(config)) {\n return config.then((resolved) => normalizeConfig(resolved))\n }\n\n return normalizeConfig(config)\n}\n"],"mappings":";;;;;;;;;;;;;AAmBA,SAAgB,UAAa,QAAkD;AAC7E,QAAO,WAAW,QAAQ,WAAW,KAAA,KAAa,OAAQ,OAAmC,YAAY;;;;;;;;;;ACF3G,SAAS,cAAsB,QAAgD;AAC7E,QAAO;EACL,GAAG;EACH,SAAS,OAAO,YAAA,GAAA,kBAAA,aAAuB;EACvC,SAAS,OAAO,SAAS,SAAS,OAAO,UAAU,CAACA,gBAAAA,UAAUC,gBAAAA,UAAU;EACzE;;AAGH,SAAS,gBAAwB,QAAwG;AACvI,KAAI,MAAM,QAAQ,OAAO,CACvB,QAAO,OAAO,IAAI,cAAc;AAGlC,QAAO,cAAc,OAAO;;AA6B9B,SAAgB,aAAqB,QAAqG;AACxI,KAAI,OAAO,WAAW,WACpB,QAAO,OAAO,QAAoB;AAChC,SAAO,gBAAgB,MAAM,OAAO,IAAI,CAAC;;AAI7C,KAAI,UAAU,OAAO,CACnB,QAAO,OAAO,MAAM,aAAa,gBAAgB,SAAS,CAAC;AAG7D,QAAO,gBAAgB,OAAO"}
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { t as __name } from "./chunk--u3MIqq1.js";
2
- import { Adapter, CLIOptions, Config, HookStylePlugin, InputData, InputPath, Parser, PluginFactoryOptions, UserPlugin } from "@kubb/core";
2
+ import { CLIOptions, UserConfig } from "@kubb/core";
3
3
 
4
4
  //#region ../../internals/utils/src/promise.d.ts
5
5
  /** A value that may already be resolved or still pending.
@@ -13,51 +13,11 @@ import { Adapter, CLIOptions, Config, HookStylePlugin, InputData, InputPath, Par
13
13
  */
14
14
  type PossiblePromise<T> = Promise<T> | T;
15
15
  //#endregion
16
- //#region src/types.d.ts
17
- type Input = InputPath | InputData;
18
- type UnknownUserPlugin = UserPlugin<PluginFactoryOptions<string, object, object, unknown, object>>;
19
- /**
20
- * Partial version of {@link Config} intended for use in `kubb.config.ts`.
21
- *
22
- * Fields that have sensible defaults (`root`, `plugins`, `parsers`, `adapter`) are optional.
23
- */
24
- type UserConfig<TInput = Input> = Omit<Config<TInput>, 'root' | 'plugins' | 'parsers' | 'adapter'> & {
25
- /**
26
- * The project root directory, which can be either an absolute path or a path relative to the location of your `kubb.config.ts` file.
27
- * @default process.cwd()
28
- */
29
- root?: string;
30
- /**
31
- * An array of parsers used to convert generated files to strings.
32
- * Each parser handles specific file extensions (e.g. `.ts`, `.tsx`).
33
- *
34
- * A catch-all fallback parser is always appended last for any unhandled extension.
35
- *
36
- * When omitted, `parserTsx` from `@kubb/parser-ts` is used automatically as the
37
- * default (requires `@kubb/parser-ts` to be installed as an optional dependency).
38
- * @default [parserTsx] — from `@kubb/parser-ts`
39
- */
40
- parsers?: Array<Parser>;
41
- /**
42
- * Adapter that converts the input file into a `@kubb/ast` `InputNode` — the universal
43
- * intermediate representation consumed by all Kubb plugins.
44
- *
45
- * When omitted, `adapterOas()` from `@kubb/adapter-oas` is used automatically as the
46
- * default (requires `@kubb/adapter-oas` to be installed as an optional dependency).
47
- *
48
- * @default adapterOas() — from `@kubb/adapter-oas`
49
- */
50
- adapter?: Adapter;
51
- /**
52
- * An array of Kubb plugins used for code generation.
53
- * Each plugin may declare additional configurable options.
54
- * If a plugin depends on another, an error is thrown when the dependency is missing.
55
- * Use `dependencies` on the plugin to declare execution order.
56
- */
57
- plugins?: Array<Omit<UnknownUserPlugin, 'inject'> | HookStylePlugin>;
58
- };
59
- //#endregion
60
16
  //#region src/defineConfig.d.ts
17
+ type ConfigValue<TInput> = UserConfig<TInput>;
18
+ type ConfigArray<TInput> = Array<ConfigValue<TInput>>;
19
+ type ConfigResult<TInput> = ConfigValue<TInput> | ConfigArray<TInput>;
20
+ type ConfigFactory<TInput> = (cli: CLIOptions) => PossiblePromise<ConfigResult<TInput>>;
61
21
  /**
62
22
  * Helper for defining a Kubb configuration with built-in defaults.
63
23
  *
@@ -75,9 +35,13 @@ type UserConfig<TInput = Input> = Omit<Config<TInput>, 'root' | 'plugins' | 'par
75
35
  * plugins: [myPlugin()],
76
36
  * }))
77
37
  */
78
- declare function defineConfig(config: (cli: CLIOptions) => PossiblePromise<UserConfig | UserConfig[]>): (cli: CLIOptions) => Promise<UserConfig | UserConfig[]>;
79
- declare function defineConfig(config: Promise<UserConfig | UserConfig[]>): Promise<UserConfig | UserConfig[]>;
80
- declare function defineConfig(config: UserConfig | UserConfig[]): UserConfig | UserConfig[];
38
+ declare function defineConfig<TInput>(config: (cli: CLIOptions) => PossiblePromise<UserConfig<TInput>>): (cli: CLIOptions) => Promise<UserConfig<TInput>>;
39
+ declare function defineConfig<TInput>(config: (cli: CLIOptions) => PossiblePromise<Array<UserConfig<TInput>>>): (cli: CLIOptions) => Promise<Array<UserConfig<TInput>>>;
40
+ declare function defineConfig<TInput>(config: Promise<UserConfig<TInput>>): Promise<UserConfig<TInput>>;
41
+ declare function defineConfig<TInput>(config: Promise<Array<UserConfig<TInput>>>): Promise<Array<UserConfig<TInput>>>;
42
+ declare function defineConfig<TInput>(config: UserConfig<TInput>): UserConfig<TInput>;
43
+ declare function defineConfig<TInput>(config: Array<UserConfig<TInput>>): Array<UserConfig<TInput>>;
44
+ declare function defineConfig<TInput>(config: ConfigFactory<TInput>): (cli: CLIOptions) => Promise<ConfigResult<TInput>>;
81
45
  //#endregion
82
46
  export { defineConfig };
83
47
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -28,18 +28,16 @@ function applyDefaults(config) {
28
28
  parsers: config.parsers?.length ? config.parsers : [parserTs, parserTsx]
29
29
  };
30
30
  }
31
+ function normalizeConfig(config) {
32
+ if (Array.isArray(config)) return config.map(applyDefaults);
33
+ return applyDefaults(config);
34
+ }
31
35
  function defineConfig(config) {
32
36
  if (typeof config === "function") return async (cli) => {
33
- const resolved = await config(cli);
34
- const result = (Array.isArray(resolved) ? resolved : [resolved]).map(applyDefaults);
35
- return result.length === 1 ? result[0] : result;
37
+ return normalizeConfig(await config(cli));
36
38
  };
37
- if (isPromise(config)) return config.then((resolved) => {
38
- const result = (Array.isArray(resolved) ? resolved : [resolved]).map(applyDefaults);
39
- return result.length === 1 ? result[0] : result;
40
- });
41
- const result = (Array.isArray(config) ? config : [config]).map(applyDefaults);
42
- return result.length === 1 ? result[0] : result;
39
+ if (isPromise(config)) return config.then((resolved) => normalizeConfig(resolved));
40
+ return normalizeConfig(config);
43
41
  }
44
42
  //#endregion
45
43
  export { defineConfig };
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 } from '@kubb/core'\nimport { parserTs, parserTsx } from '@kubb/parser-ts'\nimport type { ConfigInput, UserConfig } from './types.ts'\n\n/**\n * Applies default adapter and parsers to a single user config when not set.\n *\n * - `adapter` defaults to `adapterOas()`\n * - `parsers` defaults to `[parserTs, parserTsx]`\n */\nfunction applyDefaults(config: UserConfig): UserConfig {\n return {\n ...config,\n adapter: config.adapter ?? adapterOas(),\n parsers: config.parsers?.length ? config.parsers : [parserTs, parserTsx],\n }\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, `[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 * export default defineConfig(({ logLevel }) => ({\n * root: 'src',\n * plugins: [myPlugin()],\n * }))\n */\nexport function defineConfig(config: (cli: CLIOptions) => PossiblePromise<UserConfig | UserConfig[]>): (cli: CLIOptions) => Promise<UserConfig | UserConfig[]>\nexport function defineConfig(config: Promise<UserConfig | UserConfig[]>): Promise<UserConfig | UserConfig[]>\nexport function defineConfig(config: UserConfig | UserConfig[]): UserConfig | UserConfig[]\nexport function defineConfig(config: ConfigInput): ConfigInput {\n if (typeof config === 'function') {\n return async (cli: CLIOptions) => {\n const resolved = await config(cli)\n const configs = Array.isArray(resolved) ? resolved : [resolved]\n const result = configs.map(applyDefaults)\n\n return result.length === 1 ? result[0]! : result\n }\n }\n\n if (isPromise(config)) {\n return config.then((resolved) => {\n const configs = Array.isArray(resolved) ? resolved : [resolved]\n const result = configs.map(applyDefaults)\n\n return result.length === 1 ? result[0]! : result\n })\n }\n\n const configs = Array.isArray(config) ? config : [config]\n const result = configs.map(applyDefaults)\n\n return result.length === 1 ? result[0]! : result\n}\n"],"mappings":";;;;;;;;;;;;AAmBA,SAAgB,UAAa,QAAkD;AAC7E,QAAO,WAAW,QAAQ,WAAW,KAAA,KAAa,OAAQ,OAAmC,YAAY;;;;;;;;;;ACR3G,SAAS,cAAc,QAAgC;AACrD,QAAO;EACL,GAAG;EACH,SAAS,OAAO,WAAW,YAAY;EACvC,SAAS,OAAO,SAAS,SAAS,OAAO,UAAU,CAAC,UAAU,UAAU;EACzE;;AAuBH,SAAgB,aAAa,QAAkC;AAC7D,KAAI,OAAO,WAAW,WACpB,QAAO,OAAO,QAAoB;EAChC,MAAM,WAAW,MAAM,OAAO,IAAI;EAElC,MAAM,UADU,MAAM,QAAQ,SAAS,GAAG,WAAW,CAAC,SAAS,EACxC,IAAI,cAAc;AAEzC,SAAO,OAAO,WAAW,IAAI,OAAO,KAAM;;AAI9C,KAAI,UAAU,OAAO,CACnB,QAAO,OAAO,MAAM,aAAa;EAE/B,MAAM,UADU,MAAM,QAAQ,SAAS,GAAG,WAAW,CAAC,SAAS,EACxC,IAAI,cAAc;AAEzC,SAAO,OAAO,WAAW,IAAI,OAAO,KAAM;GAC1C;CAIJ,MAAM,UADU,MAAM,QAAQ,OAAO,GAAG,SAAS,CAAC,OAAO,EAClC,IAAI,cAAc;AAEzC,QAAO,OAAO,WAAW,IAAI,OAAO,KAAM"}
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 { parserTs, parserTsx } from '@kubb/parser-ts'\n\ntype ConfigValue<TInput> = UserConfig<TInput>\ntype ConfigArray<TInput> = Array<ConfigValue<TInput>>\ntype ConfigResult<TInput> = ConfigValue<TInput> | ConfigArray<TInput>\ntype ConfigPromise<TInput> = Promise<ConfigResult<TInput>>\ntype ConfigFactory<TInput> = (cli: CLIOptions) => PossiblePromise<ConfigResult<TInput>>\ntype DefinedConfig<TInput> = ConfigResult<TInput> | ConfigPromise<TInput> | ((cli: CLIOptions) => Promise<ConfigResult<TInput>>)\n\n/**\n * Applies default adapter and parsers to a single user config when not set.\n *\n * - `adapter` defaults to `adapterOas()`\n * - `parsers` defaults to `[parserTs, parserTsx]`\n */\nfunction applyDefaults<TInput>(config: UserConfig<TInput>): UserConfig<TInput> {\n return {\n ...config,\n adapter: config.adapter ?? adapterOas(),\n parsers: config.parsers?.length ? config.parsers : [parserTs, parserTsx],\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, `[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 * export default defineConfig(({ logLevel }) => ({\n * root: 'src',\n * plugins: [myPlugin()],\n * }))\n */\nexport function defineConfig<TInput>(config: (cli: CLIOptions) => PossiblePromise<UserConfig<TInput>>): (cli: CLIOptions) => Promise<UserConfig<TInput>>\nexport function defineConfig<TInput>(\n config: (cli: CLIOptions) => PossiblePromise<Array<UserConfig<TInput>>>,\n): (cli: CLIOptions) => Promise<Array<UserConfig<TInput>>>\nexport function defineConfig<TInput>(config: Promise<UserConfig<TInput>>): Promise<UserConfig<TInput>>\nexport function defineConfig<TInput>(config: Promise<Array<UserConfig<TInput>>>): Promise<Array<UserConfig<TInput>>>\nexport function defineConfig<TInput>(config: UserConfig<TInput>): UserConfig<TInput>\nexport function defineConfig<TInput>(config: Array<UserConfig<TInput>>): Array<UserConfig<TInput>>\nexport function defineConfig<TInput>(config: ConfigFactory<TInput>): (cli: CLIOptions) => Promise<ConfigResult<TInput>>\nexport function defineConfig<TInput>(config: ConfigResult<TInput> | ConfigPromise<TInput> | ConfigFactory<TInput>): DefinedConfig<TInput> {\n if (typeof config === 'function') {\n return async (cli: CLIOptions) => {\n return normalizeConfig(await config(cli))\n }\n }\n\n if (isPromise(config)) {\n return config.then((resolved) => normalizeConfig(resolved))\n }\n\n return normalizeConfig(config)\n}\n"],"mappings":";;;;;;;;;;;;AAmBA,SAAgB,UAAa,QAAkD;AAC7E,QAAO,WAAW,QAAQ,WAAW,KAAA,KAAa,OAAQ,OAAmC,YAAY;;;;;;;;;;ACF3G,SAAS,cAAsB,QAAgD;AAC7E,QAAO;EACL,GAAG;EACH,SAAS,OAAO,WAAW,YAAY;EACvC,SAAS,OAAO,SAAS,SAAS,OAAO,UAAU,CAAC,UAAU,UAAU;EACzE;;AAGH,SAAS,gBAAwB,QAAwG;AACvI,KAAI,MAAM,QAAQ,OAAO,CACvB,QAAO,OAAO,IAAI,cAAc;AAGlC,QAAO,cAAc,OAAO;;AA6B9B,SAAgB,aAAqB,QAAqG;AACxI,KAAI,OAAO,WAAW,WACpB,QAAO,OAAO,QAAoB;AAChC,SAAO,gBAAgB,MAAM,OAAO,IAAI,CAAC;;AAI7C,KAAI,UAAU,OAAO,CACnB,QAAO,OAAO,MAAM,aAAa,gBAAgB,SAAS,CAAC;AAG7D,QAAO,gBAAgB,OAAO"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kubb",
3
- "version": "5.0.0-alpha.39",
3
+ "version": "5.0.0-alpha.40",
4
4
  "description": "Transform OpenAPI specifications into TypeScript, React-Query, Zod, Faker.js, MSW and more with a plugin-based code generation tool.",
5
5
  "keywords": [
6
6
  "typescript",
@@ -51,12 +51,12 @@
51
51
  }
52
52
  ],
53
53
  "dependencies": {
54
- "@kubb/adapter-oas": "5.0.0-alpha.39",
55
- "@kubb/agent": "5.0.0-alpha.39",
56
- "@kubb/cli": "5.0.0-alpha.39",
57
- "@kubb/core": "5.0.0-alpha.39",
58
- "@kubb/mcp": "5.0.0-alpha.39",
59
- "@kubb/parser-ts": "5.0.0-alpha.39"
54
+ "@kubb/adapter-oas": "5.0.0-alpha.40",
55
+ "@kubb/agent": "5.0.0-alpha.40",
56
+ "@kubb/cli": "5.0.0-alpha.40",
57
+ "@kubb/core": "5.0.0-alpha.40",
58
+ "@kubb/mcp": "5.0.0-alpha.40",
59
+ "@kubb/parser-ts": "5.0.0-alpha.40"
60
60
  },
61
61
  "devDependencies": {
62
62
  "@internals/utils": "0.0.0"
@@ -1,8 +1,14 @@
1
1
  import { isPromise, type PossiblePromise } from '@internals/utils'
2
2
  import { adapterOas } from '@kubb/adapter-oas'
3
- import type { CLIOptions } from '@kubb/core'
3
+ import type { CLIOptions, UserConfig } from '@kubb/core'
4
4
  import { parserTs, parserTsx } from '@kubb/parser-ts'
5
- import type { ConfigInput, UserConfig } from './types.ts'
5
+
6
+ type ConfigValue<TInput> = UserConfig<TInput>
7
+ type ConfigArray<TInput> = Array<ConfigValue<TInput>>
8
+ type ConfigResult<TInput> = ConfigValue<TInput> | ConfigArray<TInput>
9
+ type ConfigPromise<TInput> = Promise<ConfigResult<TInput>>
10
+ type ConfigFactory<TInput> = (cli: CLIOptions) => PossiblePromise<ConfigResult<TInput>>
11
+ type DefinedConfig<TInput> = ConfigResult<TInput> | ConfigPromise<TInput> | ((cli: CLIOptions) => Promise<ConfigResult<TInput>>)
6
12
 
7
13
  /**
8
14
  * Applies default adapter and parsers to a single user config when not set.
@@ -10,7 +16,7 @@ import type { ConfigInput, UserConfig } from './types.ts'
10
16
  * - `adapter` defaults to `adapterOas()`
11
17
  * - `parsers` defaults to `[parserTs, parserTsx]`
12
18
  */
13
- function applyDefaults(config: UserConfig): UserConfig {
19
+ function applyDefaults<TInput>(config: UserConfig<TInput>): UserConfig<TInput> {
14
20
  return {
15
21
  ...config,
16
22
  adapter: config.adapter ?? adapterOas(),
@@ -18,6 +24,14 @@ function applyDefaults(config: UserConfig): UserConfig {
18
24
  }
19
25
  }
20
26
 
27
+ function normalizeConfig<TInput>(config: UserConfig<TInput> | Array<UserConfig<TInput>>): UserConfig<TInput> | Array<UserConfig<TInput>> {
28
+ if (Array.isArray(config)) {
29
+ return config.map(applyDefaults)
30
+ }
31
+
32
+ return applyDefaults(config)
33
+ }
34
+
21
35
  /**
22
36
  * Helper for defining a Kubb configuration with built-in defaults.
23
37
  *
@@ -35,31 +49,25 @@ function applyDefaults(config: UserConfig): UserConfig {
35
49
  * plugins: [myPlugin()],
36
50
  * }))
37
51
  */
38
- export function defineConfig(config: (cli: CLIOptions) => PossiblePromise<UserConfig | UserConfig[]>): (cli: CLIOptions) => Promise<UserConfig | UserConfig[]>
39
- export function defineConfig(config: Promise<UserConfig | UserConfig[]>): Promise<UserConfig | UserConfig[]>
40
- export function defineConfig(config: UserConfig | UserConfig[]): UserConfig | UserConfig[]
41
- export function defineConfig(config: ConfigInput): ConfigInput {
52
+ export function defineConfig<TInput>(config: (cli: CLIOptions) => PossiblePromise<UserConfig<TInput>>): (cli: CLIOptions) => Promise<UserConfig<TInput>>
53
+ export function defineConfig<TInput>(
54
+ config: (cli: CLIOptions) => PossiblePromise<Array<UserConfig<TInput>>>,
55
+ ): (cli: CLIOptions) => Promise<Array<UserConfig<TInput>>>
56
+ export function defineConfig<TInput>(config: Promise<UserConfig<TInput>>): Promise<UserConfig<TInput>>
57
+ export function defineConfig<TInput>(config: Promise<Array<UserConfig<TInput>>>): Promise<Array<UserConfig<TInput>>>
58
+ export function defineConfig<TInput>(config: UserConfig<TInput>): UserConfig<TInput>
59
+ export function defineConfig<TInput>(config: Array<UserConfig<TInput>>): Array<UserConfig<TInput>>
60
+ export function defineConfig<TInput>(config: ConfigFactory<TInput>): (cli: CLIOptions) => Promise<ConfigResult<TInput>>
61
+ export function defineConfig<TInput>(config: ConfigResult<TInput> | ConfigPromise<TInput> | ConfigFactory<TInput>): DefinedConfig<TInput> {
42
62
  if (typeof config === 'function') {
43
63
  return async (cli: CLIOptions) => {
44
- const resolved = await config(cli)
45
- const configs = Array.isArray(resolved) ? resolved : [resolved]
46
- const result = configs.map(applyDefaults)
47
-
48
- return result.length === 1 ? result[0]! : result
64
+ return normalizeConfig(await config(cli))
49
65
  }
50
66
  }
51
67
 
52
68
  if (isPromise(config)) {
53
- return config.then((resolved) => {
54
- const configs = Array.isArray(resolved) ? resolved : [resolved]
55
- const result = configs.map(applyDefaults)
56
-
57
- return result.length === 1 ? result[0]! : result
58
- })
69
+ return config.then((resolved) => normalizeConfig(resolved))
59
70
  }
60
71
 
61
- const configs = Array.isArray(config) ? config : [config]
62
- const result = configs.map(applyDefaults)
63
-
64
- return result.length === 1 ? result[0]! : result
72
+ return normalizeConfig(config)
65
73
  }
package/src/types.ts DELETED
@@ -1,52 +0,0 @@
1
- import type { PossiblePromise } from '@internals/utils'
2
- import type { Adapter, CLIOptions, Config, HookStylePlugin, InputData, InputPath, Parser, PluginFactoryOptions, UserPlugin } from '@kubb/core'
3
-
4
- type Input = InputPath | InputData
5
- type UnknownUserPlugin = UserPlugin<PluginFactoryOptions<string, object, object, unknown, object>>
6
-
7
- /**
8
- * Partial version of {@link Config} intended for use in `kubb.config.ts`.
9
- *
10
- * Fields that have sensible defaults (`root`, `plugins`, `parsers`, `adapter`) are optional.
11
- */
12
- export type UserConfig<TInput = Input> = Omit<Config<TInput>, 'root' | 'plugins' | 'parsers' | 'adapter'> & {
13
- /**
14
- * The project root directory, which can be either an absolute path or a path relative to the location of your `kubb.config.ts` file.
15
- * @default process.cwd()
16
- */
17
- root?: string
18
- /**
19
- * An array of parsers used to convert generated files to strings.
20
- * Each parser handles specific file extensions (e.g. `.ts`, `.tsx`).
21
- *
22
- * A catch-all fallback parser is always appended last for any unhandled extension.
23
- *
24
- * When omitted, `parserTsx` from `@kubb/parser-ts` is used automatically as the
25
- * default (requires `@kubb/parser-ts` to be installed as an optional dependency).
26
- * @default [parserTsx] — from `@kubb/parser-ts`
27
- */
28
- parsers?: Array<Parser>
29
- /**
30
- * Adapter that converts the input file into a `@kubb/ast` `InputNode` — the universal
31
- * intermediate representation consumed by all Kubb plugins.
32
- *
33
- * When omitted, `adapterOas()` from `@kubb/adapter-oas` is used automatically as the
34
- * default (requires `@kubb/adapter-oas` to be installed as an optional dependency).
35
- *
36
- * @default adapterOas() — from `@kubb/adapter-oas`
37
- */
38
- adapter?: Adapter
39
- /**
40
- * An array of Kubb plugins used for code generation.
41
- * Each plugin may declare additional configurable options.
42
- * If a plugin depends on another, an error is thrown when the dependency is missing.
43
- * Use `dependencies` on the plugin to declare execution order.
44
- */
45
- // inject needs to be omitted because else we have a clash with the PluginDriver instance
46
- plugins?: Array<Omit<UnknownUserPlugin, 'inject'> | HookStylePlugin>
47
- }
48
-
49
- /**
50
- * All accepted forms of a Kubb configuration.
51
- */
52
- export type ConfigInput = PossiblePromise<UserConfig | UserConfig[]> | ((cli: CLIOptions) => PossiblePromise<UserConfig | UserConfig[]>)