diff --git a/MIGRATION.md b/MIGRATION.md index 12dbef9..3dc7795 100644 --- a/MIGRATION.md +++ b/MIGRATION.md @@ -1,21 +1,16 @@ # Migration Guide -From `v1.x.x` to `v2.x.x`. +From `v2.x.x` to `v3.x.x`. + ## Contents -- [Preparation](#preparation) -- [Update](#update) -- [Import / Require](#import--require) -- [Routes](#routes) +- [Update Router](#update-router) +- [Handlers](#handlers) - [Fetch](#fetch--routerhandle) -## Preparation - -Follow Cloudflare's [Migration Guide](https://developers.cloudflare.com/workers/wrangler/migration/migrating-from-wrangler-1/) to update your protject to [wrangler2](https://github.com/cloudflare/wrangler2). - -## Update +## Update Router Update to the latest version version of the router. @@ -23,34 +18,53 @@ Update to the latest version version of the router. npm i -D @tsndr/cloudflare-worker-router ``` -## Import / Require -Switch from `require()` to `import`. +## Handlers + +- Remove `res` and `next` from handler parameter list. +- Replace `res.` with `return new Response()` / `return Response.json()`. +- Remove `next()` calls from middlewares. + ### Before -```javascript -const Router = require('@tsndr/cloudflare-worker-router') +```typescript +// Register global middleware +router.use(({ env, req, res, next }) => { + if (req.headers.get('authorization') !== env.SECRET_TOKEN) { + res.status = 401 + return + } + + next() +}) + +// Simple get +router.get('/user', ({ res }) => { + res.body = { + id: 1, + name: 'John Doe' + } +}) ``` ### After -```javascript -import Router from '@tsndr/cloudflare-worker-router' +```typescript +// Register global middleware +router.use(({ env, req }) => { + // Intercept if token doesn't match + if (req.headers.get('authorization') !== env.SECRET_TOKEN) { + return new Response(null, { status: 401 }) + } +}) + +// Simple get +router.get('/user', () => { + return Response.json({ + id: 1, + name: 'John Doe' + }) +}) ``` - - -## Routes - -Just add curly braces `{}`. - - -### Before - -Petrify 2022-06-24 at 5 57 01 PM - - -### After - -Petrify 2022-06-24 at 5 55 56 PM diff --git a/README.md b/README.md index 048425b..7267947 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ I worked a lot with [Express.js](https://expressjs.com/) in the past and really ## Usage -Migrating from `v1.x.x`, check out the [Migration Guide](MIGRATION.md). +Migrating from `v2.x.x`, check out the [Migration Guide](MIGRATION.md). ### TypeScript Example