dimah-s3v1.3.0

Plugins

Extend dimahS3 with definePlugin — custom endpoints, lifecycle hooks, and typed context.

Use definePlugin to create reusable server extensions (like db()). Plugins can contribute lifecycle hooks, HTTP endpoints under your basePath, and typed instance context (s3[id]).

lib/s3-audit-plugin.ts
import { definePlugin, createS3Endpoint } from "@dimah-s3/server";

export const auditPlugin = definePlugin({
  id: "audit",
  hooks: {
    upload: {
      onConfirmed: async ({ key }) => {
        console.log("[audit] Upload confirmed:", key);
      },
    },
  },
  endpoints: {
    stats: createS3Endpoint("/audit/stats", { method: "GET" }, async () => ({
      status: "ok",
    })),
  },
  context: {
    log: (msg: string) => console.log("[audit]", msg),
  },
});

Register the plugin in your instance:

lib/s3.ts
import { dimahS3, route } from "@dimah-s3/server";
import { auditPlugin } from "./s3-audit-plugin";

export const s3 = dimahS3({
  client: awsS3,
  bucket: process.env.S3_BUCKET!,
  plugins: [auditPlugin],
  routes: {
    avatar: route({ upload: true }),
  },
});

// Access plugin context
s3.audit.log("Storage ready");

Hook execution order

  • Guards: Plugins execute first in registration order, followed by user config guards.
  • Lifecycle hooks (onConfirmed, onDeleted): User config hooks execute first, followed by plugin hooks.

Frequently asked questions

On this page