-
-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #488 from hey-api/docs/examples-3
chore: add working local example with fetch client
- Loading branch information
Showing
29 changed files
with
1,637 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
pnpm-debug.log* | ||
lerna-debug.log* | ||
|
||
node_modules | ||
dist | ||
dist-ssr | ||
*.local | ||
|
||
# Editor directories and files | ||
.vscode/* | ||
!.vscode/extensions.json | ||
.idea | ||
.DS_Store | ||
*.suo | ||
*.ntvs* | ||
*.njsproj | ||
*.sln | ||
*.sw? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<link rel="icon" type="image/svg+xml" href="/vite.svg" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Hey API + Fetch API Demo</title> | ||
</head> | ||
<body> | ||
<div id="root"></div> | ||
<script type="module" src="/src/main.tsx"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { defineConfig } from '@hey-api/openapi-ts'; | ||
|
||
export default defineConfig({ | ||
base: 'https://petstore3.swagger.io/api/v3', | ||
format: 'prettier', | ||
input: | ||
'https://raw.githubusercontent.com/swagger-api/swagger-petstore/master/src/main/resources/openapi.yaml', | ||
lint: 'eslint', | ||
output: './src/client', | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
"name": "@example/openapi-ts-fetch", | ||
"private": true, | ||
"version": "0.0.0", | ||
"type": "module", | ||
"scripts": { | ||
"build": "tsc && vite build", | ||
"dev": "vite", | ||
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0", | ||
"openapi-ts": "openapi-ts", | ||
"preview": "vite preview" | ||
}, | ||
"dependencies": { | ||
"@hey-api/client-fetch": "workspace:*", | ||
"react": "18.2.0", | ||
"react-dom": "18.2.0" | ||
}, | ||
"devDependencies": { | ||
"@hey-api/openapi-ts": "workspace:*", | ||
"@types/react": "18.2.79", | ||
"@types/react-dom": "18.2.25", | ||
"@typescript-eslint/eslint-plugin": "7.7.1", | ||
"@typescript-eslint/parser": "7.7.1", | ||
"@vitejs/plugin-react": "4.2.1", | ||
"eslint": "8.57.0", | ||
"eslint-plugin-react-hooks": "4.6.0", | ||
"eslint-plugin-react-refresh": "0.4.6", | ||
"prettier": "3.2.5", | ||
"typescript": "5.4.5", | ||
"vite": "5.2.10" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
:root { | ||
font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif; | ||
line-height: 1.5; | ||
font-weight: 400; | ||
|
||
color-scheme: light dark; | ||
color: rgba(255, 255, 255, 0.87); | ||
background-color: #242424; | ||
|
||
font-synthesis: none; | ||
text-rendering: optimizeLegibility; | ||
-webkit-font-smoothing: antialiased; | ||
-moz-osx-font-smoothing: grayscale; | ||
} | ||
|
||
.flex { | ||
align-items: center; | ||
display: flex; | ||
padding: 1em 0; | ||
} | ||
|
||
.pet-name { | ||
padding: 0 1em; | ||
} | ||
|
||
.button { | ||
border: 1px solid #444; | ||
} | ||
|
||
#root { | ||
max-width: 1280px; | ||
margin: 0 auto; | ||
padding: 0.5rem 2rem; | ||
} | ||
|
||
body { | ||
margin: 0; | ||
display: flex; | ||
place-items: center; | ||
min-width: 320px; | ||
min-height: 100vh; | ||
} | ||
|
||
.h1 { | ||
font-size: 1.2em; | ||
line-height: 1.1; | ||
} | ||
|
||
.logo { | ||
height: 4em; | ||
will-change: filter; | ||
transition: filter 300ms; | ||
} | ||
.logo:hover { | ||
filter: drop-shadow(0 0 2em #646cffaa); | ||
} | ||
.logo.react:hover { | ||
filter: drop-shadow(0 0 2em #61dafbaa); | ||
} | ||
|
||
@keyframes logo-spin { | ||
from { | ||
transform: rotate(0deg); | ||
} | ||
to { | ||
transform: rotate(360deg); | ||
} | ||
} | ||
|
||
@media (prefers-reduced-motion: no-preference) { | ||
a:nth-of-type(2) .logo { | ||
animation: logo-spin infinite 20s linear; | ||
} | ||
} | ||
|
||
.card { | ||
padding: 2em; | ||
} | ||
|
||
.container { | ||
align-items: center; | ||
display: flex; | ||
grid-gap: 1em; | ||
text-align: center; | ||
} | ||
|
||
.openapi-ts { | ||
color: #444; | ||
} | ||
|
||
@media (prefers-color-scheme: light) { | ||
:root { | ||
color: #213547; | ||
background-color: #ffffff; | ||
} | ||
a:hover { | ||
color: #747bff; | ||
} | ||
button { | ||
background-color: #f9f9f9; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import './App.css'; | ||
|
||
import { OpenAPI } from '@hey-api/client-fetch'; | ||
import { useState } from 'react'; | ||
|
||
import { $Pet } from './client/schemas.gen'; | ||
import { PetService } from './client/services.gen'; | ||
|
||
OpenAPI.BASE = 'https://petstore3.swagger.io/api/v3'; | ||
|
||
// OpenAPI.interceptors.response.use((response) => { | ||
// if (response.status === 200) { | ||
// console.log(`request to ${response.url} was successful`); | ||
// } | ||
// return response; | ||
// }); | ||
|
||
function App() { | ||
const [pet, setPet] = | ||
useState<Awaited<ReturnType<typeof PetService.getPetById>>>(); | ||
|
||
const onFetchPet = async () => { | ||
const pet = await PetService.getPetById({ | ||
// random id 1-10 | ||
petId: Math.floor(Math.random() * (10 - 1 + 1) + 1), | ||
}); | ||
setPet(pet); | ||
}; | ||
|
||
return ( | ||
<> | ||
<div className="container"> | ||
<a href="https://heyapi.vercel.app/" target="_blank"> | ||
<img | ||
src="https://heyapi.vercel.app/logo.png" | ||
className="logo vanilla" | ||
alt="Hey API logo" | ||
/> | ||
</a> | ||
<h1 className="h1">@hey-api/openapi-ts 🤝 Fetch API</h1> | ||
</div> | ||
<div className="flex"> | ||
<button className="button" onClick={onFetchPet}> | ||
Fetch random pet | ||
</button> | ||
<span className="pet-name">Fetched pet's name: {pet?.name}</span> | ||
</div> | ||
<div className="openapi-ts"> | ||
<code>{"import { $Pet } from './client/schemas.gen'"}</code> | ||
<pre>{JSON.stringify($Pet, null, 2)}</pre> | ||
</div> | ||
</> | ||
); | ||
} | ||
|
||
export default App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// This file is auto-generated by @hey-api/openapi-ts | ||
export * from './schemas.gen'; | ||
export * from './services.gen'; | ||
export * from './types.gen'; |
Oops, something went wrong.