Unverified Commit 5a73859f authored by Lee Robinson's avatar Lee Robinson Committed by GitHub

Update docs and Create Next App to use API Middlewares by default. (#21639)

parent 8f0c1554
...@@ -19,13 +19,11 @@ API routes provide a straightforward solution to build your **API** with Next.js ...@@ -19,13 +19,11 @@ API routes provide a straightforward solution to build your **API** with Next.js
Any file inside the folder `pages/api` is mapped to `/api/*` and will be treated as an API endpoint instead of a `page`. They are server-side only bundles and won't increase your client-side bundle size. Any file inside the folder `pages/api` is mapped to `/api/*` and will be treated as an API endpoint instead of a `page`. They are server-side only bundles and won't increase your client-side bundle size.
For example, the following API route `pages/api/user.js` handles a `json` response: For example, the following API route `pages/api/user.js` returns a `json` response with a status code of `200`:
```js ```js
export default function handler(req, res) { export default function handler(req, res) {
res.statusCode = 200 res.status(200).json({ name: 'John Doe' }))
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify({ name: 'John Doe' }))
} }
``` ```
......
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction // Next.js API route support: https://nextjs.org/docs/api-routes/introduction
const hello = (req, res) => { const hello = (req, res) => {
res.statusCode = 200 res.status(200).json({ name: 'John Doe' })
res.json({ name: 'John Doe' })
} }
export default hello export default hello
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction // Next.js API route support: https://nextjs.org/docs/api-routes/introduction
export default function handler(req, res) { export default function handler(req, res) {
res.statusCode = 200 res.status(200).json({ name: 'John Doe' })
res.json({ name: 'John Doe' })
} }
...@@ -17,9 +17,8 @@ export default async function assetHandler(req, res) { ...@@ -17,9 +17,8 @@ export default async function assetHandler(req, res) {
}, },
}) })
} catch (e) { } catch (e) {
res.statusCode = 500
console.error('Request error', e) console.error('Request error', e)
res.json({ error: 'Error getting upload/asset' }) res.status(500).json({ error: 'Error getting upload/asset' })
} }
break break
default: default:
......
...@@ -16,9 +16,8 @@ export default async function uploadHandler(req, res) { ...@@ -16,9 +16,8 @@ export default async function uploadHandler(req, res) {
url: upload.url, url: upload.url,
}) })
} catch (e) { } catch (e) {
res.statusCode = 500
console.error('Request error', e) console.error('Request error', e)
res.json({ error: 'Error creating upload' }) res.status(500).json({ error: 'Error creating upload' })
} }
break break
default: default:
......
...@@ -16,9 +16,8 @@ export default async function uploadHandler(req, res) { ...@@ -16,9 +16,8 @@ export default async function uploadHandler(req, res) {
}, },
}) })
} catch (e) { } catch (e) {
res.statusCode = 500
console.error('Request error', e) console.error('Request error', e)
res.json({ error: 'Error getting upload/asset' }) res.status(500).json({ error: 'Error getting upload/asset' })
} }
break break
default: default:
......
...@@ -6,6 +6,5 @@ const doAsyncWork = () => Promise.reject(new Error('API Test 1')) ...@@ -6,6 +6,5 @@ const doAsyncWork = () => Promise.reject(new Error('API Test 1'))
doAsyncWork() doAsyncWork()
export default async function handler(req, res) { export default async function handler(req, res) {
res.statusCode = 200 res.status(200).json({ name: 'John Doe' })
res.json({ name: 'John Doe' })
} }
...@@ -9,6 +9,5 @@ function work() { ...@@ -9,6 +9,5 @@ function work() {
work() work()
export default async function handler(req, res) { export default async function handler(req, res) {
res.statusCode = 200 res.status(200).json({ name: 'John Doe' })
res.json({ name: 'John Doe' })
} }
...@@ -9,6 +9,5 @@ function work() { ...@@ -9,6 +9,5 @@ function work() {
export default async function handler(req, res) { export default async function handler(req, res) {
work() work()
res.statusCode = 200 res.status(200).json({ name: 'John Doe' })
res.json({ name: 'John Doe' })
} }
...@@ -14,6 +14,5 @@ export default async function handler(req, res) { ...@@ -14,6 +14,5 @@ export default async function handler(req, res) {
// Flushing before returning is necessary if deploying to Vercel, see // Flushing before returning is necessary if deploying to Vercel, see
// https://vercel.com/docs/platform/limits#streaming-responses // https://vercel.com/docs/platform/limits#streaming-responses
await Sentry.flush(2000) await Sentry.flush(2000)
res.statusCode = 200 res.status(200).json({ name: 'John Doe' })
res.json({ name: 'John Doe' })
} }
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction // Next.js API route support: https://nextjs.org/docs/api-routes/introduction
export default function helloAPI(req, res) { export default function helloAPI(req, res) {
res.statusCode = 200 res.status(200).json({ name: 'John Doe' })
res.json({ name: 'John Doe' })
} }
...@@ -3,8 +3,7 @@ ...@@ -3,8 +3,7 @@
import { NextApiRequest, NextApiResponse } from 'next' import { NextApiRequest, NextApiResponse } from 'next'
const handler = (req: NextApiRequest, res: NextApiResponse) => { const handler = (req: NextApiRequest, res: NextApiResponse) => {
res.statusCode = 200 res.status(200).json({ name: 'John Doe' })
res.json({ name: 'John Doe' })
} }
export default handler export default handler
...@@ -16,15 +16,12 @@ export default function getCollection( ...@@ -16,15 +16,12 @@ export default function getCollection(
.getCollection(parseInt(id.toString())) .getCollection(parseInt(id.toString()))
.then(toJson) .then(toJson)
.then((json) => { .then((json) => {
res.statusCode = 200
res.setHeader('Content-Type', 'application/json')
res.setHeader('Cache-Control', 'max-age=180000') res.setHeader('Cache-Control', 'max-age=180000')
res.end(JSON.stringify([json])) res.status(200).json([json])
resolve() resolve()
}) })
.catch((error) => { .catch((error) => {
res.json(error) res.status(405).json(error)
res.status(405).end()
resolve() resolve()
}) })
}) })
......
...@@ -15,15 +15,12 @@ export default function getCollections( ...@@ -15,15 +15,12 @@ export default function getCollections(
.then((json) => { .then((json) => {
json.map((c) => (c.slug = slug(c.title))) json.map((c) => (c.slug = slug(c.title)))
res.statusCode = 200
res.setHeader('Content-Type', 'application/json')
res.setHeader('Cache-Control', 'max-age=180000') res.setHeader('Cache-Control', 'max-age=180000')
res.end(JSON.stringify(json)) res.status(200).json(json)
resolve() resolve()
}) })
.catch((error) => { .catch((error) => {
res.json(error) res.status(405).json(error)
res.status(405).end()
resolve() resolve()
}) })
}) })
......
...@@ -16,15 +16,12 @@ export default function getCollectionPhotos( ...@@ -16,15 +16,12 @@ export default function getCollectionPhotos(
.getCollectionPhotos(parseInt(id.toString())) .getCollectionPhotos(parseInt(id.toString()))
.then(toJson) .then(toJson)
.then((json) => { .then((json) => {
res.statusCode = 200
res.setHeader('Content-Type', 'application/json')
res.setHeader('Cache-Control', 'max-age=180000') res.setHeader('Cache-Control', 'max-age=180000')
res.end(JSON.stringify(json)) res.status(200).json(json)
resolve() resolve()
}) })
.catch((error) => { .catch((error) => {
res.json(error) res.status(405).json(error)
res.status(405).end()
resolve() resolve()
}) })
}) })
......
...@@ -9,15 +9,12 @@ export default function getPhotos(req: NextApiRequest, res: NextApiResponse) { ...@@ -9,15 +9,12 @@ export default function getPhotos(req: NextApiRequest, res: NextApiResponse) {
.photos(process.env.UNSPLASH_USER, 1, 50, 'latest') .photos(process.env.UNSPLASH_USER, 1, 50, 'latest')
.then(toJson) .then(toJson)
.then((json: string) => { .then((json: string) => {
res.statusCode = 200
res.setHeader('Content-Type', 'application/json')
res.setHeader('Cache-Control', 'max-age=180000') res.setHeader('Cache-Control', 'max-age=180000')
res.end(JSON.stringify(json)) res.status(200).json(json)
resolve() resolve()
}) })
.catch((error) => { .catch((error) => {
res.json(error) res.status(405).json(error)
res.status(405).end()
resolve() resolve()
}) })
}) })
......
...@@ -9,15 +9,12 @@ export default function getStats(req: NextApiRequest, res: NextApiResponse) { ...@@ -9,15 +9,12 @@ export default function getStats(req: NextApiRequest, res: NextApiResponse) {
.statistics(process.env.UNSPLASH_USER, 'days', 30) .statistics(process.env.UNSPLASH_USER, 'days', 30)
.then(toJson) .then(toJson)
.then((json) => { .then((json) => {
res.statusCode = 200
res.setHeader('Content-Type', 'application/json')
res.setHeader('Cache-Control', 'max-age=180000') res.setHeader('Cache-Control', 'max-age=180000')
res.end(JSON.stringify(json)) res.status(200).json(json)
resolve() resolve()
}) })
.catch((error) => { .catch((error) => {
res.json(error) res.status(405).json(error)
res.status(405).end()
resolve() resolve()
}) })
}) })
......
...@@ -9,15 +9,12 @@ export default function getUser(req: NextApiRequest, res: NextApiResponse) { ...@@ -9,15 +9,12 @@ export default function getUser(req: NextApiRequest, res: NextApiResponse) {
.profile(process.env.UNSPLASH_USER) .profile(process.env.UNSPLASH_USER)
.then(toJson) .then(toJson)
.then((json) => { .then((json) => {
res.statusCode = 200
res.setHeader('Content-Type', 'application/json')
res.setHeader('Cache-Control', 'max-age=180000') res.setHeader('Cache-Control', 'max-age=180000')
res.end(JSON.stringify(json)) res.status(200).json([json])
resolve() resolve()
}) })
.catch((error) => { .catch((error) => {
res.json(error) res.status(405).json(error)
res.status(405).end()
resolve() resolve()
}) })
}) })
......
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction // Next.js API route support: https://nextjs.org/docs/api-routes/introduction
export default (req, res) => { export default (req, res) => {
res.statusCode = 200 res.status(200).json({ name: 'John Doe' })
res.json({ name: 'John Doe' })
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment