Skip to content

Commit

Permalink
Simplify and convert with-vercel-fetch example to TypeScript (#43403)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
maxproske committed Nov 30, 2022
1 parent 396f9cc commit faaba0f
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 53 deletions.
3 changes: 0 additions & 3 deletions examples/with-vercel-fetch/fetch/browser.js

This file was deleted.

4 changes: 0 additions & 4 deletions examples/with-vercel-fetch/fetch/package.json

This file was deleted.

4 changes: 0 additions & 4 deletions examples/with-vercel-fetch/fetch/server.js

This file was deleted.

10 changes: 10 additions & 0 deletions examples/with-vercel-fetch/github.d.ts
@@ -0,0 +1,10 @@
// 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>
12 changes: 8 additions & 4 deletions examples/with-vercel-fetch/package.json
Expand Up @@ -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"
}
}
19 changes: 0 additions & 19 deletions examples/with-vercel-fetch/pages/index.js

This file was deleted.

21 changes: 21 additions & 0 deletions examples/with-vercel-fetch/pages/index.tsx
@@ -0,0 +1,21 @@
import type { InferGetStaticPropsType } from 'next'
import type { Repository } from '../github'
import createFetch from '@vercel/fetch'

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>
</div>
)
}
19 changes: 0 additions & 19 deletions examples/with-vercel-fetch/pages/preact.js

This file was deleted.

20 changes: 20 additions & 0 deletions examples/with-vercel-fetch/tsconfig.json
@@ -0,0 +1,20 @@
{
"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"]
}

0 comments on commit faaba0f

Please sign in to comment.