Unverified Commit b8efd80c authored by Max Proske's avatar Max Proske Committed by GitHub

Convert `api-routes-rest` example to TypeScript (#38394)

Converted `api-routes-rest` example to TypeScript to match Contribution docs.

## 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)
Co-authored-by: 's avatarBalázs Orbán <18369201+balazsorban44@users.noreply.github.com>
parent 65ea43f6
...@@ -32,3 +32,6 @@ yarn-error.log* ...@@ -32,3 +32,6 @@ yarn-error.log*
# vercel # vercel
.vercel .vercel
# typescript
*.tsbuildinfo
export type User = {
id: number
name?: 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": "^0.1.18" "swr": "^1.3.0"
},
"devDependencies": {
"@types/node": "^18.0.3",
"@types/react": "^18.0.15",
"@types/react-dom": "^18.0.6",
"typescript": "^4.7.4"
} }
} }
export default function userHandler(req, res) { import type { NextApiRequest, NextApiResponse } from 'next'
export default function userHandler(req: NextApiRequest, res: NextApiResponse) {
const { const {
query: { id, name }, query: { id, name },
method, method,
......
// Fake users data
const users = [{ id: 1 }, { id: 2 }, { id: 3 }]
export default function handler(req, res) {
// Get data from your database
res.status(200).json(users)
}
import type { NextApiRequest, NextApiResponse } from 'next'
import type { User } from '../../interfaces'
// Fake users data
const users: User[] = [{ id: 1 }, { id: 2 }, { id: 3 }]
export default function handler(_req: NextApiRequest, res: NextApiResponse) {
// Get data from your database
res.status(200).json(users)
}
import type { User } from '../interfaces'
import useSwr from 'swr' import useSwr from 'swr'
import Link from 'next/link' import Link from 'next/link'
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/users', fetcher) const { data, error } = useSwr<User[]>('/api/users', fetcher)
if (error) return <div>Failed to load users</div> if (error) return <div>Failed to load users</div>
if (!data) return <div>Loading...</div> if (!data) return <div>Loading...</div>
...@@ -14,7 +15,7 @@ export default function Index() { ...@@ -14,7 +15,7 @@ export default function Index() {
{data.map((user) => ( {data.map((user) => (
<li key={user.id}> <li key={user.id}>
<Link href="/user/[id]" as={`/user/${user.id}`}> <Link href="/user/[id]" as={`/user/${user.id}`}>
<a>{`User ${user.id}`}</a> {`User ${user.id}`}
</Link> </Link>
</li> </li>
))} ))}
......
import type { User } from '../../interfaces'
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
import useSwr from 'swr' import useSwr from 'swr'
const fetcher = (url) => fetch(url).then((res) => res.json()) const fetcher = (url: string) => fetch(url).then((res) => res.json())
export default function User() { export default function UserPage() {
const router = useRouter() const router = useRouter()
const { data, error } = useSwr( const { data, error } = useSwr<User>(
router.query.id ? `/api/user/${router.query.id}` : null, router.query.id ? `/api/user/${router.query.id}` : null,
fetcher fetcher
) )
......
{
"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