Skip to content

Commit 92c5728

Browse files
committed
feat!: changing openapi generator
Changed openapi code generator to @hey-api/openapi-ts. Not supporting all properties yet. Supporting new properties: - base - serviceResponse - enums - useDateType indent and useUnionTypes properties were removed left in for backwards compatibility. BREAKING CHANGE: changed from cjs to mjs
1 parent 4c01176 commit 92c5728

17 files changed

+915
-793
lines changed

Diff for: .github/workflows/test.yml

+7-10
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,27 @@ jobs:
1717

1818
steps:
1919
- name: Checkout
20-
uses: actions/checkout@v3
21-
- uses: pnpm/action-setup@v2
20+
uses: actions/checkout@v4
21+
- uses: pnpm/action-setup@v3
2222
with:
2323
version: 8
2424
- name: Install Node.js
25-
uses: actions/setup-node@v3
25+
uses: actions/setup-node@v4
2626
with:
2727
node-version: 18
28-
cache: 'pnpm'
28+
cache: "pnpm"
2929

3030
- name: Install dependencies
31-
run: pnpm -w install
32-
31+
run: pnpm install --frozen-lockfile
32+
3333
- name: Build
3434
run: pnpm -w build
3535

36-
- name: Install dependencies in example app
37-
run: pnpm --filter @7nohe/react-app install --no-frozen-lockfile
38-
3936
- name: Run codegen
4037
run: pnpm --filter @7nohe/react-app generate:api
4138

4239
- name: Archive generated query file
43-
uses: actions/upload-artifact@v3
40+
uses: actions/upload-artifact@v4
4441
with:
4542
name: generated-query-file-${{ matrix.os }}
4643
path: examples/react-app/openapi/queries/index.ts

Diff for: README.md

+25-21
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66

77
- Supports generation of custom react hooks that use React Query's `useQuery` and `useMutation` hooks
88
- Supports generation of query keys for query caching
9-
- Supports the option to use pure TypeScript clients generated by [OpenAPI Typescript Codegen](https://github.com/ferdikoomen/openapi-typescript-codegen)
9+
- Supports the option to use pure TypeScript clients generated by [@hey-api/openapi-ts](https://github.com/hey-api/openapi-ts)
1010

1111
## Install
1212

1313
```
1414
$ npm install -D @7nohe/openapi-react-query-codegen
1515
```
1616

17-
Register the command to the `scripts` property in your package.json file.
17+
Register the command to the `scripts` property in your package.json file.
1818

1919
```json
2020
{
@@ -30,7 +30,6 @@ You can also run the command without installing it in your project using the npx
3030
$ npx --package @7nohe/openapi-react-query-codegen openapi-rq -i ./petstore.yaml -c axios
3131
```
3232

33-
3433
## Usage
3534

3635
```
@@ -45,12 +44,24 @@ Options:
4544
-i, --input <value> OpenAPI specification, can be a path, url or string content (required)
4645
-o, --output <value> Output directory (default: "openapi")
4746
-c, --client <value> HTTP client to generate [fetch, xhr, node, axios, angular] (default: "fetch")
48-
--useUnionTypes Use union types (default: false)
47+
--useUnionTypes Unused, will be removed in the next major version
4948
--exportSchemas <value> Write schemas to disk (default: false)
50-
--indent <value> Indentation options [4, 2, tabs] (default: "4")
49+
--indent <value> Unused, will be removed in the next major version
5150
--postfixServices <value> Service name postfix (default: "Service")
5251
--postfixModels <value> Modal name postfix
5352
--request <value> Path to custom request file
53+
--write <value> Write the files to disk (true or false)
54+
--useDateType Use Date type instead of string for date types for models, this will not convert the data to a Date object
55+
--enums Generate JavaScript objects from enum definitions?
56+
--base <value> Manually set base in OpenAPI config instead of inferring from server value
57+
--serviceResponse <value> Define shape of returned value from service calls ['body', 'generics', 'response']
58+
--operationId Use operation ID to generate operation names?
59+
--lint Process output folder with linter?
60+
--name Custom client class name
61+
--format Process output folder with formatter?
62+
--exportCore <value> Export core types
63+
--exportModels <value> Export models
64+
--exportServices <value> Export services
5465
-h, --help display help for command
5566
```
5667

@@ -68,16 +79,14 @@ $ openapi-rq -i ./petstore.yaml
6879
- openapi
6980
- queries
7081
- index.ts <- custom react hooks
71-
- requests <- output code generated by OpenAPI Typescript Codegen
82+
- requests <- output code generated by @hey-api/openapi-ts
7283
```
7384

7485
### In your app
7586

7687
```tsx
7788
// App.tsx
78-
import {
79-
usePetServiceFindPetsByStatus,
80-
} from "../openapi/queries";
89+
import { usePetServiceFindPetsByStatus } from "../openapi/queries";
8190
function App() {
8291
const { data } = usePetServiceFindPetsByStatus({ status: ["available"] });
8392

@@ -100,30 +109,25 @@ You can also use pure TS clients.
100109

101110
```tsx
102111
import { useQuery } from "@tanstack/react-query";
103-
import { PetService } from '../openapi/requests/services/PetService';
104-
import {
105-
usePetServiceFindPetsByStatusKey,
106-
} from "../openapi/queries";
112+
import { PetService } from "../openapi/requests/services/PetService";
113+
import { usePetServiceFindPetsByStatusKey } from "../openapi/queries";
107114

108115
function App() {
109116
// You can still use the auto-generated query key
110117
const { data } = useQuery({
111118
queryKey: [usePetServiceFindPetsByStatusKey],
112119
queryFn: () => {
113120
// Do something here
114-
return PetService.findPetsByStatus(['available']);
115-
}
116-
});
121+
return PetService.findPetsByStatus(["available"]);
122+
},
123+
});
117124

118-
return (
119-
<div className="App">
120-
{/* .... */}
121-
</div>
122-
);
125+
return <div className="App">{/* .... */}</div>;
123126
}
124127

125128
export default App;
126129
```
127130

128131
## License
132+
129133
MIT

Diff for: examples/react-app/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
"dev:mock": "prism mock ./petstore.yaml --dynamic",
1010
"build": "tsc && vite build",
1111
"preview": "vite preview",
12-
"generate:api": "node ../../dist/src/cli.js -i ./petstore.yaml -c axios --exportSchemas=true --postfixServices=Client --request ./request.ts",
13-
"test:generated": "tsc ./openapi/queries/index.ts --noEmit --target esnext --moduleResolution node"
12+
"generate:api": "node ../../dist/cli.js -i ./petstore.yaml -c axios --postfixServices=Client --request ./request.ts",
13+
"test:generated": "tsc -p ./tsconfig.openapi.json --noEmit"
1414
},
1515
"dependencies": {
1616
"@tanstack/react-query": "^5.18.1",
@@ -28,4 +28,4 @@
2828
"typescript": "^5.3.3",
2929
"vite": "^5.0.12"
3030
}
31-
}
31+
}

Diff for: examples/react-app/request.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import axios from "axios";
2-
import type { AxiosError, AxiosRequestConfig, AxiosResponse } from "axios";
2+
import type { RawAxiosRequestHeaders } from "axios";
33

44
import type { ApiRequestOptions } from "./ApiRequestOptions";
55
import { CancelablePromise } from "./CancelablePromise";
@@ -13,7 +13,7 @@ const axiosInstance = axios.create({
1313
baseURL: "http://localhost:4010",
1414
headers: {
1515
// Your custom headers
16-
},
16+
} satisfies RawAxiosRequestHeaders,
1717
});
1818

1919
// Add a request interceptor

Diff for: examples/react-app/tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"strict": true,
1111
"forceConsistentCasingInFileNames": true,
1212
"module": "ESNext",
13-
"moduleResolution": "Node",
13+
"moduleResolution": "Bundler",
1414
"resolveJsonModule": true,
1515
"isolatedModules": true,
1616
"noEmit": true,

Diff for: examples/react-app/tsconfig.openapi.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ESNext",
4+
"useDefineForClassFields": true,
5+
"allowJs": false,
6+
"skipLibCheck": true,
7+
"esModuleInterop": false,
8+
"allowSyntheticDefaultImports": true,
9+
"strict": true,
10+
"forceConsistentCasingInFileNames": true,
11+
"module": "ESNext",
12+
"moduleResolution": "Node10",
13+
"resolveJsonModule": true,
14+
"isolatedModules": true,
15+
"noEmit": true,
16+
"jsx": "react-jsx"
17+
},
18+
"include": ["openapi"]
19+
}

Diff for: package.json

+9-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
"version": "0.5.3",
44
"description": "OpenAPI React Query Codegen",
55
"bin": {
6-
"openapi-rq": "dist/src/cli.js"
6+
"openapi-rq": "dist/cli.js"
77
},
8+
"type": "module",
9+
"workspaces": [
10+
"examples/*"
11+
],
812
"scripts": {
913
"build": "tsc -p tsconfig.json",
1014
"preview": "npm run build && npm -C examples/react-app run generate:api",
@@ -27,21 +31,22 @@
2731
"openapi",
2832
"swagger",
2933
"typescript",
30-
"openapi-typescript-codegen"
34+
"openapi-typescript-codegen",
35+
"@hey-api/openapi-ts"
3136
],
3237
"author": "Daiki Urata (@7nohe)",
3338
"license": "MIT",
3439
"devDependencies": {
40+
"@hey-api/openapi-ts": "0.27.38",
3541
"@types/node": "^20.10.6",
3642
"commander": "^12.0.0",
3743
"glob": "^10.3.10",
38-
"openapi-typescript-codegen": "0.25.0",
3944
"typescript": "^5.3.3"
4045
},
4146
"peerDependencies": {
47+
"@hey-api/openapi-ts": "0.27.38",
4248
"commander": ">= 11 < 13",
4349
"glob": ">= 10",
44-
"openapi-typescript-codegen": "^0.24.0",
4550
"typescript": ">= 4.8.3"
4651
}
4752
}

0 commit comments

Comments
 (0)