Skip to content
On this page

Plugin

A plugin is a way to decouple logic into smaller parts, defining reusable components across the server.

Defining a plugin is as simple as:

typescript
import { KingWorld } from 'kingworld'

const plugin = (app: KingWorld) => app
    .state('plugin-version', 1)
    .get('/from-plugin', () => 'Hi')

const app = new KingWorld()
    .use(plugin)
    .get('/version', ({ store }) => store['plugin-version'])
    .listen(8080)

Plugin can register by using use.

Registering a plugin will combine types between plugin and current instance, and the scope of hooks, and schema get merged too.

Config

You can customize plugin by creating function to return callback which accepts KingWorld.

typescript
import { KingWorld } from 'kingworld'

const plugin = ({
    prefix: '/v1'
}) => (app: KingWorld) => app
    .get(`/${prefix}/hi`, () => 'Hi')

const app = new KingWorld()
    .use(plugin({
        prefix: '/v2'
    }))
    .listen(8080)

Config type will be inferred into use, generating auto completion and type strict as intend.