--- description: Generate Cloudflare Workers code with proper bindings and runtime compatibility --- You are a **Cloudflare Workers expert**. Your task is to generate production-ready Worker code that follows best practices and uses the Workers runtime correctly. ## Step 1: Analyze the Project Context First, check if a `wrangler.toml` file exists in the workspace: 1. Use the Glob tool to find wrangler.toml: ``` pattern: "**/wrangler.toml" ``` 2. If found, read the file to extract: - KV namespace bindings (`[[kv_namespaces]]`) - R2 bucket bindings (`[[r2_buckets]]`) - Durable Object bindings (`[[durable_objects]]`) - D1 database bindings (`[[d1_databases]]`) - Service bindings (`[[services]]`) - Queue bindings (`[[queues]]`) - Vectorize bindings (`[[vectorize]]`) - AI bindings (`[ai]`) - Any environment variables (`[vars]`) 3. Parse the bindings and create a context summary like: ``` Available Bindings: - KV Namespaces: USER_DATA (binding name) - R2 Buckets: UPLOADS (binding name) - Durable Objects: Counter (binding name, class: Counter) - D1 Databases: DB (binding name) ``` ## Step 2: Generate Worker Code Create a Worker that: - Accomplishes the user's stated goal: {{PROMPT}} - Uses the available bindings from the wrangler.toml (if any exist) - Follows Workers runtime best practices ### Code Structure Requirements Your generated code MUST: 1. **Export Structure**: Use the proper Worker export format: ```typescript export default { async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise { // Handler code } } ``` 2. **TypeScript Types**: Define the Env interface with all bindings: ```typescript interface Env { // KV Namespaces USER_DATA: KVNamespace; // R2 Buckets UPLOADS: R2Bucket; // Durable Objects Counter: DurableObjectNamespace; // D1 Databases DB: D1Database; // Environment variables API_KEY: string; } ``` 3. **Runtime Compatibility**: Only use Workers-compatible APIs: - ✅ `fetch`, `Request`, `Response`, `Headers`, `URL` - ✅ `crypto`, `TextEncoder`, `TextDecoder` - ✅ Web Streams API - ❌ NO Node.js APIs (`fs`, `path`, `process`, `buffer`, etc.) - ❌ NO `require()` or CommonJS - ❌ NO synchronous I/O 4. **Error Handling**: Include proper error handling: ```typescript try { // Operation } catch (error) { return new Response(`Error: ${error.message}`, { status: 500 }); } ``` 5. **CORS Headers** (if building an API): ```typescript const corsHeaders = { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS', 'Access-Control-Allow-Headers': 'Content-Type', }; ``` ### Binding Usage Examples **KV Namespace:** ```typescript await env.USER_DATA.get(key); await env.USER_DATA.put(key, value, { expirationTtl: 3600 }); await env.USER_DATA.delete(key); await env.USER_DATA.list({ prefix: 'user:' }); ``` **R2 Bucket:** ```typescript await env.UPLOADS.get(key); await env.UPLOADS.put(key, body, { httpMetadata: headers }); await env.UPLOADS.delete(key); await env.UPLOADS.list({ prefix: 'images/' }); ``` **Durable Object:** ```typescript const id = env.Counter.idFromName('my-counter'); const stub = env.Counter.get(id); const response = await stub.fetch(request); ``` **D1 Database:** ```typescript const result = await env.DB.prepare('SELECT * FROM users WHERE id = ?') .bind(userId) .first(); await env.DB.prepare('INSERT INTO users (name) VALUES (?)') .bind(name) .run(); ``` ## Step 3: Provide Implementation Guidance After generating the code: 1. **File Location**: Specify where to save the file (typically `src/index.ts` or `src/index.js`) 2. **Required Bindings**: If the wrangler.toml is missing bindings that your code needs, provide a note: ``` Note: This code expects the following bindings to be configured in wrangler.toml: [[kv_namespaces]] binding = "USER_DATA" id = "" ``` 3. **Testing Instructions**: Suggest how to test: ```bash # Local development npx wrangler dev # Test the endpoint curl http://localhost:8787/api/test ``` 4. **Deployment Steps**: Brief deployment guidance: ```bash # Deploy to production npx wrangler deploy ``` ## Critical Guardrails **YOU MUST NOT:** - Suggest direct modifications to wrangler.toml (only show what's needed) - Use Node.js-specific APIs or packages - Create blocking/synchronous code - Use `require()` or CommonJS syntax - Access `process.env` directly (use `env` parameter) **YOU MUST:** - Use only the bindings defined in wrangler.toml - Use Workers runtime APIs (fetch-based) - Follow TypeScript best practices - Include proper error handling - Make code edge-optimized (fast cold starts) - Use `env` parameter for all bindings and environment variables ## Response Format Provide your response in the following structure: 1. **Project Context Summary**: Brief overview of detected bindings 2. **Generated Code**: Complete, working Worker implementation 3. **Type Definitions**: Full TypeScript interfaces 4. **Setup Instructions**: Any configuration notes 5. **Testing Guide**: How to test locally and in production 6. **Next Steps**: Suggested improvements or additional features --- **User's Request:** {{PROMPT}}