update readmes
This commit is contained in:
21
MIGRATION.md
21
MIGRATION.md
@@ -17,7 +17,7 @@ Follow Cloudflare's [Migration Guide](https://developers.cloudflare.com/workers/
|
|||||||
|
|
||||||
## Update
|
## Update
|
||||||
|
|
||||||
Update to the latest version verstion
|
Update to the latest version version of the router.
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
npm i -D @tsndr/cloudflare-worker-router
|
npm i -D @tsndr/cloudflare-worker-router
|
||||||
@@ -25,7 +25,7 @@ npm i -D @tsndr/cloudflare-worker-router
|
|||||||
|
|
||||||
## Import / Require
|
## Import / Require
|
||||||
|
|
||||||
Switch to ESModules.
|
Switch from `require()` to `import`.
|
||||||
|
|
||||||
### Before
|
### Before
|
||||||
|
|
||||||
@@ -43,7 +43,7 @@ import Router from '@tsndr/cloudflare-worker-router'
|
|||||||
|
|
||||||
## Routes
|
## Routes
|
||||||
|
|
||||||
Just add curly braces.
|
Just add curly braces `{}`.
|
||||||
|
|
||||||
|
|
||||||
### Before
|
### Before
|
||||||
@@ -54,18 +54,3 @@ Just add curly braces.
|
|||||||
### After
|
### After
|
||||||
|
|
||||||
<a href="https://gist.github.com/tsndr/8db6e8dd55e348015c2ff8e93dd6aa31" target="_blank"><img width="469" alt="Petrify 2022-06-24 at 5 55 56 PM" src="https://user-images.githubusercontent.com/2940127/175572549-0eea8fc4-3d90-412a-89cc-d2f4569f1139.png"></a>
|
<a href="https://gist.github.com/tsndr/8db6e8dd55e348015c2ff8e93dd6aa31" target="_blank"><img width="469" alt="Petrify 2022-06-24 at 5 55 56 PM" src="https://user-images.githubusercontent.com/2940127/175572549-0eea8fc4-3d90-412a-89cc-d2f4569f1139.png"></a>
|
||||||
|
|
||||||
|
|
||||||
## Fetch / `router.handle()`
|
|
||||||
|
|
||||||
❗️ Be aware that with `v2.0.0` the parameters of `router.handle()` changed ❗️
|
|
||||||
|
|
||||||
|
|
||||||
### Before
|
|
||||||
|
|
||||||
<a href="https://gist.github.com/tsndr/12b0f800269760c597646c90a562ef88" target="_blank"><img width="469" alt="Petrify 2022-06-24 at 5 58 43 PM" src="https://user-images.githubusercontent.com/2940127/175572993-fb4681c2-eece-4c92-88e8-1c2f57644769.png"></a>
|
|
||||||
|
|
||||||
|
|
||||||
### After
|
|
||||||
|
|
||||||
<a href="https://gist.github.com/tsndr/30902b01b134e2a58cf3a53648dd3e47" target="_blank"><img width="393" alt="Petrify 2022-06-24 at 5 59 22 PM" src="https://user-images.githubusercontent.com/2940127/175573110-b7dfcfb2-855d-4529-a957-6f8b9ec439f3.png"></a>
|
|
||||||
|
|||||||
56
README.md
56
README.md
@@ -26,6 +26,8 @@ I worked a lot with [Express.js](https://expressjs.com/) in the past and really
|
|||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
|
Migrating from `v1.x.x`, check out the [Migration Guide](MIGRATION.md).
|
||||||
|
|
||||||
### TypeScript Example
|
### TypeScript Example
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
@@ -51,12 +53,10 @@ const router = new Router<Env>()
|
|||||||
router.cors()
|
router.cors()
|
||||||
|
|
||||||
// Register global middleware
|
// Register global middleware
|
||||||
router.use(() => {
|
router.use(({ env, req }) => {
|
||||||
return new Response(null, {
|
// Intercept if token doesn't match
|
||||||
headers: {
|
if (req.headers.get('authorization') !== env.SECRET_TOKEN)
|
||||||
'X-Global-Middlewares': 'true'
|
return new Response(null, { status: 401 })
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
// Simple get
|
// Simple get
|
||||||
@@ -85,9 +85,7 @@ router.post('/user/:id', ({ req }) => {
|
|||||||
|
|
||||||
// Delete route using a middleware
|
// Delete route using a middleware
|
||||||
router.delete('/user/:id', ({ env, req }) => {
|
router.delete('/user/:id', ({ env, req }) => {
|
||||||
const { SECRET_TOKEN } = env
|
if (req.headers.get('authorization') === env.SECRET_TOKEN)
|
||||||
|
|
||||||
if (req.headers.get('Authorization') === SECRET_TOKEN)
|
|
||||||
return new Response(null, { status: 401 })
|
return new Response(null, { status: 401 })
|
||||||
|
|
||||||
}, ({ req }) => {
|
}, ({ req }) => {
|
||||||
@@ -121,12 +119,10 @@ const router = new Router()
|
|||||||
router.cors()
|
router.cors()
|
||||||
|
|
||||||
// Register global middleware
|
// Register global middleware
|
||||||
router.use(() => {
|
router.use(({ env, req }) => {
|
||||||
return new Response(null, {
|
// Intercept if token doesn't match
|
||||||
headers: {
|
if (req.headers.get('authorization') !== env.SECRET_TOKEN)
|
||||||
'X-Global-Middlewares': 'true'
|
return new Response(null, { status: 401 })
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
// Simple get
|
// Simple get
|
||||||
@@ -139,11 +135,12 @@ router.get('/user', () => {
|
|||||||
|
|
||||||
// Post route with url parameter
|
// Post route with url parameter
|
||||||
router.post('/user/:id', ({ req }) => {
|
router.post('/user/:id', ({ req }) => {
|
||||||
|
|
||||||
const userId = req.params.id
|
const userId = req.params.id
|
||||||
|
|
||||||
// Do stuff
|
// Do stuff
|
||||||
|
|
||||||
if (errorDoingStuff) {
|
if (!true) {
|
||||||
return Response.json({
|
return Response.json({
|
||||||
error: 'Error doing stuff!'
|
error: 'Error doing stuff!'
|
||||||
}, { status: 400 })
|
}, { status: 400 })
|
||||||
@@ -154,12 +151,11 @@ router.post('/user/:id', ({ req }) => {
|
|||||||
|
|
||||||
// Delete route using a middleware
|
// Delete route using a middleware
|
||||||
router.delete('/user/:id', ({ env, req }) => {
|
router.delete('/user/:id', ({ env, req }) => {
|
||||||
const { SECRET_TOKEN } = env
|
if (req.headers.get('authorization') === env.SECRET_TOKEN)
|
||||||
|
|
||||||
if (req.headers.get('Authorization') === SECRET_TOKEN)
|
|
||||||
return new Response(null, { status: 401 })
|
return new Response(null, { status: 401 })
|
||||||
|
|
||||||
}, ({ req }) => {
|
}, ({ req }) => {
|
||||||
|
|
||||||
const userId = req.params.id
|
const userId = req.params.id
|
||||||
|
|
||||||
// Do stuff...
|
// Do stuff...
|
||||||
@@ -306,15 +302,15 @@ const router = new Router<Env>()
|
|||||||
|
|
||||||
/// Example Route
|
/// Example Route
|
||||||
//
|
//
|
||||||
// router.get(/'hi', ({ res }) => {
|
// router.get(/'hi', async () => {
|
||||||
// res.body = 'Hello World'
|
// return new Response('Hello World')
|
||||||
//}
|
//}
|
||||||
|
|
||||||
|
|
||||||
/// Example Route for splitting into multiple files
|
/// Example Route for splitting into multiple files
|
||||||
//
|
//
|
||||||
// const hiHandler: RouteHandler<Env> = ({ res }) => {
|
// const hiHandler: RouteHandler<Env> = async () => {
|
||||||
// res.body = 'Hello World'
|
// return new Response('Hello World')
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
// router.get('/hi', hiHandler)
|
// router.get('/hi', hiHandler)
|
||||||
@@ -340,10 +336,20 @@ import { Router } from '@tsndr/cloudflare-worker-router'
|
|||||||
|
|
||||||
const router = new Router()
|
const router = new Router()
|
||||||
|
|
||||||
// router.get(/'hi', ({ res }) => {
|
/// Example Route
|
||||||
// res.body = 'Hello World'
|
//
|
||||||
|
// router.get(/'hi', async () => {
|
||||||
|
// return new Response('Hello World')
|
||||||
//}
|
//}
|
||||||
|
|
||||||
|
/// Example Route for splitting into multiple files
|
||||||
|
//
|
||||||
|
// async function hiHandler() {
|
||||||
|
// return new Response('Hello World')
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// router.get('/hi', hiHandler)
|
||||||
|
|
||||||
// TODO: add your routes here
|
// TODO: add your routes here
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
|
|||||||
Reference in New Issue
Block a user