Plugin API Reference
Last updated
Was this helpful?
Last updated
Was this helpful?
The plugin system allows for extending the functionality of LearnCard Core. Plugins can implement control planes and add custom methods to a LearnCard instance.
Plugins are defined using the Plugin
type, which takes three type parameters:
Name: A string literal representing the plugin name
Planes: The control planes the plugin implements
Methods: Custom methods the plugin provides
For example, a plugin implementing the Read and Store planes would be typed like:
A plugin providing a custom method would be typed like:
Plugins can be added to a LearnCard instance using the addPlugin
method:
Plugins are provided by community and core contributors. Given the plugin’s nature, they may also host and maintain infrastructure necessary to sustain its call patterns. For example, an IPFS storage provider might also provide a pinning service, a DID document provider might host document endpoints, etc.
These details are to be provided along with the plugin.
If you're creating a plugin, it is highly recommended you use TypeScript and take advantage of the Plugin
type.
Plugin
TypeLet's break down what each of these arguments are doing one at a time.
This argument simply names a plugin. It is a required string, and is used by some Control Planes.
Specifying a name will force objects that have been set to this type to use that name!
any
or never
, which specifies that this plugin does not implement any Control Planes
A single string (such as used in the example above), which specifies that this plugin implements a single Control Plane
This argument defaults to any
, specifying that this plugin does not implement any Control Planes.
Specifying one or more Control Planes implemented will force objects that have been set to this type to implement those planes!
This argument defaults to Record<never, never>
, specifying that this plugin does not implement any methods.
Specifying methods implemented will force objects that have been set to this type to implement those methods!
any
or never
, which specifies that this plugin does not depend on any Control Planes
A single string (such as used in the example above), which specifies that this plugin depends on a single Control Plane
This argument defaults to never
, specifying that this plugin does not depend on any Control Planes.
This argument defaults to Record<never, never>
, specifying that this plugin does not depend on any methods.
To add better type safety to a project using LearnCards, it is highly recommended you use TypeScript and take advantage of the LearnCard
type.
LearnCard
typeIf you know the exact order and number of plugins you have, you can use the LearnCard
type to specify a LearnCard
with those plugins like so:
With this code, CustomLearnCard
will automatically infer all methods and planes implemented by PluginA
, PluginB
, and PluginC
!
If you don't know the exact order and number of plugins you have, but you know that you would like to specify a LearnCard
that implements certain planes or methods, you can do that like this:
Before actually implementing the localStorage functionality, let's build out the skeleton of what our implementation will look like:
Because we specified that this plugin is implementing the read
and store
planes, we must include those keys in the returned Plugin
object. The Read Plane requires we implement a get
method, and the Store Plane requires we implement at least an upload
method, so these methods have been stubbed out.
Where id
is the identifier used as a key in localStorage
.
With our URI scheme defined and ID generation in place, let's implement our Store Plane!
With this code in place, the Store Plane has successfully been implemented! Verifiable Credentials can now be stored by a LearnCard using this plugin with the following code:
Now the we can store credentials in localStorage, let's implement getting them out!
To implement the Read Plane, we simply need to verify that the incoming URI
is one that matches our scheme, and if so, read the value stored in localStorage! This can be done with the following code:
With this in place, the Read and Store planes have been implemented and LearnCards may use our plugin to store and retrieve credentials from localStorage with the following code:
The types above have defined a Plugin with three methods: get
, increment
, and reset
, which will provide basic counter controls.
With the above types in place, we can build out a skeleton plugin before actually implementing anything:
With our boilerplate out of the way, implementing the Counter plugin will be a cakewalk! 🍰
We will use the lexical scope of the getCounterPlugin
function to store out counter state, and manipulate it via the exposed methods.
Our plugin is now complete, and we have successfully implemented bespoke method. Easy as 🍰.
Our plugin can be added to and used in a LearnCard like so:
The Implicit LearnCard allows your Plugin's methods to access an up-to-date version of the LearnCard that it has been added to. This means that you can have access to a full LearnCard without having to wrap your Plugin in a function!
There are a few use-cases for using the Implicit LearnCard, such as:
Calling a method that is implemented in the same plugin
Ensuring the most up-to-date method is called
The implementation for this plugin can have generateFullName
easily call generateFirstName
and generateLastName
without having to define them outside of the function thanks to the Implicit LearnCard:
While this example may be a bit contrived, it does demonstrate a few important benefits of the Implicit LearnCard:
We were able to reuse plugin methods without defining them outside the plugin
Other plugins are now able to override the functionality of generateFirstName
and generateLastName
and generateFullName
will automatically call the overriden methods!
This allows plugins to easily define interfaces for sub-plugins or plugin extensions.
This also gives plugins the ability to monkey-patch pieces of another plugin, enhancing or changing that earlier plugin's functionality
The Implicit LearnCard can be very handy for plugin extensibility and composition. However, there are times you don't want a plugin to be able to be monkey-patched or extended. In such a case, it is a better idea not to use the Implicit LearnCard, and just define the re-usable functions outside the plugin:
The VerifyExtension
type defines one method: verifyCredential
that takes in a Verifiable Credential and returns a VerificationCheck
object. To add our extension, we depend on a LearnCard that already has the verifyCredential
function (using the same VerifyExtension
type!), then call the old verifyCredential
function at the top of our new verifyCredential
function.
This pattern allows any number of plugins to add extra verification logic to the verifyCredential
function easily!
Plugin dependence comes in two flavors:
To demonstrate this, let's create a simple base plugin, as well as two plugins that we will depend on: one for Control Planes, and one for methods.
Let's update our base plugin to see what that looks like:
With this change, LearnCards that would like to add our plugin will now look slightly different:
The operative change is right here on line 4:
The operative change is, once again, on line 4:
Because we haven't added our dependencies to the DependencePlugin
type itself, TS has no way of knowing that the Implicit LearnCard implements the ID Plane and the foo
method! We can easily fix this by updating the DependencePlugin
type:
With these in place, TS will know to add the ID Plane and the foo
method to the Implicit LearnCard, and the above errors will go away!
To demonstrate this, let's build a quick secret message plugin that gates a string behind a password. This plugin will use a constructor function that takes in a message and a password, exposing a getMessage
method that will return the message if the correct password is passed in and changePassword
/changeMessage
methods that allow updating the password/message.
This plugin can be used like so:
If you're looking for a guide on creating a plugin, check-out the guide:
The Plugin
type is a type, taking in up to five total generic parameters. A Plugin using all five parameters would look like this:
This type describes a plugin named 'Complex'
that implements the , as well as the method foo
. This plugin is also on plugins that implement the Control Plane, as well as the method bar.
This argument specifies which the plugin implements. It can be:
A of strings (e.g. 'store' | 'read'
), which specifies that this plugin implements multiple Control Planes
This argument specifies which the plugin implements. It is an object whose values are functions.
This argument specifies which the plugin depends on. It can be:
A of strings (e.g. 'store' | 'read'
), which specifies that this plugin depends on multiple Control Planes
Specifying one or more Dependent Control Planes will add those planes to the passed into each plugin method!
Specifying Dependent Planes here will not force LearnCards to implement those planes when adding this plugin! Please see for more information.
This argument specifies which the plugin depends on. It is an object whose values are functions.
Specifying dependent methods will add those methods to the passed into each plugin method!
Specifying Dependent Methods here will not force LearnCards
to implement those methods when adding this plugin! Please see for more information.
With this code, ImplementsIdPlane
will accept any LearnCard
that has plugins that implement the , ImplementsFoo
will accept any LearnCard
that implements a method named foo
that returns 'bar'
, and ImplementsBoth
will accept any LearnCard
that implements both.
In order to promote convergence across Plugin APIs to support common functionality over complex workflows, plugins may choose to implement . Because each plane is slightly different, how they are actually implemented will also be slightly different. For the most part, there are some standard conventions that plugins must follow when implementing methods for a plane.
To demonstrate this, let's build a plugin that implements the and planes using .
Let's start with the types! We will use to define a Plugin that implements the Read and Store planes like so:
Let's start by implementing the Store Plane! The mention that the upload
method should return a , so we will now devise a URI scheme. It looks like this:
To easily create unique identifiers, we will use the npm package. Creating an ID using that package looks like this:
Wondering what that unused _learnCard
variable is about? It's ! Check out what it is and how to use it !
Sometimes plugins need to expose some bespoke logic that doesn't fit neatly into one of the . Plugin methods allow plugins to expose this logic directly on the resulting LearnCard object.
We have already seen this in action in , but let's go into a bit more depth about what's happening here by making a quick plugin that implements a basic counter.
Before implementing methods on a Plugin object, it's best to get the types in order. In general, starting with the types can be easier to think through, and once they're in place, they can guide the implementation. To add types for methods, we use the third generic argument of .
When implementing or , you may have noticed This is what we call the Implicit LearnCard, and it can be very helpful!
Let's implement a quick plugin that generates names to demonstrate this. The plugin will expose three methods: generateFirstName
, generateLastName
, and generateFullName
. The types for this plugin look like this (using ):
Another reason not to use the Implicit LearnCard is when you specifically want an old version of a method you are overriding. To demonstrate this, let's build a quick
Building a Verification Extension is super easy with the VerifyExtension
type coming from the :
See for more information about how we are depending on a LearnCard with the verifyCredential
method here.
While it is useful for to add its own isolated logic to a LearnCard, part of the beauty of LearnCard plugins is to depend on other plugins 💪
Depending on a
Depending on one or more .
As a convention, plugins will often be wrapped inside of a constructor function that requires a of a certain type be passed in.
Now that we're requiring a LearnCard be passed in, we are able to add requirements to the dependent LearnCard! Let's take a look at what that looks like by attempting to take a dependency on the .
This change allows us to call learnCard.id.did
on line 5, and requires consumers of this plugin to pass in a LearnCard that the plane when adding this plugin. For example, all of the following code will throw errors:
Depending on a specific method (or methods) rather than a Control Plane looks very similar. To demonstrate this, let's stop depending on the ID Plane for a moment, and instead just depend on the foo
method from the .
With this change in place, just like when we depended on a Control Plane, we are now able to call learnCard.invoke.foo
on line 5. We also now require consumers of this plugin to pass in a LearnCard with a plugin that the foo
method. For example, all of the following code will throw errors:
Thus far, when adding dependency requirements, we have only added type safety to the argument of our constructor function. This works quite well, but does not provide type safety to the passed into every method. To add this type safety, we use the fourth and fifth generic arguments of
Let's demonstrate this by first depending on both the and the , then adding some logic that uses the Implicit LearnCard to take advantage of that dependency:
Sometimes it is important for a Plugin to keep private state/data. This can be done using the lexical scope of the constructor function described in !