Skip to content

Commit

Permalink
Merge pull request #488 from hey-api/docs/examples-3
Browse files Browse the repository at this point in the history
chore: add working local example with fetch client
  • Loading branch information
mrlubos authored Apr 24, 2024
2 parents af193f2 + f06e11e commit 3296a2e
Show file tree
Hide file tree
Showing 29 changed files with 1,637 additions and 40 deletions.
2 changes: 1 addition & 1 deletion examples/openapi-ts-axios/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@examples/openapi-ts-axios",
"name": "@example/openapi-ts-axios",
"private": true,
"version": "0.0.0",
"type": "module",
Expand Down
2 changes: 1 addition & 1 deletion examples/openapi-ts-axios/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function App() {
alt="Hey API logo"
/>
</a>
<h1 className="h1">@hey-api/openapi-ts example</h1>
<h1 className="h1">@hey-api/openapi-ts 🤝 Axios</h1>
</div>
<div className="flex">
<button className="button" onClick={onFetchPet}>
Expand Down
24 changes: 24 additions & 0 deletions examples/openapi-ts-fetch/.gitignore
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?
13 changes: 13 additions & 0 deletions examples/openapi-ts-fetch/index.html
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>
10 changes: 10 additions & 0 deletions examples/openapi-ts-fetch/openapi-ts.config.ts
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',
});
32 changes: 32 additions & 0 deletions examples/openapi-ts-fetch/package.json
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"
}
}
102 changes: 102 additions & 0 deletions examples/openapi-ts-fetch/src/App.css
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;
}
}
56 changes: 56 additions & 0 deletions examples/openapi-ts-fetch/src/App.tsx
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;
4 changes: 4 additions & 0 deletions examples/openapi-ts-fetch/src/client/index.ts
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';
Loading

0 comments on commit 3296a2e

Please sign in to comment.