Skip to content

Commit

Permalink
docs: add docs for plugin core
Browse files Browse the repository at this point in the history
  • Loading branch information
stijnvanhulle committed Dec 4, 2023
1 parent 2150e07 commit d00fd46
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/blog/v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ First of all, have a look at our [GitHub issue](https://github.com/kubb-project/
- Use of TypeScript v5.3.0.
- Support for MSW v2.
- Support for @tanstack/query v5.
- Infer with `@kubb/swagger-ts/oas`

### Exclude and include

Expand Down
3 changes: 1 addition & 2 deletions docs/config/hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ Hooks that will be called when a specific action is triggered in Kubb.
Hook that will be triggered at the end of Kubb's generation.<br/>
Useful for running Prettier or ESLint to format/lint your code.

::: info
Type: `string | string[]` <br/>
- **Type:** `string | string[]` <br/>

::: code-group

Expand Down
2 changes: 1 addition & 1 deletion docs/config/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ outline: deep
This page is a reference to the different options you can use for configuring your Kubb config.
By setting the following options you can override the default behaviour of Kubb and even extend it with your own plugins.

::: info Options
::: info INDEX

[name](/config/name)
<br/>
Expand Down
67 changes: 67 additions & 0 deletions docs/plugins/development/core.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,70 @@ outline: deep
:::

This section describes the core plugin types and APIs.

## Context/this

When calling `this.` in a plugin, the `api` of the core plugin will be used.
::: details plugin.ts
<<< @/../packages/core/src/plugin.ts
:::

### this.config

The current config, see `kubb.config.ts`.

- **Type:** `KubbConfig` <br/>

### this.plugins

Return all the plugins that are getting used.

- **Type:** `Array<KubbPlugin>` <br/>

### this.plugin

The current plugin.

- **Type:** `KubbPlugin` <br/>

### this.logger

Instance of the Logger.

- **Type:** `Logger` <br/>

### this.fileManager

Instance of the fileManager.

- **Type:** `FileManager` <br/>

### this.pluginManager

Instance of the pluginManager.

- **Type:** `PluginManager` <br/>

### this.addFile

Add a file to the current fileManager.

- **Type:** `(...files: Array<KubbFile.File>): Promise<Array<KubbFile.File>>` <br/>

### this.resolvePath

This will call pluginManager.resolvePath, see [Pluginmanager and resolving a path](/reference/pluginManager/#pluginmanager-resolvepath).

- **Type:** `(params: ResolvePathParams) => KubbFile.OptionalPath` <br/>

### this.resolveName

This will call pluginManager.resolveName, see [Pluginmanager and resolving a name](/reference/pluginManager/#pluginmanager-resolvename).

- **Type:** `(params: ResolveNameParams) => string` <br/>

### this.cache

Store something in the cache, this cache can be used in every plugin.

- **Type:** `Cache<PluginCache>` <br/>
80 changes: 79 additions & 1 deletion docs/plugins/development/system.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Our plugin system is based on the following packages:
- [Unplugin](https://github.com/unjs/unplugin)
- [Snowpack](https://www.snowpack.dev/guides/plugins)

## Developing Plugins
## Setup

Plugins provide a function similar to `(options?: PluginOptions) => KubbUserPluginWithLifeCycle` as an entry point.

Expand Down Expand Up @@ -130,6 +130,84 @@ export type PluginFactoryOptions<Name, Options, ResolveOptions, API, ResolvePath

[`plugin-template`](https://github.com/kubb-project/plugin-template) is a minimal Kubb plugin template repository that you can use as a basis for developing your Kubb plugin.

## Configure

To get started with creating plugins you need to set some options for the `PluginManager`. <br>
More info about the lifecyle: [PluginManager Lifecycle](/reference/pluginManager/lifecycle)<br/>
More info about the `PluginContext` or `this`: [Plugin Core](/plugins/development/core)

::: tip
When using type PluginOptions with `PluginFactoryOptions` you will already receive better types, name will be what you have defined as first parameter to `PluginFactoryOptions` instead of string.
:::

- **Type:** `KubbUserPluginWithLifeCycle` <br/>

### name

Name of your plugin

- **Type:** `string` <br/>

### options

Specify some options here that you want to expose for other plugins or for internal use.

- **Type:** `object` <br/>

### pre

Which plugin(s) should be executed before the current one.

- **Type:** `string[]` <br/>

### post

Which plugin(s) should be executed after the current one.

- **Type:** `string[]` <br/>

### api

Add some extra functionality to your plugin, here you can even using functions which is not possible with `options`.

- **Type:** `(this: Omit<PluginContext, "addFile">) => object` <br/>

### resolvePath

This will be called when pluginManager.resolvePath is called, see [Pluginmanager and resolving a path](/reference/pluginManager/#pluginmanager-resolvepath).

- **Type:** `(this: PluginContext, baseName: string, directory?: string | undefined, options?: object) => KubbFile.OptionalPath` <br/>

### resolveName

This will be called when pluginManager.resolveName is called, see [Pluginmanager and resolving a name](/reference/pluginManager/#pluginmanager-resolvename).

- **Type:** `(this: PluginContext, name: string, type?: "function" | "type" | "file" | undefined) => string)` <br/>

### buildStart

This will be called when the build starts, see [Lifecycle](/plugins/development/system#lifecycle).

- **Type:** `(this: PluginContext, kubbConfig: KubbConfig) => PossiblePromise<void>` <br/>

### buildEnd

This will be called when the build is done, see [Lifecycle](/plugins/development/system#lifecycle).

- **Type:** `(this: PluginContext) => PossiblePromise<void>` <br/>

### writeFile

This will be called when a new file is ready to be written to the fileSystem, see [Lifecycle](/plugins/development/system#lifecycle).

- **Type:** `(this: Omit<PluginContext, "addFile">, source: string | undefined, path: string) => PossiblePromise<string | void>` <br/>

### transform

This will be called just before we call writeFile, see [Lifecycle](/plugins/development/system#lifecycle). Here you can override the source/content of a file.

- **Type:** `(this: Omit<PluginContext, "addFile">, source: string, path: string) => PossiblePromise<TransformResult>` <br/>

## Lifecycle

The moment that `build` of `@kubb/core` is called or you trigger the CLI the following things will happen:
Expand Down
11 changes: 11 additions & 0 deletions docs/reference/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,14 @@ outline: deep
---

# Reference

::: info INDEX

[FileManager](/reference/fileManager)
<br/>
[PluginManager](/reference/pluginManager/)
<br/>
[Templates](/reference/templates)
<br/>

:::
2 changes: 1 addition & 1 deletion packages/core/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Options = {
resolvePath: PluginContext['resolvePath']
resolveName: PluginContext['resolveName']
logger: PluginContext['logger']
getPlugins: () => KubbPlugin[]
getPlugins: () => Array<KubbPlugin>
plugin?: PluginContext['plugin']
}

Expand Down

0 comments on commit d00fd46

Please sign in to comment.