Skip to content

Commit

Permalink
feat: override return types (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
7nohe authored Oct 31, 2023
1 parent c3159e0 commit 379cc65
Show file tree
Hide file tree
Showing 10 changed files with 400 additions and 240 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,23 @@
$ npm install -D @7nohe/openapi-react-query-codegen
```

Register the command to the `scripts` property in your package.json file.

```json
{
"scripts": {
"codegen": "openapi-rq -i ./petstore.yaml -c axios"
}
}
```

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

```bash
$ npx --package @7nohe/openapi-react-query-codegen openapi-rq -i ./petstore.yaml -c axios
```


## Usage

```
Expand Down
12 changes: 6 additions & 6 deletions examples/react-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@
"test:generated": "tsc ./openapi/queries/index.ts --noEmit --target esnext --moduleResolution node"
},
"dependencies": {
"@tanstack/react-query": "^5.0.0",
"axios": "^1.4.0",
"@tanstack/react-query": "^5.4.3",
"axios": "^1.6.0",
"form-data": "~4.0.0",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@stoplight/prism-cli": "^5.0.0",
"@stoplight/prism-cli": "^5.5.1",
"@types/react": "^18.2.6",
"@types/react-dom": "^18.2.4",
"@vitejs/plugin-react": "^4.0.0",
"@vitejs/plugin-react": "^4.1.0",
"npm-run-all": "^4.1.5",
"typescript": "^5.0.4",
"vite": "^4.3.8"
"typescript": "^5.2.2",
"vite": "^4.5.0"
}
}
32 changes: 15 additions & 17 deletions examples/react-app/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,33 @@ import {
useDefaultClientFindPets,
useDefaultClientFindPetsKey,
} from "../openapi/queries";
import { useState } from 'react';
import { queryClient } from './queryClient';
import { useState } from "react";
import { queryClient } from "./queryClient";

function App() {

const [tags, _setTags] = useState<string[]>([]);
const [limit, _setLimit] = useState<number>(10);

const { data, error, refetch } = useDefaultClientFindPets(
{ tags, limit },
);
const { data, error, refetch } = useDefaultClientFindPets({ tags, limit });

const { mutate: addPet } = useDefaultClientAddPet();

if (error) return (
<div>
<p>Failed to fetch pets</p>
<button onClick={() => refetch()}>Retry</button>
</div>
);
if (error)
return (
<div>
<p>Failed to fetch pets</p>
<button onClick={() => refetch()}>Retry</button>
</div>
);

return (
<div className="App">
<h1>Pet List</h1>
<ul>
{data instanceof Array && data?.map((pet, index) => (
<li key={pet.id + "-" + index}>{pet.name}</li>
))}
{data instanceof Array &&
data?.map((pet, index) => (
<li key={pet.id + "-" + index}>{pet.name}</li>
))}
</ul>
<button
onClick={() => {
Expand All @@ -44,8 +43,7 @@ function App() {
queryClient.invalidateQueries({
queryKey: [useDefaultClientFindPetsKey],
});
console.log("success")

console.log("success");
},
onError: (error) => console.error(error),
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"commander": "^11.1.0",
"glob": "^10.2.5",
"openapi-typescript-codegen": "^0.25.0",
"typescript": "^5.0.4"
"typescript": "^5.2.2"
},
"peerDependencies": {
"commander": ">= 11 < 12",
Expand Down
88 changes: 48 additions & 40 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions src/createExports.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import ts from "typescript";
import { sync } from "glob";
import { sync } from "glob";
import { join } from "path";
import fs from "fs";
import { createUseQuery } from "./createUseQuery";
import { createUseMutation } from "./createUseMutation";

export const createExports = (generatedClientsPath: string) => {
const services = sync(join(generatedClientsPath, 'services', '*.ts').replace(/\\/g, '/'));
const services = sync(
join(generatedClientsPath, "services", "*.ts").replace(/\\/g, "/")
);
const nodes = services.map((servicePath) =>
ts.createSourceFile(
servicePath, // fileName
Expand All @@ -29,7 +31,6 @@ export const createExports = (generatedClientsPath: string) => {
) as ts.MethodDeclaration[];
return methods
.map((method) => {
const methodName = method.name?.getText(node)!;
const methodBlock = method
.getChildren(node)
.find((child) => child.kind === ts.SyntaxKind.Block) as ts.Block;
Expand Down
Loading

0 comments on commit 379cc65

Please sign in to comment.