@kubb/core 2.0.0-alpha.4 → 2.0.0-alpha.5

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/src/types.ts CHANGED
@@ -1,9 +1,12 @@
1
+ import type { PossiblePromise } from '@kubb/types'
1
2
  import type { FileManager, KubbFile } from './FileManager.ts'
2
3
  import type { OptionsPlugins, PluginUnion } from './index.ts'
3
4
  import type { PluginManager } from './PluginManager.ts'
4
5
  import type { Cache } from './utils/cache.ts'
5
6
  import type { Logger, LogLevel } from './utils/logger.ts'
6
7
 
8
+ // config
9
+
7
10
  /**
8
11
  * Config used in `kubb.config.js`
9
12
  *
@@ -12,20 +15,22 @@ import type { Logger, LogLevel } from './utils/logger.ts'
12
15
  * ...
13
16
  * })
14
17
  */
15
- export type KubbUserConfig = Omit<KubbConfig, 'root' | 'plugins'> & {
16
- /**
17
- * Project root directory. Can be an absolute path, or a path relative from
18
- * the location of the config file itself.
19
- * @default process.cwd()
20
- */
21
- root?: string
22
- /**
23
- * Plugin type can be KubbJSONPlugin or KubbPlugin
24
- * Example: ['@kubb/swagger', { output: false }]
25
- * Or: createSwagger({ output: false })
26
- */
27
- plugins?: Array<Omit<KubbUserPlugin, 'api'> | KubbUnionPlugins | [name: string, options: object]>
28
- }
18
+ export type KubbUserConfig =
19
+ & Omit<KubbConfig, 'root' | 'plugins'>
20
+ & {
21
+ /**
22
+ * Project root directory. Can be an absolute path, or a path relative from
23
+ * the location of the config file itself.
24
+ * @default process.cwd()
25
+ */
26
+ root?: string
27
+ /**
28
+ * Plugin type can be KubbJSONPlugin or KubbPlugin
29
+ * Example: ['@kubb/swagger', { output: false }]
30
+ * Or: createSwagger({ output: false })
31
+ */
32
+ plugins?: Array<Omit<UnknownKubbUserPlugin, 'api'> | KubbUnionPlugins | [name: string, options: object]>
33
+ }
29
34
 
30
35
  export type InputPath = {
31
36
  /**
@@ -116,15 +121,6 @@ export type CLIOptions = {
116
121
  logLevel?: LogLevel
117
122
  }
118
123
 
119
- export type BuildOutput = {
120
- files: FileManager['files']
121
- pluginManager: PluginManager
122
- /**
123
- * Only for safeBuild
124
- */
125
- error?: Error
126
- }
127
-
128
124
  // plugin
129
125
 
130
126
  export type KubbPluginKind = 'schema' | 'controller'
@@ -133,6 +129,53 @@ export type KubbUnionPlugins = PluginUnion
133
129
 
134
130
  export type KubbObjectPlugin = keyof OptionsPlugins
135
131
 
132
+ export type PluginFactoryOptions<
133
+ /**
134
+ * Name to be used for the plugin, this will also be used for they key.
135
+ */
136
+ TName extends string = string,
137
+ /**
138
+ * @type "schema" | "controller"
139
+ */
140
+ TKind extends KubbPluginKind = KubbPluginKind,
141
+ /**
142
+ * Options of the plugin.
143
+ */
144
+ TOptions extends object = object,
145
+ /**
146
+ * Options of the plugin that can be used later on, see `options` inside your plugin config.
147
+ */
148
+ TResolvedOptions extends object = TOptions,
149
+ /**
150
+ * API that you want to expose to other plugins.
151
+ */
152
+ TAPI = any,
153
+ /**
154
+ * When calling `resolvePath` you can specify better types.
155
+ */
156
+ TResolvePathOptions extends object = object,
157
+ /**
158
+ * When using @kubb/react(based on React) you can specify here which types should be used when calling render.
159
+ * Always extend from `AppMeta` of the core.
160
+ */
161
+ TAppMeta = unknown,
162
+ > = {
163
+ name: TName
164
+ kind: TKind
165
+ /**
166
+ * Same behaviour like what has been done with `QueryKey` in `@tanstack/react-query`
167
+ */
168
+ key: [kind: TKind | undefined, name: TName | string, identifier?: string | number]
169
+ options: TOptions
170
+ resolvedOptions: TResolvedOptions
171
+ api: TAPI
172
+ resolvePathOptions: TResolvePathOptions
173
+ appMeta: {
174
+ pluginManager: PluginManager
175
+ plugin: KubbPlugin<PluginFactoryOptions<TName, TKind, TOptions, TResolvedOptions, TAPI, TResolvePathOptions, TAppMeta>>
176
+ } & TAppMeta
177
+ }
178
+
136
179
  export type GetPluginFactoryOptions<TPlugin extends KubbUserPlugin> = TPlugin extends KubbUserPlugin<infer X> ? X : never
137
180
 
138
181
  export type KubbUserPlugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions> =
@@ -150,14 +193,13 @@ export type KubbUserPlugin<TOptions extends PluginFactoryOptions = PluginFactory
150
193
  /**
151
194
  * Options set for a specific plugin(see kubb.config.js), passthrough of options.
152
195
  */
153
- options: TOptions['options'] extends never ? undefined : TOptions['options']
196
+ options: TOptions['resolvedOptions']
154
197
  }
155
- & Partial<PluginLifecycle<TOptions>>
156
198
  & (TOptions['api'] extends never ? {
157
199
  api?: never
158
200
  }
159
201
  : {
160
- api: (this: TOptions['name'] extends 'core' ? null : Omit<PluginContext, 'addFile'>) => TOptions['api']
202
+ api: (this: TOptions['name'] extends 'core' ? null : Omit<PluginContext<TOptions>, 'addFile'>) => TOptions['api']
161
203
  })
162
204
  & (TOptions['kind'] extends never ? {
163
205
  kind?: never
@@ -172,6 +214,10 @@ export type KubbUserPlugin<TOptions extends PluginFactoryOptions = PluginFactory
172
214
  kind: TOptions['kind']
173
215
  })
174
216
 
217
+ export type KubbUserPluginWithLifeCycle<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = KubbUserPlugin<TOptions> & PluginLifecycle<TOptions>
218
+
219
+ type UnknownKubbUserPlugin = KubbUserPlugin<PluginFactoryOptions<any, any, any, any, any, any, any>>
220
+
175
221
  export type KubbPlugin<TOptions extends PluginFactoryOptions = PluginFactoryOptions> =
176
222
  & {
177
223
  /**
@@ -187,19 +233,18 @@ export type KubbPlugin<TOptions extends PluginFactoryOptions = PluginFactoryOpti
187
233
  /**
188
234
  * Options set for a specific plugin(see kubb.config.js), passthrough of options.
189
235
  */
190
- options: TOptions['options'] extends never ? undefined : TOptions['options']
236
+ options: TOptions['resolvedOptions']
191
237
  /**
192
238
  * Kind/type for the plugin
193
239
  * Type 'schema' can be used for JSON schema's, TypeScript types, ...
194
240
  * Type 'controller' can be used to create generate API calls, React-Query hooks, Axios controllers, ...
195
241
  * @default undefined
196
242
  */
197
- kind?: KubbPluginKind
243
+ kind?: TOptions['kind']
198
244
  /**
199
245
  * Define an api that can be used by other plugins, see `PluginManager' where we convert from `KubbUserPlugin` to `KubbPlugin`(used when calling `createPlugin`).
200
246
  */
201
247
  }
202
- & PluginLifecycle<TOptions>
203
248
  & (TOptions['api'] extends never ? {
204
249
  api?: never
205
250
  }
@@ -207,73 +252,53 @@ export type KubbPlugin<TOptions extends PluginFactoryOptions = PluginFactoryOpti
207
252
  api: TOptions['api']
208
253
  })
209
254
 
210
- // use of type objects
211
-
212
- export type PluginFactoryOptions<
213
- Name = string,
214
- Kind extends KubbPluginKind = KubbPluginKind | never,
215
- Options = unknown | never,
216
- Nested extends boolean = false,
217
- API = unknown | never,
218
- resolvePathOptions = Record<string, unknown>,
219
- > = {
220
- name: Name
221
- kind: Kind
222
- /**
223
- * Same like `QueryKey` in `@tanstack/react-query`
224
- */
225
- key: [kind: Kind | undefined, name: Name, identifier?: string | number]
226
- options: Options
227
- nested: Nested
228
- api: API
229
- resolvePathOptions: resolvePathOptions
230
- }
255
+ export type KubbPluginWithLifeCycle<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = KubbPlugin<TOptions> & PluginLifecycle<TOptions>
231
256
 
232
257
  export type PluginLifecycle<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = {
233
258
  /**
234
259
  * Valdiate all plugins to see if their depended plugins are installed and configured.
235
260
  * @type hookParallel
236
261
  */
237
- validate?: (this: Omit<PluginContext, 'addFile'>, plugins: NonNullable<KubbConfig['plugins']>) => PossiblePromise<true>
262
+ validate?: (this: Omit<PluginContext<TOptions>, 'addFile'>, plugins: NonNullable<KubbConfig['plugins']>) => PossiblePromise<true>
238
263
  /**
239
264
  * Start of the lifecycle of a plugin.
240
265
  * @type hookParallel
241
266
  */
242
- buildStart?: (this: PluginContext, kubbConfig: KubbConfig) => PossiblePromise<void>
267
+ buildStart?: (this: PluginContext<TOptions>, kubbConfig: KubbConfig) => PossiblePromise<void>
243
268
  /**
244
269
  * Resolve to a Path based on a baseName(example: `./Pet.ts`) and directory(example: `./models`).
245
270
  * Options can als be included.
246
271
  * @type hookFirst
247
272
  * @example ('./Pet.ts', './src/gen/') => '/src/gen/Pet.ts'
248
273
  */
249
- resolvePath?: (this: PluginContext, baseName: string, directory?: string, options?: TOptions['resolvePathOptions']) => KubbFile.OptionalPath
274
+ resolvePath?: (this: PluginContext<TOptions>, baseName: string, directory?: string, options?: TOptions['resolvePathOptions']) => KubbFile.OptionalPath
250
275
  /**
251
276
  * Resolve to a name based on a string.
252
277
  * Useful when converting to PascalCase or camelCase.
253
278
  * @type hookFirst
254
279
  * @example ('pet') => 'Pet'
255
280
  */
256
- resolveName?: (this: PluginContext, name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string
281
+ resolveName?: (this: PluginContext<TOptions>, name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string
257
282
  /**
258
283
  * Makes it possible to run async logic to override the path defined previously by `resolvePath`.
259
284
  * @type hookFirst
260
285
  */
261
- load?: (this: Omit<PluginContext, 'addFile'>, path: KubbFile.Path) => PossiblePromise<TransformResult | null>
286
+ load?: (this: Omit<PluginContext<TOptions>, 'addFile'>, path: KubbFile.Path) => PossiblePromise<TransformResult | null>
262
287
  /**
263
288
  * Transform the source-code.
264
289
  * @type hookReduceArg0
265
290
  */
266
- transform?: (this: Omit<PluginContext, 'addFile'>, source: string, path: KubbFile.Path) => PossiblePromise<TransformResult>
291
+ transform?: (this: Omit<PluginContext<TOptions>, 'addFile'>, source: string, path: KubbFile.Path) => PossiblePromise<TransformResult>
267
292
  /**
268
293
  * Write the result to the file-system based on the id(defined by `resolvePath` or changed by `load`).
269
294
  * @type hookParallel
270
295
  */
271
- writeFile?: (this: Omit<PluginContext, 'addFile'>, source: string | undefined, path: KubbFile.Path) => PossiblePromise<string | void>
296
+ writeFile?: (this: Omit<PluginContext<TOptions>, 'addFile'>, source: string | undefined, path: KubbFile.Path) => PossiblePromise<string | void>
272
297
  /**
273
298
  * End of the plugin lifecycle.
274
299
  * @type hookParallel
275
300
  */
276
- buildEnd?: (this: PluginContext) => PossiblePromise<void>
301
+ buildEnd?: (this: PluginContext<TOptions>) => PossiblePromise<void>
277
302
  }
278
303
 
279
304
  export type PluginLifecycleHooks = keyof PluginLifecycle
@@ -282,7 +307,7 @@ export type PluginParameter<H extends PluginLifecycleHooks> = Parameters<Require
282
307
 
283
308
  export type PluginCache = Record<string, [number, unknown]>
284
309
 
285
- export type ResolvePathParams<TOptions = Record<string, unknown>> = {
310
+ export type ResolvePathParams<TOptions = object> = {
286
311
  pluginKey?: KubbPlugin['key']
287
312
  baseName: string
288
313
  directory?: string | undefined
@@ -295,16 +320,16 @@ export type ResolvePathParams<TOptions = Record<string, unknown>> = {
295
320
  export type ResolveNameParams = {
296
321
  name: string
297
322
  pluginKey?: KubbPlugin['key']
298
- type?: 'file' | 'function'
323
+ type?: 'file' | 'function' | 'type'
299
324
  }
300
325
 
301
- export type PluginContext<TOptions = Record<string, unknown>> = {
326
+ export type PluginContext<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = {
302
327
  config: KubbConfig
303
328
  cache: Cache<PluginCache>
304
329
  fileManager: FileManager
305
330
  pluginManager: PluginManager
306
331
  addFile: (...file: Array<KubbFile.File>) => Promise<Array<KubbFile.File>>
307
- resolvePath: (params: ResolvePathParams<TOptions>) => KubbFile.OptionalPath
332
+ resolvePath: (params: ResolvePathParams<TOptions['resolvePathOptions']>) => KubbFile.OptionalPath
308
333
  resolveName: (params: ResolveNameParams) => string
309
334
  logger: Logger
310
335
  /**
@@ -314,57 +339,8 @@ export type PluginContext<TOptions = Record<string, unknown>> = {
314
339
  /**
315
340
  * Current plugin
316
341
  */
317
- plugin: KubbPlugin
342
+ plugin: KubbPlugin<TOptions>
318
343
  }
319
344
 
320
345
  // null will mean clear the watcher for this key
321
346
  export type TransformResult = string | null
322
-
323
- export type AppMeta = { pluginManager: PluginManager }
324
-
325
- // generic types
326
-
327
- export type Prettify<T> =
328
- & {
329
- [K in keyof T]: T[K]
330
- }
331
- // eslint-disable-next-line @typescript-eslint/ban-types
332
- & {}
333
-
334
- /**
335
- * TODO move to @kubb/types
336
- * @deprecated
337
- */
338
- export type PossiblePromise<T> = Promise<T> | T
339
-
340
- type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never
341
- type LastOf<T> = UnionToIntersection<T extends any ? () => T : never> extends () => infer R ? R : never
342
-
343
- // TS4.0+
344
- type Push<T extends any[], V> = [...T, V]
345
-
346
- // TS4.1+
347
- type TuplifyUnion<T, L = LastOf<T>, N = [T] extends [never] ? true : false> = true extends N ? [] : Push<TuplifyUnion<Exclude<T, L>>, L>
348
- /**
349
- * TODO move to @kubb/types
350
- * @deprecated
351
- */
352
- export type ObjValueTuple<T, KS extends any[] = TuplifyUnion<keyof T>, R extends any[] = []> = KS extends [infer K, ...infer KT]
353
- ? ObjValueTuple<T, KT, [...R, [name: K & keyof T, options: T[K & keyof T]]]>
354
- : R
355
- /**
356
- * TODO move to @kubb/types
357
- * @deprecated
358
- */
359
- export type TupleToUnion<T> = T extends Array<infer ITEMS> ? ITEMS : never
360
-
361
- /**
362
- * TODO move to @kubb/types
363
- * @deprecated
364
- */
365
- type ArrayWithLength<T extends number, U extends any[] = []> = U['length'] extends T ? U : ArrayWithLength<T, [true, ...U]>
366
- /**
367
- * TODO move to @kubb/types
368
- * @deprecated
369
- */
370
- export type GreaterThan<T extends number, U extends number> = ArrayWithLength<U> extends [...ArrayWithLength<T>, ...infer _] ? false : true
@@ -53,7 +53,7 @@ export function createLogger({ logLevel, name, spinner }: Props): Logger {
53
53
  }
54
54
 
55
55
  const info: Logger['warn'] = (message) => {
56
- if (message && spinner) {
56
+ if (message && spinner && logLevel !== LogLevel.silent) {
57
57
  spinner.info(message)
58
58
  logs.push(message)
59
59
  }
@@ -1,4 +1,4 @@
1
- type PossiblePromise<T> = Promise<T> | T
1
+ import type { PossiblePromise } from '@kubb/types'
2
2
 
3
3
  export function isPromise<T>(result: PossiblePromise<T>): result is Promise<T> {
4
4
  return !!result && typeof (result as Promise<unknown>)?.then === 'function'
@@ -5,6 +5,7 @@ import { createIndent } from './indent.ts'
5
5
  import { nameSorter } from './nameSorter.ts'
6
6
  import { searchAndReplace } from './searchAndReplace.ts'
7
7
  import { transformReservedWord } from './transformReservedWord.ts'
8
+ import { trim } from './trim.ts'
8
9
 
9
10
  export const transformers = {
10
11
  combineCodes,
@@ -14,6 +15,7 @@ export const transformers = {
14
15
  transformReservedWord,
15
16
  nameSorter,
16
17
  searchAndReplace,
18
+ trim,
17
19
  JSDoc: {
18
20
  createJSDocBlockText,
19
21
  },
@@ -0,0 +1,3 @@
1
+ export function trim(text: string): string {
2
+ return text.replaceAll(/\n/g, '').trim()
3
+ }