feat: project scaffold, Worker router, and type definitions
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
node_modules/
|
||||
dist/
|
||||
.wrangler/
|
||||
.dev.vars
|
||||
.superpowers/
|
||||
4019
package-lock.json
generated
Normal file
4019
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
19
package.json
Normal file
19
package.json
Normal file
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"name": "vcr-calendar",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"dev": "wrangler dev",
|
||||
"deploy": "wrangler deploy",
|
||||
"test": "vitest run",
|
||||
"test:watch": "vitest",
|
||||
"types": "wrangler types"
|
||||
},
|
||||
"devDependencies": {
|
||||
"wrangler": "^4.0.0",
|
||||
"@cloudflare/workers-types": "^4.0.0",
|
||||
"@cloudflare/vitest-pool-workers": "^0.8.0",
|
||||
"vitest": "^3.0.0",
|
||||
"typescript": "^5.7.0"
|
||||
}
|
||||
}
|
||||
32
src/index.test.ts
Normal file
32
src/index.test.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { SELF } from "cloudflare:test";
|
||||
|
||||
describe("Worker router", () => {
|
||||
it("redirects / to /week", async () => {
|
||||
const response = await SELF.fetch("https://vcr.test/", { redirect: "manual" });
|
||||
expect(response.status).toBe(302);
|
||||
expect(response.headers.get("Location")).toBe("/week");
|
||||
});
|
||||
|
||||
it("returns 200 for /week", async () => {
|
||||
const response = await SELF.fetch("https://vcr.test/week");
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.headers.get("Content-Type")).toContain("text/html");
|
||||
});
|
||||
|
||||
it("returns 200 for a valid show detail page", async () => {
|
||||
const response = await SELF.fetch("https://vcr.test/show/bowl-after-bowl");
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.headers.get("Content-Type")).toContain("text/html");
|
||||
});
|
||||
|
||||
it("returns 404 for an unknown show", async () => {
|
||||
const response = await SELF.fetch("https://vcr.test/show/nonexistent");
|
||||
expect(response.status).toBe(404);
|
||||
});
|
||||
|
||||
it("returns 404 for unknown routes", async () => {
|
||||
const response = await SELF.fetch("https://vcr.test/unknown");
|
||||
expect(response.status).toBe(404);
|
||||
});
|
||||
});
|
||||
46
src/index.ts
Normal file
46
src/index.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import scheduleData from "./schedule.json";
|
||||
import { resolvePreferences } from "./preferences";
|
||||
import { renderWeekPage } from "./render/week-page";
|
||||
import { renderShowDetail } from "./render/show-detail";
|
||||
import { renderNotFound } from "./render/not-found";
|
||||
|
||||
import type { Show } from "./types";
|
||||
|
||||
const shows: Show[] = scheduleData.shows as Show[];
|
||||
|
||||
export default {
|
||||
async fetch(request: Request): Promise<Response> {
|
||||
const url = new URL(request.url);
|
||||
const path = url.pathname;
|
||||
|
||||
console.log(JSON.stringify({ message: "incoming request", method: request.method, path }));
|
||||
|
||||
if (path === "/") {
|
||||
return new Response(null, { status: 302, headers: { Location: "/week" } });
|
||||
}
|
||||
|
||||
const prefs = resolvePreferences(request, url);
|
||||
const headers = new Headers({ "Content-Type": "text/html; charset=utf-8" });
|
||||
for (const cookie of prefs.setCookies) {
|
||||
headers.append("Set-Cookie", cookie);
|
||||
}
|
||||
|
||||
if (path === "/week") {
|
||||
const html = renderWeekPage(shows, prefs);
|
||||
return new Response(html, { headers });
|
||||
}
|
||||
|
||||
const showMatch = path.match(/^\/show\/([a-z0-9-]+)$/);
|
||||
if (showMatch) {
|
||||
const slug = showMatch[1];
|
||||
const show = shows.find((s) => s.slug === slug);
|
||||
if (!show) {
|
||||
return new Response(renderNotFound(), { status: 404, headers: { "Content-Type": "text/html; charset=utf-8" } });
|
||||
}
|
||||
const html = renderShowDetail(show, prefs);
|
||||
return new Response(html, { headers });
|
||||
}
|
||||
|
||||
return new Response(renderNotFound(), { status: 404, headers: { "Content-Type": "text/html; charset=utf-8" } });
|
||||
},
|
||||
} satisfies ExportedHandler;
|
||||
5
src/preferences.ts
Normal file
5
src/preferences.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import type { Preferences } from "./types";
|
||||
|
||||
export function resolvePreferences(_request: Request, _url: URL): Preferences {
|
||||
return { tz: "eastern", theme: "system", mode: "vcr", week: "", setCookies: [] };
|
||||
}
|
||||
3
src/render/not-found.ts
Normal file
3
src/render/not-found.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export function renderNotFound(): string {
|
||||
return "<!DOCTYPE html><html><body><h1>404</h1><p>Not found.</p></body></html>";
|
||||
}
|
||||
4
src/render/show-detail.ts
Normal file
4
src/render/show-detail.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
import type { Show, Preferences } from "../types";
|
||||
export function renderShowDetail(_show: Show, _prefs: Preferences): string {
|
||||
return "<!DOCTYPE html><html><body>show</body></html>";
|
||||
}
|
||||
4
src/render/week-page.ts
Normal file
4
src/render/week-page.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
import type { Show, Preferences } from "../types";
|
||||
export function renderWeekPage(_shows: Show[], _prefs: Preferences): string {
|
||||
return "<!DOCTYPE html><html><body>week</body></html>";
|
||||
}
|
||||
23
src/schedule.json
Normal file
23
src/schedule.json
Normal file
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"shows": [
|
||||
{
|
||||
"slug": "bowl-after-bowl",
|
||||
"name": "Bowl After Bowl",
|
||||
"shortDescription": "The flagship Small Group talk show.",
|
||||
"websiteUrl": "https://bowlafterbowl.com",
|
||||
"feedUrl": "https://feed.bowlafterbowl.com/feed.xml",
|
||||
"accentColor": "#f2b705",
|
||||
"hosts": ["Spencer", "DuhLaurien"],
|
||||
"isSmallGroup": true,
|
||||
"isVcr": true,
|
||||
"isActive": true,
|
||||
"schedule": {
|
||||
"day": "tuesday",
|
||||
"startTime": "22:00",
|
||||
"endTime": "01:00+1",
|
||||
"timezone": "America/New_York",
|
||||
"recurrence": "weekly"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
33
src/types.ts
Normal file
33
src/types.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
export interface Schedule {
|
||||
day: "sunday" | "monday" | "tuesday" | "wednesday" | "thursday" | "friday" | "saturday";
|
||||
startTime: string;
|
||||
endTime: string;
|
||||
timezone: string;
|
||||
recurrence: "weekly" | "biweekly";
|
||||
}
|
||||
|
||||
export interface Show {
|
||||
slug: string;
|
||||
name: string;
|
||||
shortDescription: string;
|
||||
websiteUrl: string;
|
||||
feedUrl: string;
|
||||
accentColor: string;
|
||||
hosts: string[];
|
||||
isSmallGroup: boolean;
|
||||
isVcr: boolean;
|
||||
isActive: boolean;
|
||||
schedule: Schedule;
|
||||
}
|
||||
|
||||
export type TimezoneKey = "eastern" | "central" | "pacific" | "utc" | "amsterdam";
|
||||
export type ThemeKey = "system" | "light" | "dark";
|
||||
export type ModeKey = "vcr" | "all";
|
||||
|
||||
export interface Preferences {
|
||||
tz: TimezoneKey;
|
||||
theme: ThemeKey;
|
||||
mode: ModeKey;
|
||||
week: string;
|
||||
setCookies: string[];
|
||||
}
|
||||
15
tsconfig.json
Normal file
15
tsconfig.json
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "es2021",
|
||||
"lib": ["es2021"],
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "Bundler",
|
||||
"types": ["@cloudflare/workers-types"],
|
||||
"resolveJsonModule": true,
|
||||
"strict": true,
|
||||
"skipLibCheck": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"noUncheckedIndexedAccess": true
|
||||
},
|
||||
"include": ["src"]
|
||||
}
|
||||
11
vitest.config.ts
Normal file
11
vitest.config.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { defineWorkersConfig } from "@cloudflare/vitest-pool-workers/config";
|
||||
|
||||
export default defineWorkersConfig({
|
||||
test: {
|
||||
poolOptions: {
|
||||
workers: {
|
||||
wrangler: { configPath: "./wrangler.jsonc" },
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
12
wrangler.jsonc
Normal file
12
wrangler.jsonc
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"$schema": "node_modules/wrangler/config-schema.json",
|
||||
"name": "vcr-calendar",
|
||||
"main": "src/index.ts",
|
||||
"compatibility_date": "2026-11-02",
|
||||
"compatibility_flags": ["nodejs_compat"],
|
||||
"observability": {
|
||||
"enabled": true,
|
||||
"logs": { "enabled": true, "head_sampling_rate": 1 },
|
||||
"traces": { "enabled": true, "head_sampling_rate": 0.01 }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user