Skip to content

Commit fd82abb

Browse files
authored
chroe: Release v2 (#161)
1 parent be5cd9d commit fd82abb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+3207
-2501
lines changed

.github/workflows/comment-release.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ jobs:
5353
github-token: ${{ secrets.GITHUB_TOKEN }}
5454
script: |
5555
const { issue: { number: issue_number }, repo: { owner, repo }, payload } = context;
56-
const { name: packageName, version } = require(`${process.env.GITHUB_WORKSPACE}/package.json`);
56+
const fs = require('fs')
57+
const jsonString = fs.readFileSync(`${process.env.GITHUB_WORKSPACE}/package.json`)
58+
var packageJson = JSON.parse(jsonString)
59+
const { name: packageName, version } = packageJson;
5760
5861
const body = [
5962
`npm package published to pre tag.`,

.github/workflows/test.yml

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,30 @@ jobs:
3333
- name: Build
3434
run: pnpm -w build
3535

36-
- name: Run codegen
36+
- name: Run codegen for react-app
3737
run: pnpm --filter @7nohe/react-app generate:api
3838

39-
- name: Archive generated query file
39+
- name: Run codegen for nextjs-app
40+
run: pnpm --filter nextjs-app generate:api
41+
42+
- name: Run codegen for tanstack-router-app
43+
run: pnpm --filter tanstack-router-app generate:api
44+
45+
- name: Archive generated query files
4046
uses: actions/upload-artifact@v4
4147
with:
4248
name: generated-query-file-${{ matrix.os }}
43-
path: examples/react-app/openapi/queries/index.ts
49+
path: examples/react-app/openapi/queries
4450

45-
- name: Run tsc
51+
- name: Run tsc in react-app
4652
run: pnpm --filter @7nohe/react-app test:generated
4753

54+
- name: Run tsc in nextjs-app
55+
run: pnpm --filter nextjs-app test:generated
56+
57+
- name: Run tsc in tanstack-router-app
58+
run: pnpm --filter tanstack-router-app test:generated
59+
4860
- name: Run biome
4961
run: pnpm biome check .
5062
if: ${{ matrix.os == 'ubuntu-latest' }}

README.md

Lines changed: 28 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ Register the command to the `scripts` property in your package.json file.
2222
```json
2323
{
2424
"scripts": {
25-
"codegen": "openapi-rq -i ./petstore.yaml -c axios"
25+
"codegen": "openapi-rq -i ./petstore.yaml -c @hey-api/client-fetch"
2626
}
2727
}
2828
```
2929

3030
You can also run the command without installing it in your project using the npx command.
3131

3232
```bash
33-
$ npx --package @7nohe/openapi-react-query-codegen openapi-rq -i ./petstore.yaml -c axios
33+
$ npx --package @7nohe/openapi-react-query-codegen openapi-rq -i ./petstore.yaml -c @hey-api/client-fetch
3434
```
3535

3636
## Usage
@@ -46,23 +46,20 @@ Options:
4646
-V, --version output the version number
4747
-i, --input <value> OpenAPI specification, can be a path, url or string content (required)
4848
-o, --output <value> Output directory (default: "openapi")
49-
-c, --client <value> HTTP client to generate (choices: "angular", "axios", "fetch", "node", "xhr", default: "fetch")
50-
--request <value> Path to custom request file
49+
-c, --client <value> HTTP client to generate (choices: "@hey-api/client-fetch", "@hey-api/client-axios", default: "@hey-api/client-fetch")
5150
--format <value> Process output folder with formatter? (choices: "biome", "prettier")
5251
--lint <value> Process output folder with linter? (choices: "biome", "eslint")
5352
--operationId Use operation ID to generate operation names?
5453
--serviceResponse <value> Define shape of returned value from service calls (choices: "body", "response", default: "body")
55-
--base <value> Manually set base in OpenAPI config instead of inferring from server value
56-
--enums <value> Generate JavaScript objects from enum definitions? ['javascript', 'typescript', 'typescript+namespace']
5754
--enums <value> Generate JavaScript objects from enum definitions? (choices: "javascript", "typescript")
5855
--useDateType Use Date type instead of string for date types for models, this will not convert the data to a Date object
5956
--debug Run in debug mode?
6057
--noSchemas Disable generating JSON schemas
6158
--schemaType <value> Type of JSON schema [Default: 'json'] (choices: "form", "json")
6259
--pageParam <value> Name of the query parameter used for pagination (default: "page")
6360
--nextPageParam <value> Name of the response parameter used for next page (default: "nextPage")
64-
--initialPageParam <value> Initial value for the pagination parameter (default: "1")
65-
-h, --help display help for command
61+
--initialPageParam <value> Initial page value to query (default: "initialPageParam")
62+
-h, --help display help for command
6663
```
6764

6865
### Example Usage
@@ -95,9 +92,9 @@ $ openapi-rq -i ./petstore.yaml
9592

9693
```tsx
9794
// App.tsx
98-
import { usePetServiceFindPetsByStatus } from "../openapi/queries";
95+
import { useFindPets } from "../openapi/queries";
9996
function App() {
100-
const { data } = usePetServiceFindPetsByStatus({ status: ["available"] });
97+
const { data } = useFindPets();
10198

10299
return (
103100
<div className="App">
@@ -114,16 +111,16 @@ export default App;
114111

115112
```tsx
116113
import { useQuery } from "@tanstack/react-query";
117-
import { PetService } from "../openapi/requests/services";
118-
import { usePetServiceFindPetsByStatusKey } from "../openapi/queries";
114+
import { findPets } from "../openapi/requests/services.gen";
115+
import { useFindPetsKey } from "../openapi/queries";
119116

120117
function App() {
121118
// You can still use the auto-generated query key
122119
const { data } = useQuery({
123-
queryKey: [usePetServiceFindPetsByStatusKey],
120+
queryKey: [useFindPetsKey],
124121
queryFn: () => {
125122
// Do something here
126-
return PetService.findPetsByStatus(["available"]);
123+
return findPets();
127124
},
128125
});
129126

@@ -137,9 +134,11 @@ export default App;
137134

138135
```tsx
139136
// App.tsx
140-
import { useDefaultClientFindPetsSuspense } from "../openapi/queries/suspense";
137+
import { useFindPetsSuspense } from "../openapi/queries/suspense";
141138
function ChildComponent() {
142-
const { data } = useDefaultClientFindPetsSuspense({ tags: [], limit: 10 });
139+
const { data } = useFindPetsSuspense({
140+
query: { tags: [], limit: 10 },
141+
});
143142

144143
return <ul>{data?.map((pet, index) => <li key={pet.id}>{pet.name}</li>)}</ul>;
145144
}
@@ -170,13 +169,13 @@ export default App;
170169

171170
```tsx
172171
// App.tsx
173-
import { usePetServiceAddPet } from "../openapi/queries";
172+
import { useAddPet } from "../openapi/queries";
174173

175174
function App() {
176-
const { mutate } = usePetServiceAddPet();
175+
const { mutate } = useAddPet();
177176

178177
const handleAddPet = () => {
179-
mutate({ name: "Fluffy", status: "available" });
178+
mutate({ body: { name: "Fluffy" } });
180179
};
181180

182181
return (
@@ -200,22 +199,22 @@ To ensure the query key is created the same way as the query hook, you can use t
200199

201200
```tsx
202201
import {
203-
usePetServiceFindPetsByStatus,
204-
usePetServiceAddPet,
205-
UsePetServiceFindPetsByStatusKeyFn,
202+
useFindPetsByStatus,
203+
useAddPet,
204+
UseFindPetsByStatusKeyFn,
206205
} from "../openapi/queries";
207206

208207
// App.tsx
209208
function App() {
210209
const [status, setStatus] = React.useState(["available"]);
211-
const { data } = usePetServiceFindPetsByStatus({ status });
212-
const { mutate } = usePetServiceAddPet({
210+
const { data } = useFindPetsByStatus({ status });
211+
const { mutate } = useAddPet({
213212
onSuccess: () => {
214213
queryClient.invalidateQueries({
215214
// Call the query key function to get the query key
216215
// This is important to ensure the query key is created the same way as the query hook
217216
// This insures the cache is invalidated correctly and is typed correctly
218-
queryKey: [UsePetServiceFindPetsByStatusKeyFn({
217+
queryKey: [UseFindPetsByStatusKeyFn({
219218
status
220219
})],
221220
});
@@ -300,42 +299,13 @@ paths:
300299
Usage of Generated Hooks:
301300
302301
```ts
303-
import { useDefaultServiceFindPaginatedPetsInfinite } from "@/openapi/queries/infiniteQueries";
302+
import { useFindPaginatedPetsInfinite } from "@/openapi/queries/infiniteQueries";
304303

305-
const { data, fetchNextPage } = useDefaultServiceFindPaginatedPetsInfinite({
306-
limit: 10,
307-
tags: [],
304+
const { data, fetchNextPage } = useFindPaginatedPetsInfinite({
305+
query: { tags: [], limit: 10 }
308306
});
309307
```
310308

311-
##### Runtime Configuration
312-
313-
You can modify the default values used by the generated service calls by modifying the OpenAPI configuration singleton object.
314-
315-
It's default location is `openapi/requests/core/OpenAPI.ts` and it is also exported from `openapi/index.ts`
316-
317-
Import the constant into your runtime and modify it before setting up the react app.
318-
319-
```typescript
320-
/** main.tsx */
321-
import { OpenAPI as OpenAPIConfig } from './openapi/requests/core/OpenAPI';
322-
...
323-
OpenAPIConfig.BASE = 'www.domain.com/api';
324-
OpenAPIConfig.HEADERS = {
325-
'x-header-1': 'value-1',
326-
'x-header-2': 'value-2',
327-
};
328-
...
329-
ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
330-
<React.StrictMode>
331-
<QueryClientProvider client={queryClient}>
332-
<App />
333-
</QueryClientProvider>
334-
</React.StrictMode>
335-
);
336-
337-
```
338-
339309
## Development
340310

341311
### Install dependencies
@@ -365,6 +335,7 @@ pnpm snapshot
365335
```
366336

367337
### Build example and validate generated code
338+
368339
```bash
369340
npm run build && pnpm --filter @7nohe/react-app generate:api && pnpm --filter @7nohe/react-app test:generated
370341
```

biome.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
},
66
"files": {
77
"ignore": [
8+
".vscode",
89
"dist",
910
"examples/react-app/openapi",
1011
"coverage",

examples/nextjs-app/app/components/PaginatedPets.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
"use client";
22

3-
import { useDefaultServiceFindPaginatedPetsInfinite } from "@/openapi/queries/infiniteQueries";
3+
import { useFindPaginatedPetsInfinite } from "@/openapi/queries/infiniteQueries";
44
import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
55
import React from "react";
66

77
export default function PaginatedPets() {
8-
const { data, fetchNextPage } = useDefaultServiceFindPaginatedPetsInfinite({
9-
limit: 10,
10-
tags: [],
8+
const { data, fetchNextPage } = useFindPaginatedPetsInfinite({
9+
query: {
10+
limit: 10,
11+
tags: [],
12+
},
1113
});
1214

1315
return (
1416
<>
1517
<h1>Pet List with Pagination</h1>
1618
<ul>
1719
{data?.pages.map((group, i) => (
18-
<React.Fragment key={group.pets?.at(0)?.id}>
19-
{group.pets?.map((pet) => (
20+
<React.Fragment key={group?.pets?.at(0)?.id}>
21+
{group?.pets?.map((pet) => (
2022
<li key={pet.id}>{pet.name}</li>
2123
))}
2224
</React.Fragment>

examples/nextjs-app/app/components/Pets.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
"use client";
22

3-
import { useDefaultServiceFindPets } from "@/openapi/queries";
3+
import { useFindPets } from "@/openapi/queries";
44
import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
55

66
export default function Pets() {
7-
const { data } = useDefaultServiceFindPets({
8-
limit: 10,
9-
tags: [],
7+
const { data } = useFindPets({
8+
query: { tags: [], limit: 10 },
109
});
1110

1211
return (

examples/nextjs-app/app/page.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
import { prefetchUseDefaultServiceFindPets } from "@/openapi/queries/prefetch";
21
import {
32
HydrationBoundary,
43
QueryClient,
54
dehydrate,
65
} from "@tanstack/react-query";
76
import Link from "next/link";
7+
import { prefetchUseFindPets } from "../openapi/queries/prefetch";
88
import Pets from "./components/Pets";
99

1010
export default async function Home() {
1111
const queryClient = new QueryClient();
1212

13-
await prefetchUseDefaultServiceFindPets(queryClient, {
14-
limit: 10,
15-
tags: [],
13+
await prefetchUseFindPets(queryClient, {
14+
query: { tags: [], limit: 10 },
1615
});
1716

1817
return (

examples/nextjs-app/app/providers.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
"use client";
22

33
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
4+
import "../fetchClient";
5+
46
// We can not useState or useRef in a server component, which is why we are
5-
// extracting this part out into it's own file with 'use client' on top
6-
import { useState } from "react";
7+
// extracting this part out into
78

89
function makeQueryClient() {
910
return new QueryClient({

examples/nextjs-app/fetchClient.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { client } from "@/openapi/requests/services.gen";
2+
3+
client.setConfig({
4+
baseUrl: "http://localhost:4010",
5+
});

examples/nextjs-app/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
"build": "next build",
1010
"start": "next start",
1111
"lint": "next lint",
12-
"generate:api": "rimraf ./openapi && node ../../dist/cli.mjs -i ../petstore.yaml -c axios --request ./request.ts --format=biome --lint=biome"
12+
"generate:api": "rimraf ./openapi && node ../../dist/cli.mjs -i ../petstore.yaml --format=biome --lint=biome",
13+
"test:generated": "tsc -p ./tsconfig.json --noEmit"
1314
},
1415
"dependencies": {
1516
"@tanstack/react-query": "^5.32.1",
1617
"@tanstack/react-query-devtools": "^5.32.1",
17-
"axios": "^1.6.7",
1818
"next": "^14.2.3",
1919
"react": "^18",
2020
"react-dom": "^18"

0 commit comments

Comments
 (0)