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

Simplify and convert `with-vercel-fetch` example to TypeScript (#43403)

Simplified the example to just the specific feature, and converted to TypeScript.

- `node-fetch@2` is either polyfilled by Next.js or a peerDep of `@vercel/fetch`, I can't quite tell, but either way it can be removed from the dependencies.

## Documentation / Examples

- [X] Make sure the linting passes by running `pnpm build && pnpm lint`
- [X] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent 396f9cc6
import fetch from 'unfetch'
export default fetch
{
"browser": "./browser.js",
"main": "./server.js"
}
import createFetch from '@vercel/fetch'
// since we aren't providing createFetch a fetcher it will use node-fetch as the fetcher
export default createFetch()
// For simplicity we are creating our own types here.
// If you want the full types check out:
// https://github.com/octokit/openapi-types.ts
export type Repository = {
id: number
name: string
full_name: string
stargazers_count: number
private: boolean
} & Record<string, unknown>
......@@ -6,11 +6,15 @@
"start": "next start"
},
"dependencies": {
"@vercel/fetch": "6.1.0",
"@vercel/fetch": "^6.2.0",
"next": "latest",
"node-fetch": "2.6.7",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"unfetch": "4.2.0"
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/node": "^18.11.9",
"@types/react": "^18.0.25",
"@types/react-dom": "^18.0.9",
"typescript": "^4.9.3"
}
}
import Link from 'next/link'
import fetch from '../fetch'
import type { InferGetStaticPropsType } from 'next'
import type { Repository } from '../github'
import createFetch from '@vercel/fetch'
export default function Index({ stars }) {
export async function getStaticProps() {
const fetch = createFetch()
const res = await fetch('https://api.github.com/repos/vercel/next.js')
const repo = (await res.json()) as Repository
return { props: { stars: repo.stargazers_count } }
}
export default function HomePage({
stars,
}: InferGetStaticPropsType<typeof getStaticProps>) {
return (
<div>
<p>Next.js has {stars} ⭐️</p>
<Link href="/preact">How about preact?</Link>
</div>
)
}
export async function getStaticProps() {
const res = await fetch('https://api.github.com/repos/vercel/next.js')
const json = await res.json() // better use it inside try .. catch
return {
props: { stars: json.stargazers_count },
}
}
import Link from 'next/link'
import fetch from '../fetch'
export default function Preact({ stars }) {
return (
<div>
<p>Preact has {stars} ⭐</p>
<Link href="/">I bet Next.js has more stars (?)</Link>
</div>
)
}
export async function getStaticProps() {
const res = await fetch('https://api.github.com/repos/preactjs/preact')
const json = await res.json() // better use it inside try .. catch
return {
props: { stars: json.stargazers_count },
}
}
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"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", "github.d.ts"],
"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