Skip to content

Commit d9be9f0

Browse files
authored
chore: Add Biome as a formatter and linter (#104)
1 parent 5035fb5 commit d9be9f0

32 files changed

+642
-434
lines changed

.github/workflows/test.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ jobs:
4545
- name: Run tsc
4646
run: pnpm --filter @7nohe/react-app test:generated
4747

48+
- name: Run biome
49+
run: pnpm biome check .
50+
if: ${{ matrix.os == 'ubuntu-latest' }}
51+
4852
- name: Run test
4953
run: pnpm test
5054

biome.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"$schema": "https://biomejs.dev/schemas/1.7.2/schema.json",
3+
"organizeImports": {
4+
"enabled": true
5+
},
6+
"files": {
7+
"ignore": ["dist", "examples/react-app/openapi", "coverage"]
8+
},
9+
"linter": {
10+
"enabled": true,
11+
"rules": {
12+
"recommended": true
13+
}
14+
},
15+
"formatter": {
16+
"enabled": true,
17+
"indentStyle": "space",
18+
"indentWidth": 2
19+
}
20+
}

examples/react-app/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@
99
"dev:mock": "prism mock ./petstore.yaml --dynamic",
1010
"build": "tsc && vite build",
1111
"preview": "vite preview",
12-
"generate:api": "rimraf ./openapi && node ../../dist/cli.mjs -i ./petstore.yaml -c axios --request ./request.ts",
12+
"generate:api": "rimraf ./openapi && node ../../dist/cli.mjs -i ./petstore.yaml -c axios --request ./request.ts --format=biome --lint=biome",
1313
"test:generated": "tsc -p ./tsconfig.openapi.json --noEmit"
1414
},
1515
"dependencies": {
1616
"@tanstack/react-query": "^5.18.1",
1717
"axios": "^1.6.7",
1818
"form-data": "~4.0.0",
19-
"prettier": "^3.2.5",
2019
"react": "^18.2.0",
2120
"react-dom": "^18.2.0"
2221
},
2322
"devDependencies": {
23+
"@biomejs/biome": "1.7.2",
2424
"@stoplight/prism-cli": "^5.5.2",
2525
"@types/react": "^18.2.52",
2626
"@types/react-dom": "^18.2.18",

examples/react-app/request.ts

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,45 +18,48 @@ const axiosInstance = axios.create({
1818

1919
// Add a request interceptor
2020
axiosInstance.interceptors.request.use(
21-
function (config) {
22-
// Do something before request is sent
23-
if (!config.url || !config.params) {
24-
return config;
25-
}
26-
27-
Object.entries<any>(config.params).forEach(([key, value]) => {
28-
const stringToSearch = `{${key}}`;
29-
if(config.url !== undefined && config.url.search(stringToSearch) !== -1) {
30-
config.url = config.url.replace(`{${key}}`, encodeURIComponent(value));
31-
delete config.params[key];
32-
}
33-
});
21+
(config) => {
22+
// Do something before request is sent
23+
if (!config.url || !config.params) {
24+
return config;
25+
}
3426

35-
return config;
36-
},
37-
function (error) {
38-
// Do something with request error
39-
return Promise.reject(error);
27+
for (const [key, value] of Object.entries<string>(config.params)) {
28+
const stringToSearch = `{${key}}`;
29+
if (
30+
config.url !== undefined &&
31+
config.url.search(stringToSearch) !== -1
32+
) {
33+
config.url = config.url.replace(`{${key}}`, encodeURIComponent(value));
34+
delete config.params[key];
35+
}
4036
}
37+
38+
return config;
39+
},
40+
(error) => {
41+
// Do something with request error
42+
return Promise.reject(error);
43+
},
4144
);
4245

4346
// Add a response interceptor
4447
axiosInstance.interceptors.response.use(
45-
function (response) {
48+
(response) => {
4649
// Any status code that lie within the range of 2xx cause this function to trigger
4750
// Do something with response data
4851
return response;
4952
},
50-
function (error) {
53+
(error) => {
5154
// Any status codes that falls outside the range of 2xx cause this function to trigger
5255
// Do something with response error
5356
return Promise.reject(error);
54-
}
57+
},
5558
);
5659

5760
export const request = <T>(
5861
config: OpenAPIConfig,
59-
options: ApiRequestOptions
62+
options: ApiRequestOptions,
6063
): CancelablePromise<T> => {
6164
return new CancelablePromise((resolve, reject, onCancel) => {
6265
onCancel(() => source.cancel("The user aborted a request."));

examples/react-app/src/App.tsx

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import "./App.css";
2+
import { useState } from "react";
23
import {
4+
UseDefaultServiceFindPetsKeyFn,
35
useDefaultServiceAddPet,
46
useDefaultServiceFindPets,
5-
UseDefaultServiceFindPetsKeyFn,
67
useDefaultServiceGetNotDefined,
78
useDefaultServicePostNotDefined,
89
} from "../openapi/queries";
9-
import { useState } from "react";
10-
import { queryClient } from "./queryClient";
1110
import { SuspenseParent } from "./components/SuspenseParent";
11+
import { queryClient } from "./queryClient";
1212

1313
function App() {
1414
const [tags, _setTags] = useState<string[]>([]);
@@ -17,7 +17,7 @@ function App() {
1717
const { data, error, refetch } = useDefaultServiceFindPets({ tags, limit });
1818
// This is an example of using a hook that has all parameters optional;
1919
// Here we do not have to pass in an object
20-
const {} = useDefaultServiceFindPets();
20+
const { data: _ } = useDefaultServiceFindPets();
2121

2222
// This is an example of a query that is not defined in the OpenAPI spec
2323
// this defaults to any - here we are showing how to override the type
@@ -32,20 +32,23 @@ function App() {
3232
return (
3333
<div>
3434
<p>Failed to fetch pets</p>
35-
<button onClick={() => refetch()}>Retry</button>
35+
<button type="button" onClick={() => refetch()}>
36+
Retry
37+
</button>
3638
</div>
3739
);
3840

3941
return (
4042
<div className="App">
4143
<h1>Pet List</h1>
4244
<ul>
43-
{data instanceof Array &&
45+
{Array.isArray(data) &&
4446
data?.map((pet, index) => (
45-
<li key={pet.id + "-" + index}>{pet.name}</li>
47+
<li key={`${pet.id}-${index}`}>{pet.name}</li>
4648
))}
4749
</ul>
4850
<button
51+
type="button"
4952
onClick={() => {
5053
addPet(
5154
{
@@ -59,7 +62,7 @@ function App() {
5962
console.log("success");
6063
},
6164
onError: (error) => console.error(error),
62-
}
65+
},
6366
);
6467
}}
6568
>

examples/react-app/src/main.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import ReactDOM from "react-dom/client";
33
import App from "./App";
44
import "./index.css";
55
import { QueryClientProvider } from "@tanstack/react-query";
6-
import { queryClient } from "./queryClient";
76
import { prefetchUseDefaultServiceFindPets } from "../openapi/queries/prefetch";
7+
import { queryClient } from "./queryClient";
88

99
async function PrefetchData() {
1010
await prefetchUseDefaultServiceFindPets(queryClient);
@@ -16,6 +16,6 @@ PrefetchData().then(() => {
1616
<QueryClientProvider client={queryClient}>
1717
<App />
1818
</QueryClientProvider>
19-
</React.StrictMode>
19+
</React.StrictMode>,
2020
);
2121
});
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import { QueryClient } from '@tanstack/react-query';
1+
import { QueryClient } from "@tanstack/react-query";
22

33
export const queryClient = new QueryClient();

examples/react-app/vite.config.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { defineConfig } from 'vite'
2-
import react from '@vitejs/plugin-react'
1+
import react from "@vitejs/plugin-react";
2+
import { defineConfig } from "vite";
33

44
// https://vitejs.dev/config/
55
export default defineConfig({
6-
plugins: [react()]
7-
})
6+
plugins: [react()],
7+
});

lefthook.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
pre-commit:
2+
commands:
3+
check:
4+
glob: "*.{js,ts,cjs,mjs,d.cts,d.mts,jsx,tsx,json,jsonc}"
5+
run: npx biome check --apply --no-errors-on-unmatched --files-ignore-unknown=true ./ && git update-index --again
6+
test:
7+
glob: "*.{js,ts,cjs,mjs,d.cts,d.mts,jsx,tsx,json,jsonc}"
8+
run: npx vitest run

package.json

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
"openapi-rq": "dist/cli.mjs"
77
},
88
"type": "module",
9-
"workspaces": [
10-
"examples/*"
11-
],
9+
"workspaces": ["examples/*"],
1210
"scripts": {
1311
"build": "rimraf dist && tsc -p tsconfig.json",
1412
"preview": "npm run build && npm -C examples/react-app run generate:api",
@@ -22,9 +20,7 @@
2220
},
2321
"homepage": "https://github.com/7nohe/openapi-react-query-codegen",
2422
"bugs": "https://github.com/7nohe/openapi-react-query-codegen/issues",
25-
"files": [
26-
"dist"
27-
],
23+
"files": ["dist"],
2824
"keywords": [
2925
"codegen",
3026
"react-query",
@@ -38,11 +34,13 @@
3834
"author": "Daiki Urata (@7nohe)",
3935
"license": "MIT",
4036
"devDependencies": {
37+
"@biomejs/biome": "1.7.2",
4138
"@hey-api/openapi-ts": "0.42.1",
4239
"@types/node": "^20.10.6",
4340
"@vitest/coverage-v8": "^1.5.0",
4441
"commander": "^12.0.0",
4542
"glob": "^10.3.10",
43+
"lefthook": "^1.6.10",
4644
"rimraf": "^5.0.5",
4745
"ts-morph": "^22.0.0",
4846
"typescript": "^5.3.3",

0 commit comments

Comments
 (0)