@kubb/react-fabric 0.14.0 → 0.15.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.
@@ -1,242 +0,0 @@
1
- import { sortBy } from 'remeda'
2
-
3
- export type Param = {
4
- /**
5
- * `object` will return the pathParams as an object.
6
- *
7
- * `inline` will return the pathParams as comma separated params.
8
- * @default `'inline'`
9
- * @private
10
- */
11
- mode?: 'object' | 'inline' | 'inlineSpread'
12
- type?: 'string' | 'number' | (string & {})
13
- optional?: boolean
14
- /**
15
- * @example test = "default"
16
- */
17
- default?: string
18
- /**
19
- * Used for no TypeScript(with mode object)
20
- * @example test: "default"
21
- */
22
- value?: string
23
- children?: Params
24
- }
25
-
26
- type ParamItem =
27
- | (Pick<Param, 'mode' | 'type' | 'value'> & {
28
- optional?: true
29
- default?: never
30
- children?: Params
31
- })
32
- | (Pick<Param, 'mode' | 'type' | 'value'> & {
33
- optional?: false
34
- default?: string
35
- children?: Params
36
- })
37
-
38
- export type Params = Record<string, Param | undefined>
39
-
40
- type Options = {
41
- type: 'constructor' | 'call' | 'object' | 'objectValue'
42
- transformName?: (name: string) => string
43
- transformType?: (type: string) => string
44
- }
45
-
46
- function order(items: Array<[key: string, item?: ParamItem]>) {
47
- return sortBy(items.filter(Boolean) as Array<[key: string, item?: ParamItem]>, ([_key, item]) => {
48
- if (item?.children) {
49
- return 0 // Treat items with children as required (they'll get = {} if all children are optional)
50
- }
51
- // Priority order: required (0) → optional (1) → default-only (2)
52
- if (item?.optional) {
53
- return 1 // Optional parameters (with or without default)
54
- }
55
- if (item?.default) {
56
- // Parameters with default only (not marked as optional)
57
- // Note: While the ParamItem type suggests optional and default are mutually exclusive,
58
- // this handles the case where a parameter has a default value but isn't explicitly marked as optional
59
- return 2
60
- }
61
- return 0 // Required parameters
62
- })
63
- }
64
-
65
- function parseChild(key: string, item: ParamItem, options: Options): string | null {
66
- // @ts-expect-error
67
- const entries = order(Object.entries(item.children))
68
-
69
- const types: string[] = []
70
- const names: string[] = []
71
-
72
- const optional = entries.every(([_key, item]) => item?.optional || !!item?.default)
73
-
74
- entries.forEach(([key, entryItem]) => {
75
- if (entryItem) {
76
- const name = parseItem(key, { ...entryItem, type: undefined }, options)
77
- if (entryItem.children) {
78
- const subTypes = Object.entries(entryItem.children)
79
- .map(([key]) => {
80
- return key
81
- })
82
- .join(', ')
83
-
84
- if (subTypes) {
85
- names.push(`${name}: { ${subTypes} }`)
86
- } else {
87
- names.push(name)
88
- }
89
- } else {
90
- if (options.type === 'call' && options.transformName) {
91
- names.push(`${key}: ${name}`)
92
- } else {
93
- names.push(name)
94
- }
95
- }
96
-
97
- if (entries.some(([_key, item]) => item?.type)) {
98
- types.push(parseItem(key, { ...entryItem, default: undefined }, options))
99
- }
100
- }
101
- })
102
-
103
- const name = item.mode === 'inline' ? key : names.length ? `{ ${names.join(', ')} }` : undefined
104
- const type = item.type ? item.type : types.length ? `{ ${types.join('; ')} }` : undefined
105
-
106
- if (!name) {
107
- return null
108
- }
109
-
110
- return parseItem(
111
- name,
112
- {
113
- type,
114
- default: item.default,
115
- optional: !item.default ? optional : undefined,
116
- } as ParamItem,
117
- options,
118
- )
119
- }
120
-
121
- function parseItem(name: string, item: ParamItem, options: Options): string {
122
- const acc: string[] = []
123
- const transformedName = options.transformName ? options.transformName(name) : name
124
- const transformedType = options.transformType && item.type ? options.transformType(item.type) : item.type
125
-
126
- if (options.type === 'object') {
127
- return transformedName
128
- }
129
-
130
- if (options.type === 'objectValue') {
131
- return item.value ? `${transformedName}: ${item.value}` : transformedName
132
- }
133
-
134
- //LEGACY
135
- if (item.type && options.type === 'constructor') {
136
- if (item.optional) {
137
- // Check if this is a destructured parameter (object mode)
138
- const isDestructured = transformedName.startsWith('{')
139
- if (isDestructured) {
140
- // For destructured parameters, use ": type = {}" syntax to make it optional
141
- acc.push(`${transformedName}: ${transformedType} = {}`)
142
- } else {
143
- // For inline parameters, use "?: type" syntax
144
- acc.push(`${transformedName}?: ${transformedType}`)
145
- }
146
- } else {
147
- acc.push(`${transformedName}: ${transformedType}${item.default ? ` = ${item.default}` : ''}`)
148
- }
149
- } else if (item.default && options.type === 'constructor') {
150
- acc.push(`${transformedName} = ${item.default}`)
151
- } else if (item.value) {
152
- acc.push(`${transformedName} : ${item.value}`)
153
- } else if (item.mode === 'inlineSpread') {
154
- acc.push(`... ${transformedName}`)
155
- } else {
156
- acc.push(transformedName)
157
- }
158
-
159
- return acc[0] as string
160
- }
161
-
162
- export function getFunctionParams(params: Params, options: Options): string {
163
- const entries = order(Object.entries(params as Record<string, ParamItem | undefined>))
164
-
165
- return entries
166
- .reduce((acc, [key, item]) => {
167
- if (!item) {
168
- return acc
169
- }
170
-
171
- if (item.children) {
172
- if (Object.keys(item.children).length === 0) {
173
- return acc
174
- }
175
-
176
- if (item.mode === 'inlineSpread') {
177
- return [...acc, getFunctionParams(item.children, options)]
178
- }
179
-
180
- const parsedItem = parseChild(key, item, options)
181
- if (!parsedItem) {
182
- return acc
183
- }
184
-
185
- return [...acc, parsedItem]
186
- }
187
-
188
- const parsedItem = parseItem(key, item, options)
189
-
190
- return [...acc, parsedItem]
191
- }, [] as string[])
192
- .join(', ')
193
- }
194
-
195
- export function createFunctionParams(params: Params): Params {
196
- return params
197
- }
198
- // TODO use of zod
199
- //TODO use of string as `$name: $type` to create templates for functions instead of call/constructor
200
- export class FunctionParams {
201
- #params: Params
202
-
203
- static factory(params: Params) {
204
- return new FunctionParams(params)
205
- }
206
- constructor(params: Params) {
207
- this.#params = params
208
- }
209
-
210
- get params(): Params {
211
- return this.#params
212
- }
213
-
214
- get flatParams(): Params {
215
- const flatter = (acc: Params, [key, item]: [key: string, item?: Param]): Params => {
216
- if (item?.children) {
217
- return Object.entries(item.children).reduce(flatter, acc)
218
- }
219
- if (item) {
220
- acc[key] = item
221
- }
222
-
223
- return acc
224
- }
225
- return Object.entries(this.#params).reduce(flatter, {} as Params)
226
- }
227
-
228
- toCall({ transformName, transformType }: Pick<Options, 'transformName' | 'transformType'> = {}): string {
229
- return getFunctionParams(this.#params, { type: 'call', transformName, transformType })
230
- }
231
-
232
- toObject(): string {
233
- return getFunctionParams(this.#params, { type: 'object' })
234
- }
235
- toObjectValue(): string {
236
- return getFunctionParams(this.#params, { type: 'objectValue' })
237
- }
238
-
239
- toConstructor(): string {
240
- return getFunctionParams(this.#params, { type: 'constructor' })
241
- }
242
- }