Unverified Commit 1d8df75b authored by Max Proske's avatar Max Proske Committed by GitHub

chore(examples): Convert `api-routes` example to TypeScript (#38083)

Converted API Routes example over to TypeScript to match the Contribution guidelines.

## Documentation / Examples

- [X] Make sure the linting passes by running `pnpm lint`
- [X] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples)
parent dc3bd761
...@@ -32,3 +32,6 @@ yarn-error.log* ...@@ -32,3 +32,6 @@ yarn-error.log*
# vercel # vercel
.vercel .vercel
# typescript
*.tsbuildinfo
import Link from 'next/link' import Link from 'next/link'
import { Person } from '../interfaces'
export default function Person({ person }) { type PersonProps = {
person: Person
}
export default function PersonComponent({ person }: PersonProps) {
return ( return (
<li> <li>
<Link href="/person/[id]" as={`/person/${person.id}`}> <Link href="/person/[id]" as={`/person/${person.id}`}>
......
export type Person = {
id: string
name: string
height: string
mass: string
hair_color: string
skin_color: string
eye_color: string
gender: string
}
/// <reference types="next" />
/// <reference types="next/image-types/global" />
// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
...@@ -7,8 +7,14 @@ ...@@ -7,8 +7,14 @@
}, },
"dependencies": { "dependencies": {
"next": "latest", "next": "latest",
"react": "^17.0.2", "react": "^18.2.0",
"react-dom": "^17.0.2", "react-dom": "^18.2.0",
"swr": "^1.0.1" "swr": "^1.3.0"
},
"devDependencies": {
"@types/node": "^18.0.0",
"@types/react": "^18.0.14",
"@types/react-dom": "^18.0.5",
"typescript": "^4.7.4"
} }
} }
import { people } from '../../../data'
export default function personHandler({ query: { id } }, res) {
const filtered = people.filter((p) => p.id === id)
// User with id exists
if (filtered.length > 0) {
res.status(200).json(filtered[0])
} else {
res.status(404).json({ message: `User with id: ${id} not found.` })
}
}
import { NextApiRequest, NextApiResponse } from 'next'
import { people } from '../../../data'
import { Person } from '../../../interfaces'
type ResponseError = {
message: string
}
export default function personHandler(
req: NextApiRequest,
res: NextApiResponse<Person | ResponseError>
) {
const { query } = req
const { id } = query
const filtered = people.filter((p) => p.id === id)
// User with id exists
return filtered.length > 0
? res.status(200).json(filtered[0])
: res.status(404).json({ message: `User with id: ${id} not found.` })
}
import { people } from '../../../data'
export default function handler(req, res) {
res.status(200).json(people)
}
import { NextApiResponse, NextApiRequest } from 'next'
import { people } from '../../../data'
import { Person } from '../../../interfaces'
export default function handler(
_req: NextApiRequest,
res: NextApiResponse<Person[]>
) {
return res.status(200).json(people)
}
import useSWR from 'swr' import useSWR from 'swr'
import Person from '../components/Person' import PersonComponent from '../components/Person'
import { Person } from '../interfaces'
const fetcher = (url) => fetch(url).then((res) => res.json()) const fetcher = (url: string) => fetch(url).then((res) => res.json())
export default function Index() { export default function Index() {
const { data, error } = useSWR('/api/people', fetcher) const { data, error } = useSWR('/api/people', fetcher)
...@@ -11,8 +12,8 @@ export default function Index() { ...@@ -11,8 +12,8 @@ export default function Index() {
return ( return (
<ul> <ul>
{data.map((p, i) => ( {data.map((p: Person) => (
<Person key={i} person={p} /> <PersonComponent key={p.id} person={p} />
))} ))}
</ul> </ul>
) )
......
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
import useSWR from 'swr' import useSWR from 'swr'
const fetcher = async (url) => { const fetcher = async (url: string) => {
const res = await fetch(url) const res = await fetch(url)
const data = await res.json() const data = await res.json()
......
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}
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