Files
wehub-resource-sync 70bf21e064
Deploy (to testing) and Test Playground Preview Worker / Deploy Playground Preview Worker (testing) (push) Has been skipped
Deploy Workers Shared Staging / Deploy Workers Shared Staging (push) Failing after 0s
Prerelease / build (push) Has been skipped
Handle Changesets / Handle Changesets (push) Has been cancelled
Semgrep OSS scan / semgrep-oss (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:30:11 +08:00

43 lines
1.2 KiB
TypeScript

/**
* Web Worker: renders user profiles and reports by calling the API Worker over a service binding.
*/
export default {
async fetch(request, env) {
const url = new URL(request.url);
if (url.pathname === "/") {
return new Response("Hello World");
}
const userPathPrefix = "/users/";
if (url.pathname.startsWith(userPathPrefix)) {
const userId = url.pathname.slice(userPathPrefix.length);
const { name } = await env.API.getUser(userId);
return new Response(`Profile: ${name}`);
}
const reportsPathPrefix = "/reports/";
if (url.pathname.startsWith(reportsPathPrefix)) {
const date = url.pathname.slice(reportsPathPrefix.length).slice(0, 10); // YYYY-MM-DD
const report = await env.API.getDailyReport(date);
if (report === null) {
return new Response("No report", { status: 404 });
}
if (url.pathname.endsWith(".png")) {
return env.BROWSER.quickAction("screenshot", {
html: `<h1>Daily report (${date}): active users ${report.join(", ")}</h1>`,
viewport: { width: 600, height: 200 },
});
}
return new Response(
`Daily report (${date}): active users ${report.join(", ")}`
);
}
return new Response("Not Found", { status: 404 });
},
} satisfies ExportedHandler<WebEnv>;