add migration guide
This commit is contained in:
103
MIGRATION.md
Normal file
103
MIGRATION.md
Normal file
@@ -0,0 +1,103 @@
|
||||
# Migration Guide
|
||||
|
||||
From `v1.x.x` to `v2.x.x`.
|
||||
|
||||
## Contents
|
||||
|
||||
- [Import / Require](#import--require)
|
||||
- [Routes](#routes)
|
||||
- [Fetch](#fetch)
|
||||
|
||||
|
||||
## Import / Require
|
||||
|
||||
### Before
|
||||
|
||||
```javascript
|
||||
const Router = require('@tsndr/cloudflare-worker-router')
|
||||
```
|
||||
|
||||
|
||||
### After
|
||||
|
||||
```javascript
|
||||
import Router from '@tsndr/cloudflare-worker-router'
|
||||
```
|
||||
|
||||
|
||||
## Routes
|
||||
|
||||
Just add curly braces.
|
||||
|
||||
|
||||
### Before
|
||||
|
||||
```javascript
|
||||
// Register global middleware
|
||||
router.use((req, res, next) => {
|
||||
res.headers.set('X-Global-Middlewares', 'true')
|
||||
next()
|
||||
})
|
||||
|
||||
// Simple get
|
||||
router.get('/user', (req, res) => {
|
||||
res.body = {
|
||||
data: {
|
||||
id: 1,
|
||||
name: 'John Doe'
|
||||
}
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
|
||||
### After
|
||||
|
||||
<pre>
|
||||
// Register global middleware
|
||||
router.use((<span style="color:rgb(225, 75, 75);font-weight:bold;">{</span> req, res, next <span style="color:rgb(225, 75, 75);font-weight:bold;">}</span>) => {
|
||||
res.headers.set('X-Global-Middlewares', 'true')
|
||||
next()
|
||||
})
|
||||
|
||||
// Simple get
|
||||
router.get('/user', (<span style="color:rgb(225, 75, 75);font-weight:bold;">{</span> req, res <span style="color:rgb(225, 75, 75);font-weight:bold;">}</span>) => {
|
||||
res.body = {
|
||||
data: {
|
||||
id: 1,
|
||||
name: 'John Doe'
|
||||
}
|
||||
}
|
||||
})
|
||||
</pre>
|
||||
|
||||
|
||||
## Fetch / `router.handle()`
|
||||
|
||||
Be aware that with `v2.0.0` the parameters of `router.handle()` changed.
|
||||
|
||||
|
||||
### Before
|
||||
|
||||
`router.handle(request, extend = {})`
|
||||
|
||||
```javascript
|
||||
// Listen Cloudflare Workers Fetch Event
|
||||
addEventListener('fetch', event => {
|
||||
event.respondWith(router.handle(event.request))
|
||||
})
|
||||
```
|
||||
|
||||
|
||||
### After
|
||||
|
||||
`router.handle(env, request, extend = {})`
|
||||
|
||||
<pre>
|
||||
// Listen Cloudflare Workers Fetch Event
|
||||
export default {
|
||||
async fetch(request, env, ctx) {
|
||||
return router.handle(<span style="color:rgb(225, 75, 75);font-weight:bold;">env</span>, request)
|
||||
}
|
||||
}
|
||||
</pre>
|
||||
@@ -7,8 +7,11 @@
|
||||
|
||||
**USE AT YOUR OWN RISK!**
|
||||
|
||||
See [Migration Guide](MIGRATION.md)
|
||||
|
||||
---
|
||||
|
||||
|
||||
Cloudflare Workers Router is a super lightweight router (3.6 kB) with middleware support and ZERO dependencies for Cloudflare Workers.
|
||||
|
||||
When I was trying out Cloudflare Workers I almost immediately noticed how fast it was compared to other serverless offerings. So I wanted to build a full-fledged API to see how it performs doing real work, but since I wasn't able to find a router that suited my needs I created my own.
|
||||
@@ -101,6 +104,7 @@ export default {
|
||||
|
||||
Enable or disable debug mode. Which will return the `error.stack` in case of an exception instead of and empty `500` response. Debug mode is disabled by default.
|
||||
|
||||
|
||||
#### `state`
|
||||
State is a `boolean` which determines if debug mode should be enabled or not (default: `true`)
|
||||
|
||||
@@ -109,6 +113,7 @@ State is a `boolean` which determines if debug mode should be enabled or not (de
|
||||
|
||||
Register a global middleware handler.
|
||||
|
||||
|
||||
#### `handler` (function)
|
||||
|
||||
Handler is a `function` which will be called for every request.
|
||||
@@ -118,6 +123,7 @@ Handler is a `function` which will be called for every request.
|
||||
|
||||
If enabled will overwrite other `OPTIONS` requests.
|
||||
|
||||
|
||||
#### `config` (object, optional)
|
||||
|
||||
Key | Type | Default Value
|
||||
@@ -145,6 +151,7 @@ Key | Type | Default Value
|
||||
The URL starting with a `/`.
|
||||
Supports the use of dynamic parameters, prefixed with a `:` (i.e. `/user/:userId/edit`) which will be available through the [`req`-Object](#req-object) (i.e. `req.params.userId`).
|
||||
|
||||
|
||||
#### `handlers` (function, optional)
|
||||
|
||||
An unlimited number of functions getting [`req`](#req-object) and [`res`](#res-object) passed into them.
|
||||
|
||||
Reference in New Issue
Block a user