Skip to content

Commit 379cc65

Browse files
authored
feat: override return types (#30)
1 parent c3159e0 commit 379cc65

File tree

10 files changed

+400
-240
lines changed

10 files changed

+400
-240
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,23 @@
1414
$ npm install -D @7nohe/openapi-react-query-codegen
1515
```
1616

17+
Register the command to the `scripts` property in your package.json file.
18+
19+
```json
20+
{
21+
"scripts": {
22+
"codegen": "openapi-rq -i ./petstore.yaml -c axios"
23+
}
24+
}
25+
```
26+
27+
You can also run the command without installing it in your project using the npx command.
28+
29+
```bash
30+
$ npx --package @7nohe/openapi-react-query-codegen openapi-rq -i ./petstore.yaml -c axios
31+
```
32+
33+
1734
## Usage
1835

1936
```

examples/react-app/package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@
1313
"test:generated": "tsc ./openapi/queries/index.ts --noEmit --target esnext --moduleResolution node"
1414
},
1515
"dependencies": {
16-
"@tanstack/react-query": "^5.0.0",
17-
"axios": "^1.4.0",
16+
"@tanstack/react-query": "^5.4.3",
17+
"axios": "^1.6.0",
1818
"form-data": "~4.0.0",
1919
"react": "^18.2.0",
2020
"react-dom": "^18.2.0"
2121
},
2222
"devDependencies": {
23-
"@stoplight/prism-cli": "^5.0.0",
23+
"@stoplight/prism-cli": "^5.5.1",
2424
"@types/react": "^18.2.6",
2525
"@types/react-dom": "^18.2.4",
26-
"@vitejs/plugin-react": "^4.0.0",
26+
"@vitejs/plugin-react": "^4.1.0",
2727
"npm-run-all": "^4.1.5",
28-
"typescript": "^5.0.4",
29-
"vite": "^4.3.8"
28+
"typescript": "^5.2.2",
29+
"vite": "^4.5.0"
3030
}
3131
}

examples/react-app/src/App.tsx

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,33 @@ import {
44
useDefaultClientFindPets,
55
useDefaultClientFindPetsKey,
66
} from "../openapi/queries";
7-
import { useState } from 'react';
8-
import { queryClient } from './queryClient';
7+
import { useState } from "react";
8+
import { queryClient } from "./queryClient";
99

1010
function App() {
11-
1211
const [tags, _setTags] = useState<string[]>([]);
1312
const [limit, _setLimit] = useState<number>(10);
1413

15-
const { data, error, refetch } = useDefaultClientFindPets(
16-
{ tags, limit },
17-
);
14+
const { data, error, refetch } = useDefaultClientFindPets({ tags, limit });
1815

1916
const { mutate: addPet } = useDefaultClientAddPet();
2017

21-
if (error) return (
22-
<div>
23-
<p>Failed to fetch pets</p>
24-
<button onClick={() => refetch()}>Retry</button>
25-
</div>
26-
);
18+
if (error)
19+
return (
20+
<div>
21+
<p>Failed to fetch pets</p>
22+
<button onClick={() => refetch()}>Retry</button>
23+
</div>
24+
);
2725

2826
return (
2927
<div className="App">
3028
<h1>Pet List</h1>
3129
<ul>
32-
{data instanceof Array && data?.map((pet, index) => (
33-
<li key={pet.id + "-" + index}>{pet.name}</li>
34-
))}
30+
{data instanceof Array &&
31+
data?.map((pet, index) => (
32+
<li key={pet.id + "-" + index}>{pet.name}</li>
33+
))}
3534
</ul>
3635
<button
3736
onClick={() => {
@@ -44,8 +43,7 @@ function App() {
4443
queryClient.invalidateQueries({
4544
queryKey: [useDefaultClientFindPetsKey],
4645
});
47-
console.log("success")
48-
46+
console.log("success");
4947
},
5048
onError: (error) => console.error(error),
5149
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"commander": "^11.1.0",
3737
"glob": "^10.2.5",
3838
"openapi-typescript-codegen": "^0.25.0",
39-
"typescript": "^5.0.4"
39+
"typescript": "^5.2.2"
4040
},
4141
"peerDependencies": {
4242
"commander": ">= 11 < 12",

pnpm-lock.yaml

Lines changed: 48 additions & 40 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/createExports.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import ts from "typescript";
2-
import { sync } from "glob";
2+
import { sync } from "glob";
33
import { join } from "path";
44
import fs from "fs";
55
import { createUseQuery } from "./createUseQuery";
66
import { createUseMutation } from "./createUseMutation";
77

88
export const createExports = (generatedClientsPath: string) => {
9-
const services = sync(join(generatedClientsPath, 'services', '*.ts').replace(/\\/g, '/'));
9+
const services = sync(
10+
join(generatedClientsPath, "services", "*.ts").replace(/\\/g, "/")
11+
);
1012
const nodes = services.map((servicePath) =>
1113
ts.createSourceFile(
1214
servicePath, // fileName
@@ -29,7 +31,6 @@ export const createExports = (generatedClientsPath: string) => {
2931
) as ts.MethodDeclaration[];
3032
return methods
3133
.map((method) => {
32-
const methodName = method.name?.getText(node)!;
3334
const methodBlock = method
3435
.getChildren(node)
3536
.find((child) => child.kind === ts.SyntaxKind.Block) as ts.Block;

0 commit comments

Comments
 (0)