Overview of Files SDK Plugins
mainA plugin is an opt-in, ordered pipeline passed to the createFiles constructor. Unlike hooks which only observe operations, plugins can transform inputs, veto calls (by throwing), or observe results. They can also contribute entirely new namespaced methods to the Files instance.
Use plugins for logic that requires changing behavior, such as:
- Envelope-encrypting bodies at rest.
- Gating uploads through a virus scanner.
- Metering bandwidth.
- Mirroring writes to a backup region.
import { createFiles, handlers } from "files-sdk";
import { s3 } from "files-sdk/s3";
const files = createFiles({
adapter: s3({ bucket: "uploads" }),
plugins: [
{
name: "uppercase",
wrap: handlers({
upload: (op, next) =>
next({ ...op, body: (op.body as string).toUpperCase() }),
}),
},
],
});
await files.upload("a.txt", "hello"); // stored as "HELLO"