@kubb/react-fabric 0.13.3 → 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.
Files changed (56) hide show
  1. package/README.md +2 -2
  2. package/dist/{chunk-DbZtQ4qb.js → chunk-BGCRLu6H.js} +12 -21
  3. package/dist/globals.d.ts +2 -2
  4. package/dist/globals.js +1 -1
  5. package/dist/index.cjs +79 -204
  6. package/dist/index.cjs.map +1 -1
  7. package/dist/index.d.ts +27 -30
  8. package/dist/index.js +27 -148
  9. package/dist/index.js.map +1 -1
  10. package/dist/jsx-dev-runtime.cjs +6 -245
  11. package/dist/jsx-dev-runtime.cjs.map +1 -1
  12. package/dist/jsx-dev-runtime.d.ts +5 -5
  13. package/dist/jsx-dev-runtime.js +6 -245
  14. package/dist/jsx-dev-runtime.js.map +1 -1
  15. package/dist/{jsx-namespace-Bg7Kfg54.d.ts → jsx-namespace-CYofvEUt.d.ts} +3 -3
  16. package/dist/{react-B4mAwc8c.cjs → jsx-runtime-Bl0DfUmV.js} +262 -61
  17. package/dist/jsx-runtime-Bl0DfUmV.js.map +1 -0
  18. package/dist/{react-C2cYsofv.js → jsx-runtime-Cua1md-Z.cjs} +317 -7
  19. package/dist/jsx-runtime-Cua1md-Z.cjs.map +1 -0
  20. package/dist/jsx-runtime.cjs +4 -6
  21. package/dist/jsx-runtime.cjs.map +1 -1
  22. package/dist/jsx-runtime.d.ts +3 -3
  23. package/dist/jsx-runtime.js +3 -5
  24. package/dist/jsx-runtime.js.map +1 -1
  25. package/dist/parsers.cjs +7 -7
  26. package/dist/parsers.js +2 -3
  27. package/dist/plugins.cjs +10 -10
  28. package/dist/plugins.d.ts +1 -1
  29. package/dist/plugins.js +4 -7
  30. package/dist/reactPlugin-Cnw1Sdnt.d.ts +35 -0
  31. package/dist/{reactPlugin-BshFnF2Y.cjs → reactPlugin-D74A1eG4.cjs} +50 -65
  32. package/dist/{reactPlugin-BshFnF2Y.cjs.map → reactPlugin-D74A1eG4.cjs.map} +1 -1
  33. package/dist/{reactPlugin-DGKBWjiR.js → reactPlugin-QQPrjNuQ.js} +13 -28
  34. package/dist/{reactPlugin-DGKBWjiR.js.map → reactPlugin-QQPrjNuQ.js.map} +1 -1
  35. package/dist/types-B6MJKUw9.d.ts +61 -0
  36. package/dist/types.cjs +7 -7
  37. package/dist/types.d.ts +2 -62
  38. package/dist/types.js +2 -3
  39. package/package.json +7 -2
  40. package/src/components/{App.tsx → Fabric.tsx} +6 -6
  41. package/src/composables/useFabric.ts +1 -0
  42. package/src/devtools.ts +0 -2
  43. package/src/index.ts +2 -5
  44. package/src/jsx-dev-runtime.ts +2 -2
  45. package/src/plugins/reactPlugin.ts +8 -8
  46. package/src/types.ts +0 -1
  47. package/dist/getFunctionParams-CINBJQGH.d.ts +0 -49
  48. package/dist/jsx-runtime-CZxrhUx8.js +0 -266
  49. package/dist/jsx-runtime-CZxrhUx8.js.map +0 -1
  50. package/dist/jsx-runtime-C_GM3QQI.cjs +0 -270
  51. package/dist/jsx-runtime-C_GM3QQI.cjs.map +0 -1
  52. package/dist/react-B4mAwc8c.cjs.map +0 -1
  53. package/dist/react-C2cYsofv.js.map +0 -1
  54. package/dist/reactPlugin-OUKiWH1q.d.ts +0 -463
  55. package/src/composables/useApp.ts +0 -1
  56. package/src/utils/getFunctionParams.ts +0 -242
@@ -1,463 +0,0 @@
1
- import { r as __name } from "./chunk-DbZtQ4qb.js";
2
- import { FabricReactElement, t as types_d_exports } from "./types.js";
3
- import { TreeNode } from "@kubb/fabric-core";
4
-
5
- //#region ../fabric-core/src/KubbFile.d.ts
6
- type ImportName = string | Array<string | {
7
- propertyName: string;
8
- name?: string;
9
- }>;
10
- type Import = {
11
- /**
12
- * Import name to be used
13
- * @example ["useState"]
14
- * @example "React"
15
- */
16
- name: ImportName;
17
- /**
18
- * Path for the import
19
- * @example '@kubb/core'
20
- */
21
- path: string;
22
- /**
23
- * Add type-only import prefix.
24
- * - `true` generates `import type { Type } from './path'`
25
- * - `false` generates `import { Type } from './path'`
26
- * @default false
27
- */
28
- isTypeOnly?: boolean;
29
- /**
30
- * Import entire module as namespace.
31
- * - `true` generates `import * as Name from './path'`
32
- * - `false` generates standard import
33
- * @default false
34
- */
35
- isNameSpace?: boolean;
36
- /**
37
- * When root is set it will get the path with relative getRelativePath(root, path).
38
- */
39
- root?: string;
40
- };
41
- type Source = {
42
- name?: string;
43
- value?: string;
44
- /**
45
- * Make this source a type-only export.
46
- * - `true` marks source as type export
47
- * - `false` marks source as value export
48
- * @default false
49
- */
50
- isTypeOnly?: boolean;
51
- /**
52
- * Include export keyword in source.
53
- * - `true` generates exportable const or type
54
- * - `false` generates internal declaration
55
- * @default false
56
- */
57
- isExportable?: boolean;
58
- /**
59
- * Include in barrel file generation.
60
- * - `true` adds to barrel exports
61
- * - `false` excludes from barrel exports
62
- * @default false
63
- */
64
- isIndexable?: boolean;
65
- };
66
- type Export = {
67
- /**
68
- * Export name to be used.
69
- * @example ["useState"]
70
- * @example "React"
71
- */
72
- name?: string | Array<string>;
73
- /**
74
- * Path for the import.
75
- * @example '@kubb/core'
76
- */
77
- path: string;
78
- /**
79
- * Add type-only export prefix.
80
- * - `true` generates `export type { Type } from './path'`
81
- * - `false` generates `export { Type } from './path'`
82
- * @default false
83
- */
84
- isTypeOnly?: boolean;
85
- /**
86
- * Export as aliased namespace.
87
- * - `true` generates `export * as aliasName from './path'`
88
- * - `false` generates standard export
89
- * @default false
90
- */
91
- asAlias?: boolean;
92
- };
93
- type Extname = '.ts' | '.js' | '.tsx' | '.json' | `.${string}`;
94
- /**
95
- * Name to be used to dynamically create the baseName(based on input.path)
96
- * Based on UNIX basename
97
- * @link https://nodejs.org/api/path.html#pathbasenamepath-suffix
98
- */
99
- type BaseName = `${string}.${string}`;
100
- /**
101
- * Path will be full qualified path to a specified file
102
- */
103
- type Path = string;
104
- type File<TMeta extends object = object> = {
105
- /**
106
- * Name to be used to create the path
107
- * Based on UNIX basename, `${name}.extname`
108
- * @link https://nodejs.org/api/path.html#pathbasenamepath-suffix
109
- */
110
- baseName: BaseName;
111
- /**
112
- * Path will be full qualified path to a specified file
113
- */
114
- path: Path;
115
- sources: Array<Source>;
116
- imports: Array<Import>;
117
- exports: Array<Export>;
118
- /**
119
- * Use extra meta, this is getting used to generate the barrel/index files.
120
- */
121
- meta?: TMeta;
122
- banner?: string;
123
- footer?: string;
124
- };
125
- type ResolvedFile<TMeta extends object = object> = File<TMeta> & {
126
- /**
127
- * @default hash
128
- */
129
- id: string;
130
- /**
131
- * Contains the first part of the baseName, generated based on baseName
132
- * @link https://nodejs.org/api/path.html#pathformatpathobject
133
- */
134
- name: string;
135
- extname: Extname;
136
- imports: Array<Import>;
137
- exports: Array<Export>;
138
- };
139
- //#endregion
140
- //#region ../fabric-core/src/parsers/types.d.ts
141
- type PrintOptions = {
142
- extname?: Extname;
143
- };
144
- type Parser<TOptions = unknown, TMeta extends object = any> = {
145
- name: string;
146
- type: 'parser';
147
- /**
148
- * Undefined is being used for the defaultParser
149
- */
150
- extNames: Array<Extname> | undefined;
151
- install: Install<TOptions>;
152
- /**
153
- * Convert a file to string
154
- */
155
- parse(file: ResolvedFile<TMeta>, options: PrintOptions): Promise<string> | string;
156
- };
157
- //#endregion
158
- //#region ../fabric-core/src/utils/AsyncEventEmitter.d.ts
159
- type Options$3 = {
160
- mode?: FabricMode;
161
- maxListener?: number;
162
- };
163
- declare class AsyncEventEmitter<TEvents extends Record<string, any>> {
164
- #private;
165
- constructor({
166
- maxListener,
167
- mode
168
- }?: Options$3);
169
- emit<TEventName extends keyof TEvents & string>(eventName: TEventName, ...eventArgs: TEvents[TEventName]): Promise<void>;
170
- on<TEventName extends keyof TEvents & string>(eventName: TEventName, handler: (...eventArg: TEvents[TEventName]) => void): void;
171
- onOnce<TEventName extends keyof TEvents & string>(eventName: TEventName, handler: (...eventArgs: TEvents[TEventName]) => void): void;
172
- off<TEventName extends keyof TEvents & string>(eventName: TEventName, handler: (...eventArg: TEvents[TEventName]) => void): void;
173
- removeAll(): void;
174
- }
175
- //#endregion
176
- //#region ../fabric-core/src/FileProcessor.d.ts
177
- type ProcessFilesProps = {
178
- parsers?: Map<Extname, Parser>;
179
- extension?: Record<Extname, Extname | ''>;
180
- dryRun?: boolean;
181
- /**
182
- * @default 'sequential'
183
- */
184
- mode?: FabricMode;
185
- };
186
- type GetParseOptions = {
187
- parsers?: Map<Extname, Parser>;
188
- extension?: Record<Extname, Extname | ''>;
189
- };
190
- type Options$2 = {
191
- events?: AsyncEventEmitter<FabricEvents>;
192
- };
193
- declare class FileProcessor {
194
- #private;
195
- events: AsyncEventEmitter<FabricEvents>;
196
- constructor({
197
- events
198
- }?: Options$2);
199
- parse(file: ResolvedFile, {
200
- parsers,
201
- extension
202
- }?: GetParseOptions): Promise<string>;
203
- run(files: Array<ResolvedFile>, {
204
- parsers,
205
- mode,
206
- dryRun,
207
- extension
208
- }?: ProcessFilesProps): Promise<ResolvedFile[]>;
209
- }
210
- //#endregion
211
- //#region ../fabric-core/src/FileManager.d.ts
212
- type Options$1 = {
213
- events?: AsyncEventEmitter<FabricEvents>;
214
- };
215
- declare class FileManager$1 {
216
- #private;
217
- events: AsyncEventEmitter<FabricEvents>;
218
- processor: FileProcessor;
219
- constructor({
220
- events
221
- }?: Options$1);
222
- add(...files: Array<File>): Array<ResolvedFile>;
223
- upsert(...files: Array<File>): Array<ResolvedFile>;
224
- flush(): void;
225
- getByPath(path: Path): ResolvedFile | null;
226
- deleteByPath(path: Path): void;
227
- clear(): void;
228
- get files(): Array<ResolvedFile>;
229
- write(options: ProcessFilesProps): Promise<ResolvedFile[]>;
230
- }
231
- //#endregion
232
- //#region ../fabric-core/src/Fabric.d.ts
233
- /**
234
- * Defines core runtime options for Fabric.
235
- */
236
- interface FabricOptions {
237
- /**
238
- * Determines how Fabric processes files.
239
- * - `sequential`: files are processed one by one
240
- * - `parallel`: files are processed concurrently
241
- *
242
- * @default 'sequential'
243
- */
244
- mode?: FabricMode;
245
- }
246
- /**
247
- * Available modes for file processing.
248
- * @default 'sequential'
249
- */
250
- type FabricMode = 'sequential' | 'parallel';
251
- /**
252
- * Event definitions emitted during the Fabric lifecycle.
253
- *
254
- * These events allow plugins and external code to hook into different stages
255
- * of the file generation process. All events are asynchronous and can be
256
- * listened to using `fabric.context.on()` or `fabric.context.onOnce()`.
257
- *
258
- * @example
259
- * ```ts
260
- * fabric.context.on('lifecycle:start', async () => {
261
- * console.log('Fabric started!')
262
- * })
263
- * ```
264
- */
265
- interface FabricEvents {
266
- /**
267
- * Emitted when the Fabric application lifecycle begins.
268
- * This is typically the first event fired when starting a Fabric run.
269
- * Use this to perform initial setup or logging.
270
- */
271
- 'lifecycle:start': [];
272
- /**
273
- * Emitted when the Fabric application lifecycle completes.
274
- * This is typically the last event fired after all processing is done.
275
- * Use this for cleanup tasks or final reporting.
276
- */
277
- 'lifecycle:end': [];
278
- /**
279
- * Emitted when Fabric starts rendering (used with reactPlugin).
280
- * Provides access to the Fabric instance for render-time operations.
281
- */
282
- 'lifecycle:render': [fabric: Fabric];
283
- /**
284
- * Emitted once before file processing begins.
285
- * Provides the complete list of files that will be processed.
286
- * Use this to prepare for batch operations or display initial file counts.
287
- */
288
- 'files:processing:start': [files: Array<ResolvedFile>];
289
- /**
290
- * Emitted when files are successfully added to the FileManager's internal cache.
291
- * This happens after files pass through path and name resolution.
292
- * Use this to track which files have been registered.
293
- */
294
- 'files:added': [files: Array<ResolvedFile>];
295
- /**
296
- * Emitted during file path resolution, before a file is cached.
297
- * Listeners can modify the file's path property to customize output location.
298
- * This is called for each file being added via `addFile()` or `upsertFile()`.
299
- */
300
- 'file:resolve:path': [file: File];
301
- /**
302
- * Emitted during file name resolution, before a file is cached.
303
- * Listeners can modify the file's name-related properties to customize naming.
304
- * This is called for each file being added via `addFile()` or `upsertFile()`.
305
- */
306
- 'file:resolve:name': [file: File];
307
- /**
308
- * Emitted just before files are written to disk.
309
- * Provides all files that will be written in this batch.
310
- * Use this to perform pre-write operations like creating directories.
311
- */
312
- 'files:writing:start': [files: Array<ResolvedFile>];
313
- /**
314
- * Emitted after all files have been successfully written to disk.
315
- * Provides all files that were written in this batch.
316
- * Use this for post-write operations like running formatters or reporting.
317
- */
318
- 'files:writing:end': [files: Array<ResolvedFile>];
319
- /**
320
- * Emitted when an individual file starts being processed.
321
- * This happens for each file in the queue, before parsing.
322
- * Use this for per-file setup or detailed logging.
323
- */
324
- 'file:processing:start': [file: ResolvedFile, index: number, total: number];
325
- /**
326
- * Emitted when an individual file completes processing.
327
- * This happens after the file has been parsed and handled.
328
- * Use this for per-file cleanup or progress tracking.
329
- */
330
- 'file:processing:end': [file: ResolvedFile, index: number, total: number];
331
- /**
332
- * Emitted after each file is processed, providing progress metrics.
333
- * This is the primary event for implementing progress bars or tracking.
334
- * Plugins like fsPlugin use this to write files to disk.
335
- *
336
- * @property processed - Number of files processed so far
337
- * @property total - Total number of files to process
338
- * @property percentage - Completion percentage (0-100)
339
- * @property source - Optional parsed source code of the file
340
- * @property file - The file that was just processed
341
- */
342
- 'file:processing:update': [{
343
- processed: number;
344
- total: number;
345
- percentage: number;
346
- source?: string;
347
- file: ResolvedFile;
348
- }];
349
- /**
350
- * Emitted once all files have been successfully processed.
351
- * This marks the completion of the processing phase.
352
- * Use this to perform batch operations on all processed files.
353
- */
354
- 'files:processing:end': [files: Array<ResolvedFile>];
355
- }
356
- /**
357
- * Shared context passed to all plugins, parsers, and Fabric internals.
358
- */
359
- interface FabricContext<T extends FabricOptions = FabricOptions> extends AsyncEventEmitter<FabricEvents> {
360
- /** The active Fabric configuration. */
361
- config: FabricConfig<T>;
362
- /** The internal file manager handling file creation, merging, and writing. */
363
- fileManager: FileManager$1;
364
- /** List of files currently in memory. */
365
- files: ResolvedFile[];
366
- /** Add new files to the file manager. */
367
- addFile(...files: File[]): Promise<void>;
368
- /** Track installed plugins and parsers to prevent duplicates. */
369
- installedPlugins: Set<Plugin>;
370
- installedParsers: Map<Extname, Parser>;
371
- }
372
- /**
373
- * Base configuration object for Fabric.
374
- */
375
- type FabricConfig<T extends FabricOptions = FabricOptions> = T;
376
- /**
377
- * Utility type that checks whether all properties of `T` are optional.
378
- */
379
- type AllOptional<T> = {} extends T ? true : false;
380
- /**
381
- * Defines the signature of a plugin or parser's `install` function.
382
- */
383
- type Install<TOptions = unknown> = TOptions extends any[] ? (context: FabricContext, ...options: TOptions) => void | Promise<void> : AllOptional<TOptions> extends true ? (context: FabricContext, options?: TOptions) => void | Promise<void> : (context: FabricContext, options: TOptions) => void | Promise<void>;
384
- /**
385
- * Defines the signature of a plugin or parser's `inject` function.
386
- * Returns an object that extends the Fabric instance.
387
- */
388
- type Inject<TOptions = unknown, TExtension extends Record<string, any> = {}> = TOptions extends any[] ? (context: FabricContext, ...options: TOptions) => Partial<TExtension> : AllOptional<TOptions> extends true ? (context: FabricContext, options?: TOptions) => Partial<TExtension> : (context: FabricContext, options: TOptions) => Partial<TExtension>;
389
- /**
390
- * The main Fabric runtime interface.
391
- * Provides access to the current context, registered plugins, files, and utility methods.
392
- */
393
- interface Fabric<T extends FabricOptions = FabricOptions> extends Kubb.Fabric {
394
- /** The shared context for this Fabric instance. */
395
- context: FabricContext<T>;
396
- /** The files managed by this Fabric instance. */
397
- files: ResolvedFile[];
398
- /**
399
- * Install a plugin or parser into Fabric.
400
- *
401
- * @param target - The plugin or parser to install.
402
- * @param options - Optional configuration or arguments for the target.
403
- * @returns A Fabric instance extended by the plugin (if applicable).
404
- */
405
- use<TPluginOptions = unknown, TMeta extends object = object, TExtension extends Record<string, any> = {}>(target: Plugin<TPluginOptions, TExtension> | Parser<TPluginOptions, TMeta>, ...options: TPluginOptions extends any[] ? NoInfer<TPluginOptions> : AllOptional<TPluginOptions> extends true ? [NoInfer<TPluginOptions>?] : [NoInfer<TPluginOptions>]): (this & TExtension) | Promise<this & TExtension>;
406
- /**
407
- * Add one or more files to the Fabric file manager.
408
- */
409
- addFile(...files: File[]): Promise<void>;
410
- /**
411
- * Add one or more files to the Fabric file manager and merge the source, imports, exports
412
- */
413
- upsertFile(...files: File[]): Promise<void>;
414
- /**
415
- * Unmount the Fabric instance and remove all registered event listeners.
416
- * Plugins may extend this to perform additional cleanup (e.g. process signal listeners).
417
- */
418
- unmount(error?: Error | number | null): void;
419
- }
420
- //#endregion
421
- //#region ../fabric-core/src/plugins/types.d.ts
422
- type Plugin<TOptions = unknown, TAppExtension extends Record<string, any> = {}> = {
423
- name: string;
424
- type: 'plugin';
425
- install: Install<TOptions>;
426
- /**
427
- * Runtime app overrides or extensions.
428
- * Merged into the app instance after install.
429
- * This cannot be async
430
- */
431
- inject?: Inject<TOptions, TAppExtension>;
432
- };
433
- //#endregion
434
- //#region src/plugins/reactPlugin.d.ts
435
- type Options = {
436
- stdout?: NodeJS.WriteStream;
437
- stdin?: NodeJS.ReadStream;
438
- stderr?: NodeJS.WriteStream;
439
- treeNode?: TreeNode<types_d_exports.ComponentNode>;
440
- /**
441
- * Set this to true to always see the result of the render in the console(line per render)
442
- */
443
- debug?: boolean;
444
- };
445
- type ExtendOptions = {
446
- render(App: FabricReactElement): Promise<void>;
447
- renderToString(App: FabricReactElement): Promise<string>;
448
- waitUntilExit(): Promise<void>;
449
- unmount(error?: Error | number | null): void;
450
- };
451
- declare global {
452
- namespace Kubb {
453
- interface Fabric {
454
- render(App: FabricReactElement): Promise<void>;
455
- renderToString(App: FabricReactElement): Promise<string>;
456
- waitUntilExit(): Promise<void>;
457
- }
458
- }
459
- }
460
- declare const reactPlugin: Plugin<Options, ExtendOptions>;
461
- //#endregion
462
- export { reactPlugin as n, Options as t };
463
- //# sourceMappingURL=reactPlugin-OUKiWH1q.d.ts.map
@@ -1 +0,0 @@
1
- export { useApp } from '@kubb/fabric-core'
@@ -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
- }