@kubb/core 0.37.6 → 0.37.8

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.
@@ -0,0 +1,383 @@
1
+ import EventEmitter from 'events';
2
+ import { DirectoryTreeOptions } from 'directory-tree';
3
+
4
+ interface Cache<TCache = any> {
5
+ delete(id: string): boolean;
6
+ get<T = TCache>(id: string): T;
7
+ has(id: string): boolean;
8
+ set<T = TCache>(id: string, value: T): void;
9
+ }
10
+ declare function createPluginCache(cache: any): Cache;
11
+
12
+ type MaybePromise<T> = Promise<T> | T;
13
+ type KubbUserConfig<IsJSON = false> = Omit<KubbConfig, 'root'> & {
14
+ /**
15
+ * Project root directory. Can be an absolute path, or a path relative from
16
+ * the location of the config file itself.
17
+ * @default process.cwd()
18
+ */
19
+ root?: string;
20
+ plugins?: IsJSON extends true ? KubbJSONPlugin[] : KubbPlugin[];
21
+ };
22
+ /**
23
+ * Global/internal config used through out the full generation.
24
+ */
25
+ type KubbConfig = {
26
+ /**
27
+ * Project root directory. Can be an absolute path, or a path relative from
28
+ * the location of the config file itself.
29
+ * @default process.cwd()
30
+ */
31
+ root: string;
32
+ input: {
33
+ /**
34
+ * Path to be used as the input. Can be an absolute path, or a path relative from
35
+ * the defined root option.
36
+ */
37
+ path: string;
38
+ } | string;
39
+ output: {
40
+ /**
41
+ * Path to be used to export all generated files. Can be an absolute path, or a path relative based of the defined root option.
42
+ */
43
+ path: string;
44
+ /**
45
+ * Remove previous generated files and folders.
46
+ */
47
+ clean?: boolean;
48
+ };
49
+ /**
50
+ * Array of Kubb plugins to use.
51
+ * The plugin/package can forsee some options that you need to pass through.
52
+ * Sometimes a plugin is depended on another plugin, if that's the case you will get an error back from the plugin you installed.
53
+ */
54
+ plugins?: KubbPlugin[];
55
+ /**
56
+ * Hooks that will be called when a specific action is triggered in Kubb.
57
+ */
58
+ hooks?: {
59
+ /**
60
+ * Hook that will be triggerend at the end of all executions.
61
+ */
62
+ done?: string | string[];
63
+ };
64
+ /**
65
+ * Log level to report when using the CLI
66
+ */
67
+ logLevel?: LogLevel;
68
+ };
69
+ type CLIOptions = {
70
+ config?: string;
71
+ mode?: 'development' | 'production';
72
+ debug?: boolean;
73
+ watch?: string;
74
+ };
75
+ type KubbPluginKind = 'schema' | 'controller';
76
+ type KubbJSONPlugin = [string, Record<string, any>];
77
+ type KubbPlugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = {
78
+ /**
79
+ * Unique name used for the plugin
80
+ * @example @kubb/typescript
81
+ */
82
+ name: string;
83
+ /**
84
+ * Kind/type for the plugin
85
+ * Type 'schema' can be used for JSON schema's, Typescript types, ...
86
+ * Type 'controller' can be used to create generate API calls, React-Query hooks, Axios controllers, ...
87
+ * @default undefined
88
+ */
89
+ kind?: KubbPluginKind;
90
+ /**
91
+ * Defined an api that can be used by other plugins
92
+ */
93
+ api?: TOptions['api'];
94
+ } & Partial<PluginLifecycle>;
95
+ type PluginFactoryOptions<Options = unknown, Nested extends boolean = false, Api = any> = {
96
+ options: Options;
97
+ nested: Nested;
98
+ api: Api;
99
+ };
100
+ type PluginLifecycle = {
101
+ /**
102
+ * Valdiate all plugins to see if their depended plugins are installed and configured.
103
+ * @type hookParallel
104
+ */
105
+ validate: (this: PluginContext, plugins: KubbPlugin[]) => MaybePromise<true>;
106
+ /**
107
+ * Start of the lifecycle of a plugin.
108
+ * @type hookParallel
109
+ */
110
+ buildStart: (this: PluginContext, kubbConfig: KubbConfig) => MaybePromise<void>;
111
+ /**
112
+ * Resolve to an id based on importee(example: `./Pet.ts`) and directory(example: `./models`).
113
+ * @type hookFirst
114
+ * @example ('./Pet.ts', './src/gen/')
115
+ */
116
+ resolveId: (this: PluginContext, fileName: string, directory?: string, options?: Record<string, any>) => OptionalPath;
117
+ /**
118
+ * Makes it possible to run async logic to override the path defined previously by `resolveId`.
119
+ * @type hookFirst
120
+ */
121
+ load: (this: PluginContext, path: Path) => MaybePromise<TransformResult | null>;
122
+ /**
123
+ * Transform the source-code.
124
+ * @type hookReduceArg0
125
+ */
126
+ transform: (this: PluginContext, source: string, path: Path) => MaybePromise<TransformResult>;
127
+ /**
128
+ * Write the result to the file-system based on the id(defined by `resolveId` or changed by `load`).
129
+ * @type hookParallel
130
+ */
131
+ writeFile: (this: PluginContext, source: string | undefined, path: Path) => MaybePromise<void>;
132
+ /**
133
+ * End of the plugin lifecycle.
134
+ * @type hookParallel
135
+ */
136
+ buildEnd: (this: PluginContext) => MaybePromise<void>;
137
+ };
138
+ type PluginLifecycleHooks = keyof PluginLifecycle;
139
+ type ResolveIdParams = {
140
+ fileName: string;
141
+ directory?: string | undefined;
142
+ /**
143
+ * When set, resolveId will only call resolveId of the name of the plugin set here.
144
+ * If not defined it will fall back on the resolveId of the core plugin.
145
+ */
146
+ pluginName?: string;
147
+ /**
148
+ * Options to be passed to 'resolveId' 3th parameter
149
+ */
150
+ options?: Record<string, any>;
151
+ };
152
+ type PluginContext = {
153
+ config: KubbConfig;
154
+ cache: Cache;
155
+ fileManager: FileManager;
156
+ addFile: (emitedFile: EmittedFile | File, options?: {
157
+ root?: true;
158
+ }) => Promise<File>;
159
+ resolveId: (params: ResolveIdParams) => MaybePromise<OptionalPath>;
160
+ load: (id: string) => MaybePromise<TransformResult | void>;
161
+ };
162
+ type TransformResult = string | null;
163
+ /**
164
+ * @description Computing the name of a file or directory together with its position in relation to other directories traced back in a line to the root
165
+ */
166
+ type Path = string;
167
+ type OptionalPath = Path | null | undefined;
168
+ type FileName = string | null | undefined;
169
+ type LogType = 'error' | 'warn' | 'info';
170
+ type LogLevel = LogType | 'silent';
171
+
172
+ declare const isPromise: <T>(result: MaybePromise<T>) => result is Promise<T>;
173
+
174
+ type WriteOptions = {
175
+ format: boolean;
176
+ };
177
+ declare const write: (data: string, path: string, options?: WriteOptions) => Promise<void>;
178
+ declare const clean: (path: string) => Promise<void>;
179
+
180
+ declare const format: (text: string) => string;
181
+
182
+ declare const getRelativePath: (root?: string | null, file?: string | null) => string;
183
+ type PathMode = 'file' | 'directory';
184
+ declare const getPathMode: (path: string | undefined | null) => PathMode | undefined;
185
+ declare const read: (path: string, encoding?: string) => Promise<string>;
186
+
187
+ /**
188
+ * @deprecated Use userFile instead
189
+ */
190
+ type EmittedFile = {
191
+ /**
192
+ * equal to importee when getting passed through resolveId
193
+ */
194
+ id: string;
195
+ /**
196
+ * The importer is the fully resolved id of the importing module.
197
+ */
198
+ importer?: string;
199
+ /**
200
+ * Name to be used to dynamicly create the fileName(based on input.path)
201
+ */
202
+ name?: string;
203
+ /**
204
+ * FileName will be the end result so no input.path will not be added
205
+ */
206
+ fileName?: string;
207
+ source?: string;
208
+ options?: Record<string, any>;
209
+ };
210
+ type File = {
211
+ /**
212
+ * Name to be used to dynamicly create the fileName(based on input.path)
213
+ */
214
+ fileName: string;
215
+ /**
216
+ * Path will be full qualified path to a specified file
217
+ */
218
+ path: string;
219
+ source: string;
220
+ };
221
+ type UUID = string;
222
+ type CacheStore = {
223
+ id: UUID;
224
+ file: File;
225
+ status: Status;
226
+ };
227
+ type Status = 'new' | 'success' | 'removed';
228
+
229
+ declare class FileManager {
230
+ private cache;
231
+ emitter: EventEmitter;
232
+ events: {
233
+ emitFile: (id: string, file: File) => void;
234
+ emitStatusChange: (file: File) => void;
235
+ emitStatusChangeById: (id: string, status: Status) => void;
236
+ emitSuccess: () => void;
237
+ emitRemove: (id: string, file: File) => void;
238
+ onAdd: (callback: (id: string, file: File) => void) => () => void;
239
+ onStatusChange: (callback: (file: File) => void) => () => void;
240
+ onStatusChangeById: (id: string, callback: (status: Status) => void) => () => void;
241
+ onSuccess: (callback: () => void) => () => void;
242
+ onRemove: (id: string, callback: (file: File) => void) => () => void;
243
+ };
244
+ constructor();
245
+ private getCache;
246
+ private getCacheByPath;
247
+ private getCountByStatus;
248
+ get files(): File[];
249
+ add(file: File): Promise<File>;
250
+ addOrAppend(file: File): Promise<File>;
251
+ setStatus(id: UUID, status: Status): void;
252
+ get(id: UUID): File | undefined;
253
+ remove(id: UUID): void;
254
+ write(...params: Parameters<typeof write>): Promise<void>;
255
+ read(...params: Parameters<typeof read>): Promise<string>;
256
+ }
257
+
258
+ declare class TreeNode<T = unknown> {
259
+ data: T;
260
+ parent?: TreeNode<T>;
261
+ children: Array<TreeNode<T>>;
262
+ constructor(data: T, parent?: TreeNode<T>);
263
+ addChild(data: T): any;
264
+ find(data: T): any;
265
+ leaves(): this[];
266
+ root(): any;
267
+ forEach(callback: (treeNode: TreeNode<T>) => void): this;
268
+ static build(path: string, options?: DirectoryTreeOptions): TreeNode<{
269
+ name: string;
270
+ path: string;
271
+ type: "file" | "directory";
272
+ }> | undefined;
273
+ }
274
+
275
+ type BuildOutput = {
276
+ fileManager: FileManager;
277
+ };
278
+ type Spinner = {
279
+ start: (text?: string) => Spinner;
280
+ succeed: (text: string) => Spinner;
281
+ fail: (text?: string) => Spinner;
282
+ stopAndPersist: (options: {
283
+ text: string;
284
+ }) => Spinner;
285
+ render: () => Spinner;
286
+ text: string;
287
+ info: (text: string) => Spinner;
288
+ };
289
+ type Logger = {
290
+ log: (message: string, logLevel: LogLevel) => void;
291
+ spinner?: Spinner;
292
+ };
293
+ type BuildOptions = {
294
+ config: PluginContext['config'];
295
+ mode: 'development' | 'production';
296
+ logger?: Logger;
297
+ };
298
+ type KubbBuild = (options: BuildOptions) => Promise<BuildOutput>;
299
+ declare function build(options: BuildOptions): Promise<BuildOutput>;
300
+
301
+ /**
302
+ * Type helper to make it easier to use kubb.config.ts
303
+ * accepts a direct {@link KubbConfig} object, or a function that returns it.
304
+ * The function receives a {@link ConfigEnv} object that exposes two properties:
305
+ */
306
+ declare const defineConfig: (options: MaybePromise<KubbUserConfig<false>> | ((cliOptions: CLIOptions) => MaybePromise<KubbUserConfig>)) => MaybePromise<KubbUserConfig<false>> | ((cliOptions: CLIOptions) => MaybePromise<KubbUserConfig>);
307
+
308
+ type KubbPluginFactory<T extends PluginFactoryOptions = PluginFactoryOptions> = (options: T['options']) => T['nested'] extends true ? Array<KubbPlugin<T>> : KubbPlugin<T>;
309
+ declare function createPlugin<T extends PluginFactoryOptions = PluginFactoryOptions>(factory: KubbPluginFactory<T>): (options: T['options']) => T["nested"] extends true ? KubbPlugin<T>[] : KubbPlugin<T>;
310
+ type Options = {
311
+ config: PluginContext['config'];
312
+ fileManager: FileManager;
313
+ resolveId: PluginContext['resolveId'];
314
+ load: PluginContext['load'];
315
+ };
316
+ type CorePluginOptions = PluginFactoryOptions<Options, false, PluginContext>;
317
+
318
+ /**
319
+ * Get the type of the first argument in a function.
320
+ * @example Arg0<(a: string, b: number) => void> -> string
321
+ */
322
+ type Argument0<H extends keyof PluginLifecycle> = Parameters<PluginLifecycle[H]>[0];
323
+ type Strategy = 'hookFirst' | 'hookForPlugin' | 'hookParallel' | 'hookReduceArg0' | 'hookSeq';
324
+
325
+ declare const hooks: [keyof PluginLifecycle];
326
+ declare class PluginManager {
327
+ plugins: KubbPlugin[];
328
+ readonly fileManager: FileManager;
329
+ private readonly logger?;
330
+ private readonly config;
331
+ readonly core: KubbPlugin<CorePluginOptions>;
332
+ constructor(config: KubbConfig, options: {
333
+ logger?: Logger;
334
+ });
335
+ resolveId: (params: ResolveIdParams) => Promise<OptionalPath>;
336
+ load: (id: string) => Promise<TransformResult>;
337
+ hookForPlugin<H extends PluginLifecycleHooks>(pluginName: string, hookName: H, parameters: Parameters<PluginLifecycle[H]>, skipped?: ReadonlySet<KubbPlugin> | null): Promise<ReturnType<PluginLifecycle[H]> | null>;
338
+ hookFirst<H extends PluginLifecycleHooks>(hookName: H, parameters: Parameters<PluginLifecycle[H]>, skipped?: ReadonlySet<KubbPlugin> | null): Promise<ReturnType<PluginLifecycle[H]> | null>;
339
+ hookParallel<H extends PluginLifecycleHooks, TOuput = void>(hookName: H, parameters?: Parameters<PluginLifecycle[H]> | undefined): Promise<Awaited<TOuput>[]>;
340
+ hookReduceArg0<H extends PluginLifecycleHooks>(hookName: H, [argument0, ...rest]: Parameters<PluginLifecycle[H]>, reduce: (reduction: Argument0<H>, result: ReturnType<PluginLifecycle[H]>, plugin: KubbPlugin) => MaybePromise<Argument0<H> | null>): Promise<Argument0<H>>;
341
+ hookSeq<H extends PluginLifecycleHooks>(hookName: H, parameters?: Parameters<PluginLifecycle[H]>): Promise<void>;
342
+ private getSortedPlugins;
343
+ /**
344
+ * Run an async plugin hook and return the result.
345
+ * @param hookName Name of the plugin hook. Must be either in `PluginHooks` or `OutputPluginValueHooks`.
346
+ * @param args Arguments passed to the plugin hook.
347
+ * @param plugin The actual pluginObject to run.
348
+ */
349
+ private run;
350
+ /**
351
+ * Run a sync plugin hook and return the result.
352
+ * @param hookName Name of the plugin hook. Must be in `PluginHooks`.
353
+ * @param args Arguments passed to the plugin hook.
354
+ * @param plugin The acutal plugin
355
+ * @param replaceContext When passed, the plugin context can be overridden.
356
+ */
357
+ private runSync;
358
+ private catcher;
359
+ }
360
+
361
+ declare class ValidationPluginError extends Error {
362
+ }
363
+ declare const validatePlugins: (plugins: KubbPlugin[], dependedPluginNames: string | string[]) => true;
364
+
365
+ /**
366
+ * Abstract class that contains the building blocks for plugins to create their own Generator
367
+ * @link idea based on https://github.com/colinhacks/zod/blob/master/src/types.ts#L137
368
+ */
369
+ declare abstract class Generator<TOptions extends object = object> {
370
+ private _options;
371
+ constructor(options?: TOptions);
372
+ get options(): TOptions;
373
+ abstract build(...params: unknown[]): unknown;
374
+ }
375
+
376
+ /**
377
+ * Abstract class that contains the building blocks for plugins to create their own SchemaGenerator
378
+ */
379
+ declare abstract class SchemaGenerator<TOptions extends object, TInput, TOutput> extends Generator<TOptions> {
380
+ abstract build(schema: TInput, name: string, description?: string): TOutput;
381
+ }
382
+
383
+ export { Argument0, CLIOptions, Cache, CacheStore, CorePluginOptions, EmittedFile, File, FileManager, FileName, Generator, KubbBuild, KubbConfig, KubbJSONPlugin, KubbPlugin, KubbPluginKind, KubbUserConfig, LogLevel, LogType, Logger, MaybePromise, OptionalPath, Path, PathMode, PluginContext, PluginFactoryOptions, PluginLifecycle, PluginLifecycleHooks, PluginManager, ResolveIdParams, SchemaGenerator, Status, Strategy, TransformResult, TreeNode, UUID, ValidationPluginError, build, clean, createPlugin, createPluginCache, build as default, defineConfig, format, getPathMode, getRelativePath, hooks, isPromise, read, validatePlugins, write };