Writing a plugin

A plugin adds to FlowAssembly without changing it: connector types, form fields, pages in the web application, and settings administrators fill in. It is a Python package. An organisation reviews it, pins it in its review record, and switches it on under Admin, Plugins (ADR 0016).

The example plugin in tests/plugins/fa_example_plugin has one of each.

The package

Declare a subclass of flowassembly.plugins.Plugin under the flowassembly.plugins entry point:

[project.entry-points."flowassembly.plugins"]
example-signature = "fa_example_plugin:ExamplePlugin"
from flowassembly.plugins import FormField, Page, Plugin

class ExamplePlugin(Plugin):
    name = "example-signature"            # lower case, digits and hyphens
    title = "Signatures"
    publisher = "Example Microfinance Bank"
    description = "Customers sign on the screen; a page shows today's signatures."
    connector_types = (LedgerNote(),)     # see connector-types.md
    fields = (FormField("signature", "Signature", "signature.js", ("string",), check),)
    pages = (Page("today", "Signatures today", "page.js", section="reports",
                  permission="assurance.reports"),)
    settings_schema = {"type": "object", "properties": {"heading": {"type": "string"}}}
    static = "static"                     # browser files, beside this module

    def api(self, call):                  # the page's own API
        if call.method == "GET" and call.path == "today":
            return {"heading": call.settings.get("heading"), "count": 3}
        return super().api(call)

A form field

script is an ES module in the static folder. FlowAssembly calls its field(fa) with form-js’s building blocks (fa.h, fa.useRef, fa.useState, fa.useEffect, fa.Label, fa.Description, fa.Errors) and registers what it returns:

export function field(fa) {
  function Signature({ field, value, onChange, errors, domId }) { /* ... */ }
  return { type: 'signature', component: Signature,
           config: { label: 'Signature', group: 'basic-input', keyed: true, emptyValue: '' } };
}

The server never trusts the browser: on every submission it calls the field’s check(value), which returns what is wrong, as messages, or nothing. schema_types says which case data types the field can hold, so fa validate can compare a form with its case data.

A page

script exports mount(element, fa). fa.api(path, {method, body}) calls the plugin’s api as the person viewing the page, and returns its answer:

export async function mount(element, fa) {
  const today = await fa.api('today');
  element.textContent = `${today.count} signed so far.`;
}

The page appears in its section (home, reports, build or admin) for people who hold its permission, at /pages/<plugin>/<page>. Its API answers only them.

Reviewing and switching it on

  1. Install the package beside FlowAssembly on every instance and worker.

  2. fa plugins review <name> prints a record entry, with the version and the digest of the installed files. The reviewer completes purpose, review and reviewed, and adds it to the organisation’s record, named by the instance settings (plugins.reviewed).

  3. Under Admin, Plugins, Installed plugins, switch it on; under Plugin settings, fill in its settings. fa plugins list says whether each installed plugin may run.

A changed file, another version, or a plugin missing from the record cannot be switched on, and stops running if already on.