Skip to content

Commit baafa2d

Browse files
committed
feat: support useOptions of @hey-api/openapi-ts
- removed deprecated options of @hey-api/openapi-ts - using ts-morph to help traverse typescript source files
1 parent 41d2ac6 commit baafa2d

17 files changed

+413
-248
lines changed

README.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,14 @@ Options:
4444
-i, --input <value> OpenAPI specification, can be a path, url or string content (required)
4545
-o, --output <value> Output directory (default: "openapi")
4646
-c, --client <value> HTTP client to generate [fetch, xhr, node, axios, angular] (default: "fetch")
47-
--useUnionTypes Unused, will be removed in the next major version
48-
--exportSchemas <value> Write schemas to disk (default: false)
49-
--indent <value> Unused, will be removed in the next major version
50-
--postfixServices <value> Service name postfix (default: "Service")
51-
--postfixModels <value> Unused, will be removed in the next major version
5247
--request <value> Path to custom request file
53-
--write <value> Write the files to disk (true or false)
5448
--useDateType Use Date type instead of string for date types for models, this will not convert the data to a Date object
5549
--enums Generate JavaScript objects from enum definitions?
5650
--base <value> Manually set base in OpenAPI config instead of inferring from server value
5751
--serviceResponse <value> Define shape of returned value from service calls ['body', 'generics', 'response']
5852
--operationId Use operation ID to generate operation names?
5953
--lint Process output folder with linter?
60-
--name Custom client class name
6154
--format Process output folder with formatter?
62-
--exportCore <value> Export core types
63-
--exportModels <value> Export models
64-
--exportServices <value> Export services
6555
-h, --help display help for command
6656
```
6757

@@ -158,6 +148,15 @@ function ParentComponent() {
158148
);
159149
}
160150

151+
function App() {
152+
return (
153+
<div className="App">
154+
<h1>Pet List</h1>
155+
<ParentComponent />
156+
</div>
157+
);
158+
}
159+
161160
export default App;
162161
```
163162

examples/react-app/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"dev:mock": "prism mock ./petstore.yaml --dynamic",
1010
"build": "tsc && vite build",
1111
"preview": "vite preview",
12-
"generate:api": "node ../../dist/cli.mjs -i ./petstore.yaml -c axios --request ./request.ts --postfixServices=Client",
12+
"generate:api": "node ../../dist/cli.mjs -i ./petstore.yaml -c axios --request ./request.ts",
1313
"test:generated": "tsc -p ./tsconfig.openapi.json --noEmit"
1414
},
1515
"dependencies": {

examples/react-app/src/App.tsx

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import "./App.css";
22
import {
3-
useDefaultClientAddPet,
4-
useDefaultClientFindPets,
5-
useDefaultClientFindPetsKey,
6-
useDefaultClientGetNotDefined,
7-
useDefaultClientPostNotDefined,
3+
useDefaultServiceAddPet,
4+
useDefaultServiceFindPets,
5+
useDefaultServiceFindPetsKey,
6+
useDefaultServiceGetNotDefined,
7+
useDefaultServicePostNotDefined,
88
} from "../openapi/queries";
99
import { useState } from "react";
1010
import { queryClient } from "./queryClient";
@@ -14,18 +14,16 @@ function App() {
1414
const [tags, _setTags] = useState<string[]>([]);
1515
const [limit, _setLimit] = useState<number>(10);
1616

17-
const { data, error, refetch } = useDefaultClientFindPets({
18-
data: { tags, limit },
19-
});
17+
const { data, error, refetch } = useDefaultServiceFindPets({ tags, limit });
2018

2119
// This is an example of a query that is not defined in the OpenAPI spec
2220
// this defaults to any - here we are showing how to override the type
2321
// Note - this is marked as deprecated in the OpenAPI spec and being passed to the client
24-
const { data: notDefined } = useDefaultClientGetNotDefined<undefined>();
22+
const { data: notDefined } = useDefaultServiceGetNotDefined<undefined>();
2523
const { mutate: mutateNotDefined } =
26-
useDefaultClientPostNotDefined<undefined>();
24+
useDefaultServicePostNotDefined<undefined>();
2725

28-
const { mutate: addPet } = useDefaultClientAddPet();
26+
const { mutate: addPet } = useDefaultServiceAddPet();
2927

3028
if (error)
3129
return (
@@ -48,14 +46,12 @@ function App() {
4846
onClick={() => {
4947
addPet(
5048
{
51-
data: {
52-
requestBody: { name: "Duggy" },
53-
},
49+
requestBody: { name: "Duggy" },
5450
},
5551
{
5652
onSuccess: () => {
5753
queryClient.invalidateQueries({
58-
queryKey: [useDefaultClientFindPetsKey],
54+
queryKey: [useDefaultServiceFindPetsKey],
5955
});
6056
console.log("success");
6157
},

examples/react-app/src/components/SuspenseChild.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { useDefaultClientFindPetsSuspense } from "../../openapi/queries/suspense";
1+
import { useDefaultServiceFindPetsSuspense } from "../../openapi/queries/suspense";
22

33
export const SuspenseChild = () => {
4-
const { data } = useDefaultClientFindPetsSuspense({ tags: [], limit: 10 });
4+
const { data } = useDefaultServiceFindPetsSuspense({ tags: [], limit: 10 });
55

66
if (!Array.isArray(data)) {
77
return <div>Error!</div>;

examples/react-app/src/components/SuspenseParent.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ import { SuspenseChild } from "./SuspenseChild";
33

44
export const SuspenseParent = () => {
55
return (
6-
<>
7-
<Suspense fallback={<>loading...</>}>
8-
<SuspenseChild />
9-
</Suspense>
10-
</>
6+
<Suspense fallback={<>loading...</>}>
7+
<SuspenseChild />
8+
</Suspense>
119
);
1210
};

examples/react-app/src/main.tsx

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
import React from 'react'
2-
import ReactDOM from 'react-dom/client'
3-
import App from './App'
4-
import './index.css'
5-
import { QueryClientProvider } from '@tanstack/react-query'
6-
import { queryClient } from './queryClient'
7-
ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
1+
import React from "react";
2+
import ReactDOM from "react-dom/client";
3+
import App from "./App";
4+
import "./index.css";
5+
import { QueryClientProvider } from "@tanstack/react-query";
6+
import { queryClient } from "./queryClient";
7+
8+
ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
89
<React.StrictMode>
910
<QueryClientProvider client={queryClient}>
1011
<App />
1112
</QueryClientProvider>
1213
</React.StrictMode>
13-
)
14+
);

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,18 @@
3737
"author": "Daiki Urata (@7nohe)",
3838
"license": "MIT",
3939
"devDependencies": {
40-
"@hey-api/openapi-ts": "0.34.5",
40+
"@hey-api/openapi-ts": "0.36.0",
4141
"@types/node": "^20.10.6",
4242
"commander": "^12.0.0",
4343
"glob": "^10.3.10",
44+
"ts-morph": "^22.0.0",
4445
"typescript": "^5.3.3"
4546
},
4647
"peerDependencies": {
47-
"@hey-api/openapi-ts": "0.34.5",
48+
"@hey-api/openapi-ts": "0.36.0",
4849
"commander": ">= 11 < 13",
4950
"glob": ">= 10",
51+
"ts-morph": ">= 22 < 23",
5052
"typescript": ">= 4.8.3"
5153
}
5254
}

0 commit comments

Comments
 (0)