@kubb/core 2.0.0-canary.20231030T124958 → 2.0.0

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 (100) hide show
  1. package/README.md +1 -1
  2. package/dist/chunk-4A7WG6IA.js +128 -0
  3. package/dist/chunk-4A7WG6IA.js.map +1 -0
  4. package/dist/chunk-54P4AWHI.js +71 -0
  5. package/dist/chunk-54P4AWHI.js.map +1 -0
  6. package/dist/chunk-5TK7TMV6.cjs +131 -0
  7. package/dist/chunk-5TK7TMV6.cjs.map +1 -0
  8. package/dist/chunk-7S67BJXQ.js +85 -0
  9. package/dist/chunk-7S67BJXQ.js.map +1 -0
  10. package/dist/chunk-E3ANGQ5N.cjs +2290 -0
  11. package/dist/chunk-E3ANGQ5N.cjs.map +1 -0
  12. package/dist/chunk-H47IKRXJ.cjs +129 -0
  13. package/dist/chunk-H47IKRXJ.cjs.map +1 -0
  14. package/dist/chunk-HIE46T3F.js +129 -0
  15. package/dist/chunk-HIE46T3F.js.map +1 -0
  16. package/dist/chunk-K2H7BYQB.js +155 -0
  17. package/dist/chunk-K2H7BYQB.js.map +1 -0
  18. package/dist/chunk-NAWI7UXW.js +67 -0
  19. package/dist/chunk-NAWI7UXW.js.map +1 -0
  20. package/dist/chunk-PLVKILIY.cjs +162 -0
  21. package/dist/chunk-PLVKILIY.cjs.map +1 -0
  22. package/dist/chunk-W2FP7ZWW.cjs +71 -0
  23. package/dist/chunk-W2FP7ZWW.cjs.map +1 -0
  24. package/dist/chunk-WZQO3EPM.cjs +91 -0
  25. package/dist/chunk-WZQO3EPM.cjs.map +1 -0
  26. package/dist/chunk-XDHI63G7.cjs +104 -0
  27. package/dist/chunk-XDHI63G7.cjs.map +1 -0
  28. package/dist/chunk-XPOF4D5N.js +18 -0
  29. package/dist/chunk-XPOF4D5N.js.map +1 -0
  30. package/dist/fs.cjs +31 -0
  31. package/dist/fs.cjs.map +1 -0
  32. package/dist/fs.d.cts +5 -0
  33. package/dist/fs.d.ts +5 -0
  34. package/dist/fs.js +11 -0
  35. package/dist/fs.js.map +1 -0
  36. package/dist/index.cjs +1866 -977
  37. package/dist/index.cjs.map +1 -1
  38. package/dist/index.d.cts +302 -319
  39. package/dist/index.d.ts +302 -319
  40. package/dist/index.js +1071 -846
  41. package/dist/index.js.map +1 -1
  42. package/dist/logger.cjs +26 -0
  43. package/dist/logger.cjs.map +1 -0
  44. package/dist/logger.d.cts +32 -0
  45. package/dist/logger.d.ts +32 -0
  46. package/dist/logger.js +8 -0
  47. package/dist/logger.js.map +1 -0
  48. package/dist/transformers.cjs +124 -0
  49. package/dist/transformers.cjs.map +1 -0
  50. package/dist/transformers.d.cts +55 -0
  51. package/dist/transformers.d.ts +55 -0
  52. package/dist/transformers.js +95 -0
  53. package/dist/transformers.js.map +1 -0
  54. package/dist/utils.cjs +23 -1163
  55. package/dist/utils.cjs.map +1 -1
  56. package/dist/utils.d.cts +2 -143
  57. package/dist/utils.d.ts +2 -143
  58. package/dist/utils.js +15 -1118
  59. package/dist/utils.js.map +1 -1
  60. package/dist/write-A6VgHkYA.d.cts +10 -0
  61. package/dist/write-A6VgHkYA.d.ts +10 -0
  62. package/package.json +40 -23
  63. package/src/BarrelManager.ts +113 -0
  64. package/src/FileManager.ts +581 -0
  65. package/src/Generator.ts +34 -0
  66. package/src/PackageManager.ts +178 -0
  67. package/src/PluginManager.ts +645 -0
  68. package/src/PromiseManager.ts +51 -0
  69. package/src/build.ts +221 -0
  70. package/src/config.ts +22 -0
  71. package/src/errors.ts +12 -0
  72. package/src/fs/clean.ts +5 -0
  73. package/src/fs/index.ts +3 -0
  74. package/src/fs/read.ts +68 -0
  75. package/src/fs/write.ts +79 -0
  76. package/src/index.ts +27 -0
  77. package/src/logger.ts +121 -0
  78. package/src/plugin.ts +80 -0
  79. package/src/transformers/casing.ts +9 -0
  80. package/src/transformers/combineCodes.ts +3 -0
  81. package/src/transformers/createJSDocBlockText.ts +9 -0
  82. package/src/transformers/escape.ts +31 -0
  83. package/src/transformers/indent.ts +3 -0
  84. package/src/transformers/index.ts +36 -0
  85. package/src/transformers/nameSorter.ts +9 -0
  86. package/src/transformers/searchAndReplace.ts +25 -0
  87. package/src/transformers/transformReservedWord.ts +97 -0
  88. package/src/transformers/trim.ts +7 -0
  89. package/src/types.ts +334 -0
  90. package/src/utils/EventEmitter.ts +24 -0
  91. package/src/utils/FunctionParams.ts +86 -0
  92. package/src/utils/TreeNode.ts +125 -0
  93. package/src/utils/URLPath.ts +133 -0
  94. package/src/utils/cache.ts +35 -0
  95. package/src/utils/executeStrategies.ts +83 -0
  96. package/src/utils/index.ts +8 -0
  97. package/src/utils/promise.ts +13 -0
  98. package/src/utils/renderTemplate.ts +31 -0
  99. package/src/utils/timeout.ts +7 -0
  100. package/src/utils/uniqueName.ts +20 -0
@@ -0,0 +1,125 @@
1
+ import dirTree from 'directory-tree'
2
+
3
+ import { FileManager } from '../FileManager.ts'
4
+
5
+ import type { DirectoryTree, DirectoryTreeOptions } from 'directory-tree'
6
+ import type { KubbFile } from '../FileManager.ts'
7
+
8
+ export type TreeNodeOptions = DirectoryTreeOptions
9
+
10
+ type BarrelData = { type: KubbFile.Mode; path: KubbFile.Path; name: string }
11
+
12
+ export class TreeNode<T = BarrelData> {
13
+ public data: T
14
+
15
+ public parent?: TreeNode<T>
16
+
17
+ public children: Array<TreeNode<T>> = []
18
+
19
+ constructor(data: T, parent?: TreeNode<T>) {
20
+ this.data = data
21
+ this.parent = parent
22
+ return this
23
+ }
24
+
25
+ addChild(data: T): TreeNode<T> {
26
+ const child = new TreeNode(data, this)
27
+ if (!this.children) {
28
+ this.children = []
29
+ }
30
+ this.children.push(child)
31
+ return child
32
+ }
33
+
34
+ find(data?: T): TreeNode<T> | null {
35
+ if (!data) {
36
+ return null
37
+ }
38
+
39
+ if (data === this.data) {
40
+ return this
41
+ }
42
+
43
+ if (this.children?.length) {
44
+ for (let i = 0, { length } = this.children, target: TreeNode<T> | null = null; i < length; i++) {
45
+ target = this.children[i]!.find(data)
46
+ if (target) {
47
+ return target
48
+ }
49
+ }
50
+ }
51
+
52
+ return null
53
+ }
54
+
55
+ get leaves(): TreeNode<T>[] {
56
+ if (!this.children || this.children.length === 0) {
57
+ // this is a leaf
58
+ return [this]
59
+ }
60
+
61
+ // if not a leaf, return all children's leaves recursively
62
+ const leaves: TreeNode<T>[] = []
63
+ if (this.children) {
64
+ for (let i = 0, { length } = this.children; i < length; i++) {
65
+ // eslint-disable-next-line prefer-spread
66
+ leaves.push.apply(leaves, this.children[i]!.leaves)
67
+ }
68
+ }
69
+ return leaves
70
+ }
71
+
72
+ get root(): TreeNode<T> {
73
+ if (!this.parent) {
74
+ return this
75
+ }
76
+ return this.parent.root
77
+ }
78
+
79
+ forEach(callback: (treeNode: TreeNode<T>) => void): this {
80
+ if (typeof callback !== 'function') {
81
+ throw new TypeError('forEach() callback must be a function')
82
+ }
83
+
84
+ // run this node through function
85
+ callback(this)
86
+
87
+ // do the same for all children
88
+ if (this.children) {
89
+ for (let i = 0, { length } = this.children; i < length; i++) {
90
+ this.children[i]!.forEach(callback)
91
+ }
92
+ }
93
+
94
+ return this
95
+ }
96
+
97
+ public static build(path: string, options: TreeNodeOptions = {}): TreeNode | null {
98
+ try {
99
+ const exclude = Array.isArray(options.exclude) ? options.exclude : [options.exclude].filter(Boolean)
100
+ const filteredTree = dirTree(path, { extensions: options.extensions, exclude: [/node_modules/, ...exclude] })
101
+
102
+ if (!filteredTree) {
103
+ return null
104
+ }
105
+
106
+ const treeNode = new TreeNode({ name: filteredTree.name, path: filteredTree.path, type: filteredTree.type || FileManager.getMode(filteredTree.path) })
107
+
108
+ const recurse = (node: typeof treeNode, item: DirectoryTree) => {
109
+ const subNode = node.addChild({ name: item.name, path: item.path, type: item.type || FileManager.getMode(item.path) })
110
+
111
+ if (item.children?.length) {
112
+ item.children?.forEach((child) => {
113
+ recurse(subNode, child)
114
+ })
115
+ }
116
+ }
117
+
118
+ filteredTree.children?.forEach((child) => recurse(treeNode, child))
119
+
120
+ return treeNode
121
+ } catch (e) {
122
+ throw new Error('Something went wrong with creating index files with the TreehNode class', { cause: e })
123
+ }
124
+ }
125
+ }
@@ -0,0 +1,133 @@
1
+ import { camelCase } from '../transformers/casing.ts'
2
+
3
+ export type URLObject = {
4
+ url: string
5
+ params?: Record<string, string>
6
+ }
7
+
8
+ type ObjectOptions = {
9
+ type?: 'path' | 'template'
10
+ replacer?: (pathParam: string) => string
11
+ stringify?: boolean
12
+ }
13
+
14
+ export class URLPath {
15
+ path: string
16
+
17
+ constructor(path: string) {
18
+ this.path = path
19
+
20
+ return this
21
+ }
22
+
23
+ /**
24
+ * Convert Swagger path to URLPath(syntax of Express)
25
+ * @example /pet/{petId} => /pet/:petId
26
+ */
27
+ get URL(): string {
28
+ return this.toURLPath()
29
+ }
30
+ get isURL(): boolean {
31
+ try {
32
+ const url = new URL(this.path)
33
+ if (url?.href) {
34
+ return true
35
+ }
36
+ } catch (error) {
37
+ return false
38
+ }
39
+ return false
40
+ }
41
+
42
+ /**
43
+ * Convert Swagger path to template literals/ template strings(camelcase)
44
+ * @example /pet/{petId} => `/pet/${petId}`
45
+ * @example /account/monetary-accountID => `/account/${monetaryAccountId}`
46
+ * @example /account/userID => `/account/${userId}`
47
+ */
48
+ get template(): string {
49
+ return this.toTemplateString()
50
+ }
51
+ get object(): URLObject | string {
52
+ return this.toObject()
53
+ }
54
+ get params(): Record<string, string> | undefined {
55
+ return this.getParams()
56
+ }
57
+
58
+ toObject({ type = 'path', replacer, stringify }: ObjectOptions = {}): URLObject | string {
59
+ const object = {
60
+ url: type === 'path' ? this.toURLPath() : this.toTemplateString(replacer),
61
+ params: this.getParams(),
62
+ }
63
+
64
+ if (stringify) {
65
+ if (type === 'template') {
66
+ return JSON.stringify(object).replaceAll("'", '').replaceAll(`"`, '')
67
+ }
68
+
69
+ if (object.params) {
70
+ return `{ url: '${object.url}', params: ${JSON.stringify(object.params).replaceAll("'", '').replaceAll(`"`, '')} }`
71
+ }
72
+
73
+ return `{ url: '${object.url}' }`
74
+ }
75
+
76
+ return object
77
+ }
78
+
79
+ /**
80
+ * Convert Swagger path to template literals/ template strings(camelcase)
81
+ * @example /pet/{petId} => `/pet/${petId}`
82
+ * @example /account/monetary-accountID => `/account/${monetaryAccountId}`
83
+ * @example /account/userID => `/account/${userId}`
84
+ */
85
+ toTemplateString(replacer?: (pathParam: string) => string): string {
86
+ const regex = /{(\w|-)*}/g
87
+ const found = this.path.match(regex)
88
+ let newPath = this.path.replaceAll('{', '${')
89
+
90
+ if (found) {
91
+ newPath = found.reduce((prev, curr) => {
92
+ const pathParam = replacer
93
+ ? replacer(camelCase(curr))
94
+ : camelCase(curr)
95
+ const replacement = `\${${pathParam}}`
96
+
97
+ return prev.replace(curr, replacement)
98
+ }, this.path)
99
+ }
100
+
101
+ return `\`${newPath}\``
102
+ }
103
+
104
+ getParams(replacer?: (pathParam: string) => string): Record<string, string> | undefined {
105
+ const regex = /{(\w|-)*}/g
106
+ const found = this.path.match(regex)
107
+
108
+ if (!found) {
109
+ return undefined
110
+ }
111
+
112
+ const params: Record<string, string> = {}
113
+ found.forEach((item) => {
114
+ item = item.replaceAll('{', '').replaceAll('}', '')
115
+
116
+ const pathParam = replacer
117
+ ? replacer(camelCase(item))
118
+ : camelCase(item)
119
+
120
+ params[pathParam] = pathParam
121
+ }, this.path)
122
+
123
+ return params
124
+ }
125
+
126
+ /**
127
+ * Convert Swagger path to URLPath(syntax of Express)
128
+ * @example /pet/{petId} => /pet/:petId
129
+ */
130
+ toURLPath(): string {
131
+ return this.path.replaceAll('{', ':').replaceAll('}', '')
132
+ }
133
+ }
@@ -0,0 +1,35 @@
1
+ import type { PluginCache } from '../types.ts'
2
+
3
+ export interface Cache<TStore extends object = object> {
4
+ delete(id: keyof TStore): boolean
5
+ get(id: keyof TStore): TStore[keyof TStore] | null
6
+ has(id: keyof TStore): boolean
7
+ set(id: keyof TStore, value: unknown): void
8
+ }
9
+
10
+ export function createPluginCache<TStore extends PluginCache>(Store: TStore = Object.create(null) as TStore): Cache<TStore> {
11
+ return {
12
+ set(id, value): void {
13
+ Store[id] = [0, value] as TStore[keyof TStore]
14
+ },
15
+ get(id): TStore[keyof TStore] | null {
16
+ const item = Store[id]
17
+ if (!item) {
18
+ return null
19
+ }
20
+ item[0] = 0
21
+ return item[1] as TStore[keyof TStore]
22
+ },
23
+ has(id): boolean {
24
+ const item = Store[id]
25
+ if (!item) {
26
+ return false
27
+ }
28
+ item[0] = 0
29
+ return true
30
+ },
31
+ delete(id: keyof TStore): boolean {
32
+ return delete Store[id]
33
+ },
34
+ }
35
+ }
@@ -0,0 +1,83 @@
1
+ /* eslint-disable unused-imports/no-unused-vars */
2
+ /* eslint-disable @typescript-eslint/no-unused-vars */
3
+ type PromiseFunc<T = unknown, T2 = never> = (state?: T) => T2 extends never ? Promise<T> : Promise<T> | T2
4
+
5
+ export type ValueOfPromiseFuncArray<TInput extends Array<unknown>> = TInput extends Array<PromiseFunc<infer X, infer Y>> ? X | Y : never
6
+
7
+ export function noReturn(): void {}
8
+
9
+ type SeqOutput<TInput extends Array<PromiseFunc<TValue, null>>, TValue> = Array<Awaited<ValueOfPromiseFuncArray<TInput>>>
10
+
11
+ /**
12
+ * Chains promises
13
+ */
14
+ export function hookSeq<TInput extends Array<PromiseFunc<TValue, null>>, TValue, TOutput = SeqOutput<TInput, TValue>>(
15
+ promises: TInput,
16
+ ): TOutput {
17
+ return promises.filter(Boolean).reduce(
18
+ (promise, func) => {
19
+ if (typeof func !== 'function') {
20
+ throw new Error('HookSeq needs a function that returns a promise `() => Promise<unknown>`')
21
+ }
22
+
23
+ return promise.then((state) => {
24
+ const calledFunc = func(state as TValue)
25
+
26
+ if (calledFunc) {
27
+ return calledFunc.then(Array.prototype.concat.bind(state))
28
+ }
29
+ })
30
+ },
31
+ Promise.resolve([] as unknown),
32
+ ) as TOutput
33
+ }
34
+
35
+ type HookFirstOutput<TInput extends Array<PromiseFunc<TValue, null>>, TValue = unknown> = ValueOfPromiseFuncArray<TInput>
36
+
37
+ /**
38
+ * Chains promises, first non-null result stops and returns
39
+ */
40
+ export function hookFirst<TInput extends Array<PromiseFunc<TValue, null>>, TValue = unknown, TOutput = HookFirstOutput<TInput, TValue>>(
41
+ promises: TInput,
42
+ nullCheck = (state: any) => state !== null,
43
+ ): TOutput {
44
+ let promise: Promise<unknown> = Promise.resolve(null) as Promise<unknown>
45
+
46
+ for (const func of promises.filter(Boolean)) {
47
+ promise = promise.then((state) => {
48
+ if (nullCheck(state)) {
49
+ return state
50
+ }
51
+
52
+ const calledFunc = func(state as TValue)
53
+
54
+ return calledFunc
55
+ })
56
+ }
57
+
58
+ return promise as TOutput
59
+ }
60
+
61
+ type HookParallelOutput<TInput extends Array<PromiseFunc<TValue, null>>, TValue> = Promise<PromiseSettledResult<Awaited<ValueOfPromiseFuncArray<TInput>>>[]>
62
+
63
+ /**
64
+ * Run promises in parallel with allSettled
65
+ */
66
+ export function hookParallel<TInput extends Array<PromiseFunc<TValue, null>>, TValue = unknown, TOutput = HookParallelOutput<TInput, TValue>>(
67
+ promises: TInput,
68
+ ): TOutput {
69
+ return Promise.allSettled(promises.filter(Boolean).map(promise => promise())) as TOutput
70
+ }
71
+
72
+ export type Strategy = 'seq' | 'first' | 'parallel'
73
+
74
+ export type StrategySwitch<TStrategy extends Strategy, TInput extends Array<PromiseFunc<TValue, null>>, TValue> = TStrategy extends 'first'
75
+ ? HookFirstOutput<TInput, TValue>
76
+ : TStrategy extends 'seq' ? SeqOutput<TInput, TValue>
77
+ : TStrategy extends 'parallel' ? HookParallelOutput<TInput, TValue>
78
+ : never
79
+
80
+ // tests
81
+
82
+ type test = ValueOfPromiseFuncArray<Array<PromiseFunc<number, null>>>
83
+ // ^?
@@ -0,0 +1,8 @@
1
+ export type { FunctionParamsAST } from './FunctionParams.ts'
2
+ export { FunctionParams } from './FunctionParams.ts'
3
+ export { isPromise, isPromiseFulfilledResult, isPromiseRejectedResult } from './promise.ts'
4
+ export { renderTemplate } from './renderTemplate.ts'
5
+ export { timeout } from './timeout.ts'
6
+ export { getUniqueName, setUniqueName } from './uniqueName.ts'
7
+ export type { URLObject } from './URLPath.ts'
8
+ export { URLPath } from './URLPath.ts'
@@ -0,0 +1,13 @@
1
+ import type { PossiblePromise } from '@kubb/types'
2
+
3
+ export function isPromise<T>(result: PossiblePromise<T>): result is Promise<T> {
4
+ return !!result && typeof (result as Promise<unknown>)?.then === 'function'
5
+ }
6
+
7
+ export function isPromiseFulfilledResult<T = unknown>(result: PromiseSettledResult<unknown>): result is PromiseFulfilledResult<T> {
8
+ return result.status === 'fulfilled'
9
+ }
10
+
11
+ export function isPromiseRejectedResult<T>(result: PromiseSettledResult<unknown>): result is Omit<PromiseRejectedResult, 'reason'> & { reason: T } {
12
+ return result.status === 'rejected'
13
+ }
@@ -0,0 +1,31 @@
1
+ export function renderTemplate<TData extends Record<string, unknown> = Record<string, unknown>>(template: string, data: TData | undefined = undefined): string {
2
+ if (!data || !Object.keys(data).length) {
3
+ return template.replace(/{{(.*?)}}/g, '')
4
+ }
5
+
6
+ const matches = template.match(/{{(.*?)}}/g)
7
+
8
+ return (
9
+ matches?.reduce((prev, curr) => {
10
+ const index = curr.split(/{{|}}/).filter(Boolean)[0]?.trim()
11
+ if (index === undefined) {
12
+ return prev
13
+ }
14
+ const value = data[index]
15
+
16
+ if (value === undefined) {
17
+ return prev
18
+ }
19
+
20
+ return prev
21
+ .replace(curr, () => {
22
+ if (typeof value === 'boolean') {
23
+ return `${value.toString()}` || 'false'
24
+ }
25
+
26
+ return (value as string) || ''
27
+ })
28
+ .trim()
29
+ }, template) || ''
30
+ )
31
+ }
@@ -0,0 +1,7 @@
1
+ export async function timeout(ms: number): Promise<unknown> {
2
+ return new Promise((resolve) => {
3
+ setTimeout(() => {
4
+ resolve(true)
5
+ }, ms)
6
+ })
7
+ }
@@ -0,0 +1,20 @@
1
+ export function getUniqueName(originalName: string, data: Record<string, number>): string {
2
+ let used = data[originalName] || 0
3
+ if (used) {
4
+ data[originalName] = ++used
5
+ originalName += used
6
+ }
7
+ data[originalName] = 1
8
+ return originalName
9
+ }
10
+
11
+ export function setUniqueName(originalName: string, data: Record<string, number>): string {
12
+ let used = data[originalName] || 0
13
+ if (used) {
14
+ data[originalName] = ++used
15
+
16
+ return originalName
17
+ }
18
+ data[originalName] = 1
19
+ return originalName
20
+ }