Skip to content

Commit 47f4387

Browse files
authored
docs: add example usage with authorization
1 parent 09d0430 commit 47f4387

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ A more functional version of body parsing.
77
Use this module if you want to "lazily" parse the body.
88
Other middleware automatically parse the body in the middleware chain, which might not be ideal as business logic like authentication, authorization, and routing are not done prior to body parsing.
99

10-
Includes a `json` and `urlencoded` parsers,
11-
as well as a utility to save streams to disk.
10+
Includes a `json` and `urlencoded` parsers.
1211

1312
## API
1413

@@ -20,6 +19,16 @@ import Koa from 'koa'
2019

2120
const app = new Koa()
2221
koaBodyParsers(app)
22+
23+
// example usage
24+
app.use(async (ctx) => {
25+
const currentUser = UserService.getCurrentUser(ctx)
26+
ctx.assert(currentUser, 401)
27+
28+
ctx.assert(ctx.request.is('json'), 415)
29+
const body = await ctx.request.json('100kb')
30+
ctx.body = body
31+
})
2332
```
2433

2534
Because this module is a plugin for the `context`, the API signature is different.

0 commit comments

Comments
 (0)