@kubb/core 5.0.0-alpha.2 → 5.0.0-alpha.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/hooks.cjs +24 -0
- package/dist/hooks.cjs.map +1 -1
- package/dist/hooks.d.ts +36 -2
- package/dist/hooks.js +24 -1
- package/dist/hooks.js.map +1 -1
- package/dist/index.cjs +108 -12
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +38 -2
- package/dist/index.js +109 -15
- package/dist/index.js.map +1 -1
- package/dist/{types-B7eZvqwD.d.ts → types-Bbh1o0yW.d.ts} +85 -9
- package/package.json +2 -2
- package/src/PluginManager.ts +32 -16
- package/src/build.ts +1 -0
- package/src/defineGenerator.ts +106 -0
- package/src/hooks/index.ts +5 -0
- package/src/hooks/useKubb.ts +22 -0
- package/src/hooks/useMode.ts +3 -0
- package/src/hooks/usePlugin.ts +3 -0
- package/src/hooks/usePluginManager.ts +3 -0
- package/src/index.ts +2 -0
- package/src/types.ts +29 -7
- package/src/utils/resolveOptions.ts +93 -0
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { t as __name } from "./chunk--u3MIqq1.js";
|
|
2
|
-
import { A as UserPluginWithLifeCycle, B as
|
|
2
|
+
import { A as UserPluginWithLifeCycle, B as formatters, C as PrinterFactoryOptions, D as UserConfig, E as UnknownUserPlugin, F as KubbEvents, H as logLevel, I as PluginManager, L as getMode, M as Generator, N as ReactGeneratorV2, O as UserLogger, P as defineGenerator, R as DefineStorage, S as Printer, T as ResolvePathParams, U as AsyncEventEmitter, V as linters, W as URLPath, _ as PluginFactoryOptions, a as Config, b as PluginParameter, c as Group, d as Logger, f as LoggerContext, g as PluginContext, h as Plugin, i as BarrelType, j as CoreGeneratorV2, k as UserPlugin, l as InputData, m as Output, n as AdapterFactoryOptions, o as DevtoolsOptions, p as LoggerOptions, r as AdapterSource, s as GetPluginFactoryOptions, t as Adapter, u as InputPath, v as PluginLifecycle, w as ResolveNameParams, x as PluginWithLifeCycle, y as PluginLifecycleHooks, z as defineStorage } from "./types-Bbh1o0yW.js";
|
|
3
3
|
import { definePrinter } from "@kubb/ast";
|
|
4
4
|
import { Fabric } from "@kubb/react-fabric";
|
|
5
|
+
import { Node } from "@kubb/ast/types";
|
|
5
6
|
import { KubbFile } from "@kubb/fabric-core/types";
|
|
6
7
|
|
|
7
8
|
//#region src/build.d.ts
|
|
@@ -311,5 +312,40 @@ declare function getConfigs(config: ConfigInput | UserConfig, args: CLIOptions):
|
|
|
311
312
|
type Linter = keyof typeof linters;
|
|
312
313
|
declare function detectLinter(): Promise<Linter | undefined>;
|
|
313
314
|
//#endregion
|
|
314
|
-
|
|
315
|
+
//#region src/utils/resolveOptions.d.ts
|
|
316
|
+
type FilterItem = {
|
|
317
|
+
type: string;
|
|
318
|
+
pattern: string | RegExp;
|
|
319
|
+
};
|
|
320
|
+
type OverrideItem<TOptions> = FilterItem & {
|
|
321
|
+
options: Partial<TOptions>;
|
|
322
|
+
};
|
|
323
|
+
type ResolveOptionsContext<TOptions> = {
|
|
324
|
+
options: TOptions;
|
|
325
|
+
exclude?: Array<FilterItem>;
|
|
326
|
+
include?: Array<FilterItem>;
|
|
327
|
+
override?: Array<OverrideItem<TOptions>>;
|
|
328
|
+
};
|
|
329
|
+
/**
|
|
330
|
+
* Resolves the effective plugin options for a given AST node by applying
|
|
331
|
+
* `exclude`, `include`, and `override` rules from the plugin configuration.
|
|
332
|
+
*
|
|
333
|
+
* Returns `null` when the node is excluded or not matched by `include`.
|
|
334
|
+
* Returns the merged options (base options merged with any matching `override`) otherwise.
|
|
335
|
+
*
|
|
336
|
+
* Supported filter types for `OperationNode`: `tag`, `operationId`, `path`, `method`.
|
|
337
|
+
* Supported filter types for `SchemaNode`: `schemaName`.
|
|
338
|
+
*
|
|
339
|
+
* @example
|
|
340
|
+
* const resolved = resolveOptions(operationNode, { options, exclude, include, override })
|
|
341
|
+
* if (!resolved) return // excluded
|
|
342
|
+
*/
|
|
343
|
+
declare function resolveOptions<TOptions>(node: Node, {
|
|
344
|
+
options,
|
|
345
|
+
exclude,
|
|
346
|
+
include,
|
|
347
|
+
override
|
|
348
|
+
}: ResolveOptionsContext<TOptions>): TOptions | null;
|
|
349
|
+
//#endregion
|
|
350
|
+
export { Adapter, AdapterFactoryOptions, AdapterSource, AsyncEventEmitter, BarrelType, type CLIOptions, Config, type ConfigInput, CoreGeneratorV2, DefineStorage, DevtoolsOptions, type FileMetaBase, FunctionParams, type FunctionParamsAST, Generator, GetPluginFactoryOptions, Group, InputData, InputPath, KubbEvents, Logger, LoggerContext, LoggerOptions, Output, PackageManager, Plugin, PluginContext, PluginFactoryOptions, PluginLifecycle, PluginLifecycleHooks, PluginManager, PluginParameter, PluginWithLifeCycle, Printer, PrinterFactoryOptions, PromiseManager, ReactGeneratorV2, ResolveNameParams, ResolvePathParams, URLPath, UnknownUserPlugin, UserConfig, UserLogger, UserPlugin, UserPluginWithLifeCycle, build, build as default, defineAdapter, defineConfig, defineGenerator, defineLogger, definePlugin, definePrinter, defineStorage, detectFormatter, detectLinter, formatters, fsStorage, getBarrelFiles, getConfigs, getMode, isInputPath, linters, logLevel, memoryStorage, resolveOptions, safeBuild, setup };
|
|
315
351
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -4,8 +4,8 @@ import { EventEmitter } from "node:events";
|
|
|
4
4
|
import { parseArgs, styleText } from "node:util";
|
|
5
5
|
import { readFileSync } from "node:fs";
|
|
6
6
|
import { access, mkdir, readFile, readdir, rm, writeFile } from "node:fs/promises";
|
|
7
|
-
import path, { dirname, join, posix, relative, resolve } from "node:path";
|
|
8
|
-
import { definePrinter } from "@kubb/ast";
|
|
7
|
+
import path, { basename, dirname, extname, join, posix, relative, resolve } from "node:path";
|
|
8
|
+
import { definePrinter, isOperationNode, isSchemaNode } from "@kubb/ast";
|
|
9
9
|
import { createFabric } from "@kubb/react-fabric";
|
|
10
10
|
import { typescriptParser } from "@kubb/react-fabric/parsers";
|
|
11
11
|
import { fsPlugin } from "@kubb/react-fabric/plugins";
|
|
@@ -951,7 +951,7 @@ function isPromiseRejectedResult(result) {
|
|
|
951
951
|
//#region src/PluginManager.ts
|
|
952
952
|
function getMode(fileOrFolder) {
|
|
953
953
|
if (!fileOrFolder) return "split";
|
|
954
|
-
return
|
|
954
|
+
return extname(fileOrFolder) ? "single" : "split";
|
|
955
955
|
}
|
|
956
956
|
var PluginManager = class {
|
|
957
957
|
config;
|
|
@@ -961,6 +961,7 @@ var PluginManager = class {
|
|
|
961
961
|
* the build pipeline after the adapter's `parse()` resolves.
|
|
962
962
|
*/
|
|
963
963
|
rootNode = void 0;
|
|
964
|
+
adapter = void 0;
|
|
964
965
|
#studioIsOpen = false;
|
|
965
966
|
#plugins = /* @__PURE__ */ new Set();
|
|
966
967
|
#usedPluginNames = {};
|
|
@@ -986,7 +987,7 @@ var PluginManager = class {
|
|
|
986
987
|
plugin,
|
|
987
988
|
events: this.options.events,
|
|
988
989
|
pluginManager: this,
|
|
989
|
-
mode: getMode(
|
|
990
|
+
mode: getMode(resolve(this.config.root, this.config.output.path)),
|
|
990
991
|
addFile: async (...files) => {
|
|
991
992
|
await this.options.fabric.addFile(...files);
|
|
992
993
|
},
|
|
@@ -996,10 +997,13 @@ var PluginManager = class {
|
|
|
996
997
|
get rootNode() {
|
|
997
998
|
return pluginManager.rootNode;
|
|
998
999
|
},
|
|
1000
|
+
get adapter() {
|
|
1001
|
+
return pluginManager.adapter;
|
|
1002
|
+
},
|
|
999
1003
|
openInStudio(options) {
|
|
1004
|
+
if (!pluginManager.config.devtools || pluginManager.#studioIsOpen) return;
|
|
1000
1005
|
if (typeof pluginManager.config.devtools !== "object") throw new Error("Devtools must be an object");
|
|
1001
|
-
if (!pluginManager.rootNode) throw new Error("
|
|
1002
|
-
if (pluginManager.#studioIsOpen) return;
|
|
1006
|
+
if (!pluginManager.rootNode || !pluginManager.adapter) throw new Error("adapter is not defined, make sure you have set the parser in kubb.config.ts");
|
|
1003
1007
|
pluginManager.#studioIsOpen = true;
|
|
1004
1008
|
const studioUrl = pluginManager.config.devtools?.studioUrl ?? "https://studio.kubb.dev";
|
|
1005
1009
|
return openInStudio(pluginManager.rootNode, studioUrl, options);
|
|
@@ -1019,17 +1023,21 @@ var PluginManager = class {
|
|
|
1019
1023
|
return this.#getSortedPlugins();
|
|
1020
1024
|
}
|
|
1021
1025
|
getFile({ name, mode, extname, pluginName, options }) {
|
|
1022
|
-
const
|
|
1026
|
+
const resolvedName = mode ? mode === "single" ? "" : this.resolveName({
|
|
1027
|
+
name,
|
|
1028
|
+
pluginName,
|
|
1029
|
+
type: "file"
|
|
1030
|
+
}) : name;
|
|
1023
1031
|
const path = this.resolvePath({
|
|
1024
|
-
baseName
|
|
1032
|
+
baseName: `${resolvedName}${extname}`,
|
|
1025
1033
|
mode,
|
|
1026
1034
|
pluginName,
|
|
1027
1035
|
options
|
|
1028
1036
|
});
|
|
1029
|
-
if (!path) throw new Error(`Filepath should be defined for resolvedName "${
|
|
1037
|
+
if (!path) throw new Error(`Filepath should be defined for resolvedName "${resolvedName}" and pluginName "${pluginName}"`);
|
|
1030
1038
|
return {
|
|
1031
1039
|
path,
|
|
1032
|
-
baseName,
|
|
1040
|
+
baseName: basename(path),
|
|
1033
1041
|
meta: { pluginName },
|
|
1034
1042
|
sources: [],
|
|
1035
1043
|
imports: [],
|
|
@@ -1037,8 +1045,7 @@ var PluginManager = class {
|
|
|
1037
1045
|
};
|
|
1038
1046
|
}
|
|
1039
1047
|
resolvePath = (params) => {
|
|
1040
|
-
const
|
|
1041
|
-
const defaultPath = path.resolve(root, params.baseName);
|
|
1048
|
+
const defaultPath = resolve(resolve(this.config.root, this.config.output.path), params.baseName);
|
|
1042
1049
|
if (params.pluginName) return this.hookForPluginSync({
|
|
1043
1050
|
pluginName: params.pluginName,
|
|
1044
1051
|
hookName: "resolvePath",
|
|
@@ -1226,7 +1233,8 @@ var PluginManager = class {
|
|
|
1226
1233
|
if (hookName) return plugins.filter((plugin) => hookName in plugin);
|
|
1227
1234
|
return plugins.map((plugin) => {
|
|
1228
1235
|
if (plugin.pre) {
|
|
1229
|
-
|
|
1236
|
+
let missingPlugins = plugin.pre.filter((pluginName) => !plugins.find((pluginToFind) => pluginToFind.name === pluginName));
|
|
1237
|
+
if (missingPlugins.includes("plugin-oas") && this.adapter) missingPlugins = missingPlugins.filter((pluginName) => pluginName !== "plugin-oas");
|
|
1230
1238
|
if (missingPlugins.length > 0) throw new ValidationPluginError(`The plugin '${plugin.name}' has a pre set that references missing plugins for '${missingPlugins.join(", ")}'`);
|
|
1231
1239
|
}
|
|
1232
1240
|
return plugin;
|
|
@@ -1448,7 +1456,7 @@ const fsStorage = defineStorage(() => ({
|
|
|
1448
1456
|
}));
|
|
1449
1457
|
//#endregion
|
|
1450
1458
|
//#region package.json
|
|
1451
|
-
var version$1 = "5.0.0-alpha.
|
|
1459
|
+
var version$1 = "5.0.0-alpha.4";
|
|
1452
1460
|
//#endregion
|
|
1453
1461
|
//#region src/utils/diagnostics.ts
|
|
1454
1462
|
/**
|
|
@@ -1573,6 +1581,7 @@ async function setup(options) {
|
|
|
1573
1581
|
date: /* @__PURE__ */ new Date(),
|
|
1574
1582
|
logs: [`Running adapter: ${definedConfig.adapter.name}`]
|
|
1575
1583
|
});
|
|
1584
|
+
pluginManager.adapter = definedConfig.adapter;
|
|
1576
1585
|
pluginManager.rootNode = await definedConfig.adapter.parse(source);
|
|
1577
1586
|
await events.emit("debug", {
|
|
1578
1587
|
date: /* @__PURE__ */ new Date(),
|
|
@@ -1780,6 +1789,36 @@ function defineAdapter(build) {
|
|
|
1780
1789
|
return (options) => build(options ?? {});
|
|
1781
1790
|
}
|
|
1782
1791
|
//#endregion
|
|
1792
|
+
//#region src/defineGenerator.ts
|
|
1793
|
+
function defineGenerator(generator) {
|
|
1794
|
+
if (generator.type === "react") return {
|
|
1795
|
+
version: "2",
|
|
1796
|
+
Operations() {
|
|
1797
|
+
return null;
|
|
1798
|
+
},
|
|
1799
|
+
Operation() {
|
|
1800
|
+
return null;
|
|
1801
|
+
},
|
|
1802
|
+
Schema() {
|
|
1803
|
+
return null;
|
|
1804
|
+
},
|
|
1805
|
+
...generator
|
|
1806
|
+
};
|
|
1807
|
+
return {
|
|
1808
|
+
version: "2",
|
|
1809
|
+
async operations() {
|
|
1810
|
+
return [];
|
|
1811
|
+
},
|
|
1812
|
+
async operation() {
|
|
1813
|
+
return [];
|
|
1814
|
+
},
|
|
1815
|
+
async schema() {
|
|
1816
|
+
return [];
|
|
1817
|
+
},
|
|
1818
|
+
...generator
|
|
1819
|
+
};
|
|
1820
|
+
}
|
|
1821
|
+
//#endregion
|
|
1783
1822
|
//#region src/defineLogger.ts
|
|
1784
1823
|
function defineLogger(logger) {
|
|
1785
1824
|
return { ...logger };
|
|
@@ -2293,6 +2332,61 @@ async function detectLinter() {
|
|
|
2293
2332
|
]) if (await isLinterAvailable(linter)) return linter;
|
|
2294
2333
|
}
|
|
2295
2334
|
//#endregion
|
|
2296
|
-
|
|
2335
|
+
//#region src/utils/resolveOptions.ts
|
|
2336
|
+
function matchesOperationPattern(node, type, pattern) {
|
|
2337
|
+
switch (type) {
|
|
2338
|
+
case "tag": return node.tags.some((tag) => !!tag.match(pattern));
|
|
2339
|
+
case "operationId": return !!node.operationId.match(pattern);
|
|
2340
|
+
case "path": return !!node.path.match(pattern);
|
|
2341
|
+
case "method": return !!node.method.toLowerCase().match(pattern);
|
|
2342
|
+
default: return false;
|
|
2343
|
+
}
|
|
2344
|
+
}
|
|
2345
|
+
function matchesSchemaPattern(node, type, pattern) {
|
|
2346
|
+
switch (type) {
|
|
2347
|
+
case "schemaName": return node.name ? !!node.name.match(pattern) : false;
|
|
2348
|
+
default: return null;
|
|
2349
|
+
}
|
|
2350
|
+
}
|
|
2351
|
+
/**
|
|
2352
|
+
* Resolves the effective plugin options for a given AST node by applying
|
|
2353
|
+
* `exclude`, `include`, and `override` rules from the plugin configuration.
|
|
2354
|
+
*
|
|
2355
|
+
* Returns `null` when the node is excluded or not matched by `include`.
|
|
2356
|
+
* Returns the merged options (base options merged with any matching `override`) otherwise.
|
|
2357
|
+
*
|
|
2358
|
+
* Supported filter types for `OperationNode`: `tag`, `operationId`, `path`, `method`.
|
|
2359
|
+
* Supported filter types for `SchemaNode`: `schemaName`.
|
|
2360
|
+
*
|
|
2361
|
+
* @example
|
|
2362
|
+
* const resolved = resolveOptions(operationNode, { options, exclude, include, override })
|
|
2363
|
+
* if (!resolved) return // excluded
|
|
2364
|
+
*/
|
|
2365
|
+
function resolveOptions(node, { options, exclude = [], include, override = [] }) {
|
|
2366
|
+
if (isOperationNode(node)) {
|
|
2367
|
+
if (exclude.some(({ type, pattern }) => matchesOperationPattern(node, type, pattern))) return null;
|
|
2368
|
+
if (include && !include.some(({ type, pattern }) => matchesOperationPattern(node, type, pattern))) return null;
|
|
2369
|
+
const overrideOptions = override.find(({ type, pattern }) => matchesOperationPattern(node, type, pattern))?.options;
|
|
2370
|
+
return {
|
|
2371
|
+
...options,
|
|
2372
|
+
...overrideOptions
|
|
2373
|
+
};
|
|
2374
|
+
}
|
|
2375
|
+
if (isSchemaNode(node)) {
|
|
2376
|
+
if (exclude.some(({ type, pattern }) => matchesSchemaPattern(node, type, pattern) === true)) return null;
|
|
2377
|
+
if (include) {
|
|
2378
|
+
const applicable = include.map(({ type, pattern }) => matchesSchemaPattern(node, type, pattern)).filter((r) => r !== null);
|
|
2379
|
+
if (applicable.length > 0 && !applicable.includes(true)) return null;
|
|
2380
|
+
}
|
|
2381
|
+
const overrideOptions = override.find(({ type, pattern }) => matchesSchemaPattern(node, type, pattern) === true)?.options;
|
|
2382
|
+
return {
|
|
2383
|
+
...options,
|
|
2384
|
+
...overrideOptions
|
|
2385
|
+
};
|
|
2386
|
+
}
|
|
2387
|
+
return options;
|
|
2388
|
+
}
|
|
2389
|
+
//#endregion
|
|
2390
|
+
export { AsyncEventEmitter, FunctionParams, PackageManager, PluginManager, PromiseManager, URLPath, build, build as default, defineAdapter, defineConfig, defineGenerator, defineLogger, definePlugin, definePrinter, defineStorage, detectFormatter, detectLinter, formatters, fsStorage, getBarrelFiles, getConfigs, getMode, isInputPath, linters, logLevel, memoryStorage, resolveOptions, safeBuild, setup };
|
|
2297
2391
|
|
|
2298
2392
|
//# sourceMappingURL=index.js.map
|