Skip to content

Commit

Permalink
docs: fix usage with next-connect
Browse files Browse the repository at this point in the history
jellydn committed Jun 25, 2023
1 parent a775f5a commit d6cc072
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -44,7 +44,7 @@ yarn add yup joi next-validations
```typescript
import Joi from 'joi';
import { NextApiRequest, NextApiResponse } from 'next';
import connect from 'next-connect';
import { createRouter } from 'next-connect';
import { withValidations } from 'next-validations';
import * as yup from 'yup';

@@ -104,7 +104,18 @@ const handler = (req: NextApiRequest, res: NextApiResponse) => {
res.status(200).json(req.query);
};

export default validate(handler);
const router = createRouter();

router.post(validate(), handler);

export default router.handler({
onError: (err, _req, _event) => {
return new NextResponse('Something broke!', {
status: (err as any)?.statusCode ?? 500,
});
},
});

```

### Validate custom API endpoint with Zod
@@ -173,7 +184,7 @@ yarn add joi next-connect next-validations
```typescript
import Joi from 'joi';
import { NextApiRequest, NextApiResponse } from 'next';
import connect from 'next-connect';
import { createRouter } from 'next-connect';
import { withValidation } from 'next-validations';

const schema = Joi.object({
@@ -192,7 +203,17 @@ const handler = (req: NextApiRequest, res: NextApiResponse) => {
res.status(200).json(req.body);
};

export default connect().post(validate(), handler);
const router = createRouter();

router.post(validate(), handler);

export default router.handler({
onError: (err, _req, _event) => {
return new NextResponse('Something broke!', {
status: (err as any)?.statusCode ?? 500,
});
},
});
```

## Run tests

0 comments on commit d6cc072

Please sign in to comment.