|
| 1 | +# ⬆ Netacea Cloudflare v5 to v6 Upgrade Guide |
| 2 | + |
| 3 | +This template has been updated to support the **v6** release of `@netacea/cloudflare`. |
| 4 | +This version introduces changes to the interface, |
| 5 | +requiring updates to how the module is integrated within the cloudflare handler. |
| 6 | + |
| 7 | +This guide walks you through the required changes and best practices when using v6. |
| 8 | + |
| 9 | +You can install v6 using `npm install --save @netacea/cloudflare@6` |
| 10 | + |
| 11 | +## 📦 What's New in v6? |
| 12 | + |
| 13 | +The `run` method now returns a result object with a `response` property, rather than returning a `Response` directly. |
| 14 | +This change allows for greater extensibility in the result metadata. |
| 15 | + |
| 16 | +### Old Usage (v5) |
| 17 | + |
| 18 | +```ts |
| 19 | +const response = await worker.run(event, originRequest) |
| 20 | +event.waitUntil(worker.ingest(event.request, response)) |
| 21 | +return response |
| 22 | +``` |
| 23 | + |
| 24 | +### New Usage (v6) |
| 25 | + |
| 26 | +```ts |
| 27 | +const result = await worker.run(event, originRequest) |
| 28 | +event.waitUntil(worker.ingest(event.request, result)) |
| 29 | +return result.response |
| 30 | +``` |
| 31 | + |
| 32 | +--- |
| 33 | + |
| 34 | +## 🔧 Code Changes |
| 35 | + |
| 36 | +### Updated Handler |
| 37 | + |
| 38 | +Once updated, your handler should look something like: |
| 39 | + |
| 40 | +```ts |
| 41 | +import { Cloudflare } from '@netacea/cloudflare' |
| 42 | + |
| 43 | +const worker = new Cloudflare(NetaceaConfig) |
| 44 | + |
| 45 | +export async function handleRequestWithNetacea(event: FetchEvent): Promise<Response> { |
| 46 | + const result = await worker.run(event, originRequest) |
| 47 | + event.waitUntil(worker.ingest(event.request, result)) |
| 48 | + return result.response |
| 49 | +} |
| 50 | + |
| 51 | +async function originRequest(request: Request): Promise<Response> { |
| 52 | + // Your origin logic here |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +### Updated Tests |
| 57 | + |
| 58 | +The `run` method must now return an object with a `response` property in your mocks: |
| 59 | + |
| 60 | +```ts |
| 61 | +const { handleRequestWithNetacea } = proxyquire('../src/handler', { |
| 62 | + '@netacea/cloudflare': function () { |
| 63 | + return { |
| 64 | + run: () => ({ |
| 65 | + response: new Response() |
| 66 | + }), |
| 67 | + ingest: () => { |
| 68 | + // no-op |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | +}) |
| 73 | +``` |
| 74 | + |
| 75 | +Also, ensure your test requests include a fully qualified URL |
| 76 | +(e.g., `https://example.com`) to avoid errors when constructing `Request` objects. |
| 77 | + |
| 78 | +--- |
| 79 | + |
| 80 | +## ✅ Summary of Required Changes |
| 81 | + |
| 82 | +| Area | Change | |
| 83 | +|-------------|--------------------------------------------------| |
| 84 | +| `run()` usage | Use `result.response` instead of `response` | |
| 85 | +| `ingest()` | Pass the full `result` to `ingest()` | |
| 86 | +| Tests | Update mocks to return `{ response: Response }` | |
0 commit comments