Skip to content

Commit

Permalink
feat!: changing openapi generator
Browse files Browse the repository at this point in the history
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
  • Loading branch information
seriouslag committed Apr 1, 2024
1 parent 4c01176 commit c214d72
Show file tree
Hide file tree
Showing 15 changed files with 890 additions and 703 deletions.
46 changes: 25 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

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

## Install

```
$ npm install -D @7nohe/openapi-react-query-codegen
```

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

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


## Usage

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

Expand All @@ -68,16 +79,14 @@ $ openapi-rq -i ./petstore.yaml
- openapi
- queries
- index.ts <- custom react hooks
- requests <- output code generated by OpenAPI Typescript Codegen
- requests <- output code generated by @hey-api/openapi-ts
```

### In your app

```tsx
// App.tsx
import {
usePetServiceFindPetsByStatus,
} from "../openapi/queries";
import { usePetServiceFindPetsByStatus } from "../openapi/queries";
function App() {
const { data } = usePetServiceFindPetsByStatus({ status: ["available"] });

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

```tsx
import { useQuery } from "@tanstack/react-query";
import { PetService } from '../openapi/requests/services/PetService';
import {
usePetServiceFindPetsByStatusKey,
} from "../openapi/queries";
import { PetService } from "../openapi/requests/services/PetService";
import { usePetServiceFindPetsByStatusKey } from "../openapi/queries";

function App() {
// You can still use the auto-generated query key
const { data } = useQuery({
queryKey: [usePetServiceFindPetsByStatusKey],
queryFn: () => {
// Do something here
return PetService.findPetsByStatus(['available']);
}
});
return PetService.findPetsByStatus(["available"]);
},
});

return (
<div className="App">
{/* .... */}
</div>
);
return <div className="App">{/* .... */}</div>;
}

export default App;
```

## License

MIT
6 changes: 3 additions & 3 deletions examples/react-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"dev:mock": "prism mock ./petstore.yaml --dynamic",
"build": "tsc && vite build",
"preview": "vite preview",
"generate:api": "node ../../dist/src/cli.js -i ./petstore.yaml -c axios --exportSchemas=true --postfixServices=Client --request ./request.ts",
"test:generated": "tsc ./openapi/queries/index.ts --noEmit --target esnext --moduleResolution node"
"generate:api": "node ../../dist/cli.js -i ./petstore.yaml -c axios --postfixServices=Client --request ./request.ts --useDateType",
"test:generated": "tsc -p ./tsconfig.openapi.json --noEmit"
},
"dependencies": {
"@tanstack/react-query": "^5.18.1",
Expand All @@ -28,4 +28,4 @@
"typescript": "^5.3.3",
"vite": "^5.0.12"
}
}
}
2 changes: 1 addition & 1 deletion examples/react-app/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "ESNext",
"moduleResolution": "Node",
"moduleResolution": "Bundler",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
Expand Down
19 changes: 19 additions & 0 deletions examples/react-app/tsconfig.openapi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"target": "ESNext",
"useDefineForClassFields": true,
"allowJs": false,
"skipLibCheck": true,
"esModuleInterop": false,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "ESNext",
"moduleResolution": "Node10",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx"
},
"include": ["openapi"]
}
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
"version": "0.5.3",
"description": "OpenAPI React Query Codegen",
"bin": {
"openapi-rq": "dist/src/cli.js"
"openapi-rq": "dist/cli.js"
},
"type": "module",
"scripts": {
"build": "tsc -p tsconfig.json",
"preview": "npm run build && npm -C examples/react-app run generate:api",
Expand All @@ -27,21 +28,22 @@
"openapi",
"swagger",
"typescript",
"openapi-typescript-codegen"
"openapi-typescript-codegen",
"@hey-api/openapi-ts"
],
"author": "Daiki Urata (@7nohe)",
"license": "MIT",
"devDependencies": {
"@hey-api/openapi-ts": "0.27.38",
"@types/node": "^20.10.6",
"commander": "^12.0.0",
"glob": "^10.3.10",
"openapi-typescript-codegen": "0.25.0",
"typescript": "^5.3.3"
},
"peerDependencies": {
"@hey-api/openapi-ts": "0.27.38",
"commander": ">= 11 < 13",
"glob": ">= 10",
"openapi-typescript-codegen": "^0.24.0",
"typescript": ">= 4.8.3"
}
}
Loading

0 comments on commit c214d72

Please sign in to comment.