function CloudflarePlugin
Since 0.2.0
CloudflarePlugin(options: CloudflarePluginOptions): IPlugin

Creates the Cloudflare Workers plugin.

The Worker's env is passed in rather than imported, because cloudflare:workers is not a specifier any other runtime can resolve. The plugin captures the bindings at register() and never calls a method on one there: Cloudflare prohibits I/O outside a request context, so a probe read at startup would throw on a real deployment while passing against every fake.

Examples

Example 1

import { env, waitUntil } from 'cloudflare:workers';
import { CloudflarePlugin } from '@setu-ts/cloudflare-plugin';

const app = createApplication({
  plugins: [
    RuntimePlugin({ env }),
    CloudflarePlugin({
      env,
      waitUntil,
      cache: { binding: 'CACHE_KV', prefix: 'cache:' },
    }),
  ],
});

export default { fetch: app.fetch };

Parameters

The Worker env plus the capabilities to serve from it

Return Type

IPlugin

The plugin instance

Throws

CloudflareBindingMissingError

At register(), when a required or configured binding is absent, or is present with the wrong shape

Usage

import { CloudflarePlugin } from "cloudflare-plugin/src/index.ts";