Is there an example using the fetch-event workflow discussed in componentizejs? It sounds nice and I imagine it means I can create a JS file using service worker fetch events:
addEventListener("fetch", (event) => {
console.error('We did it!');
return Response.json({
"hello": "world"
});
});
But the only examples I've seen like the recently added jco http example seem to use the raw wasi:http apis
Hey. You can check the e2e
tests in the StarlingMonkey
repository for an example of fetch
API usage. E.g.: https://github.com/bytecodealliance/StarlingMonkey/blob/main/tests/e2e/blob/blob.js
Appreciate it! Just the confidence that there are tests was enough to push forward. There are a few issues to file hopefully later this week with jco + componentizejs to make it work smoothly but I was able to get the following to work with a few workarounds:
(addEventListener as ServiceWorkerGlobalScope['addEventListener'])(
"fetch", (event) =>
event.respondWith(
(async () => {
try {
const request = await fetch('https://httpbin.org/json');
const result = await request.text();
return new Response(`Hello from TypeScript Fetch!, result ${result}`);
} catch (e) {
return new Response('whoopsies');
}
})(),
),
);
Last updated: Feb 27 2025 at 23:03 UTC