@workkit/astro
v0.1.1
Published
Astro integration for Cloudflare Pages — typed env access, binding helpers, middleware
Downloads
268
Maintainers
Readme
@workkit/astro
Astro middleware and helpers for Cloudflare Workers bindings
Install
bun add @workkit/astroUsage
Before (raw Astro + Cloudflare)
// src/pages/api/users.ts
export async function GET({ locals }) {
// Access runtime manually — no validation, no types
const runtime = locals.runtime as any
const db = runtime.env.DB // untyped, might be undefined
const data = await db.prepare("SELECT * FROM users").all()
return new Response(JSON.stringify(data))
}After (workkit astro)
// src/middleware.ts
import { workkitMiddleware } from "@workkit/astro"
import { z } from "zod"
export const onRequest = workkitMiddleware({
env: { DB: z.any(), API_KEY: z.string().min(1) },
onError: (error) => new Response("Config error", { status: 500 }),
})
// src/env.ts
import { defineEnv } from "@workkit/astro"
import { z } from "zod"
export const env = defineEnv({
DB: z.any(),
API_KEY: z.string().min(1),
})
// src/pages/api/users.ts
import { getBinding, getCFProperties } from "@workkit/astro"
export async function GET(context) {
const db = getBinding(context, "DB") // typed, throws if missing
const cf = getCFProperties(context) // typed CF properties (country, colo, etc.)
const apiKey = getBinding(context, "API_KEY") // validated by middleware
return new Response(JSON.stringify({ colo: cf?.colo }))
}API
Middleware
workkitMiddleware(options)— Validates env bindings on every request. Options:env(schema),onError?
Env
defineEnv(schema)— Define a reusable env schema
Bindings
getBinding(context, name)— Get a validated binding from Astro context (throws if missing)getOptionalBinding(context, name)— Get a binding orundefined
Context Helpers
getCFProperties(context)— Get Cloudflare-specific request properties (country, colo, etc.)getWaitUntil(context)— Get thewaitUntilfunction from the Cloudflare runtime
License
MIT
