Shovel.js

The portable meta-framework built on web standards

server.ts
import {Router} from "@b9g/router";

const router = new Router();
const logger = self.loggers.get(["app", "cache"]);

router.route("/kv/:key")
  .get(async (req, ctx) => {
    logger.info`GET ${ctx.params.key}`;
    const cache = await self.caches.open("kv");
    const cached = await cache.match(req.url);
    return cached ?? new Response(null, {status: 404});
  })
  .put(async (req, ctx) => {
    logger.info`PUT ${ctx.params.key}`;
    const cache = await self.caches.open("kv");
    await cache.put(req.url, new Response(await req.text()));
    return new Response(null, {status: 201});
  })
  .delete(async (req, ctx) => {
    logger.info`DELETE ${ctx.params.key}`;
    const cache = await self.caches.open("kv");
    await cache.delete(req.url);
    return new Response(null, {status: 204});
  });

self.addEventListener("fetch", (ev) => {
  ev.respondWith(router.handle(ev.request));
});
terminal
$ shovel develop server.ts
 INF shovel·build Building...
 INF shovel·build Build complete in 35ms
 INF shovel·build Watching 27 files in 9 directories
 INF shovel·platform Server ready
 INF shovel·develop http://localhost:7777

$ curl -X PUT :7777/kv/hello -d "world"
 INF app·cache PUT hello
# 201
$ curl :7777/kv/hello
 INF app·cache GET hello
world
$ curl -X DELETE :7777/kv/hello
 INF app·cache DELETE hello
# 204
$ npm create shovel
Get StartedGitHub