Skip to content

Commit

Permalink
docs(README): add parse examples
Browse files Browse the repository at this point in the history
  • Loading branch information
vansergen committed Mar 5, 2023
1 parent af22458 commit 8f3dd1e
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,32 @@ class MyMiddleware extends Middleware {
app.use("/deposit", new Router().post(BodyParser, new MyMiddleware()));
```

### Custom `parse` function

```typescript
import { Binden } from "binden";
import AJV, { JTDDataType } from "ajv/dist/jtd.js";
import BodyParser from "@binden/body";
const ajv = new AJV();
const schema = {
properties: { username: { type: "string" }, password: { type: "string" } },
additionalProperties: false,
};
const parse = ajv.compileParser<JTDDataType<typeof schema>>(schema);
const body_parser = new BodyParser({ parse });
const echo = (context) => {
const { body } = context.request;
context.log.info("Body has been parsed (or not)", { body });
return context.json(body);
};
new Binden()
.use("/login", new Router().post(BodyParser, echo))
.createServer()
.listen(port, () => {
console.log(`Server is listening on the port ${port}`);
});
```

### Test

```bash
Expand Down

0 comments on commit 8f3dd1e

Please sign in to comment.