Moving Towards Cloudflare Workers Modules

Sunder now supports the the new ES Modules syntax for Cloudflare Workers. By opting into modules you can get rid of relying on global variables floating around your codebase, and also unlock Durable Objects.

Before this was an option the entrypoint to your application would look something like this:

import {Sunder, Router} from "sunder";
const app = new Sunder();
const router = new Router();
router.get("/hello/:username", ({response, params}) => {
response.body = `Hello ${params.username}`;
});
app.use(router.middleware);
addEventListener('fetch', (event) => {
const resp = app.handle(event);
event.respondWith(resp);
});

Now, new in version 0.8.0, there is also a fetch function on the app which allows you to use the new syntax easily.

import {Sunder, Router} from "sunder";
const app = new Sunder();
const router = new Router();
router.get("/hello/:username", ({response, params}) => {
response.body = `Hello ${params.username}`;
});
app.use(router.middleware);
export default {
fetch(request, env, ctx) {
return app.fetch(request, env, ctx);
}
}
// Technically you could also use `export default app` if you only want to
// subscribe to the fetch event.