Skip to content

Commit

Permalink
docs: add example usage with authorization
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanong authored Aug 31, 2024
1 parent 09d0430 commit 47f4387
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ A more functional version of body parsing.
Use this module if you want to "lazily" parse the body.
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.

Includes a `json` and `urlencoded` parsers,
as well as a utility to save streams to disk.
Includes a `json` and `urlencoded` parsers.

## API

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

const app = new Koa()
koaBodyParsers(app)

// example usage
app.use(async (ctx) => {
const currentUser = UserService.getCurrentUser(ctx)
ctx.assert(currentUser, 401)

ctx.assert(ctx.request.is('json'), 415)
const body = await ctx.request.json('100kb')
ctx.body = body
})
```

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

0 comments on commit 47f4387

Please sign in to comment.