Skip to content

Commit 82b932b

Browse files
authored
feat/new openapi client support 2 (#131)
1 parent be5cd9d commit 82b932b

38 files changed

+1947
-1844
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.`,

README.md

Lines changed: 5 additions & 6 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,22 @@ 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")
49+
-c, --client <value> HTTP client to generate (choices: "@hey-api/client-fetch", "@hey-api/client-axios", default: "@hey-api/client-fetch")
5050
--request <value> Path to custom request file
5151
--format <value> Process output folder with formatter? (choices: "biome", "prettier")
5252
--lint <value> Process output folder with linter? (choices: "biome", "eslint")
5353
--operationId Use operation ID to generate operation names?
5454
--serviceResponse <value> Define shape of returned value from service calls (choices: "body", "response", default: "body")
5555
--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']
5756
--enums <value> Generate JavaScript objects from enum definitions? (choices: "javascript", "typescript")
5857
--useDateType Use Date type instead of string for date types for models, this will not convert the data to a Date object
5958
--debug Run in debug mode?
6059
--noSchemas Disable generating JSON schemas
6160
--schemaType <value> Type of JSON schema [Default: 'json'] (choices: "form", "json")
6261
--pageParam <value> Name of the query parameter used for pagination (default: "page")
6362
--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
63+
--initialPageParam <value> Initial page value to query (default: "initialPageParam")
64+
-h, --help display help for command
6665
```
6766

6867
### Example Usage

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/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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
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 --request ./request.ts --format=biome --lint=biome --base http://localhost:4010"
1313
},
1414
"dependencies": {
1515
"@tanstack/react-query": "^5.32.1",

examples/petstore.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,12 +197,12 @@ components:
197197
NewPet:
198198
type: object
199199
required:
200-
- name
200+
- name
201201
properties:
202202
name:
203203
type: string
204204
tag:
205-
type: string
205+
type: string
206206

207207
Error:
208208
type: object
@@ -214,4 +214,4 @@ components:
214214
type: integer
215215
format: int32
216216
message:
217-
type: string
217+
type: string

examples/react-app/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
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 --format=biome --lint=biome",
12+
"generate:api": "rimraf ./openapi && node ../../dist/cli.mjs -i ../petstore.yaml --format=biome --lint=biome -c @hey-api/client-axios",
1313
"test:generated": "tsc -p ./tsconfig.openapi.json --noEmit"
1414
},
1515
"dependencies": {
16+
"@hey-api/client-axios": "^0.2.7",
1617
"@tanstack/react-query": "^5.32.1",
1718
"axios": "^1.6.7",
1819
"form-data": "~4.0.0",

examples/react-app/request.ts

Lines changed: 0 additions & 94 deletions
This file was deleted.

examples/react-app/src/App.tsx

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,35 @@
11
import "./App.css";
22
import { useState } from "react";
3+
4+
import { createClient } from "@hey-api/client-fetch";
35
import {
4-
UseDefaultServiceFindPetsKeyFn,
5-
useDefaultServiceAddPet,
6-
useDefaultServiceFindPets,
7-
useDefaultServiceGetNotDefined,
8-
useDefaultServicePostNotDefined,
6+
UseFindPetsKeyFn,
7+
useAddPet,
8+
useFindPets,
9+
useGetNotDefined,
10+
usePostNotDefined,
911
} from "../openapi/queries";
1012
import { SuspenseParent } from "./components/SuspenseParent";
1113
import { queryClient } from "./queryClient";
1214

1315
function App() {
16+
createClient({ baseUrl: "http://localhost:4010" });
17+
1418
const [tags, _setTags] = useState<string[]>([]);
1519
const [limit, _setLimit] = useState<number>(10);
1620

17-
const { data, error, refetch } = useDefaultServiceFindPets({ tags, limit });
21+
const { data, error, refetch } = useFindPets({ query: { tags, limit } });
1822
// This is an example of using a hook that has all parameters optional;
1923
// Here we do not have to pass in an object
20-
const { data: _ } = useDefaultServiceFindPets();
24+
const { data: _ } = useFindPets();
2125

2226
// This is an example of a query that is not defined in the OpenAPI spec
2327
// this defaults to any - here we are showing how to override the type
2428
// Note - this is marked as deprecated in the OpenAPI spec and being passed to the client
25-
const { data: notDefined } = useDefaultServiceGetNotDefined<undefined>();
26-
const { mutate: mutateNotDefined } =
27-
useDefaultServicePostNotDefined<undefined>();
29+
const { data: notDefined } = useGetNotDefined<undefined>();
30+
const { mutate: mutateNotDefined } = usePostNotDefined<undefined>();
2831

29-
const { mutate: addPet } = useDefaultServiceAddPet();
32+
const { mutate: addPet } = useAddPet();
3033

3134
if (error)
3235
return (
@@ -52,12 +55,12 @@ function App() {
5255
onClick={() => {
5356
addPet(
5457
{
55-
requestBody: { name: "Duggy" },
58+
body: { name: "Duggy" },
5659
},
5760
{
5861
onSuccess: () => {
5962
queryClient.invalidateQueries({
60-
queryKey: UseDefaultServiceFindPetsKeyFn(),
63+
queryKey: UseFindPetsKeyFn({ query: { tags, limit } }),
6164
});
6265
console.log("success");
6366
},

examples/react-app/src/axios.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/react-app/src/components/SuspenseChild.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
import { useDefaultServiceFindPetsSuspense } from "../../openapi/queries/suspense";
1+
import { useFindPetsSuspense } from "../../openapi/queries/suspense";
22

33
export const SuspenseChild = () => {
4-
const { data } = useDefaultServiceFindPetsSuspense({ tags: [], limit: 10 });
5-
4+
const { data, error } = useFindPetsSuspense({
5+
query: { tags: [], limit: 10 },
6+
});
7+
console.log({ error });
68
if (!Array.isArray(data)) {
79
return <div>Error!</div>;
810
}
911

1012
return (
1113
<ul>
12-
{data?.map((pet, index) => (
14+
{data?.map((pet) => (
1315
<li key={pet.id}>{pet.name}</li>
1416
))}
1517
</ul>

examples/react-app/src/main.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import App from "./App";
44
import "./index.css";
55
import { QueryClientProvider } from "@tanstack/react-query";
66
import { queryClient } from "./queryClient";
7+
import "./axios";
78

89
ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
910
<React.StrictMode>

lefthook.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ pre-commit:
22
commands:
33
check:
44
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
5+
run: npx biome check --write --no-errors-on-unmatched --files-ignore-unknown=true ./ && git update-index --again
66
test:
77
glob: "*.{js,ts,cjs,mjs,d.cts,d.mts,jsx,tsx,json,jsonc}"
88
run: npx vitest run

0 commit comments

Comments
 (0)