@kubb/core 2.0.0-beta.1 → 2.0.0-beta.11

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 (67) hide show
  1. package/dist/Queue-2-6pMcCx.d.cts +32 -0
  2. package/dist/Queue-2-6pMcCx.d.ts +32 -0
  3. package/dist/fs.cjs +2383 -0
  4. package/dist/fs.cjs.map +1 -0
  5. package/dist/fs.d.cts +5 -0
  6. package/dist/fs.d.ts +5 -0
  7. package/dist/fs.js +2380 -0
  8. package/dist/fs.js.map +1 -0
  9. package/dist/index.cjs +3448 -394
  10. package/dist/index.cjs.map +1 -1
  11. package/dist/index.d.cts +77 -127
  12. package/dist/index.d.ts +77 -127
  13. package/dist/index.js +3761 -423
  14. package/dist/index.js.map +1 -1
  15. package/dist/logger.cjs +90 -0
  16. package/dist/logger.cjs.map +1 -0
  17. package/dist/logger.d.cts +32 -0
  18. package/dist/logger.d.ts +32 -0
  19. package/dist/logger.js +78 -0
  20. package/dist/logger.js.map +1 -0
  21. package/dist/transformers.cjs +222 -0
  22. package/dist/transformers.cjs.map +1 -0
  23. package/dist/transformers.d.cts +55 -0
  24. package/dist/transformers.d.ts +55 -0
  25. package/dist/transformers.js +207 -0
  26. package/dist/transformers.js.map +1 -0
  27. package/dist/utils.cjs +174 -899
  28. package/dist/utils.cjs.map +1 -1
  29. package/dist/utils.d.cts +2 -146
  30. package/dist/utils.d.ts +2 -146
  31. package/dist/utils.js +175 -859
  32. package/dist/utils.js.map +1 -1
  33. package/dist/write-46ytbnu9.d.cts +7 -0
  34. package/dist/write-46ytbnu9.d.ts +7 -0
  35. package/package.json +27 -12
  36. package/src/BarrelManager.ts +55 -65
  37. package/src/FileManager.ts +109 -68
  38. package/src/PluginManager.ts +55 -32
  39. package/src/build.ts +5 -16
  40. package/src/fs/index.ts +3 -0
  41. package/src/index.ts +4 -5
  42. package/src/{utils/logger.ts → logger.ts} +37 -0
  43. package/src/plugin.ts +4 -4
  44. package/src/transformers/casing.ts +9 -0
  45. package/src/transformers/createJSDocBlockText.ts +9 -0
  46. package/src/transformers/index.ts +36 -0
  47. package/src/transformers/trim.ts +7 -0
  48. package/src/types.ts +23 -42
  49. package/src/utils/FunctionParams.ts +3 -2
  50. package/src/utils/TreeNode.ts +6 -3
  51. package/src/utils/URLPath.ts +5 -5
  52. package/src/utils/index.ts +10 -19
  53. package/src/SchemaGenerator.ts +0 -8
  54. package/src/utils/randomColour.ts +0 -39
  55. package/src/utils/throttle.ts +0 -30
  56. package/src/utils/transformers/createJSDocBlockText.ts +0 -15
  57. package/src/utils/transformers/index.ts +0 -22
  58. package/src/utils/transformers/trim.ts +0 -3
  59. /package/src/{utils → fs}/clean.ts +0 -0
  60. /package/src/{utils → fs}/read.ts +0 -0
  61. /package/src/{utils → fs}/write.ts +0 -0
  62. /package/src/{utils/transformers → transformers}/combineCodes.ts +0 -0
  63. /package/src/{utils/transformers → transformers}/escape.ts +0 -0
  64. /package/src/{utils/transformers → transformers}/indent.ts +0 -0
  65. /package/src/{utils/transformers → transformers}/nameSorter.ts +0 -0
  66. /package/src/{utils/transformers → transformers}/searchAndReplace.ts +0 -0
  67. /package/src/{utils/transformers → transformers}/transformReservedWord.ts +0 -0
package/src/plugin.ts CHANGED
@@ -26,10 +26,10 @@ type Options = {
26
26
  }
27
27
 
28
28
  // not publicly exported
29
- export type CorePluginOptions = PluginFactoryOptions<'core', 'controller', Options, Options, PluginContext, never>
29
+ export type CorePluginOptions = PluginFactoryOptions<'core', Options, Options, PluginContext, never>
30
30
 
31
31
  export const pluginName = 'core' satisfies CorePluginOptions['name']
32
- export const pluginKey: CorePluginOptions['key'] = ['controller', pluginName] satisfies CorePluginOptions['key']
32
+ export const pluginKey: CorePluginOptions['key'] = [pluginName] satisfies CorePluginOptions['key']
33
33
 
34
34
  export const definePlugin = createPlugin<CorePluginOptions>((options) => {
35
35
  const { fileManager, pluginManager, resolvePath, resolveName, logger } = options
@@ -37,8 +37,8 @@ export const definePlugin = createPlugin<CorePluginOptions>((options) => {
37
37
  return {
38
38
  name: pluginName,
39
39
  options,
40
- key: ['controller', 'core'],
41
- kind: 'controller',
40
+ key: ['core'],
41
+
42
42
  api() {
43
43
  return {
44
44
  get config() {
@@ -0,0 +1,9 @@
1
+ import { camelCase as changeCaseCamel, camelCaseTransformMerge, pascalCase as changePascalCase, pascalCaseTransformMerge } from 'change-case'
2
+
3
+ export function camelCase(text: string): string {
4
+ return changeCaseCamel(text, { delimiter: '', stripRegexp: /[^A-Z0-9$]/gi, transform: camelCaseTransformMerge })
5
+ }
6
+
7
+ export function pascalCase(text: string): string {
8
+ return changePascalCase(text, { delimiter: '', stripRegexp: /[^A-Z0-9$]/gi, transform: pascalCaseTransformMerge })
9
+ }
@@ -0,0 +1,9 @@
1
+ export function createJSDocBlockText({ comments }: { comments: Array<string> }): string {
2
+ const filteredComments = comments.filter(Boolean)
3
+
4
+ if (!filteredComments.length) {
5
+ return ''
6
+ }
7
+
8
+ return `/**\n * ${filteredComments.join('\n * ')}\n */`
9
+ }
@@ -0,0 +1,36 @@
1
+ import { camelCase, pascalCase } from './casing.ts'
2
+ import { combineCodes } from './combineCodes.ts'
3
+ import { createJSDocBlockText } from './createJSDocBlockText.ts'
4
+ import { escape, jsStringEscape } from './escape.ts'
5
+ import { createIndent } from './indent.ts'
6
+ import { nameSorter } from './nameSorter.ts'
7
+ import { searchAndReplace } from './searchAndReplace.ts'
8
+ import { transformReservedWord } from './transformReservedWord.ts'
9
+ import { trim, trimExtName } from './trim.ts'
10
+
11
+ export { camelCase, pascalCase } from './casing.ts'
12
+ export { combineCodes } from './combineCodes.ts'
13
+ export { createJSDocBlockText } from './createJSDocBlockText.ts'
14
+ export { escape, jsStringEscape } from './escape.ts'
15
+ export { createIndent } from './indent.ts'
16
+ export { nameSorter } from './nameSorter.ts'
17
+ export { searchAndReplace } from './searchAndReplace.ts'
18
+ export { transformReservedWord } from './transformReservedWord.ts'
19
+ export { trim, trimExtName } from './trim.ts'
20
+
21
+ export default {
22
+ combineCodes,
23
+ escape,
24
+ jsStringEscape,
25
+ createIndent,
26
+ transformReservedWord,
27
+ nameSorter,
28
+ searchAndReplace,
29
+ trim,
30
+ trimExtName,
31
+ JSDoc: {
32
+ createJSDocBlockText,
33
+ },
34
+ camelCase,
35
+ pascalCase,
36
+ } as const
@@ -0,0 +1,7 @@
1
+ export function trim(text: string): string {
2
+ return text.replaceAll(/\n/g, '').trim()
3
+ }
4
+
5
+ export function trimExtName(text: string): string {
6
+ return text.replace(/\.[^/.]+$/, '')
7
+ }
package/src/types.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  import type { PossiblePromise } from '@kubb/types'
2
2
  import type { FileManager, KubbFile } from './FileManager.ts'
3
3
  import type { OptionsPlugins, PluginUnion } from './index.ts'
4
+ import type { Logger, LogLevel } from './logger.ts'
4
5
  import type { PluginManager } from './PluginManager.ts'
5
6
  import type { Cache } from './utils/cache.ts'
6
- import type { Logger, LogLevel } from './utils/logger.ts'
7
7
 
8
8
  // config
9
9
 
@@ -123,8 +123,6 @@ export type CLIOptions = {
123
123
 
124
124
  // plugin
125
125
 
126
- export type KubbPluginKind = 'schema' | 'controller'
127
-
128
126
  export type KubbUnionPlugins = PluginUnion
129
127
 
130
128
  export type KubbObjectPlugin = keyof OptionsPlugins
@@ -134,10 +132,6 @@ export type PluginFactoryOptions<
134
132
  * Name to be used for the plugin, this will also be used for they key.
135
133
  */
136
134
  TName extends string = string,
137
- /**
138
- * @type "schema" | "controller"
139
- */
140
- TKind extends KubbPluginKind = KubbPluginKind,
141
135
  /**
142
136
  * Options of the plugin.
143
137
  */
@@ -161,18 +155,17 @@ export type PluginFactoryOptions<
161
155
  TAppMeta = unknown,
162
156
  > = {
163
157
  name: TName
164
- kind: TKind
165
158
  /**
166
159
  * Same behaviour like what has been done with `QueryKey` in `@tanstack/react-query`
167
160
  */
168
- key: [kind: TKind | undefined, name: TName | string, identifier?: string | number]
161
+ key: [name: TName | string, identifier?: string | number]
169
162
  options: TOptions
170
163
  resolvedOptions: TResolvedOptions
171
164
  api: TAPI
172
165
  resolvePathOptions: TResolvePathOptions
173
166
  appMeta: {
174
167
  pluginManager: PluginManager
175
- plugin: KubbPlugin<PluginFactoryOptions<TName, TKind, TOptions, TResolvedOptions, TAPI, TResolvePathOptions, TAppMeta>>
168
+ plugin: KubbPlugin<PluginFactoryOptions<TName, TOptions, TResolvedOptions, TAPI, TResolvePathOptions, TAppMeta>>
176
169
  } & TAppMeta
177
170
  }
178
171
 
@@ -182,18 +175,23 @@ export type KubbUserPlugin<TOptions extends PluginFactoryOptions = PluginFactory
182
175
  & {
183
176
  /**
184
177
  * Unique name used for the plugin
178
+ * The name of the plugin follows the format scope:foo-bar or foo-bar, adding scope: can avoid naming conflicts with other plugins.
185
179
  * @example @kubb/typescript
186
180
  */
187
181
  name: TOptions['name']
188
- /**
189
- * Internal key used when a developer uses more than one of the same plugin
190
- * @private
191
- */
192
- key?: TOptions['key']
193
182
  /**
194
183
  * Options set for a specific plugin(see kubb.config.js), passthrough of options.
195
184
  */
196
185
  options: TOptions['resolvedOptions']
186
+ /**
187
+ * Specifies the preceding plugins for the current plugin. You can pass an array of preceding plugin names, and the current plugin will be executed after these plugins.
188
+ * Can be used to validate depended plugins.
189
+ */
190
+ pre?: Array<string>
191
+ /**
192
+ * Specifies the succeeding plugins for the current plugin. You can pass an array of succeeding plugin names, and the current plugin will be executed before these plugins.
193
+ */
194
+ post?: Array<string>
197
195
  }
198
196
  & (TOptions['api'] extends never ? {
199
197
  api?: never
@@ -201,24 +199,10 @@ export type KubbUserPlugin<TOptions extends PluginFactoryOptions = PluginFactory
201
199
  : {
202
200
  api: (this: TOptions['name'] extends 'core' ? null : Omit<PluginContext<TOptions>, 'addFile'>) => TOptions['api']
203
201
  })
204
- & (TOptions['kind'] extends never ? {
205
- kind?: never
206
- }
207
- : {
208
- /**
209
- * Kind/type for the plugin
210
- *
211
- * Type 'schema' can be used for JSON schema's, TypeScript types, ...
212
- *
213
- * Type 'controller' can be used to create generate API calls, React-Query hooks, Axios controllers, ...
214
- * @default undefined
215
- */
216
- kind: TOptions['kind']
217
- })
218
202
 
219
203
  export type KubbUserPluginWithLifeCycle<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = KubbUserPlugin<TOptions> & PluginLifecycle<TOptions>
220
204
 
221
- type UnknownKubbUserPlugin = KubbUserPlugin<PluginFactoryOptions<any, any, any, any, any, any, any>>
205
+ type UnknownKubbUserPlugin = KubbUserPlugin<PluginFactoryOptions<any, any, any, any, any, any>>
222
206
 
223
207
  export type KubbPlugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions> =
224
208
  & {
@@ -233,16 +217,18 @@ export type KubbPlugin<TOptions extends PluginFactoryOptions = PluginFactoryOpti
233
217
  */
234
218
  key: TOptions['key']
235
219
  /**
236
- * Options set for a specific plugin(see kubb.config.js), passthrough of options.
220
+ * Specifies the preceding plugins for the current plugin. You can pass an array of preceding plugin names, and the current plugin will be executed after these plugins.
221
+ * Can be used to validate depended plugins.
237
222
  */
238
- options: TOptions['resolvedOptions']
223
+ pre?: Array<string>
239
224
  /**
240
- * Kind/type for the plugin
241
- * Type 'schema' can be used for JSON schema's, TypeScript types, ...
242
- * Type 'controller' can be used to create generate API calls, React-Query hooks, Axios controllers, ...
243
- * @default undefined
225
+ * Specifies the succeeding plugins for the current plugin. You can pass an array of succeeding plugin names, and the current plugin will be executed before these plugins.
244
226
  */
245
- kind?: TOptions['kind']
227
+ post?: Array<string>
228
+ /**
229
+ * Options set for a specific plugin(see kubb.config.js), passthrough of options.
230
+ */
231
+ options: TOptions['resolvedOptions']
246
232
  /**
247
233
  * Define an api that can be used by other plugins, see `PluginManager' where we convert from `KubbUserPlugin` to `KubbPlugin`(used when calling `createPlugin`).
248
234
  */
@@ -257,11 +243,6 @@ export type KubbPlugin<TOptions extends PluginFactoryOptions = PluginFactoryOpti
257
243
  export type KubbPluginWithLifeCycle<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = KubbPlugin<TOptions> & PluginLifecycle<TOptions>
258
244
 
259
245
  export type PluginLifecycle<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = {
260
- /**
261
- * Valdiate all plugins to see if their depended plugins are installed and configured.
262
- * @type hookParallel
263
- */
264
- validate?: (this: Omit<PluginContext<TOptions>, 'addFile'>, plugins: NonNullable<KubbConfig['plugins']>) => PossiblePromise<true>
265
246
  /**
266
247
  * Start of the lifecycle of a plugin.
267
248
  * @type hookParallel
@@ -1,6 +1,7 @@
1
- import { camelCase, camelCaseTransformMerge } from 'change-case'
2
1
  import { orderBy } from 'natural-orderby'
3
2
 
3
+ import transformers from '../transformers/index.ts'
4
+
4
5
  type FunctionParamsASTWithoutType = {
5
6
  name?: string
6
7
  type?: string
@@ -66,7 +67,7 @@ export class FunctionParams {
66
67
  return acc
67
68
  }
68
69
  // TODO check whey we still need the camelcase here
69
- const parameterName = name.startsWith('{') ? name : camelCase(name, { delimiter: '', transform: camelCaseTransformMerge })
70
+ const parameterName = name.startsWith('{') ? name : transformers.camelCase(name)
70
71
 
71
72
  if (type) {
72
73
  if (required) {
@@ -3,10 +3,13 @@ import dirTree from 'directory-tree'
3
3
  import { FileManager } from '../FileManager.ts'
4
4
 
5
5
  import type { DirectoryTree, DirectoryTreeOptions } from 'directory-tree'
6
+ import type { KubbFile } from '../FileManager.ts'
6
7
 
7
8
  export type TreeNodeOptions = DirectoryTreeOptions
8
9
 
9
- export class TreeNode<T = unknown> {
10
+ type BarrelData = { type: KubbFile.Mode; path: KubbFile.Path; name: string }
11
+
12
+ export class TreeNode<T = BarrelData> {
10
13
  public data: T
11
14
 
12
15
  public parent?: TreeNode<T>
@@ -91,7 +94,7 @@ export class TreeNode<T = unknown> {
91
94
  return this
92
95
  }
93
96
 
94
- public static build<T = unknown>(path: string, options: TreeNodeOptions = {}): TreeNode<T> | null {
97
+ public static build(path: string, options: TreeNodeOptions = {}): TreeNode | null {
95
98
  try {
96
99
  const exclude = Array.isArray(options.exclude) ? options.exclude : [options.exclude].filter(Boolean)
97
100
  const filteredTree = dirTree(path, { extensions: options.extensions, exclude: [/node_modules/, ...exclude] })
@@ -114,7 +117,7 @@ export class TreeNode<T = unknown> {
114
117
 
115
118
  filteredTree.children?.forEach((child) => recurse(treeNode, child))
116
119
 
117
- return treeNode as TreeNode<T>
120
+ return treeNode
118
121
  } catch (e) {
119
122
  throw new Error('Something went wrong with creating index files with the TreehNode class', { cause: e })
120
123
  }
@@ -1,4 +1,4 @@
1
- import { camelCase, camelCaseTransformMerge } from 'change-case'
1
+ import transformers from '../transformers/index.ts'
2
2
 
3
3
  export type URLObject = {
4
4
  url: string
@@ -90,8 +90,8 @@ export class URLPath {
90
90
  if (found) {
91
91
  newPath = found.reduce((prev, curr) => {
92
92
  const pathParam = replacer
93
- ? replacer(camelCase(curr, { delimiter: '', transform: camelCaseTransformMerge }))
94
- : camelCase(curr, { delimiter: '', transform: camelCaseTransformMerge })
93
+ ? replacer(transformers.camelCase(curr))
94
+ : transformers.camelCase(curr)
95
95
  const replacement = `\${${pathParam}}`
96
96
 
97
97
  return prev.replace(curr, replacement)
@@ -114,8 +114,8 @@ export class URLPath {
114
114
  item = item.replaceAll('{', '').replaceAll('}', '')
115
115
 
116
116
  const pathParam = replacer
117
- ? replacer(camelCase(item, { delimiter: '', transform: camelCaseTransformMerge }))
118
- : camelCase(item, { delimiter: '', transform: camelCaseTransformMerge })
117
+ ? replacer(transformers.camelCase(item))
118
+ : transformers.camelCase(item)
119
119
 
120
120
  params[pathParam] = pathParam
121
121
  }, this.path)
@@ -1,19 +1,10 @@
1
- export * from './cache.ts'
2
- export * from './cache.ts'
3
- export * from './clean.ts'
4
- export * from './FunctionParams.ts'
5
- export * from './FunctionParams.ts'
6
- export * from './logger.ts'
7
- export * from './promise.ts'
8
- export * from './Queue.ts'
9
- export * from './Queue.ts'
10
- export * from './randomColour.ts'
11
- export * from './read.ts'
12
- export * from './renderTemplate.ts'
13
- export * from './throttle.ts'
14
- export * from './timeout.ts'
15
- export * from './transformers/index.ts'
16
- export * from './TreeNode.ts'
17
- export * from './uniqueName.ts'
18
- export * from './URLPath.ts'
19
- export * from './write.ts'
1
+ export type { FunctionParamsAST } from './FunctionParams.ts'
2
+ export { FunctionParams } from './FunctionParams.ts'
3
+ export { isPromise, isPromiseFulfilledResult, isPromiseRejectedResult } from './promise.ts'
4
+ export type { QueueJob } from './Queue.ts'
5
+ export { Queue } from './Queue.ts'
6
+ export { renderTemplate } from './renderTemplate.ts'
7
+ export { timeout } from './timeout.ts'
8
+ export { getUniqueName, setUniqueName } from './uniqueName.ts'
9
+ export type { URLObject } from './URLPath.ts'
10
+ export { URLPath } from './URLPath.ts'
@@ -1,8 +0,0 @@
1
- import { Generator } from './Generator.ts'
2
-
3
- /**
4
- * Abstract class that contains the building blocks for plugins to create their own SchemaGenerator
5
- */
6
- export abstract class SchemaGenerator<TOptions extends object, TInput, TOutput> extends Generator<TOptions> {
7
- abstract build(schema: TInput, name: string, description?: string): TOutput
8
- }
@@ -1,39 +0,0 @@
1
- import pc from 'picocolors'
2
- import seedrandom from 'seedrandom'
3
-
4
- import type { Formatter } from 'picocolors/types.ts'
5
-
6
- const defaultColours = ['black', 'blue', 'darkBlue', 'cyan', 'gray', 'green', 'darkGreen', 'magenta', 'red', 'darkRed', 'yellow', 'darkYellow'] as const
7
-
8
- export function randomColour(text?: string, colours = defaultColours): string {
9
- if (!text) {
10
- return 'white'
11
- }
12
-
13
- const random = seedrandom(text)
14
- const colour = colours.at(Math.floor(random() * colours.length)) || 'white'
15
-
16
- return colour
17
- }
18
-
19
- export function randomPicoColour(text?: string, colors = defaultColours): string {
20
- const colours = pc.createColors(true)
21
-
22
- if (!text) {
23
- return colours.white(text)
24
- }
25
-
26
- const colour = randomColour(text, colors)
27
- const isDark = colour.includes('dark')
28
- const key = colour.replace('dark', '').toLowerCase() as keyof typeof colours
29
- const formatter: Formatter = colours[key] as Formatter
30
-
31
- if (isDark) {
32
- return pc.bold(formatter(text))
33
- }
34
-
35
- if (typeof formatter !== 'function') {
36
- throw new Error('Formatter for picoColor is not of type function/Formatter')
37
- }
38
- return formatter(text)
39
- }
@@ -1,30 +0,0 @@
1
- export const throttle = <R, A extends any[]>(fn: (...args: A) => R, delay: number): [(...args: A) => R | undefined, () => void] => {
2
- let wait = false
3
- let timeout: NodeJS.Timeout
4
- let cancelled = false
5
-
6
- return [
7
- (...args: A) => {
8
- if (cancelled) {
9
- return undefined
10
- }
11
- if (wait) {
12
- return undefined
13
- }
14
-
15
- const val = fn(...args)
16
-
17
- wait = true
18
-
19
- timeout = setTimeout(() => {
20
- wait = false
21
- }, delay)
22
-
23
- return val
24
- },
25
- () => {
26
- cancelled = true
27
- clearTimeout(timeout)
28
- },
29
- ]
30
- }
@@ -1,15 +0,0 @@
1
- export function createJSDocBlockText({ comments, newLine }: { comments: Array<string>; newLine?: boolean }): string {
2
- const filteredComments = comments.filter(Boolean)
3
-
4
- if (!filteredComments.length) {
5
- return ''
6
- }
7
-
8
- const source = `/**\n * ${filteredComments.join('\n * ')}\n */`
9
-
10
- if (newLine) {
11
- return `${source}\n`
12
- }
13
-
14
- return source
15
- }
@@ -1,22 +0,0 @@
1
- import { combineCodes } from './combineCodes.ts'
2
- import { createJSDocBlockText } from './createJSDocBlockText.ts'
3
- import { escape, jsStringEscape } from './escape.ts'
4
- import { createIndent } from './indent.ts'
5
- import { nameSorter } from './nameSorter.ts'
6
- import { searchAndReplace } from './searchAndReplace.ts'
7
- import { transformReservedWord } from './transformReservedWord.ts'
8
- import { trim } from './trim.ts'
9
-
10
- export const transformers = {
11
- combineCodes,
12
- escape,
13
- jsStringEscape,
14
- createIndent,
15
- transformReservedWord,
16
- nameSorter,
17
- searchAndReplace,
18
- trim,
19
- JSDoc: {
20
- createJSDocBlockText,
21
- },
22
- } as const
@@ -1,3 +0,0 @@
1
- export function trim(text: string): string {
2
- return text.replaceAll(/\n/g, '').trim()
3
- }
File without changes
File without changes
File without changes