Skip to Content
Pet Adopt
Plugins

Writing a plugin

What a plugin is, what it can add to a Pet Adopt site, and the smallest one that works.

A plugin adds features to a Pet Adopt site without changing the site's own code. This section is for plugin authors.

A plugin imports one package, @pet-adopt/plugin-sdk, plus @pet-adopt/ui when it has admin screens. It never imports the site. Everything the site does for a plugin comes through the SDK.

Three plugins in the repository show the whole thing in code:

  • plugins/adoption-tips: the smallest useful plugin. It is an example to copy, and is not on npm. See The example plugin.
  • plugins/form-builder (@pet-adopt/plugin-form-builder): a full one, with tables, server actions, email, settings and the Form page builder block.
  • plugins/instagram-feed (@pet-adopt/plugin-instagram-feed): a plugin built around one page builder block, the Instagram feed, which reads another service and keeps what it read in its own tables.

What a plugin can add

PartKey on the pluginWhat it is
ScreensscreensPages in the admin under /admin/plugins/<name>/, with a menu entry
Dashboard cardsdashboardWidgetsCards on the admin dashboard
Its own tablesstorageDatabase tables with migrations, on SQLite and PostgreSQL
Page builder blocksblockLoaders, plus a block moduleBlocks in the page builder, under Plugins. See Page builder blocks
Default email templatesemailTemplatesTemplates the site creates in Email → Templates and the plugin sends with
An email senderemailSends all of the site's email, in place of the built-in SMTP sender

A plugin can also set title and description, which the admin shows in its list of plugins, and plugins, a list of other plugins it is built from. Those are loaded before it.

The smallest plugin

import { definePlugin } from '@pet-adopt/plugin-sdk'

export const hello = definePlugin({
  name: 'hello',
  title: 'Hello',
  description: 'Adds one screen to the admin.',
  screens: [
    {
      path: '',
      title: 'Hello',
      component: ({ user }) => <p>Hello {user.name}.</p>,
    },
  ],
})

The site lists it in apps/web/pet-adopt.config.ts:

import { defineConfig } from '@pet-adopt/plugin-sdk'
import { hello } from 'pet-adopt-plugin-hello'

export default defineConfig({ plugins: [hello()] })
  • name is lowercase letters, digits and dashes. It is part of the plugin's addresses, and the start of its table names and activity log entries. Two plugins cannot share a name.

  • definePlugin returns a function. The site calls it in the config. You can pass definePlugin the plugin itself, as above, or a function that returns it.

  • Options: pass a function to definePlugin to take options, as the form builder does with formBuilder({ title: 'Applications' }):

    export const hello = definePlugin<{ greeting?: string } | void>((options) => ({
      name: 'hello',
      screens: [{ path: '', title: options?.greeting ?? 'Hello', component: () => null }],
    }))
    // in the config: hello({ greeting: 'Welcome' })

A mistake in a plugin (a bad name, path, icon, template, table or block) stops the site when the config loads, with a message that names the plugin.

Installing the SDK

List the SDK as a peer dependency, so your plugin uses the same copy as the site it is added to, and as a dev dependency to build against:

{
  "peerDependencies": {
    "@pet-adopt/plugin-sdk": "^0.1.0"
  },
  "devDependencies": {
    "@pet-adopt/plugin-sdk": "^0.1.0"
  }
}

A plugin that bundles its own copy would have its errors go unrecognised by the site, because errors are matched by class (see Errors).

react and react-icons are peer dependencies too. drizzle-orm is one as well, and is only needed by a plugin with its own tables.

Versions: the SDK is below 1.0, so a minor version may change the API. Pin the minor version you built against.

What is in the SDK

  • definePlugin, defineConfig: declare a plugin and a site's list of plugins.
  • definePluginBlock, defineBlocks: page builder blocks and a site's list of them.
  • definePluginStorage: a plugin's own tables and migrations, for SQLite and PostgreSQL. Table names start with the plugin's prefix (pluginTablePrefix).
  • host(): the site's services: permission checks, rate limits, templated email, the activity log, encrypted secrets, the privacy policy, the site's address and time zone.
  • Roles and permissions: ROLES, can, accessPermission.
  • Errors and results: UserError, ValidationError, ActionResult.
  • Helpers: formatWhen and the time zone functions, foldText for search, csvCell for safe CSV.

The SDK contains no part of the site itself.

The example plugin

plugins/adoption-tips (@pet-adopt/plugin-adoption-tips) is a small plugin kept in the repository as a starting point. It is not published to npm. Everything is in one file, src/index.tsx.

It adds:

  • A Tips screen at /admin/plugins/adoption-tips, with a Tips entry in the admin's Plugins menu (a react-icons icon).
  • A second screen at /admin/plugins/adoption-tips/tips/<id>, showing how a screen takes a parameter from its address (params.id).
  • A dashboard card (id tip-of-the-day), titled by the heading option below.

It takes one option, heading, the title of the screen and the card (default "Adoption tips"):

import { adoptionTips } from '@pet-adopt/plugin-adoption-tips'

export default defineConfig({
  plugins: [adoptionTips({ heading: 'Tips for new owners' })],
})

To remove it from a site, delete it from plugins in apps/web/pet-adopt.config.ts. To start your own plugin, copy its folder (see Local development). For a plugin with tables, server actions and email, read the form builder.

Rules for plugins

  • Depend only on the SDK (and the site's UI package, @pet-adopt/ui, for admin screens). Never import the site, so the plugin works from npm as well as from a folder.
  • Check permissions in every server action with host().requirePermission(...), wrapped in host().run(...). See Checking in a server action.
  • Log errors with host().logError, never the error object: a database error can carry the values it was given.
  • Ship compiled JavaScript when you publish a plugin to npm, and keep 'use client' and 'use server' as the first line of the files that have them. Plain tsc, file by file, does. Plugins kept as a folder in a site ship TypeScript, and the site compiles them. See Packaging.

The checklist before publishing collects every rule in one list.

On this page