Plugins


Plugins are used to extend the functionality of Kilpi.

Available plugins

List of all available plugins.

Audit Plugin

Utility for auditing your authorizations.

Endpoint Plugin

Create a web standard endpoint for requesting authorization decisions, used with the Kilpi client.

React Server Components Plugin

React Server Components for working with Kilpi and automatic RSC scope.

React Client Plugin

React Client components for working with Kilpi client.

Extending KilpiCore or KilpiClient

Plugins can extend the KilpiCore or KilpiClient instance with new methods. For example, the audit plugin adds a flush() method under the Kilpi.audit namespace.

const Kilpi = await createKilpi({
...,
plugins: [AuditPlugin()],
});
await Kilpi.audit.flush();

Creating custom plugins

Creating custom plugins is easy with the createKilpiPlugin and createKilpiClientPlugin methods. The respectively take in the KilpiCore or KilpiClient instance and return the public interface for the plugin (or {} if no public interface).

Example server-side plugin

Let’s create an utility plugin for counting the total number of authorizations as an example.

// 1. CREATE A PLUGIN
// Create generic function for instantiating your plugin
export function AuthorizationCounterPlugin<T extends AnyKilpiCore>(
// Allow for configuring the plugin's functionality
options: { initialValue: number }
) {
// All plugins must return `createKilpiPlugin(...)`
return createKilpiPlugin<T>((Kilpi: T) => {
// Your plugin logic here: Create state, call hooks, etc.
let count = options.initialValue;
Kilpi.hooks.onAfterAuthorization(() => {
count += 1;
});
// Return the public interface and methods (or {}) for the plugin.
// The methods should be namespaced, as done here with the
// `Kilpi.authorizationCounter` namespace.
return {
authorizationCounter: {
// Custom method
get() {
return count;
},
}
}
});
}
// 2. USE THE PLUGIN
const Kilpi = createKilpi({
...,
plugins: [
// Apply and configure the plugin
AuthorizationCounterPlugin({ initialValue: 10 })
],
});
// Use custom methods if any provided
Kilpi.authorizationCounter.get();

Learn more about hooks

Learn how to use hooks to extend Kilpi and create custom, advanced functionality.

Example client-side plugin

Similarly, let’s create a counter plugin for the client-side KilpiClient instance.

Hooks for KilpiClient are upcoming.

// 1. CREATE A PLUGIN
// Create generic function for instantiating your plugin
export function AuthorizationCounterClientPlugin<T extends AnyKilpiCore>(
// Allow for configuring the plugin's functionality
options: { initialValue: number }
) {
// All plugins must return `createKilpiClientPlugin(...)`
return createKilpiClientPlugin<T>((KilpiClient: T) => {
// Your plugin logic here: Create state, call hooks, etc.
let count = options.initialValue;
// Return the public interface and methods (or {}) for the plugin.
// The methods should be namespaced, as done here with the
// `KilpiClient.authorizationCounter` namespace.
return {
authorizationCounter: {
// Custom methods
get() {
return count;
},
increment() {
count += 1;
},
}
}
});
}
// 2. USE THE PLUGIN
const KilpiClient = createKilpiClient({
...,
plugins: [
// Apply and configure the plugin
AuthorizationCounterClientPlugin({ initialValue: 10 })
],
});
// Use custom methods if any provided
KilpiClient.authorizationCounter.increment();
KilpiClient.authorizationCounter.get(); // 11