Skip to content

Commit

Permalink
fix(docs): adjust api route documentation examples (#56660)
Browse files Browse the repository at this point in the history
Fixed the example functions in the documentation to use `async` in order to use `await` within the function body. 

Co-authored-by: Michael Novotny <446260+manovotny@users.noreply.github.com>
  • Loading branch information
bahag-buttf and manovotny authored Oct 10, 2023
1 parent f6d6acd commit 1c07848
Showing 1 changed file with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,10 @@ The following example sends a JSON response with the status code `200` (`OK`) an
```ts filename="pages/api/hello.ts" switcher
import type { NextApiRequest, NextApiResponse } from 'next'

export default function handler(req: NextApiRequest, res: NextApiResponse) {
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
try {
const result = await someAsyncOperation()
res.status(200).json({ result })
Expand All @@ -228,7 +231,7 @@ export default function handler(req: NextApiRequest, res: NextApiResponse) {
```

```js filename="pages/api/hello.js" switcher
export default function handler(req, res) {
export default async function handler(req, res) {
try {
const result = await someAsyncOperation()
res.status(200).json({ result })
Expand All @@ -247,7 +250,10 @@ The following example sends a HTTP response with the status code `200` (`OK`) an
```ts filename="pages/api/hello.ts" switcher
import type { NextApiRequest, NextApiResponse } from 'next'

export default function handler(req: NextApiRequest, res: NextApiResponse) {
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
try {
const result = await someAsyncOperation()
res.status(200).send({ result })
Expand All @@ -258,7 +264,7 @@ export default function handler(req: NextApiRequest, res: NextApiResponse) {
```

```js filename="pages/api/hello.js" switcher
export default function handler(req, res) {
export default async function handler(req, res) {
try {
const result = await someAsyncOperation()
res.status(200).send({ result })
Expand All @@ -277,7 +283,10 @@ The following example redirects the client to the `/` path if the form is succes
```ts filename="pages/api/hello.ts" switcher
import type { NextApiRequest, NextApiResponse } from 'next'

export default function handler(req: NextApiRequest, res: NextApiResponse) {
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const { name, message } = req.body

try {
Expand All @@ -290,7 +299,7 @@ export default function handler(req: NextApiRequest, res: NextApiResponse) {
```

```js filename="pages/api/hello.js" switcher
export default function handler(req, res) {
export default async function handler(req, res) {
const { name, message } = req.body

try {
Expand Down

0 comments on commit 1c07848

Please sign in to comment.