-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: Add react router examples (#166)
- Loading branch information
Showing
35 changed files
with
2,268 additions
and
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
title: React Router 6 Example | ||
description: A simple example of using React Router 6 with OpenAPI React Query Codegen. | ||
--- | ||
|
||
Example of using React Router 6 can be found in the [`examples/react-router-6-app`](https://github.com/7nohe/openapi-react-query-codegen/tree/main/examples/react-router-6-app) directory of the repository. |
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,6 @@ | ||
--- | ||
title: React Router 7 Example | ||
description: A simple example of using React Router 7 with OpenAPI React Query Codegen. | ||
--- | ||
|
||
Example of using React Router 7 can be found in the [`examples/react-router-7-app`](https://github.com/7nohe/openapi-react-query-codegen/tree/main/examples/react-router-7-app) directory of the repository. |
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>Vite + React + TS</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,35 @@ | ||
{ | ||
"name": "@7nohe/react-router-6-app", | ||
"private": true, | ||
"version": "0.0.0", | ||
"type": "module", | ||
"scripts": { | ||
"dev": "run-p dev:mock dev:client", | ||
"dev:client": "vite --clearScreen=false", | ||
"dev:mock": "prism mock ../petstore.yaml --dynamic", | ||
"build": "tsc && vite build", | ||
"preview": "vite preview", | ||
"generate:api": "rimraf ./openapi && node ../../dist/cli.mjs -i ../petstore.yaml --format=biome --lint=biome", | ||
"test:generated": "tsc -p ./tsconfig.json --noEmit" | ||
}, | ||
"dependencies": { | ||
"@hey-api/client-axios": "^0.2.7", | ||
"@tanstack/react-query": "^5.59.13", | ||
"@tanstack/react-query-devtools": "^5.32.1", | ||
"axios": "^1.7.7", | ||
"form-data": "~4.0.0", | ||
"react": "^18.3.1", | ||
"react-dom": "^18.3.1", | ||
"react-router-dom": "^6.27.0" | ||
}, | ||
"devDependencies": { | ||
"@biomejs/biome": "^1.7.2", | ||
"@stoplight/prism-cli": "^5.5.2", | ||
"@types/react": "^18.3.1", | ||
"@types/react-dom": "^18.2.18", | ||
"@vitejs/plugin-react": "^4.2.1", | ||
"npm-run-all": "^4.1.5", | ||
"typescript": "^5.4.5", | ||
"vite": "^5.0.12" | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,41 @@ | ||
#root { | ||
max-width: 1280px; | ||
margin: 0 auto; | ||
padding: 2rem; | ||
text-align: center; | ||
} | ||
|
||
.logo { | ||
height: 6em; | ||
padding: 1.5em; | ||
will-change: filter; | ||
} | ||
.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; | ||
} | ||
|
||
.read-the-docs { | ||
color: #888; | ||
} |
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,94 @@ | ||
import "./App.css"; | ||
import type { QueryClient } from "@tanstack/react-query"; | ||
import { useState } from "react"; | ||
import { type LoaderFunctionArgs, useLoaderData } from "react-router-dom"; | ||
import { UseFindPetsKeyFn, useAddPet } from "../openapi/queries"; | ||
import { ensureUseFindPetsData } from "../openapi/queries/ensureQueryData"; | ||
import { useFindPetsSuspense } from "../openapi/queries/suspense"; | ||
import { queryClient } from "./queryClient"; | ||
|
||
export const loader = | ||
(queryClient: QueryClient) => async (_: LoaderFunctionArgs) => { | ||
const queryParameters = { | ||
query: { tags: [], limit: 10 }, | ||
}; | ||
|
||
await ensureUseFindPetsData(queryClient, { | ||
query: { tags: [], limit: 10 }, | ||
}); | ||
return queryParameters; | ||
}; | ||
|
||
export function Compoment() { | ||
const queryParameters = useLoaderData() as Awaited< | ||
ReturnType<ReturnType<typeof loader>> | ||
>; | ||
|
||
const { data, error, refetch } = useFindPetsSuspense(queryParameters); | ||
|
||
const { mutate: addPet, isError } = useAddPet(); | ||
|
||
const [text, setText] = useState<string>(""); | ||
const [errorText, setErrorText] = useState<string>(); | ||
|
||
if (error) | ||
return ( | ||
<div> | ||
<p>Failed to fetch pets</p> | ||
<button type="button" onClick={() => refetch()}> | ||
Retry | ||
</button> | ||
</div> | ||
); | ||
|
||
return ( | ||
<div className="App"> | ||
<h1>Pet List</h1> | ||
<input | ||
type="text" | ||
value={text} | ||
placeholder="Type pet name" | ||
onChange={(e) => setText(e.target.value)} | ||
/> | ||
<button | ||
type="button" | ||
onClick={() => { | ||
addPet( | ||
{ | ||
body: { name: text }, | ||
}, | ||
{ | ||
onSuccess: () => { | ||
queryClient.invalidateQueries({ | ||
queryKey: UseFindPetsKeyFn(queryParameters), | ||
}); | ||
console.log("success"); | ||
}, | ||
onError: (error) => { | ||
console.log(error.message); | ||
setErrorText(`Error: ${error.message}`); | ||
}, | ||
}, | ||
); | ||
}} | ||
> | ||
Create a pet | ||
</button> | ||
{isError && ( | ||
<p | ||
style={{ | ||
color: "red", | ||
}} | ||
> | ||
{errorText} | ||
</p> | ||
)} | ||
<ul> | ||
{Array.isArray(data) && | ||
data?.map((pet, index) => ( | ||
<li key={`${pet.id}-${index}`}>{pet.name}</li> | ||
))} | ||
</ul> | ||
</div> | ||
); | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,6 @@ | ||
import { client } from "../openapi/requests/services.gen"; | ||
|
||
client.setConfig({ | ||
baseUrl: "http://localhost:4010", | ||
throwOnError: true, | ||
}); |
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,87 @@ | ||
:root { | ||
font-family: Inter, Avenir, Helvetica, Arial, sans-serif; | ||
font-size: 16px; | ||
line-height: 24px; | ||
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; | ||
-webkit-text-size-adjust: 100%; | ||
} | ||
|
||
a { | ||
font-weight: 500; | ||
color: #646cff; | ||
text-decoration: inherit; | ||
} | ||
a:hover { | ||
color: #535bf2; | ||
} | ||
|
||
body { | ||
margin: 0; | ||
display: flex; | ||
place-items: center; | ||
min-width: 320px; | ||
min-height: 100vh; | ||
} | ||
|
||
h1 { | ||
font-size: 3.2em; | ||
line-height: 1.1; | ||
} | ||
|
||
button { | ||
border-radius: 8px; | ||
border: 1px solid transparent; | ||
padding: 0.6em 1.2em; | ||
font-size: 1em; | ||
font-weight: 500; | ||
font-family: inherit; | ||
background-color: #1a1a1a; | ||
cursor: pointer; | ||
transition: border-color 0.25s; | ||
} | ||
button:hover { | ||
border-color: #646cff; | ||
} | ||
button:focus, | ||
button:focus-visible { | ||
outline: 4px auto -webkit-focus-ring-color; | ||
} | ||
|
||
@media (prefers-color-scheme: light) { | ||
:root { | ||
color: #213547; | ||
background-color: #ffffff; | ||
} | ||
a:hover { | ||
color: #747bff; | ||
} | ||
button { | ||
background-color: #f9f9f9; | ||
} | ||
} | ||
|
||
input { | ||
border-radius: 8px; | ||
border: 1px solid #ccc; | ||
padding: 0.5em; | ||
font-size: 1em; | ||
font-family: inherit; | ||
background-color: #fff; | ||
color: #000; | ||
transition: border-color 0.25s; | ||
margin: 1em; | ||
} | ||
|
||
input:focus { | ||
border-color: #646cff; | ||
outline: none; | ||
} |
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,26 @@ | ||
import React from "react"; | ||
import ReactDOM from "react-dom/client"; | ||
import { Compoment, loader } from "./App"; | ||
import "./index.css"; | ||
import { QueryClientProvider } from "@tanstack/react-query"; | ||
import { ReactQueryDevtools } from "@tanstack/react-query-devtools"; | ||
import { queryClient } from "./queryClient"; | ||
import "./axios"; | ||
import { RouterProvider, createBrowserRouter } from "react-router-dom"; | ||
|
||
const router = createBrowserRouter([ | ||
{ | ||
path: "/", | ||
element: <Compoment />, | ||
loader: loader(queryClient), | ||
}, | ||
]); | ||
|
||
ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render( | ||
<React.StrictMode> | ||
<QueryClientProvider client={queryClient}> | ||
<RouterProvider router={router} /> | ||
<ReactQueryDevtools buttonPosition="bottom-left" /> | ||
</QueryClientProvider> | ||
</React.StrictMode>, | ||
); |
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,3 @@ | ||
import { QueryClient } from "@tanstack/react-query"; | ||
|
||
export const queryClient = new QueryClient(); |
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 @@ | ||
/// <reference types="vite/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,25 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ESNext", | ||
"useDefineForClassFields": true, | ||
"lib": ["DOM", "DOM.Iterable", "ESNext"], | ||
"allowJs": false, | ||
"skipLibCheck": true, | ||
"esModuleInterop": false, | ||
"allowSyntheticDefaultImports": true, | ||
"strict": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"module": "ESNext", | ||
"moduleResolution": "Bundler", | ||
"resolveJsonModule": true, | ||
"isolatedModules": true, | ||
"noEmit": true, | ||
"jsx": "react-jsx" | ||
}, | ||
"include": ["src"], | ||
"references": [ | ||
{ | ||
"path": "./tsconfig.node.json" | ||
} | ||
] | ||
} |
Oops, something went wrong.