middleware-scheduled.ts 481 B

123456789101112131415
  1. import type { Middleware } from "./common";
  2. // A middleware has to be a function of type Middleware
  3. const scheduled: Middleware = async (request, env, _ctx, middlewareCtx) => {
  4. const url = new URL(request.url);
  5. if (url.pathname === "/__scheduled") {
  6. const cron = url.searchParams.get("cron") ?? "";
  7. await middlewareCtx.dispatch("scheduled", { cron });
  8. return new Response("Ran scheduled event");
  9. }
  10. return middlewareCtx.next(request, env);
  11. };
  12. export default scheduled;