@kubb/core 5.0.0-alpha.4 → 5.0.0-alpha.41

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.
Files changed (71) hide show
  1. package/dist/PluginDriver-BQwm8hDd.cjs +1729 -0
  2. package/dist/PluginDriver-BQwm8hDd.cjs.map +1 -0
  3. package/dist/PluginDriver-CgXFtmNP.js +1617 -0
  4. package/dist/PluginDriver-CgXFtmNP.js.map +1 -0
  5. package/dist/index.cjs +915 -1901
  6. package/dist/index.cjs.map +1 -1
  7. package/dist/index.d.ts +268 -264
  8. package/dist/index.js +894 -1863
  9. package/dist/index.js.map +1 -1
  10. package/dist/mocks.cjs +164 -0
  11. package/dist/mocks.cjs.map +1 -0
  12. package/dist/mocks.d.ts +74 -0
  13. package/dist/mocks.js +159 -0
  14. package/dist/mocks.js.map +1 -0
  15. package/dist/types-C6NCtNqM.d.ts +2151 -0
  16. package/package.json +11 -14
  17. package/src/FileManager.ts +131 -0
  18. package/src/FileProcessor.ts +84 -0
  19. package/src/Kubb.ts +174 -85
  20. package/src/PluginDriver.ts +941 -0
  21. package/src/constants.ts +33 -43
  22. package/src/createAdapter.ts +25 -0
  23. package/src/createKubb.ts +605 -0
  24. package/src/createPlugin.ts +31 -0
  25. package/src/createRenderer.ts +57 -0
  26. package/src/createStorage.ts +58 -0
  27. package/src/defineGenerator.ts +88 -100
  28. package/src/defineLogger.ts +13 -3
  29. package/src/defineParser.ts +45 -0
  30. package/src/definePlugin.ts +90 -7
  31. package/src/defineResolver.ts +453 -0
  32. package/src/devtools.ts +14 -14
  33. package/src/index.ts +12 -17
  34. package/src/mocks.ts +234 -0
  35. package/src/renderNode.ts +35 -0
  36. package/src/storages/fsStorage.ts +29 -9
  37. package/src/storages/memoryStorage.ts +2 -2
  38. package/src/types.ts +821 -152
  39. package/src/utils/TreeNode.ts +47 -9
  40. package/src/utils/diagnostics.ts +4 -1
  41. package/src/utils/executeStrategies.ts +16 -13
  42. package/src/utils/getBarrelFiles.ts +88 -15
  43. package/src/utils/isInputPath.ts +10 -0
  44. package/src/utils/packageJSON.ts +75 -0
  45. package/dist/chunk-ByKO4r7w.cjs +0 -38
  46. package/dist/hooks.cjs +0 -50
  47. package/dist/hooks.cjs.map +0 -1
  48. package/dist/hooks.d.ts +0 -49
  49. package/dist/hooks.js +0 -46
  50. package/dist/hooks.js.map +0 -1
  51. package/dist/types-Bbh1o0yW.d.ts +0 -1057
  52. package/src/BarrelManager.ts +0 -74
  53. package/src/PackageManager.ts +0 -180
  54. package/src/PluginManager.ts +0 -668
  55. package/src/PromiseManager.ts +0 -40
  56. package/src/build.ts +0 -420
  57. package/src/config.ts +0 -56
  58. package/src/defineAdapter.ts +0 -22
  59. package/src/defineStorage.ts +0 -56
  60. package/src/errors.ts +0 -1
  61. package/src/hooks/index.ts +0 -8
  62. package/src/hooks/useKubb.ts +0 -22
  63. package/src/hooks/useMode.ts +0 -11
  64. package/src/hooks/usePlugin.ts +0 -11
  65. package/src/hooks/usePluginManager.ts +0 -11
  66. package/src/utils/FunctionParams.ts +0 -155
  67. package/src/utils/formatters.ts +0 -56
  68. package/src/utils/getConfigs.ts +0 -30
  69. package/src/utils/getPlugins.ts +0 -23
  70. package/src/utils/linters.ts +0 -25
  71. package/src/utils/resolveOptions.ts +0 -93
@@ -1,155 +0,0 @@
1
- import { camelCase } from '@internals/utils'
2
- // TODO replace with @internals/utils
3
- import { sortBy } from 'remeda'
4
-
5
- type FunctionParamsASTWithoutType = {
6
- name?: string
7
- type?: string
8
- /**
9
- * @default true
10
- */
11
- required?: boolean
12
- /**
13
- * @default true
14
- */
15
- enabled?: boolean
16
- default?: string
17
- }
18
-
19
- type FunctionParamsASTWithType = {
20
- name?: never
21
- type: string
22
- /**
23
- * @default true
24
- */
25
- required?: boolean
26
- /**
27
- * @default true
28
- */
29
- enabled?: boolean
30
- default?: string
31
- }
32
- /**
33
- * @deprecated
34
- */
35
- export type FunctionParamsAST = FunctionParamsASTWithoutType | FunctionParamsASTWithType
36
-
37
- /**
38
- * @deprecated
39
- */
40
- export class FunctionParams {
41
- #items: Array<FunctionParamsAST | FunctionParamsAST[]> = []
42
-
43
- get items(): FunctionParamsAST[] {
44
- return this.#items.flat()
45
- }
46
-
47
- add(item: FunctionParamsAST | Array<FunctionParamsAST | FunctionParamsAST[] | undefined> | undefined): FunctionParams {
48
- if (!item) {
49
- return this
50
- }
51
-
52
- if (Array.isArray(item)) {
53
- item
54
- .filter((x): x is FunctionParamsAST | FunctionParamsAST[] => x !== undefined)
55
- .forEach((it) => {
56
- this.#items.push(it)
57
- })
58
- return this
59
- }
60
- this.#items.push(item)
61
-
62
- return this
63
- }
64
- static #orderItems(items: Array<FunctionParamsAST | FunctionParamsAST[]>) {
65
- return sortBy(
66
- items.filter(Boolean),
67
- [(item) => Array.isArray(item), 'desc'], // arrays (rest params) first
68
- [(item) => !Array.isArray(item) && (item as FunctionParamsAST).default !== undefined, 'asc'], // no-default before has-default
69
- [(item) => Array.isArray(item) || ((item as FunctionParamsAST).required ?? true), 'desc'], // required before optional
70
- )
71
- }
72
-
73
- static #addParams(acc: string[], item: FunctionParamsAST) {
74
- const { enabled = true, name, type, required = true, ...rest } = item
75
-
76
- if (!enabled) {
77
- return acc
78
- }
79
-
80
- if (!name) {
81
- // when name is not se we uses TypeScript generics
82
- acc.push(`${type}${rest.default ? ` = ${rest.default}` : ''}`)
83
-
84
- return acc
85
- }
86
- // TODO check why we still need the camelcase here
87
- const parameterName = name.startsWith('{') ? name : camelCase(name)
88
-
89
- if (type) {
90
- if (required) {
91
- acc.push(`${parameterName}: ${type}${rest.default ? ` = ${rest.default}` : ''}`)
92
- } else {
93
- acc.push(`${parameterName}?: ${type}`)
94
- }
95
- } else {
96
- acc.push(`${parameterName}`)
97
- }
98
-
99
- return acc
100
- }
101
-
102
- static toObject(items: FunctionParamsAST[]): FunctionParamsAST {
103
- let type: string[] = []
104
- let name: string[] = []
105
-
106
- const enabled = items.every((item) => item.enabled) ? items.at(0)?.enabled : true
107
- const required = items.every((item) => item.required) ?? true
108
-
109
- items.forEach((item) => {
110
- name = FunctionParams.#addParams(name, { ...item, type: undefined })
111
- if (items.some((item) => item.type)) {
112
- type = FunctionParams.#addParams(type, item)
113
- }
114
- })
115
-
116
- return {
117
- name: `{ ${name.join(', ')} }`,
118
- type: type.length ? `{ ${type.join('; ')} }` : undefined,
119
- enabled,
120
- required,
121
- }
122
- }
123
-
124
- toObject(): FunctionParamsAST {
125
- const items = FunctionParams.#orderItems(this.#items).flat()
126
-
127
- return FunctionParams.toObject(items)
128
- }
129
-
130
- static toString(items: (FunctionParamsAST | FunctionParamsAST[])[]): string {
131
- const sortedData = FunctionParams.#orderItems(items)
132
-
133
- return sortedData
134
- .reduce((acc, item) => {
135
- if (Array.isArray(item)) {
136
- if (item.length <= 0) {
137
- return acc
138
- }
139
- const subItems = FunctionParams.#orderItems(item) as FunctionParamsAST[]
140
- const objectItem = FunctionParams.toObject(subItems)
141
-
142
- return FunctionParams.#addParams(acc, objectItem)
143
- }
144
-
145
- return FunctionParams.#addParams(acc, item)
146
- }, [] as string[])
147
- .join(', ')
148
- }
149
-
150
- toString(): string {
151
- const items = FunctionParams.#orderItems(this.#items)
152
-
153
- return FunctionParams.toString(items)
154
- }
155
- }
@@ -1,56 +0,0 @@
1
- import { x } from 'tinyexec'
2
- import type { formatters } from '../constants.ts'
3
-
4
- type Formatter = keyof typeof formatters
5
-
6
- /**
7
- * Check if a formatter command is available in the system.
8
- *
9
- * @param formatter - The formatter to check ('biome', 'prettier', or 'oxfmt')
10
- * @returns Promise that resolves to true if the formatter is available, false otherwise
11
- *
12
- * @remarks
13
- * This function checks availability by running `<formatter> --version` command.
14
- * All supported formatters (biome, prettier, oxfmt) implement the --version flag.
15
- */
16
- async function isFormatterAvailable(formatter: Formatter): Promise<boolean> {
17
- try {
18
- // Try to get the version of the formatter to check if it's installed
19
- await x(formatter, ['--version'], { nodeOptions: { stdio: 'ignore' } })
20
- return true
21
- } catch {
22
- return false
23
- }
24
- }
25
-
26
- /**
27
- * Detect which formatter is available in the system.
28
- *
29
- * @returns Promise that resolves to the first available formatter or undefined if none are found
30
- *
31
- * @remarks
32
- * Checks in order of preference: biome, oxfmt, prettier.
33
- * Uses the `--version` flag to detect if each formatter command is available.
34
- * This is a reliable method as all supported formatters implement this flag.
35
- *
36
- * @example
37
- * ```typescript
38
- * const formatter = await detectFormatter()
39
- * if (formatter) {
40
- * console.log(`Using ${formatter} for formatting`)
41
- * } else {
42
- * console.log('No formatter found')
43
- * }
44
- * ```
45
- */
46
- export async function detectFormatter(): Promise<Formatter | undefined> {
47
- const formatterNames: Formatter[] = ['biome', 'oxfmt', 'prettier']
48
-
49
- for (const formatter of formatterNames) {
50
- if (await isFormatterAvailable(formatter)) {
51
- return formatter
52
- }
53
- }
54
-
55
- return undefined
56
- }
@@ -1,30 +0,0 @@
1
- import type { CLIOptions, ConfigInput } from '../config.ts'
2
- import type { Config, UserConfig } from '../types.ts'
3
- import { getPlugins } from './getPlugins.ts'
4
-
5
- /**
6
- * Converting UserConfig to Config Array without a change in the object beside the JSON convert.
7
- */
8
- export async function getConfigs(config: ConfigInput | UserConfig, args: CLIOptions): Promise<Array<Config>> {
9
- const resolvedConfig: Promise<UserConfig | Array<UserConfig>> =
10
- typeof config === 'function' ? Promise.resolve(config(args as CLIOptions)) : Promise.resolve(config)
11
-
12
- let userConfigs = await resolvedConfig
13
-
14
- if (!Array.isArray(userConfigs)) {
15
- userConfigs = [userConfigs]
16
- }
17
-
18
- const results: Array<Config> = []
19
-
20
- for (const item of userConfigs) {
21
- const plugins = item.plugins ? await getPlugins(item.plugins) : undefined
22
-
23
- results.push({
24
- ...item,
25
- plugins,
26
- } as Config)
27
- }
28
-
29
- return results
30
- }
@@ -1,23 +0,0 @@
1
- import type { UnknownUserPlugin, UserConfig } from '../types.ts'
2
-
3
- type PluginsArray = Array<Omit<UnknownUserPlugin, 'inject'>>
4
-
5
- function isJSONPlugins(plugins: UserConfig['plugins']): boolean {
6
- return Array.isArray(plugins) && plugins.some((plugin) => Array.isArray(plugin) && typeof (plugin as unknown[])[0] === 'string')
7
- }
8
-
9
- function isObjectPlugins(plugins: UserConfig['plugins']): boolean {
10
- return plugins instanceof Object && !Array.isArray(plugins)
11
- }
12
-
13
- export function getPlugins(plugins: UserConfig['plugins']): Promise<PluginsArray | undefined> {
14
- if (isObjectPlugins(plugins)) {
15
- throw new Error('Object plugins are not supported anymore, best to use http://kubb.dev/getting-started/configure#json')
16
- }
17
-
18
- if (isJSONPlugins(plugins)) {
19
- throw new Error('JSON plugins are not supported anymore, best to use http://kubb.dev/getting-started/configure#json')
20
- }
21
-
22
- return Promise.resolve(plugins)
23
- }
@@ -1,25 +0,0 @@
1
- import { x } from 'tinyexec'
2
- import type { linters } from '../constants.ts'
3
-
4
- type Linter = keyof typeof linters
5
-
6
- async function isLinterAvailable(linter: Linter): Promise<boolean> {
7
- try {
8
- await x(linter, ['--version'], { nodeOptions: { stdio: 'ignore' } })
9
- return true
10
- } catch {
11
- return false
12
- }
13
- }
14
-
15
- export async function detectLinter(): Promise<Linter | undefined> {
16
- const linterNames: Linter[] = ['biome', 'oxlint', 'eslint']
17
-
18
- for (const linter of linterNames) {
19
- if (await isLinterAvailable(linter)) {
20
- return linter
21
- }
22
- }
23
-
24
- return undefined
25
- }
@@ -1,93 +0,0 @@
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
- }