@kubb/core 3.0.0-alpha.0 → 3.0.0-alpha.2

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 (35) hide show
  1. package/dist/FileManager-BW--rO8q.d.ts +437 -0
  2. package/dist/FileManager-Bw-FNS3q.d.cts +437 -0
  3. package/dist/{chunk-XRC6KXC3.cjs → chunk-67C6RBGQ.cjs} +5 -5
  4. package/dist/{chunk-XRC6KXC3.cjs.map → chunk-67C6RBGQ.cjs.map} +1 -1
  5. package/dist/{chunk-Y3LLJA4H.cjs → chunk-ADC5UNZ5.cjs} +145 -145
  6. package/dist/{chunk-Y3LLJA4H.cjs.map → chunk-ADC5UNZ5.cjs.map} +1 -1
  7. package/dist/{chunk-XRUOSVKX.cjs → chunk-LM2YQC3T.cjs} +12 -12
  8. package/dist/{chunk-XRUOSVKX.cjs.map → chunk-LM2YQC3T.cjs.map} +1 -1
  9. package/dist/{chunk-DKW7IBJV.cjs → chunk-PZT4CTBV.cjs} +18 -18
  10. package/dist/{chunk-DKW7IBJV.cjs.map → chunk-PZT4CTBV.cjs.map} +1 -1
  11. package/dist/{chunk-LKXUCYWU.cjs → chunk-XCPFG6DO.cjs} +3 -3
  12. package/dist/chunk-XCPFG6DO.cjs.map +1 -0
  13. package/dist/{chunk-OPOT6TCT.cjs → chunk-YTSNYMHW.cjs} +28 -28
  14. package/dist/{chunk-OPOT6TCT.cjs.map → chunk-YTSNYMHW.cjs.map} +1 -1
  15. package/dist/index.cjs +152 -196
  16. package/dist/index.cjs.map +1 -1
  17. package/dist/index.d.cts +10 -465
  18. package/dist/index.d.ts +10 -465
  19. package/dist/index.js +11 -55
  20. package/dist/index.js.map +1 -1
  21. package/dist/logger.cjs +3 -3
  22. package/dist/mocks.cjs +8 -8
  23. package/dist/mocks.d.cts +1 -1
  24. package/dist/mocks.d.ts +1 -1
  25. package/dist/transformers.cjs +4 -4
  26. package/dist/utils.cjs +4 -4
  27. package/package.json +9 -12
  28. package/src/PluginManager.ts +10 -19
  29. package/src/build.ts +3 -52
  30. package/src/index.ts +0 -3
  31. package/src/plugin.ts +1 -1
  32. package/src/types.ts +12 -37
  33. package/dist/chunk-LKXUCYWU.cjs.map +0 -1
  34. package/globals.d.ts +0 -49
  35. package/src/kubb.ts +0 -11
@@ -0,0 +1,437 @@
1
+ import PQueue from 'p-queue';
2
+ import { write, read } from '@kubb/fs';
3
+ import * as KubbFile from '@kubb/fs/types';
4
+ import { BaseName, File, UUID } from '@kubb/fs/src/types.ts';
5
+ import { PossiblePromise, GreaterThan } from '@kubb/types';
6
+ import { DirectoryTreeOptions } from 'directory-tree';
7
+ import { E as EventEmitter, L as Logger } from './logger-DChjnJMn.js';
8
+
9
+ type BarrelManagerOptions = {
10
+ treeNode?: DirectoryTreeOptions;
11
+ isTypeOnly?: boolean;
12
+ /**
13
+ * Add .ts or .js
14
+ */
15
+ extName?: KubbFile.Extname;
16
+ };
17
+
18
+ type RequiredPluginLifecycle = Required<PluginLifecycle>;
19
+ /**
20
+ * Get the type of the first argument in a function.
21
+ * @example Arg0<(a: string, b: number) => void> -> string
22
+ */
23
+ type Argument0<H extends keyof PluginLifecycle> = Parameters<RequiredPluginLifecycle[H]>[0];
24
+ type Strategy = 'hookFirst' | 'hookForPlugin' | 'hookParallel' | 'hookReduceArg0' | 'hookSeq';
25
+ type Executer<H extends PluginLifecycleHooks = PluginLifecycleHooks> = {
26
+ strategy: Strategy;
27
+ hookName: H;
28
+ plugin: Plugin;
29
+ parameters?: unknown[] | undefined;
30
+ output?: unknown;
31
+ };
32
+ type ParseResult<H extends PluginLifecycleHooks> = RequiredPluginLifecycle[H];
33
+ type SafeParseResult<H extends PluginLifecycleHooks, Result = ReturnType<ParseResult<H>>> = {
34
+ result: Result;
35
+ plugin: Plugin;
36
+ };
37
+ type Options$1 = {
38
+ logger: Logger;
39
+ /**
40
+ * Task for the FileManager
41
+ */
42
+ task: (file: ResolvedFile) => Promise<ResolvedFile>;
43
+ };
44
+ type Events = {
45
+ execute: [executer: Executer];
46
+ executed: [executer: Executer];
47
+ error: [error: Error];
48
+ };
49
+ type GetFileProps<TOptions = object> = {
50
+ name: string;
51
+ mode?: KubbFile.Mode;
52
+ extName: KubbFile.Extname;
53
+ pluginKey: Plugin['key'];
54
+ options?: TOptions;
55
+ };
56
+ declare class PluginManager {
57
+ #private;
58
+ readonly plugins: PluginWithLifeCycle[];
59
+ readonly fileManager: FileManager;
60
+ readonly events: EventEmitter<Events>;
61
+ readonly config: Config;
62
+ readonly executed: Array<Executer>;
63
+ readonly logger: Logger;
64
+ readonly queue: PQueue;
65
+ constructor(config: Config, options: Options$1);
66
+ getFile<TOptions = object>({ name, mode, extName, pluginKey, options }: GetFileProps<TOptions>): KubbFile.File<{
67
+ pluginKey: Plugin['key'];
68
+ }>;
69
+ resolvePath: <TOptions = object>(params: ResolvePathParams<TOptions>) => KubbFile.OptionalPath;
70
+ resolveName: (params: ResolveNameParams) => string;
71
+ /**
72
+ * Instead of calling `pluginManager.events.on` you can use `pluginManager.on`. This one also has better types.
73
+ */
74
+ on<TEventName extends keyof Events & string>(eventName: TEventName, handler: (...eventArg: Events[TEventName]) => void): void;
75
+ /**
76
+ * Run a specific hookName for plugin x.
77
+ */
78
+ hookForPlugin<H extends PluginLifecycleHooks>({ pluginKey, hookName, parameters, }: {
79
+ pluginKey: Plugin['key'];
80
+ hookName: H;
81
+ parameters: PluginParameter<H>;
82
+ }): Promise<Array<ReturnType<ParseResult<H>> | null>> | null;
83
+ /**
84
+ * Run a specific hookName for plugin x.
85
+ */
86
+ hookForPluginSync<H extends PluginLifecycleHooks>({ pluginKey, hookName, parameters, }: {
87
+ pluginKey: Plugin['key'];
88
+ hookName: H;
89
+ parameters: PluginParameter<H>;
90
+ }): Array<ReturnType<ParseResult<H>>> | null;
91
+ /**
92
+ * First non-null result stops and will return it's value.
93
+ */
94
+ hookFirst<H extends PluginLifecycleHooks>({ hookName, parameters, skipped, }: {
95
+ hookName: H;
96
+ parameters: PluginParameter<H>;
97
+ skipped?: ReadonlySet<Plugin> | null;
98
+ }): Promise<SafeParseResult<H>>;
99
+ /**
100
+ * First non-null result stops and will return it's value.
101
+ */
102
+ hookFirstSync<H extends PluginLifecycleHooks>({ hookName, parameters, skipped, }: {
103
+ hookName: H;
104
+ parameters: PluginParameter<H>;
105
+ skipped?: ReadonlySet<Plugin> | null;
106
+ }): SafeParseResult<H>;
107
+ /**
108
+ * Run all plugins in parallel(order will be based on `this.plugin` and if `pre` or `post` is set).
109
+ */
110
+ hookParallel<H extends PluginLifecycleHooks, TOuput = void>({ hookName, parameters, }: {
111
+ hookName: H;
112
+ parameters?: Parameters<RequiredPluginLifecycle[H]> | undefined;
113
+ }): Promise<Awaited<TOuput>[]>;
114
+ /**
115
+ * Chain all plugins, `reduce` can be passed through to handle every returned value. The return value of the first plugin will be used as the first parameter for the plugin after that.
116
+ */
117
+ hookReduceArg0<H extends PluginLifecycleHooks>({ hookName, parameters, reduce, }: {
118
+ hookName: H;
119
+ parameters: PluginParameter<H>;
120
+ reduce: (reduction: Argument0<H>, result: ReturnType<ParseResult<H>>, plugin: Plugin) => PossiblePromise<Argument0<H> | null>;
121
+ }): Promise<Argument0<H>>;
122
+ /**
123
+ * Chains plugins
124
+ */
125
+ hookSeq<H extends PluginLifecycleHooks>({ hookName, parameters }: {
126
+ hookName: H;
127
+ parameters?: PluginParameter<H>;
128
+ }): Promise<void>;
129
+ getPluginsByKey(hookName: keyof PluginLifecycle, pluginKey: Plugin['key']): Plugin[];
130
+ static getDependedPlugins<T1 extends PluginFactoryOptions, T2 extends PluginFactoryOptions = never, T3 extends PluginFactoryOptions = never, TOutput = T3 extends never ? (T2 extends never ? [T1: Plugin<T1>] : [T1: Plugin<T1>, T2: Plugin<T2>]) : [T1: Plugin<T1>, T2: Plugin<T2>, T3: Plugin<T3>]>(plugins: Array<Plugin>, dependedPluginNames: string | string[]): TOutput;
131
+ static get hooks(): readonly ["buildStart", "resolvePath", "resolveName", "buildEnd"];
132
+ }
133
+
134
+ interface Cache<TStore extends object = object> {
135
+ delete(id: keyof TStore): boolean;
136
+ get(id: keyof TStore): TStore[keyof TStore] | null;
137
+ has(id: keyof TStore): boolean;
138
+ set(id: keyof TStore, value: unknown): void;
139
+ }
140
+
141
+ /**
142
+ * Config used in `kubb.config.js`
143
+ *
144
+ * @example import { defineConfig } from '@kubb/core'
145
+ * export default defineConfig({
146
+ * ...
147
+ * })
148
+ */
149
+ type UserConfig = Omit<Config, 'root' | 'plugins'> & {
150
+ /**
151
+ * Project root directory. Can be an absolute path, or a path relative from
152
+ * the location of the config file itself.
153
+ * @default process.cwd()
154
+ */
155
+ root?: string;
156
+ /**
157
+ * Plugin type should be a Kubb plugin
158
+ */
159
+ plugins?: Array<Omit<UnknownUserPlugin, 'context'>>;
160
+ };
161
+ type InputPath = {
162
+ /**
163
+ * Path to be used as the input. This can be an absolute path or a path relative to the `root`.
164
+ */
165
+ path: string;
166
+ };
167
+ type InputData = {
168
+ /**
169
+ * `string` or `object` containing the data.
170
+ */
171
+ data: string | unknown;
172
+ };
173
+ type Input = InputPath | InputData;
174
+ /**
175
+ * @private
176
+ */
177
+ type Config<TInput = Input> = {
178
+ /**
179
+ * Optional config name to show in CLI output
180
+ */
181
+ name?: string;
182
+ /**
183
+ * Project root directory. Can be an absolute path, or a path relative from
184
+ * the location of the config file itself.
185
+ * @default process.cwd()
186
+ */
187
+ root: string;
188
+ input: TInput;
189
+ output: {
190
+ /**
191
+ * Path to be used to export all generated files.
192
+ * This can be an absolute path, or a path relative based of the defined `root` option.
193
+ */
194
+ path: string;
195
+ /**
196
+ * Clean output directory before each build.
197
+ */
198
+ clean?: boolean;
199
+ /**
200
+ * Write files to the fileSystem
201
+ * This is being used for the playground.
202
+ * @default true
203
+ */
204
+ write?: boolean;
205
+ };
206
+ /**
207
+ * Array of Kubb plugins to use.
208
+ * The plugin/package can forsee some options that you need to pass through.
209
+ * Sometimes a plugin is depended on another plugin, if that's the case you will get an error back from the plugin you installed.
210
+ */
211
+ plugins?: Array<Plugin>;
212
+ /**
213
+ * Hooks that will be called when a specific action is triggered in Kubb.
214
+ */
215
+ hooks?: {
216
+ /**
217
+ * Hook that will be triggered at the end of all executions.
218
+ * Useful for running Prettier or ESLint to format/lint your code.
219
+ */
220
+ done?: string | Array<string>;
221
+ };
222
+ };
223
+ type PluginFactoryOptions<
224
+ /**
225
+ * Name to be used for the plugin, this will also be used for they key.
226
+ */
227
+ TName extends string = string,
228
+ /**
229
+ * Options of the plugin.
230
+ */
231
+ TOptions extends object = object,
232
+ /**
233
+ * Options of the plugin that can be used later on, see `options` inside your plugin config.
234
+ */
235
+ TResolvedOptions extends object = TOptions,
236
+ /**
237
+ * Context that you want to expose to other plugins.
238
+ */
239
+ TContext = any,
240
+ /**
241
+ * When calling `resolvePath` you can specify better types.
242
+ */
243
+ TResolvePathOptions extends object = object> = {
244
+ name: TName;
245
+ /**
246
+ * Same behaviour like what has been done with `QueryKey` in `@tanstack/react-query`
247
+ */
248
+ key: PluginKey<TName | string>;
249
+ options: TOptions;
250
+ resolvedOptions: TResolvedOptions;
251
+ context: TContext;
252
+ resolvePathOptions: TResolvePathOptions;
253
+ };
254
+ type PluginKey<TName> = [name: TName, identifier?: string | number];
255
+ type GetPluginFactoryOptions<TPlugin extends UserPlugin> = TPlugin extends UserPlugin<infer X> ? X : never;
256
+ type UserPlugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = {
257
+ /**
258
+ * Unique name used for the plugin
259
+ * The name of the plugin follows the format scope:foo-bar or foo-bar, adding scope: can avoid naming conflicts with other plugins.
260
+ * @example @kubb/typescript
261
+ */
262
+ name: TOptions['name'];
263
+ /**
264
+ * Options set for a specific plugin(see kubb.config.js), passthrough of options.
265
+ */
266
+ options: TOptions['resolvedOptions'];
267
+ /**
268
+ * 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.
269
+ * Can be used to validate depended plugins.
270
+ */
271
+ pre?: Array<string>;
272
+ /**
273
+ * 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.
274
+ */
275
+ post?: Array<string>;
276
+ } & (TOptions['context'] extends never ? {
277
+ context?: never;
278
+ } : {
279
+ context: (this: TOptions['name'] extends 'core' ? null : Omit<PluginContext<TOptions>, 'addFile'>) => TOptions['context'];
280
+ });
281
+ type UserPluginWithLifeCycle<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = UserPlugin<TOptions> & PluginLifecycle<TOptions>;
282
+ type UnknownUserPlugin = UserPlugin<PluginFactoryOptions<any, any, any, any, any>>;
283
+ type Plugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = {
284
+ /**
285
+ * Unique name used for the plugin
286
+ * @example @kubb/typescript
287
+ */
288
+ name: TOptions['name'];
289
+ /**
290
+ * Internal key used when a developer uses more than one of the same plugin
291
+ * @private
292
+ */
293
+ key: TOptions['key'];
294
+ /**
295
+ * 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.
296
+ * Can be used to validate depended plugins.
297
+ */
298
+ pre?: Array<string>;
299
+ /**
300
+ * 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.
301
+ */
302
+ post?: Array<string>;
303
+ /**
304
+ * Options set for a specific plugin(see kubb.config.js), passthrough of options.
305
+ */
306
+ options: TOptions['resolvedOptions'];
307
+ } & (TOptions['context'] extends never ? {
308
+ context?: never;
309
+ } : {
310
+ context: TOptions['context'];
311
+ });
312
+ type PluginWithLifeCycle<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = Plugin<TOptions> & PluginLifecycle<TOptions>;
313
+ type PluginLifecycle<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = {
314
+ /**
315
+ * Start of the lifecycle of a plugin.
316
+ * @type hookParallel
317
+ */
318
+ buildStart?: (this: PluginContext<TOptions>, Config: Config) => PossiblePromise<void>;
319
+ /**
320
+ * Resolve to a Path based on a baseName(example: `./Pet.ts`) and directory(example: `./models`).
321
+ * Options can als be included.
322
+ * @type hookFirst
323
+ * @example ('./Pet.ts', './src/gen/') => '/src/gen/Pet.ts'
324
+ */
325
+ resolvePath?: (this: PluginContext<TOptions>, baseName: string, mode?: KubbFile.Mode, options?: TOptions['resolvePathOptions']) => KubbFile.OptionalPath;
326
+ /**
327
+ * Resolve to a name based on a string.
328
+ * Useful when converting to PascalCase or camelCase.
329
+ * @type hookFirst
330
+ * @example ('pet') => 'Pet'
331
+ */
332
+ resolveName?: (this: PluginContext<TOptions>, name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string;
333
+ /**
334
+ * End of the plugin lifecycle.
335
+ * @type hookParallel
336
+ */
337
+ buildEnd?: (this: PluginContext<TOptions>) => PossiblePromise<void>;
338
+ };
339
+ type PluginLifecycleHooks = keyof PluginLifecycle;
340
+ type PluginParameter<H extends PluginLifecycleHooks> = Parameters<Required<PluginLifecycle>[H]>;
341
+ type PluginCache = Record<string, [number, unknown]>;
342
+ type ResolvePathParams<TOptions = object> = {
343
+ pluginKey?: Plugin['key'];
344
+ baseName: string;
345
+ mode?: KubbFile.Mode;
346
+ /**
347
+ * Options to be passed to 'resolvePath' 3th parameter
348
+ */
349
+ options?: TOptions;
350
+ };
351
+ type ResolveNameParams = {
352
+ name: string;
353
+ pluginKey?: Plugin['key'];
354
+ /**
355
+ * `file` will be used to customize the name of the created file(use of camelCase)
356
+ * `function` can be used used to customize the exported functions(use of camelCase)
357
+ * `type` is a special type for TypeScript(use of PascalCase)
358
+ */
359
+ type?: 'file' | 'function' | 'type';
360
+ };
361
+ type PluginContext<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = {
362
+ config: Config;
363
+ cache: Cache<PluginCache>;
364
+ fileManager: FileManager;
365
+ pluginManager: PluginManager;
366
+ addFile: (...file: Array<KubbFile.File>) => Promise<Array<KubbFile.File>>;
367
+ resolvePath: (params: ResolvePathParams<TOptions['resolvePathOptions']>) => KubbFile.OptionalPath;
368
+ resolveName: (params: ResolveNameParams) => string;
369
+ logger: Logger;
370
+ /**
371
+ * All plugins
372
+ */
373
+ plugins: Plugin[];
374
+ /**
375
+ * Current plugin
376
+ */
377
+ plugin: Plugin<TOptions>;
378
+ };
379
+
380
+ type ResolvedFile<TMeta extends FileMetaBase = FileMetaBase, TBaseName extends BaseName = BaseName> = File<TMeta, TBaseName> & {
381
+ /**
382
+ * @default crypto.randomUUID()
383
+ */
384
+ id: UUID;
385
+ /**
386
+ * Contains the first part of the baseName, generated based on baseName
387
+ * @link https://nodejs.org/api/path.html#pathformatpathobject
388
+ */
389
+ name: string;
390
+ };
391
+ type FileMetaBase = {
392
+ pluginKey?: Plugin['key'];
393
+ };
394
+ type FileWithMeta<TMeta extends FileMetaBase = FileMetaBase> = KubbFile.File<TMeta>;
395
+ type AddResult<T extends Array<FileWithMeta>> = Promise<Awaited<GreaterThan<T['length'], 1> extends true ? Promise<ResolvedFile[]> : Promise<ResolvedFile>>>;
396
+ type AddIndexesProps = {
397
+ /**
398
+ * Root based on root and output.path specified in the config
399
+ */
400
+ root: string;
401
+ /**
402
+ * Output for plugin
403
+ */
404
+ output: {
405
+ path: string;
406
+ exportAs?: string;
407
+ extName?: KubbFile.Extname;
408
+ exportType?: 'barrel' | 'barrelNamed' | false;
409
+ };
410
+ logger: Logger;
411
+ options?: BarrelManagerOptions;
412
+ meta?: FileWithMeta['meta'];
413
+ };
414
+ type Options = {
415
+ queue?: PQueue;
416
+ task?: (file: ResolvedFile) => Promise<ResolvedFile>;
417
+ };
418
+ declare class FileManager {
419
+ #private;
420
+ constructor({ task, queue }?: Options);
421
+ get files(): Array<FileWithMeta>;
422
+ get isExecuting(): boolean;
423
+ add<T extends Array<FileWithMeta> = Array<FileWithMeta>>(...files: T): AddResult<T>;
424
+ addIndexes({ root, output, meta, logger, options }: AddIndexesProps): Promise<void>;
425
+ getCacheByUUID(UUID: KubbFile.UUID): FileWithMeta | undefined;
426
+ get(path: KubbFile.Path): Array<FileWithMeta> | undefined;
427
+ remove(path: KubbFile.Path): void;
428
+ write(...params: Parameters<typeof write>): Promise<string | undefined>;
429
+ read(...params: Parameters<typeof read>): Promise<string>;
430
+ static getSource<TMeta extends FileMetaBase = FileMetaBase>(file: FileWithMeta<TMeta>): Promise<string>;
431
+ static combineFiles<TMeta extends FileMetaBase = FileMetaBase>(files: Array<FileWithMeta<TMeta> | null>): Array<FileWithMeta<TMeta>>;
432
+ static getMode(path: string | undefined | null): KubbFile.Mode;
433
+ static get extensions(): Array<KubbFile.Extname>;
434
+ static isJavascript(baseName: string): boolean;
435
+ }
436
+
437
+ export { type Config as C, FileManager as F, type GetPluginFactoryOptions as G, type InputPath as I, type PluginContext as P, type ResolvePathParams as R, type UserConfig as U, PluginManager as a, type PluginFactoryOptions as b, type UserPluginWithLifeCycle as c, type FileMetaBase as d, type InputData as e, type PluginKey as f, type UserPlugin as g, type Plugin as h, type PluginWithLifeCycle as i, type PluginLifecycle as j, type PluginLifecycleHooks as k, type PluginParameter as l, type PluginCache as m, type ResolveNameParams as n };