npm package: @flight-framework/adapter-cloudflare
Description: Cloudflare adapter for Flight Framework - deploy to Workers/Pages
Latest version: 0.0.6
Deprecated: DEPRECATED: Movido a @flightdev/adapter-cloudflare

# @flight-framework/adapter-cloudflare

Cloudflare Workers and Pages deployment adapter for Flight Framework. Deploy to 300+ edge locations worldwide.

## Table of Contents

- [Installation](#installation)
- [Quick Start](#quick-start)
- [Configuration](#configuration)
- [Cloudflare Pages](#cloudflare-pages)
- [Cloudflare Workers](#cloudflare-workers)
- [Bindings](#bindings)
- [Build Output](#build-output)
- [Deployment](#deployment)
- [API Reference](#api-reference)
- [License](#license)

---

## Installation

```bash
npm install @flight-framework/adapter-cloudflare
```

---

## Quick Start

```typescript
// flight.config.ts
import { defineConfig } from '@flight-framework/core';
import cloudflare from '@flight-framework/adapter-cloudflare';

export default defineConfig({
    adapter: cloudflare(),
});
```

Deploy:

```bash
npm run build
wrangler pages deploy .cloudflare
```

---

## Configuration

```typescript
cloudflare({
    // 'pages' or 'workers'
    mode: 'pages',
    
    // Routes configuration
    routes: {
        include: ['/*'],
        exclude: ['/static/*', '/assets/*'],
    },
    
    // Compatibility settings
    compatibility_date: '2024-01-01',
    compatibility_flags: ['nodejs_compat'],
    
    // Workers-specific
    minify: true,
});
```

---

## Cloudflare Pages

Pages is recommended for most apps. It provides:
- Automatic static asset hosting
- Git-connected deployments
- Preview deployments for PRs

```typescript
cloudflare({
    mode: 'pages',
});
```

### Build Output

```
.cloudflare/
├── _worker.js           # Edge worker
├── _routes.json         # Route configuration
└── static/              # Static assets
    ├── assets/
    └── index.html
```

### Deploy

```bash
wrangler pages deploy .cloudflare
```

---

## Cloudflare Workers

For more control or specific Workers features:

```typescript
cloudflare({
    mode: 'workers',
});
```

### wrangler.toml

```toml
name = "my-flight-app"
main = ".cloudflare/worker.js"
compatibility_date = "2024-01-01"
compatibility_flags = ["nodejs_compat"]

[site]
bucket = ".cloudflare/static"
```

### Deploy

```bash
wrangler deploy
```

---

## Bindings

Access Cloudflare bindings (KV, D1, R2, etc.) in your routes:

### KV Namespace

```typescript
// src/routes/api/cache.get.ts
export async function GET({ platform }) {
    const kv = platform.env.MY_KV;
    
    const value = await kv.get('key');
    return Response.json({ value });
}

export async function PUT({ platform, request }) {
    const kv = platform.env.MY_KV;
    const { key, value } = await request.json();
    
    await kv.put(key, value);
    return Response.json({ success: true });
}
```

### D1 Database

```typescript
// src/routes/api/users.get.ts
export async function GET({ platform }) {
    const db = platform.env.MY_D1;
    
    const { results } = await db.prepare(
        'SELECT * FROM users LIMIT 10'
    ).all();
    
    return Response.json(results);
}
```

### R2 Storage

```typescript
// src/routes/api/files/[key].get.ts
export async function GET({ platform, params }) {
    const bucket = platform.env.MY_R2;
    
    const object = await bucket.get(params.key);
    if (!object) {
        return new Response('Not found', { status: 404 });
    }
    
    return new Response(object.body, {
        headers: { 'Content-Type': object.httpMetadata.contentType },
    });
}
```

### Durable Objects

```typescript
// src/routes/api/counter.ts
export async function GET({ platform, request }) {
    const id = platform.env.COUNTER.idFromName('global');
    const stub = platform.env.COUNTER.get(id);
    
    return stub.fetch(request);
}
```

### Configure Bindings

In `wrangler.toml`:

```toml
[[kv_namespaces]]
binding = "MY_KV"
id = "abc123"

[[d1_databases]]
binding = "MY_D1"
database_name = "my-database"
database_id = "def456"

[[r2_buckets]]
binding = "MY_R2"
bucket_name = "my-bucket"
```

---

## Build Output

### Pages Mode

```
.cloudflare/
├── _worker.js           # Edge worker
├── _routes.json         # Route config
└── static/
```

### Workers Mode

```
.cloudflare/
├── worker.js            # Bundled worker
└── static/              # Static assets
```

---

## Deployment

### Wrangler CLI

```bash
# Install Wrangler
npm install -g wrangler

# Login
wrangler login

# Deploy Pages
wrangler pages deploy .cloudflare

# Deploy Workers
wrangler deploy
```

### Git Integration

Connect your repository in the Cloudflare dashboard for automatic deployments.

---

## API Reference

### Adapter Options

| Option | Type | Default | Description |
|--------|------|---------|-------------|
| `mode` | `string` | `'pages'` | Deployment mode |
| `routes.include` | `string[]` | `['/*']` | Included routes |
| `routes.exclude` | `string[]` | `[]` | Excluded routes |
| `compatibility_date` | `string` | latest | Compat date |
| `compatibility_flags` | `string[]` | `[]` | Compat flags |
| `minify` | `boolean` | `true` | Minify output |

---

## License

MIT