Skip to content

Commit a57cae5

Browse files
authored
feat!: changing openapi generator (#50)
1 parent 4374671 commit a57cae5

29 files changed

+1759
-1167
lines changed

Diff for: .github/workflows/test.yml

+4-7
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,19 @@ jobs:
2525
uses: actions/setup-node@v4
2626
with:
2727
node-version: 20
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: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ dist-ssr
2424
*.sw?
2525

2626
openapi
27+
*.tsbuildinfo

Diff for: README.md

+18-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
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

@@ -44,12 +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 Use union types (default: false)
48-
--exportSchemas <value> Write schemas to disk (default: false)
49-
--indent <value> Indentation options [4, 2, tabs] (default: "4")
50-
--postfixServices <value> Service name postfix (default: "Service")
51-
--postfixModels <value> Modal name postfix
5247
--request <value> Path to custom request file
48+
--useDateType Use Date type instead of string for date types for models, this will not convert the data to a Date object
49+
--enums Generate JavaScript objects from enum definitions?
50+
--base <value> Manually set base in OpenAPI config instead of inferring from server value
51+
--serviceResponse <value> Define shape of returned value from service calls ['body', 'generics', 'response']
52+
--operationId Use operation ID to generate operation names?
53+
--lint Process output folder with linter?
54+
--format Process output folder with formatter?
5355
-h, --help display help for command
5456
```
5557

@@ -70,7 +72,7 @@ $ openapi-rq -i ./petstore.yaml
7072
- common.ts <- common types
7173
- queries.ts <- generated query hooks
7274
- suspenses.ts <- generated suspense hooks
73-
- requests <- output code generated by OpenAPI Typescript Codegen
75+
- requests <- output code generated by @hey-api/openapi-ts
7476
```
7577

7678
### In your app
@@ -146,6 +148,15 @@ function ParentComponent() {
146148
);
147149
}
148150

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

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.mjs -i ./petstore.yaml -c axios --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

+5-5
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
@@ -49,12 +49,12 @@ export const request = <T>(
4949
return new CancelablePromise((resolve, reject, onCancel) => {
5050
onCancel(() => source.cancel("The user aborted a request."));
5151

52-
let formattedHeaders = options.headers;
53-
if(options.mediaType) {
52+
let formattedHeaders = options.headers as RawAxiosRequestHeaders;
53+
if (options.mediaType) {
5454
formattedHeaders = {
5555
...options.headers,
5656
"Content-Type": options.mediaType,
57-
};
57+
} satisfies RawAxiosRequestHeaders;
5858
}
5959

6060
return axiosInstance

Diff for: examples/react-app/src/App.tsx

+10-10
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,16 +14,16 @@ function App() {
1414
const [tags, _setTags] = useState<string[]>([]);
1515
const [limit, _setLimit] = useState<number>(10);
1616

17-
const { data, error, refetch } = useDefaultClientFindPets({ tags, limit });
17+
const { data, error, refetch } = useDefaultServiceFindPets({ tags, limit });
1818

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

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

2828
if (error)
2929
return (
@@ -51,7 +51,7 @@ function App() {
5151
{
5252
onSuccess: () => {
5353
queryClient.invalidateQueries({
54-
queryKey: [useDefaultClientFindPetsKey],
54+
queryKey: [useDefaultServiceFindPetsKey],
5555
});
5656
console.log("success");
5757
},

Diff for: examples/react-app/src/components/SuspenseChild.tsx

+2-2
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>;

Diff for: examples/react-app/src/components/SuspenseParent.tsx

+3-5
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
};

Diff for: examples/react-app/src/main.tsx

+9-8
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+
);

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@
1010
"strict": true,
1111
"forceConsistentCasingInFileNames": true,
1212
"module": "ESNext",
13-
"moduleResolution": "Node",
13+
"moduleResolution": "Bundler",
1414
"resolveJsonModule": true,
1515
"isolatedModules": true,
1616
"noEmit": true,
1717
"jsx": "react-jsx"
1818
},
19-
"include": ["src", "openapi"],
20-
"references": [{ "path": "./tsconfig.node.json" }]
19+
"include": ["src"],
20+
"references": [
21+
{ "path": "./tsconfig.node.json" },
22+
{ "path": "./tsconfig.openapi.json" }
23+
]
2124
}

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

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

Diff for: package.json

+14-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,27 @@
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.36.0",
3541
"@types/node": "^20.10.6",
3642
"commander": "^12.0.0",
3743
"glob": "^10.3.10",
38-
"openapi-typescript-codegen": "0.25.0",
44+
"ts-morph": "^22.0.0",
3945
"typescript": "^5.3.3"
4046
},
4147
"peerDependencies": {
48+
"@hey-api/openapi-ts": "0.36.0",
4249
"commander": ">= 11 < 13",
4350
"glob": ">= 10",
44-
"openapi-typescript-codegen": "^0.24.0",
51+
"ts-morph": ">= 22 < 23",
4552
"typescript": ">= 4.8.3"
53+
},
54+
"engines": {
55+
"node": ">=14"
4656
}
4757
}

0 commit comments

Comments
 (0)