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

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