From 1c078482977ef092b4d36948fc48bc96e4dcf1e8 Mon Sep 17 00:00:00 2001 From: Farsab <121871535+bahag-buttf@users.noreply.github.com> Date: Tue, 10 Oct 2023 17:23:00 +0200 Subject: [PATCH] fix(docs): adjust api route documentation examples (#56660) 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> --- .../01-routing/07-api-routes.mdx | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/docs/03-pages/01-building-your-application/01-routing/07-api-routes.mdx b/docs/03-pages/01-building-your-application/01-routing/07-api-routes.mdx index 5804e6d3cdfd4..5c2b728845b1c 100644 --- a/docs/03-pages/01-building-your-application/01-routing/07-api-routes.mdx +++ b/docs/03-pages/01-building-your-application/01-routing/07-api-routes.mdx @@ -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 }) @@ -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 }) @@ -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 }) @@ -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 }) @@ -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 { @@ -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 {