@kubb/core 5.0.0-alpha.3 → 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.
@@ -1,8 +1,9 @@
1
1
  import { t as __name } from "./chunk--u3MIqq1.js";
2
2
  import { EventEmitter } from "node:events";
3
3
  import { Fabric } from "@kubb/react-fabric";
4
- import { Printer, PrinterFactoryOptions, RootNode, SchemaNode } from "@kubb/ast/types";
4
+ import { OperationNode, Printer, PrinterFactoryOptions, RootNode, SchemaNode } from "@kubb/ast/types";
5
5
  import { KubbFile } from "@kubb/fabric-core/types";
6
+ import { FabricReactNode } from "@kubb/react-fabric/types";
6
7
 
7
8
  //#region ../../internals/utils/dist/index.d.ts
8
9
  /**
@@ -530,6 +531,61 @@ interface KubbEvents {
530
531
  'plugins:hook:processing:end': [meta: ExecutedMeta];
531
532
  }
532
533
  //#endregion
534
+ //#region src/defineGenerator.d.ts
535
+ type OperationsV2Props<TPlugin extends PluginFactoryOptions = PluginFactoryOptions> = {
536
+ config: Config;
537
+ adapter: Adapter;
538
+ options: Plugin<TPlugin>['options'];
539
+ nodes: Array<OperationNode>;
540
+ };
541
+ type OperationV2Props<TPlugin extends PluginFactoryOptions = PluginFactoryOptions> = {
542
+ config: Config;
543
+ adapter: Adapter;
544
+ options: Plugin<TPlugin>['options'];
545
+ node: OperationNode;
546
+ };
547
+ type SchemaV2Props<TPlugin extends PluginFactoryOptions = PluginFactoryOptions> = {
548
+ config: Config;
549
+ adapter: Adapter;
550
+ options: Plugin<TPlugin>['options'];
551
+ node: SchemaNode;
552
+ };
553
+ type UserCoreGeneratorV2<TPlugin extends PluginFactoryOptions> = {
554
+ name: string;
555
+ type: 'core';
556
+ version?: '2';
557
+ operations?(props: OperationsV2Props<TPlugin>): Promise<Array<KubbFile.File>>;
558
+ operation?(props: OperationV2Props<TPlugin>): Promise<Array<KubbFile.File>>;
559
+ schema?(props: SchemaV2Props<TPlugin>): Promise<Array<KubbFile.File>>;
560
+ };
561
+ type UserReactGeneratorV2<TPlugin extends PluginFactoryOptions> = {
562
+ name: string;
563
+ type: 'react';
564
+ version?: '2';
565
+ Operations?(props: OperationsV2Props<TPlugin>): FabricReactNode;
566
+ Operation?(props: OperationV2Props<TPlugin>): FabricReactNode;
567
+ Schema?(props: SchemaV2Props<TPlugin>): FabricReactNode;
568
+ };
569
+ type CoreGeneratorV2<TPlugin extends PluginFactoryOptions = PluginFactoryOptions> = {
570
+ name: string;
571
+ type: 'core';
572
+ version: '2';
573
+ operations(props: OperationsV2Props<TPlugin>): Promise<Array<KubbFile.File>>;
574
+ operation(props: OperationV2Props<TPlugin>): Promise<Array<KubbFile.File>>;
575
+ schema(props: SchemaV2Props<TPlugin>): Promise<Array<KubbFile.File>>;
576
+ };
577
+ type ReactGeneratorV2<TPlugin extends PluginFactoryOptions = PluginFactoryOptions> = {
578
+ name: string;
579
+ type: 'react';
580
+ version: '2';
581
+ Operations(props: OperationsV2Props<TPlugin>): FabricReactNode;
582
+ Operation(props: OperationV2Props<TPlugin>): FabricReactNode;
583
+ Schema(props: SchemaV2Props<TPlugin>): FabricReactNode;
584
+ };
585
+ type Generator<TPlugin extends PluginFactoryOptions = PluginFactoryOptions> = UserCoreGeneratorV2<TPlugin> | UserReactGeneratorV2<TPlugin>;
586
+ declare function defineGenerator<TPlugin extends PluginFactoryOptions = PluginFactoryOptions>(generator: UserReactGeneratorV2<TPlugin>): ReactGeneratorV2<TPlugin>;
587
+ declare function defineGenerator<TPlugin extends PluginFactoryOptions = PluginFactoryOptions>(generator: UserCoreGeneratorV2<TPlugin>): CoreGeneratorV2<TPlugin>;
588
+ //#endregion
533
589
  //#region src/types.d.ts
534
590
  declare global {
535
591
  namespace Kubb {
@@ -997,5 +1053,5 @@ type Logger<TOptions extends LoggerOptions = LoggerOptions> = {
997
1053
  };
998
1054
  type UserLogger<TOptions extends LoggerOptions = LoggerOptions> = Omit<Logger<TOptions>, 'logLevel'>;
999
1055
  //#endregion
1000
- export { UserPluginWithLifeCycle as A, URLPath as B, PrinterFactoryOptions as C, UserConfig as D, UnknownUserPlugin as E, defineStorage as F, formatters as I, linters as L, PluginManager as M, getMode as N, UserLogger as O, DefineStorage as P, logLevel as R, Printer as S, ResolvePathParams as T, PluginFactoryOptions as _, Config as a, PluginParameter as b, Group as c, Logger as d, LoggerContext as f, PluginContext as g, Plugin as h, BarrelType as i, KubbEvents as j, UserPlugin as k, InputData as l, Output as m, AdapterFactoryOptions as n, DevtoolsOptions as o, LoggerOptions as p, AdapterSource as r, GetPluginFactoryOptions as s, Adapter as t, InputPath as u, PluginLifecycle as v, ResolveNameParams as w, PluginWithLifeCycle as x, PluginLifecycleHooks as y, AsyncEventEmitter as z };
1001
- //# sourceMappingURL=types-CiPWLv-5.d.ts.map
1056
+ export { UserPluginWithLifeCycle as A, formatters as B, PrinterFactoryOptions as C, UserConfig as D, UnknownUserPlugin as E, KubbEvents as F, logLevel as H, PluginManager as I, getMode as L, Generator as M, ReactGeneratorV2 as N, UserLogger as O, defineGenerator as P, DefineStorage as R, Printer as S, ResolvePathParams as T, AsyncEventEmitter as U, linters as V, URLPath as W, PluginFactoryOptions as _, Config as a, PluginParameter as b, Group as c, Logger as d, LoggerContext as f, PluginContext as g, Plugin as h, BarrelType as i, CoreGeneratorV2 as j, UserPlugin as k, InputData as l, Output as m, AdapterFactoryOptions as n, DevtoolsOptions as o, LoggerOptions as p, AdapterSource as r, GetPluginFactoryOptions as s, Adapter as t, InputPath as u, PluginLifecycle as v, ResolveNameParams as w, PluginWithLifeCycle as x, PluginLifecycleHooks as y, defineStorage as z };
1057
+ //# sourceMappingURL=types-Bbh1o0yW.d.ts.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubb/core",
3
- "version": "5.0.0-alpha.3",
3
+ "version": "5.0.0-alpha.4",
4
4
  "description": "Core functionality for Kubb's plugin-based code generation system, providing the foundation for transforming OpenAPI specifications.",
5
5
  "keywords": [
6
6
  "typescript",
@@ -71,7 +71,7 @@
71
71
  "remeda": "^2.33.6",
72
72
  "semver": "^7.7.4",
73
73
  "tinyexec": "^1.0.4",
74
- "@kubb/ast": "5.0.0-alpha.3"
74
+ "@kubb/ast": "5.0.0-alpha.4"
75
75
  },
76
76
  "devDependencies": {
77
77
  "@types/semver": "^7.7.1",
@@ -0,0 +1,106 @@
1
+ import type { OperationNode, SchemaNode } from '@kubb/ast/types'
2
+ import type { KubbFile } from '@kubb/fabric-core/types'
3
+ import type { FabricReactNode } from '@kubb/react-fabric/types'
4
+ import type { Adapter, Config, Plugin, PluginFactoryOptions } from './types.ts'
5
+
6
+ export type Version = '1' | '2'
7
+
8
+ // V2 props — fully typed with @kubb/ast (already a @kubb/core dependency)
9
+ export type OperationsV2Props<TPlugin extends PluginFactoryOptions = PluginFactoryOptions> = {
10
+ config: Config
11
+ adapter: Adapter
12
+ options: Plugin<TPlugin>['options']
13
+ nodes: Array<OperationNode>
14
+ }
15
+
16
+ export type OperationV2Props<TPlugin extends PluginFactoryOptions = PluginFactoryOptions> = {
17
+ config: Config
18
+ adapter: Adapter
19
+ options: Plugin<TPlugin>['options']
20
+ node: OperationNode
21
+ }
22
+
23
+ export type SchemaV2Props<TPlugin extends PluginFactoryOptions = PluginFactoryOptions> = {
24
+ config: Config
25
+ adapter: Adapter
26
+ options: Plugin<TPlugin>['options']
27
+ node: SchemaNode
28
+ }
29
+
30
+ type UserCoreGeneratorV2<TPlugin extends PluginFactoryOptions> = {
31
+ name: string
32
+ type: 'core'
33
+ version?: '2'
34
+ operations?(props: OperationsV2Props<TPlugin>): Promise<Array<KubbFile.File>>
35
+ operation?(props: OperationV2Props<TPlugin>): Promise<Array<KubbFile.File>>
36
+ schema?(props: SchemaV2Props<TPlugin>): Promise<Array<KubbFile.File>>
37
+ }
38
+
39
+ type UserReactGeneratorV2<TPlugin extends PluginFactoryOptions> = {
40
+ name: string
41
+ type: 'react'
42
+ version?: '2'
43
+ Operations?(props: OperationsV2Props<TPlugin>): FabricReactNode
44
+ Operation?(props: OperationV2Props<TPlugin>): FabricReactNode
45
+ Schema?(props: SchemaV2Props<TPlugin>): FabricReactNode
46
+ }
47
+
48
+ export type CoreGeneratorV2<TPlugin extends PluginFactoryOptions = PluginFactoryOptions> = {
49
+ name: string
50
+ type: 'core'
51
+ version: '2'
52
+ operations(props: OperationsV2Props<TPlugin>): Promise<Array<KubbFile.File>>
53
+ operation(props: OperationV2Props<TPlugin>): Promise<Array<KubbFile.File>>
54
+ schema(props: SchemaV2Props<TPlugin>): Promise<Array<KubbFile.File>>
55
+ }
56
+
57
+ export type ReactGeneratorV2<TPlugin extends PluginFactoryOptions = PluginFactoryOptions> = {
58
+ name: string
59
+ type: 'react'
60
+ version: '2'
61
+ Operations(props: OperationsV2Props<TPlugin>): FabricReactNode
62
+ Operation(props: OperationV2Props<TPlugin>): FabricReactNode
63
+ Schema(props: SchemaV2Props<TPlugin>): FabricReactNode
64
+ }
65
+
66
+ export type Generator<TPlugin extends PluginFactoryOptions = PluginFactoryOptions> = UserCoreGeneratorV2<TPlugin> | UserReactGeneratorV2<TPlugin>
67
+
68
+ export function defineGenerator<TPlugin extends PluginFactoryOptions = PluginFactoryOptions>(
69
+ generator: UserReactGeneratorV2<TPlugin>,
70
+ ): ReactGeneratorV2<TPlugin>
71
+
72
+ export function defineGenerator<TPlugin extends PluginFactoryOptions = PluginFactoryOptions>(generator: UserCoreGeneratorV2<TPlugin>): CoreGeneratorV2<TPlugin>
73
+
74
+ export function defineGenerator<TPlugin extends PluginFactoryOptions = PluginFactoryOptions>(
75
+ generator: UserCoreGeneratorV2<TPlugin> | UserReactGeneratorV2<TPlugin>,
76
+ ): unknown {
77
+ if (generator.type === 'react') {
78
+ return {
79
+ version: '2',
80
+ Operations() {
81
+ return null
82
+ },
83
+ Operation() {
84
+ return null
85
+ },
86
+ Schema() {
87
+ return null
88
+ },
89
+ ...generator,
90
+ }
91
+ }
92
+
93
+ return {
94
+ version: '2',
95
+ async operations() {
96
+ return []
97
+ },
98
+ async operation() {
99
+ return []
100
+ },
101
+ async schema() {
102
+ return []
103
+ },
104
+ ...generator,
105
+ }
106
+ }
package/src/index.ts CHANGED
@@ -4,6 +4,7 @@ export { build, build as default, safeBuild, setup } from './build.ts'
4
4
  export { type CLIOptions, type ConfigInput, defineConfig, isInputPath } from './config.ts'
5
5
  export { formatters, linters, logLevel } from './constants.ts'
6
6
  export { defineAdapter } from './defineAdapter.ts'
7
+ export { defineGenerator } from './defineGenerator.ts'
7
8
  export { defineLogger } from './defineLogger.ts'
8
9
  export { definePlugin } from './definePlugin.ts'
9
10
  export { defineStorage } from './defineStorage.ts'
@@ -20,3 +21,4 @@ export type { FileMetaBase } from './utils/getBarrelFiles.ts'
20
21
  export { getBarrelFiles } from './utils/getBarrelFiles.ts'
21
22
  export { getConfigs } from './utils/getConfigs.ts'
22
23
  export { detectLinter } from './utils/linters.ts'
24
+ export { resolveOptions } from './utils/resolveOptions.ts'
package/src/types.ts CHANGED
@@ -508,5 +508,6 @@ export type Logger<TOptions extends LoggerOptions = LoggerOptions> = {
508
508
 
509
509
  export type UserLogger<TOptions extends LoggerOptions = LoggerOptions> = Omit<Logger<TOptions>, 'logLevel'>
510
510
 
511
+ export type { CoreGeneratorV2, Generator, ReactGeneratorV2 } from './defineGenerator.ts'
511
512
  export type { DefineStorage } from './defineStorage.ts'
512
513
  export type { KubbEvents } from './Kubb.ts'
@@ -0,0 +1,93 @@
1
+ import { isOperationNode, isSchemaNode } from '@kubb/ast'
2
+ import type { Node, OperationNode, SchemaNode } from '@kubb/ast/types'
3
+
4
+ type FilterItem = {
5
+ type: string
6
+ pattern: string | RegExp
7
+ }
8
+
9
+ type OverrideItem<TOptions> = FilterItem & {
10
+ options: Partial<TOptions>
11
+ }
12
+
13
+ type ResolveOptionsContext<TOptions> = {
14
+ options: TOptions
15
+ exclude?: Array<FilterItem>
16
+ include?: Array<FilterItem>
17
+ override?: Array<OverrideItem<TOptions>>
18
+ }
19
+
20
+ function matchesOperationPattern(node: OperationNode, type: string, pattern: string | RegExp): boolean {
21
+ switch (type) {
22
+ case 'tag':
23
+ return node.tags.some((tag) => !!tag.match(pattern))
24
+ case 'operationId':
25
+ return !!node.operationId.match(pattern)
26
+ case 'path':
27
+ return !!node.path.match(pattern)
28
+ case 'method':
29
+ return !!(node.method.toLowerCase() as string).match(pattern)
30
+ default:
31
+ return false
32
+ }
33
+ }
34
+
35
+ function matchesSchemaPattern(node: SchemaNode, type: string, pattern: string | RegExp): boolean | null {
36
+ switch (type) {
37
+ case 'schemaName':
38
+ return node.name ? !!node.name.match(pattern) : false
39
+ default:
40
+ return null
41
+ }
42
+ }
43
+
44
+ /**
45
+ * Resolves the effective plugin options for a given AST node by applying
46
+ * `exclude`, `include`, and `override` rules from the plugin configuration.
47
+ *
48
+ * Returns `null` when the node is excluded or not matched by `include`.
49
+ * Returns the merged options (base options merged with any matching `override`) otherwise.
50
+ *
51
+ * Supported filter types for `OperationNode`: `tag`, `operationId`, `path`, `method`.
52
+ * Supported filter types for `SchemaNode`: `schemaName`.
53
+ *
54
+ * @example
55
+ * const resolved = resolveOptions(operationNode, { options, exclude, include, override })
56
+ * if (!resolved) return // excluded
57
+ */
58
+ export function resolveOptions<TOptions>(node: Node, { options, exclude = [], include, override = [] }: ResolveOptionsContext<TOptions>): TOptions | null {
59
+ if (isOperationNode(node)) {
60
+ const isExcluded = exclude.some(({ type, pattern }) => matchesOperationPattern(node, type, pattern))
61
+ if (isExcluded) {
62
+ return null
63
+ }
64
+
65
+ if (include && !include.some(({ type, pattern }) => matchesOperationPattern(node, type, pattern))) {
66
+ return null
67
+ }
68
+
69
+ const overrideOptions = override.find(({ type, pattern }) => matchesOperationPattern(node, type, pattern))?.options
70
+
71
+ return { ...options, ...overrideOptions }
72
+ }
73
+
74
+ if (isSchemaNode(node)) {
75
+ if (exclude.some(({ type, pattern }) => matchesSchemaPattern(node, type, pattern) === true)) {
76
+ return null
77
+ }
78
+
79
+ if (include) {
80
+ const results = include.map(({ type, pattern }) => matchesSchemaPattern(node, type, pattern))
81
+ const applicable = results.filter((r) => r !== null)
82
+ if (applicable.length > 0 && !applicable.includes(true)) {
83
+ return null
84
+ }
85
+ }
86
+
87
+ const overrideOptions = override.find(({ type, pattern }) => matchesSchemaPattern(node, type, pattern) === true)?.options
88
+
89
+ return { ...options, ...overrideOptions }
90
+ }
91
+
92
+ return options
93
+ }