@kubb/core 1.2.0 → 1.2.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.
package/dist/index.d.ts CHANGED
@@ -11,8 +11,9 @@ type Logger = {
11
11
  spinner?: Ora;
12
12
  };
13
13
  declare function createLogger(spinner?: Ora): Logger;
14
+ declare function canLogHierarchy(input: LogLevels | undefined, compareTo: LogLevels): boolean;
14
15
 
15
- declare function isPromise<T>(result: MaybePromise<T>): result is Promise<T>;
16
+ declare function isPromise<T>(result: PossiblePromise<T>): result is Promise<T>;
16
17
  declare function isPromiseFulfilledResult<T = unknown>(result: PromiseSettledResult<unknown>): result is PromiseFulfilledResult<T>;
17
18
  declare function isPromiseRejectedResult<T>(result: PromiseSettledResult<unknown>): result is Omit<PromiseRejectedResult, 'reason'> & {
18
19
  reason: T;
@@ -26,7 +27,7 @@ interface Cache<TStore extends object = object> {
26
27
  has(id: keyof TStore): boolean;
27
28
  set(id: keyof TStore, value: unknown): void;
28
29
  }
29
- declare function createPluginCache<TStore extends Record<string, [number, unknown]>>(Store: TStore): Cache<TStore>;
30
+ declare function createPluginCache<TStore extends PluginCache>(Store?: TStore): Cache<TStore>;
30
31
 
31
32
  declare function getRelativePath(rootDir?: string | null, filePath?: string | null, platform?: 'windows' | 'mac' | 'linux'): string;
32
33
  type PathMode = 'file' | 'directory';
@@ -47,7 +48,7 @@ declare function objectToParameters(data: Data, options?: Options$2): string;
47
48
 
48
49
  declare function nameSorter<T extends {
49
50
  name: string;
50
- }>(a: T, b: T): 1 | -1 | 0;
51
+ }>(a: T, b: T): 0 | 1 | -1;
51
52
 
52
53
  declare function createJSDocBlockText({ comments }: {
53
54
  comments: Array<string>;
@@ -60,12 +61,17 @@ declare function timeout(ms: number): Promise<unknown>;
60
61
  type QueueTask<T = unknown> = {
61
62
  (...args: unknown[]): Promise<T> | Promise<void>;
62
63
  };
64
+ type RunOptions = {
65
+ name: string;
66
+ description: string;
67
+ };
63
68
  declare class Queue {
64
69
  private readonly queue;
65
70
  workerCount: number;
66
71
  private maxParallel;
67
- constructor(maxParallel: number);
68
- run<T>(task: QueueTask<T>): Promise<T>;
72
+ private debug;
73
+ constructor(maxParallel: number, debug?: boolean);
74
+ run<T>(task: QueueTask<T>, options?: RunOptions): Promise<T>;
69
75
  private work;
70
76
  }
71
77
 
@@ -89,7 +95,7 @@ declare class TreeNode<T = unknown> {
89
95
  static build<T = unknown>(path: string, options?: TreeNodeOptions): TreeNode<T> | null;
90
96
  }
91
97
 
92
- declare function transformReservedWord(word?: string | null): string | null | undefined;
98
+ declare function transformReservedWord(word: string): string;
93
99
 
94
100
  declare function getStackTrace(belowFn?: Function): NodeJS.CallSite[];
95
101
 
@@ -128,6 +134,10 @@ declare class Warning extends Error {
128
134
  });
129
135
  }
130
136
 
137
+ declare const defaultColours: readonly ["black", "blue", "darkBlue", "cyan", "gray", "green", "darkGreen", "magenta", "red", "darkRed", "yellow", "darkYellow"];
138
+ declare function randomColour(text?: string, colours?: readonly ["black", "blue", "darkBlue", "cyan", "gray", "green", "darkGreen", "magenta", "red", "darkRed", "yellow", "darkYellow"]): string;
139
+ declare function randomPicoColour(text?: string, colors?: readonly ["black", "blue", "darkBlue", "cyan", "gray", "green", "darkGreen", "magenta", "red", "darkRed", "yellow", "darkYellow"]): string;
140
+
131
141
  type Import = {
132
142
  name: string | string[];
133
143
  path: string;
@@ -139,6 +149,8 @@ type Export = {
139
149
  isTypeOnly?: boolean;
140
150
  asAlias?: boolean;
141
151
  };
152
+ type UUID = string;
153
+ type Source = string;
142
154
  type File = {
143
155
  /**
144
156
  * Name to be used to dynamicly create the fileName(based on input.path)
@@ -147,8 +159,8 @@ type File = {
147
159
  /**
148
160
  * Path will be full qualified path to a specified file
149
161
  */
150
- path: string;
151
- source: string;
162
+ path: Path;
163
+ source: Source;
152
164
  imports?: Import[];
153
165
  exports?: Export[];
154
166
  /**
@@ -164,11 +176,11 @@ type File = {
164
176
  */
165
177
  env?: NodeJS.ProcessEnv;
166
178
  };
167
- type UUID = string;
168
- type CacheStore = {
179
+ type ResolvedFile = File & {
180
+ /**
181
+ * crypto.randomUUID()
182
+ */
169
183
  id: UUID;
170
- file: File;
171
- status: Status;
172
184
  };
173
185
  type Status = 'new' | 'success' | 'removed';
174
186
 
@@ -184,18 +196,16 @@ declare class FileManager {
184
196
  private queue?;
185
197
  constructor(options?: {
186
198
  queue: Queue;
187
- task?: QueueTask<File>;
199
+ task?: QueueTask<ResolvedFile>;
188
200
  });
189
- private getCache;
190
201
  get extensions(): Extension[];
191
- getCacheByPath(path: string | undefined): CacheStore | undefined;
192
202
  get files(): File[];
193
- get cachedFiles(): CacheStore[];
194
- add(file: File): Promise<File>;
195
- addOrAppend(file: File): Promise<File>;
196
- setStatus(id: UUID, status: Status): void;
197
- get(id: UUID): File | undefined;
198
- remove(id: UUID): void;
203
+ add(file: File): Promise<ResolvedFile>;
204
+ addOrAppend(file: File): Promise<ResolvedFile>;
205
+ private append;
206
+ getCacheByUUID(UUID: UUID): File | undefined;
207
+ get(path: Path): File[] | undefined;
208
+ remove(path: Path): void;
199
209
  write(...params: Parameters<typeof write>): Promise<void>;
200
210
  read(...params: Parameters<typeof read>): Promise<string>;
201
211
  }
@@ -210,6 +220,8 @@ type Executer<H extends PluginLifecycleHooks = PluginLifecycleHooks> = {
210
220
  strategy: Strategy;
211
221
  hookName: H;
212
222
  plugin: KubbPlugin;
223
+ input?: unknown[] | undefined;
224
+ output?: unknown;
213
225
  };
214
226
  type OnExecute<H extends PluginLifecycleHooks = PluginLifecycleHooks> = (this: PluginManager, executer: Executer<H> | undefined, pluginManager: PluginManager) => void;
215
227
  type ParseResult<H extends PluginLifecycleHooks> = PluginLifecycle[H];
@@ -218,9 +230,10 @@ type SafeParseResult<H extends PluginLifecycleHooks, Result = ReturnType<ParseRe
218
230
  plugin: KubbPlugin;
219
231
  };
220
232
 
221
- declare const hooks: [keyof PluginLifecycle<PluginFactoryOptions<string, unknown, false, any, Record<string, any>>>];
233
+ declare const hooks: [keyof PluginLifecycle<PluginFactoryOptions<string, unknown, false, any, Record<string, unknown>>>];
222
234
  type Options$1 = {
223
- task: QueueTask<File>;
235
+ debug?: boolean;
236
+ task: QueueTask<ResolvedFile>;
224
237
  logger: Logger;
225
238
  onExecute?: OnExecute<PluginLifecycleHooks>;
226
239
  };
@@ -230,14 +243,12 @@ declare class PluginManager {
230
243
  private readonly onExecute?;
231
244
  private readonly core;
232
245
  queue: Queue;
233
- executer: Executer | undefined;
234
246
  executed: Executer[];
235
247
  logger: Logger;
236
248
  constructor(config: KubbConfig, options: Options$1);
237
- getExecuter(): Executer | undefined;
238
249
  resolvePath: (params: ResolvePathParams) => OptionalPath;
239
- resolveName: (params: ResolveNameParams) => string | null;
240
- load: (id: string) => Promise<SafeParseResult<"load">>;
250
+ resolveName: (params: ResolveNameParams) => string;
251
+ load: (id: string) => Promise<SafeParseResult<'load'>>;
241
252
  /**
242
253
  *
243
254
  * Run only hook for a specific plugin name
@@ -285,7 +296,7 @@ declare class PluginManager {
285
296
  hookReduceArg0<H extends PluginLifecycleHooks>({ hookName, parameters, reduce, }: {
286
297
  hookName: H;
287
298
  parameters: Parameters<PluginLifecycle[H]>;
288
- reduce: (reduction: Argument0<H>, result: ReturnType<ParseResult<H>>, plugin: KubbPlugin) => MaybePromise<Argument0<H> | null>;
299
+ reduce: (reduction: Argument0<H>, result: ReturnType<ParseResult<H>>, plugin: KubbPlugin) => PossiblePromise<Argument0<H> | null>;
289
300
  }): Promise<Argument0<H>>;
290
301
  /**
291
302
  * Chains plugins
@@ -296,7 +307,7 @@ declare class PluginManager {
296
307
  }): Promise<void>;
297
308
  private getSortedPlugins;
298
309
  getPlugin(hookName: keyof PluginLifecycle, pluginName: string): KubbPlugin;
299
- private addExecuter;
310
+ private addExecutedToCallStack;
300
311
  /**
301
312
  * Run an async plugin hook and return the result.
302
313
  * @param hookName Name of the plugin hook. Must be either in `PluginHooks` or `OutputPluginValueHooks`.
@@ -341,7 +352,7 @@ declare class ParallelPluginError extends Error {
341
352
 
342
353
  interface Register {
343
354
  }
344
- type MaybePromise<T> = Promise<T> | T;
355
+ type PossiblePromise<T> = Promise<T> | T;
345
356
  /**
346
357
  * Config used in `kubb.config.js`
347
358
  *
@@ -414,10 +425,15 @@ type KubbConfig = {
414
425
  };
415
426
  /**
416
427
  * Log level to report when using the CLI
417
- * Under construction, only info is implemented.
428
+ *
429
+ * `silent` will hide all information that is not relevant
430
+ *
431
+ * `info` will show all information possible(not related to the PluginManager)
432
+ *
433
+ * `stacktrace` will show all information possible(related to the PluginManager), handy for seeing logs
418
434
  * @default `silent`
419
435
  */
420
- logLevel?: LogLevel;
436
+ logLevel?: LogLevels;
421
437
  };
422
438
  type CLIOptions = {
423
439
  /**
@@ -432,15 +448,11 @@ type CLIOptions = {
432
448
  * Watch changes on input
433
449
  */
434
450
  watch?: string;
435
- /**
436
- * Override `input` defined in `kubb.config.js`
437
- */
438
- input?: string;
439
451
  /**
440
452
  * Override `logLevel` defined in `kubb.config.js`
441
453
  * @default `'silent'`
442
454
  */
443
- logLevel?: LogLevel;
455
+ logLevel?: LogLevels;
444
456
  init?: unknown;
445
457
  };
446
458
  type BuildOutput = {
@@ -453,6 +465,28 @@ type KubbObjectPlugin = keyof Register;
453
465
  type KubbObjectPlugins = {
454
466
  [K in keyof Register]: Register[K] | object;
455
467
  };
468
+ type KubbUserPlugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = {
469
+ /**
470
+ * Unique name used for the plugin
471
+ * @example @kubb/typescript
472
+ */
473
+ name: PluginFactoryOptions['name'];
474
+ /**
475
+ * Options set for a specific plugin(see kubb.config.js), passthrough of options.
476
+ */
477
+ options?: TOptions['options'];
478
+ /**
479
+ * Kind/type for the plugin
480
+ * Type 'schema' can be used for JSON schema's, TypeScript types, ...
481
+ * Type 'controller' can be used to create generate API calls, React-Query hooks, Axios controllers, ...
482
+ * @default undefined
483
+ */
484
+ kind?: KubbPluginKind;
485
+ /**
486
+ * Define an api that can be used by other plugins, see `PluginManager' where we convert from `KubbUserPlugin` to `KubbPlugin`(used when calling `createPlugin`).
487
+ */
488
+ api?: (this: Omit<PluginContext, 'addFile'>) => TOptions['api'];
489
+ } & Partial<PluginLifecycle<TOptions>>;
456
490
  type KubbPlugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = {
457
491
  /**
458
492
  * Unique name used for the plugin
@@ -471,11 +505,11 @@ type KubbPlugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions> =
471
505
  */
472
506
  kind?: KubbPluginKind;
473
507
  /**
474
- * Defined an api that can be used by other plugins
508
+ * Define an api that can be used by other plugins, see `PluginManager' where we convert from `KubbUserPlugin` to `KubbPlugin`(used when calling `createPlugin`).
475
509
  */
476
510
  api?: TOptions['api'];
477
511
  } & Partial<PluginLifecycle<TOptions>>;
478
- type PluginFactoryOptions<Name = string, Options = unknown, Nested extends boolean = false, API = any, resolvePathOptions = Record<string, any>> = {
512
+ type PluginFactoryOptions<Name = string, Options = unknown, Nested extends boolean = false, API = any, resolvePathOptions = Record<string, unknown>> = {
479
513
  name: Name;
480
514
  options: Options;
481
515
  nested: Nested;
@@ -487,12 +521,12 @@ type PluginLifecycle<TOptions extends PluginFactoryOptions = PluginFactoryOption
487
521
  * Valdiate all plugins to see if their depended plugins are installed and configured.
488
522
  * @type hookParallel
489
523
  */
490
- validate: (this: Omit<PluginContext, 'addFile'>, plugins: KubbPlugin[]) => MaybePromise<true>;
524
+ validate: (this: Omit<PluginContext, 'addFile'>, plugins: KubbPlugin[]) => PossiblePromise<true>;
491
525
  /**
492
526
  * Start of the lifecycle of a plugin.
493
527
  * @type hookParallel
494
528
  */
495
- buildStart: (this: PluginContext, kubbConfig: KubbConfig) => MaybePromise<void>;
529
+ buildStart: (this: PluginContext, kubbConfig: KubbConfig) => PossiblePromise<void>;
496
530
  /**
497
531
  * Resolve to a Path based on a fileName(example: `./Pet.ts`) and directory(example: `./models`).
498
532
  * Options can als be included.
@@ -511,25 +545,26 @@ type PluginLifecycle<TOptions extends PluginFactoryOptions = PluginFactoryOption
511
545
  * Makes it possible to run async logic to override the path defined previously by `resolvePath`.
512
546
  * @type hookFirst
513
547
  */
514
- load: (this: Omit<PluginContext, 'addFile'>, path: Path) => MaybePromise<TransformResult | null>;
548
+ load: (this: Omit<PluginContext, 'addFile'>, path: Path) => PossiblePromise<TransformResult | null>;
515
549
  /**
516
550
  * Transform the source-code.
517
551
  * @type hookReduceArg0
518
552
  */
519
- transform: (this: Omit<PluginContext, 'addFile'>, source: string, path: Path) => MaybePromise<TransformResult>;
553
+ transform: (this: Omit<PluginContext, 'addFile'>, source: string, path: Path) => PossiblePromise<TransformResult>;
520
554
  /**
521
555
  * Write the result to the file-system based on the id(defined by `resolvePath` or changed by `load`).
522
556
  * @type hookParallel
523
557
  */
524
- writeFile: (this: Omit<PluginContext, 'addFile'>, source: string | undefined, path: Path) => MaybePromise<void>;
558
+ writeFile: (this: Omit<PluginContext, 'addFile'>, source: string | undefined, path: Path) => PossiblePromise<void>;
525
559
  /**
526
560
  * End of the plugin lifecycle.
527
561
  * @type hookParallel
528
562
  */
529
- buildEnd: (this: PluginContext) => MaybePromise<void>;
563
+ buildEnd: (this: PluginContext) => PossiblePromise<void>;
530
564
  };
531
565
  type PluginLifecycleHooks = keyof PluginLifecycle;
532
- type ResolvePathParams<TOptions = Record<string, any>> = {
566
+ type PluginCache = Record<string, [number, unknown]>;
567
+ type ResolvePathParams<TOptions = Record<string, unknown>> = {
533
568
  /**
534
569
  * When set, resolvePath will only call resolvePath of the name of the plugin set here.
535
570
  * If not defined it will fall back on the resolvePath of the core plugin.
@@ -550,13 +585,13 @@ type ResolveNameParams = {
550
585
  pluginName?: string;
551
586
  name: string;
552
587
  };
553
- type PluginContext<TOptions = Record<string, any>> = {
588
+ type PluginContext<TOptions = Record<string, unknown>> = {
554
589
  config: KubbConfig;
555
- cache: Cache;
590
+ cache: Cache<PluginCache>;
556
591
  fileManager: FileManager;
557
592
  addFile: (...file: File[]) => Promise<File[]>;
558
593
  resolvePath: (params: ResolvePathParams<TOptions>) => OptionalPath;
559
- resolveName: (params: ResolveNameParams) => string | null | undefined;
594
+ resolveName: (params: ResolveNameParams) => string;
560
595
  load: (id: string) => Promise<SafeParseResult<'load'>>;
561
596
  logger: Logger;
562
597
  };
@@ -567,7 +602,12 @@ type TransformResult = string | null;
567
602
  type Path = string;
568
603
  type OptionalPath = Path | null | undefined;
569
604
  type FileName = string | null | undefined;
570
- type LogLevel = LogType | 'silent';
605
+ declare const LogLevel: {
606
+ readonly silent: "silent";
607
+ readonly info: "info";
608
+ readonly stacktrace: "stacktrace";
609
+ };
610
+ type LogLevels = keyof typeof LogLevel;
571
611
 
572
612
  type BuildOptions = {
573
613
  config: PluginContext['config'];
@@ -575,6 +615,7 @@ type BuildOptions = {
575
615
  * @default Logger without the spinner
576
616
  */
577
617
  logger?: Logger;
618
+ debug?: boolean;
578
619
  };
579
620
  declare function build(options: BuildOptions): Promise<BuildOutput>;
580
621
 
@@ -583,10 +624,12 @@ declare function build(options: BuildOptions): Promise<BuildOutput>;
583
624
  * accepts a direct {@link KubbConfig} object, or a function that returns it.
584
625
  * The function receives a {@link ConfigEnv} object that exposes two properties:
585
626
  */
586
- declare const defineConfig: (options: MaybePromise<KubbUserConfig> | ((cliOptions: CLIOptions) => MaybePromise<KubbUserConfig>)) => MaybePromise<KubbUserConfig> | ((cliOptions: CLIOptions) => MaybePromise<KubbUserConfig>);
627
+ declare function defineConfig(options: PossiblePromise<KubbUserConfig> | ((
628
+ /** The options derived from the CLI flags */
629
+ cliOptions: CLIOptions) => PossiblePromise<KubbUserConfig>)): typeof options;
587
630
 
588
- type KubbPluginFactory<T extends PluginFactoryOptions = PluginFactoryOptions> = (options: T['options']) => T['nested'] extends true ? Array<KubbPlugin<T>> : KubbPlugin<T>;
589
- declare function createPlugin<T extends PluginFactoryOptions = PluginFactoryOptions>(factory: KubbPluginFactory<T>): (options: T['options']) => T["nested"] extends true ? KubbPlugin<T>[] : KubbPlugin<T>;
631
+ type KubbPluginFactory<T extends PluginFactoryOptions = PluginFactoryOptions> = (options: T['options']) => T['nested'] extends true ? Array<KubbUserPlugin<T>> : KubbUserPlugin<T>;
632
+ declare function createPlugin<T extends PluginFactoryOptions = PluginFactoryOptions>(factory: KubbPluginFactory<T>): (options: T['options']) => ReturnType<KubbPluginFactory<T>>;
590
633
  type Options = {
591
634
  config: PluginContext['config'];
592
635
  fileManager: FileManager;
@@ -594,7 +637,6 @@ type Options = {
594
637
  resolveName: PluginContext['resolveName'];
595
638
  load: PluginContext['load'];
596
639
  logger: PluginContext['logger'];
597
- getExecuter: () => Executer<PluginLifecycleHooks> | undefined;
598
640
  };
599
641
  type CorePluginOptions = PluginFactoryOptions<'core', Options, false, PluginContext>;
600
642
  declare const pluginName: CorePluginOptions['name'];
@@ -617,4 +659,4 @@ declare abstract class SchemaGenerator<TOptions extends object, TInput, TOutput>
617
659
  abstract build(schema: TInput, name: string, description?: string): TOutput;
618
660
  }
619
661
 
620
- export { Argument0, BuildOutput, CLIOptions, Cache, CacheStore, CorePluginOptions, Executer, Extension, File, FileManager, FileName, Generator, KubbConfig, KubbJSONPlugins, KubbObjectPlugin, KubbObjectPlugins, KubbPlugin, KubbPluginKind, KubbUserConfig, LogLevel, LogType, Logger, MaybePromise, OnExecute, OptionalPath, ParallelPluginError, ParseResult, Path, PathMode, PluginContext, PluginError, PluginFactoryOptions, PluginLifecycle, PluginLifecycleHooks, PluginManager, Queue, QueueTask, Register, ResolveNameParams, ResolvePathParams, SafeParseResult, SchemaGenerator, Status, Strategy, SummaryError, TransformResult, TreeNode, TreeNodeOptions, UUID, ValidationPluginError, Warning, build, clean, combineFiles, createJSDocBlockText, createLogger, createPlugin, createPluginCache, build as default, defineConfig, extensions, getEncodedText, getFileSource, getLocation, getPathMode, getRelativePath, getStackTrace, getUniqueName, hooks, importModule, isPromise, isPromiseFulfilledResult, isPromiseRejectedResult, isURL, pluginName as name, nameSorter, normalizeDirectory, objectToParameters, pluginName, read, renderTemplate, throttle, timeout, transformReservedWord, uniqueId, validatePlugins, write, writeIndexes };
662
+ export { Argument0, BuildOutput, CLIOptions, Cache, CorePluginOptions, Executer, Extension, File, FileManager, FileName, Generator, KubbConfig, KubbJSONPlugins, KubbObjectPlugin, KubbObjectPlugins, KubbPlugin, KubbPluginKind, KubbUserConfig, KubbUserPlugin, LogLevel, LogLevels, LogType, Logger, OnExecute, OptionalPath, ParallelPluginError, ParseResult, Path, PathMode, PluginCache, PluginContext, PluginError, PluginFactoryOptions, PluginLifecycle, PluginLifecycleHooks, PluginManager, PossiblePromise, Queue, QueueTask, Register, ResolveNameParams, ResolvePathParams, ResolvedFile, SafeParseResult, SchemaGenerator, Source, Status, Strategy, SummaryError, TransformResult, TreeNode, TreeNodeOptions, UUID, ValidationPluginError, Warning, build, canLogHierarchy, clean, combineFiles, createJSDocBlockText, createLogger, createPlugin, createPluginCache, build as default, defaultColours, defineConfig, extensions, getEncodedText, getFileSource, getLocation, getPathMode, getRelativePath, getStackTrace, getUniqueName, hooks, importModule, isPromise, isPromiseFulfilledResult, isPromiseRejectedResult, isURL, pluginName as name, nameSorter, normalizeDirectory, objectToParameters, pluginName, randomColour, randomPicoColour, read, renderTemplate, throttle, timeout, transformReservedWord, uniqueId, validatePlugins, write, writeIndexes };