Skip to content

Commit

Permalink
chore: add sample app for swr with fetch as an http client (#1561)
Browse files Browse the repository at this point in the history
  • Loading branch information
soartec-lab authored Aug 3, 2024
1 parent e67d4b0 commit 44ac828
Show file tree
Hide file tree
Showing 27 changed files with 945 additions and 1 deletion.
18 changes: 18 additions & 0 deletions samples/react-app-with-swr/fetch-client/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = {
root: true,
env: { browser: true, es2020: true },
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:react-hooks/recommended',
],
ignorePatterns: ['dist', '.eslintrc.cjs'],
parser: '@typescript-eslint/parser',
plugins: ['react-refresh'],
rules: {
'react-refresh/only-export-components': [
'warn',
{ allowConstantExport: true },
],
},
};
24 changes: 24 additions & 0 deletions samples/react-app-with-swr/fetch-client/.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?
30 changes: 30 additions & 0 deletions samples/react-app-with-swr/fetch-client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# React + TypeScript + Vite

This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.

Currently, two official plugins are available:

- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md) uses [Babel](https://babeljs.io/) for Fast Refresh
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh

## Expanding the ESLint configuration

If you are developing a production application, we recommend updating the configuration to enable type aware lint rules:

- Configure the top-level `parserOptions` property like this:

```js
export default {
// other rules...
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
project: ['./tsconfig.json', './tsconfig.node.json', './tsconfig.app.json'],
tsconfigRootDir: __dirname,
},
};
```

- Replace `plugin:@typescript-eslint/recommended` to `plugin:@typescript-eslint/recommended-type-checked` or `plugin:@typescript-eslint/strict-type-checked`
- Optionally add `plugin:@typescript-eslint/stylistic-type-checked`
- Install [eslint-plugin-react](https://github.com/jsx-eslint/eslint-plugin-react) and add `plugin:react/recommended` & `plugin:react/jsx-runtime` to the `extends` list
13 changes: 13 additions & 0 deletions samples/react-app-with-swr/fetch-client/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>Vite + React + TS</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
19 changes: 19 additions & 0 deletions samples/react-app-with-swr/fetch-client/orval.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { faker } from '@faker-js/faker';
import { defineConfig } from 'orval';

export default defineConfig({
petstore: {
output: {
baseUrl: 'http://localhost:8000',
mode: 'split',
target: 'src/api/endpoints',
schemas: 'src/api/models',
client: 'swr',
httpClient: 'fetch',
mock: true,
},
input: {
target: './petstore.yaml',
},
},
});
31 changes: 31 additions & 0 deletions samples/react-app-with-swr/fetch-client/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "swr-with-fetch-client",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite --host",
"build": "tsc -b && vite build",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview",
"generate-api": "node ../../../packages/orval/dist/bin/orval.js"
},
"dependencies": {
"react": "^18.3.1",
"react-dom": "^18.3.1",
"swr": "^2.2.5"
},
"devDependencies": {
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"@typescript-eslint/eslint-plugin": "^7.15.0",
"@typescript-eslint/parser": "^7.15.0",
"@vitejs/plugin-react": "^4.3.1",
"eslint": "^8.57.0",
"eslint-plugin-react-hooks": "^4.6.2",
"eslint-plugin-react-refresh": "^0.4.7",
"orval": "link:../../../packages/orval/dist",
"typescript": "^5.2.2",
"vite": "^5.3.4"
}
}
140 changes: 140 additions & 0 deletions samples/react-app-with-swr/fetch-client/petstore.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
openapi: '3.0.0'
info:
version: 1.0.0
title: Swagger Petstore
license:
name: MIT
servers:
- url: http://petstore.swagger.io/v1
paths:
/pets:
get:
summary: List all pets
operationId: listPets
tags:
- pets
parameters:
- name: limit
in: query
description: How many items to return at one time (max 100)
required: false
schema:
type: string
responses:
'200':
description: A paged array of pets
headers:
x-next:
description: A link to the next page of responses
schema:
type: string
content:
application/json:
schema:
$ref: '#/components/schemas/Pets'
default:
description: unexpected error
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
post:
summary: Create a pet
operationId: createPets
tags:
- pets
requestBody:
required: true
content:
application/json:
schema:
type: object
required:
- 'name'
- 'tag'
properties:
name:
type: string
tag:
type: string
responses:
'200':
description: Created Pet
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
default:
description: unexpected error
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
/pets/{petId}:
get:
summary: Info for a specific pet
operationId: showPetById
tags:
- pets
parameters:
- name: petId
in: path
required: true
description: The id of the pet to retrieve
schema:
type: string
- name: testId
in: path
required: true
description: The id of the pet to retrieve
schema:
type: string
responses:
'200':
description: Expected response to a valid request
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
default:
description: unexpected error
content:
application/json:
schema:
$ref: '#/components/schemas/Error'
components:
schemas:
Pet:
type: object
required:
- id
- name
properties:
'@id':
type: string
format: iri-reference
id:
type: integer
format: int64
name:
type: string
tag:
type: string
email:
type: string
format: email
Pets:
type: array
items:
$ref: '#/components/schemas/Pet'
Error:
type: object
required:
- code
- message
properties:
code:
type: integer
format: int32
message:
type: string
1 change: 1 addition & 0 deletions samples/react-app-with-swr/fetch-client/public/vite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 42 additions & 0 deletions samples/react-app-with-swr/fetch-client/src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#root {
max-width: 1280px;
margin: 0 auto;
padding: 2rem;
text-align: center;
}

.logo {
height: 6em;
padding: 1.5em;
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;
}

.read-the-docs {
color: #888;
}
46 changes: 46 additions & 0 deletions samples/react-app-with-swr/fetch-client/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import reactLogo from './assets/react.svg';
import viteLogo from '/vite.svg';
import './App.css';

import { Pet } from './api/models/pet';
import { useListPets } from './api/endpoints/swaggerPetstore';
import { useCreatePets } from './api/endpoints/swaggerPetstore';

function App() {
const { data } = useListPets();
const { trigger } = useCreatePets();

return (
<>
<h1>SWR with fetch client</h1>
<div>
{data?.data &&
data.data.map((pet: Pet, index: number) => (
<div key={index}>
<p>id: {pet.id}</p>
<p>name: {pet.name}</p>
<p>tag: {pet.tag}</p>
</div>
))}
<button
onClick={() => {
trigger({
name: 'test',
tag: 'test',
});
}}
>
Add pet
</button>
<a href="https://vitejs.dev" target="_blank">
<img src={viteLogo} className="logo" alt="Vite logo" />
</a>
<a href="https://react.dev" target="_blank">
<img src={reactLogo} className="logo react" alt="React logo" />
</a>
</div>
</>
);
}

export default App;
Loading

0 comments on commit 44ac828

Please sign in to comment.