UMI Plugins
Umi Plugins
Whilst Umi is a small zero-dependency framework, it is designed to be extended with plugins. Plugins allow us to not only interact with its interfaces or swap out its interface implementations but also to add new features to Umi itself.
Using plugins
To install a Umi plugin, you may simply call the use
method on the Umi instance. This use
method returns the Umi instance so they can be chained together.
It is worth noting that libraries will often provide a function that returns a plugin instead of the plugin itself. This is done so that we can pass any arguments to configure the behaviour of the plugin.
To stay consistent, the plugins provided by Umi always follow this pattern even if they don't require any arguments. Here are some examples:
Creating plugins
Under the hood, Umi defines a plugin as an object with an install
function that can be used to extend the Umi instance however we want.
As mentioned above, it is recommended to export plugin functions so we can request any argument that might be needed from the end user.
What to do in a plugin
Now that we know how to create a plugin, let's have a look at some examples of what we can do with them.
Setting interface implementations
One of the most common use cases for plugins is to assign an implementation to one or several Umi interfaces. Here's an example of setting a fictional MyRpc
implementation to the rpc
interface. Notice how we can pass the Umi instance to the MyRpc
implementation so it can rely on other interfaces if needed.
Decorating interface implementations
Another way of setting interface implementations is to decorate existing ones. This allows the end-user to compose plugins together by adding extra functionality to existing implementations without worrying about their underlying implementation details.
Here's an example of a plugin that decorates the rpc
interface such that it logs all sent transactions to a third-party service.
Creating bundles
Since plugins can also call the use
method on the Umi instance, it is possible to install plugins within plugins. This allows us to create bundles of plugins that can be installed together.
For instance, this is how Umi's "defaults" plugin bundle is defined:
Using interfaces
On top of setting and updating Umi's interfaces, plugins can also use them. One common use case for this is to allow libraries to register new programs to the program repository interfaces. Here's an example illustrating how the Token Metadata library registers its program. Notice how it sets the override
argument to false
so that the program is only registered if it doesn't already exist.
Extending the Umi instance
Last but not least, plugins can also extend the feature set of the Umi instance. This allows libraries to provide their own interfaces, extend existing ones, etc.
A good example of that is the Candy Machine library which stores all candy guards in a repository — much like the program repository. This allows end-users to register their own guards so they can be recognised when creating, fetching and minting from candy machines with associated candy guards. To make this work, the library adds a new guards
property to the Umi instance and assigns a new guard repository to it.
The slight issue with the code above is that the Umi
type no longer reflects the actual instance. That is, TypeScript will complain that the guards
property doesn't exist on the Umi
type. To fix this, we can use TypeScript's Module Augmentation to extend the Umi
type so it includes the new property like so
This module augmentation can also be used to extend an existing interface. For instance, we could assign a new RPC interface that contains additional methods whilst letting TypeScript know about our added methods like so.
Last updated