auto-api-hooks 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +51 -12
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,21 +1,34 @@
|
|
|
1
|
-
# auto-api-hooks
|
|
1
|
+
# React Hooks Generator from OpenAPI, Swagger & GraphQL | auto-api-hooks
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Generate React Query hooks from OpenAPI specs. Generate SWR hooks from Swagger. Generate type-safe TypeScript API hooks from GraphQL schemas. One command, fully typed, tree-shakeable output with optional Zod schemas and MSW v2 mocks.
|
|
4
4
|
|
|
5
|
-
`auto-api-hooks`
|
|
5
|
+
> `auto-api-hooks` -- the OpenAPI to React hooks generator that also supports Swagger 2.0 and GraphQL. Produces TanStack React Query v5 hooks, SWR hooks, or plain fetch hooks with zero config.
|
|
6
|
+
|
|
7
|
+
`auto-api-hooks` is an API client generator and code generation tool that reads any OpenAPI 3.x, Swagger 2.0, or GraphQL schema and produces ready-to-use React hooks, TypeScript types, Zod validation schemas, and MSW v2 mock handlers -- all from a single `npx` command or programmatic API call.
|
|
8
|
+
|
|
9
|
+
## Why Use an OpenAPI to React Hooks Generator?
|
|
10
|
+
|
|
11
|
+
Maintaining hand-written fetch logic, TypeScript interfaces, and data-fetching hooks across dozens of API endpoints doesn't scale. Every backend change means manually updating types, adjusting error handling, and keeping request/response shapes in sync -- across every component that calls the API. Most OpenAPI code generation tools stop at a raw API client, leaving you to wire up React Query, SWR, or custom hooks yourself. `auto-api-hooks` generates idiomatic React hooks directly: `useQuery` for reads, `useMutation` for writes, `useInfiniteQuery` for paginated endpoints -- with full TypeScript types, optional Zod runtime validation, and MSW v2 mock servers.
|
|
12
|
+
|
|
13
|
+
**Compared to alternatives:**
|
|
14
|
+
|
|
15
|
+
- **vs orval** -- supports GraphQL schemas in addition to OpenAPI/Swagger, generates MSW v2 handlers (not v1) out of the box
|
|
16
|
+
- **vs openapi-codegen** -- single `npx` command with no config file required; programmatic API for CI/CD integration included
|
|
17
|
+
- **vs swagger-typescript-api** -- produces React hooks directly (React Query v5, SWR, or plain fetch) rather than a raw API client, with built-in pagination detection and infinite query generation
|
|
18
|
+
- **vs openapi-typescript-codegen** -- generates Zod validation schemas from OpenAPI with format-aware refinements and watch mode for development workflows
|
|
6
19
|
|
|
7
20
|
## Features
|
|
8
21
|
|
|
9
|
-
- **
|
|
10
|
-
- **
|
|
11
|
-
- **
|
|
12
|
-
- **
|
|
13
|
-
- **MSW v2 mock server
|
|
22
|
+
- **OpenAPI to TypeScript React hooks** -- Generate React hooks from OpenAPI 3.x, Swagger 2.0, and GraphQL schemas
|
|
23
|
+
- **Generate React Query hooks from OpenAPI** -- TanStack React Query v5, SWR, Axios, or plain `fetch`
|
|
24
|
+
- **Generate Zod schemas from OpenAPI** -- Format-aware refinements (`email`, `uuid`, `date-time`) for runtime response validation
|
|
25
|
+
- **Auto-detect pagination, generate infinite query hooks** -- Cursor-based, offset-limit, and page-number patterns detected automatically
|
|
26
|
+
- **Generate MSW v2 mock server from API spec** -- Request handlers, mock data factories, and server/browser setup files
|
|
14
27
|
- **CLI tool + programmatic API** -- Use from the terminal or import `generate()` in your build scripts
|
|
15
28
|
- **Watch mode** -- File-watching with debounced regeneration for development workflows
|
|
16
|
-
- **
|
|
29
|
+
- **Fully typed TypeScript output** -- Per-operation params, body, and response types emitted as interfaces
|
|
17
30
|
- **One hook per file** -- Maximum tree-shakeability; bundlers only include what you import
|
|
18
|
-
- **
|
|
31
|
+
- **React Query cache key factories** -- Following TanStack v5 best practices
|
|
19
32
|
|
|
20
33
|
## Quick Start
|
|
21
34
|
|
|
@@ -25,7 +38,7 @@ Install the package:
|
|
|
25
38
|
npm install auto-api-hooks
|
|
26
39
|
```
|
|
27
40
|
|
|
28
|
-
Generate hooks from your
|
|
41
|
+
Generate React hooks from your OpenAPI specification:
|
|
29
42
|
|
|
30
43
|
```bash
|
|
31
44
|
npx auto-api-hooks generate --spec ./openapi.yaml --fetcher react-query --output ./src/hooks
|
|
@@ -58,7 +71,7 @@ function UserList() {
|
|
|
58
71
|
}
|
|
59
72
|
```
|
|
60
73
|
|
|
61
|
-
## CLI Usage
|
|
74
|
+
## CLI Usage -- Generate React Hooks from OpenAPI, Swagger, or GraphQL
|
|
62
75
|
|
|
63
76
|
```
|
|
64
77
|
auto-api-hooks generate [options]
|
|
@@ -1027,6 +1040,32 @@ test('renders todo list from mock data', async () => {
|
|
|
1027
1040
|
|
|
1028
1041
|
If the backend returns data that does not match the schema, the Zod `.parse()` call inside the hook throws a `ZodError` with a detailed path to the invalid field. This surfaces API contract violations immediately during development rather than silently producing incorrect UI state.
|
|
1029
1042
|
|
|
1043
|
+
## FAQ
|
|
1044
|
+
|
|
1045
|
+
**Does auto-api-hooks support OpenAPI 3.1?**
|
|
1046
|
+
Yes. Both OpenAPI 3.0 and 3.1 are fully supported, including multi-file specs with external `$ref` references.
|
|
1047
|
+
|
|
1048
|
+
**Can I use it with Swagger 2.0?**
|
|
1049
|
+
Yes. Swagger 2.0 YAML and JSON files are supported alongside OpenAPI 3.x and GraphQL SDL/introspection.
|
|
1050
|
+
|
|
1051
|
+
**Which React data-fetching libraries does it support?**
|
|
1052
|
+
Plain `fetch` (no extra dependencies), Axios, TanStack React Query v5, and SWR. Each generates idiomatic hooks for that library.
|
|
1053
|
+
|
|
1054
|
+
**Does it generate TypeScript types automatically?**
|
|
1055
|
+
Yes. Every generated hook is fully typed -- path params, query params, request body, and response types are all emitted as TypeScript interfaces.
|
|
1056
|
+
|
|
1057
|
+
**Can I generate Zod schemas from my OpenAPI spec?**
|
|
1058
|
+
Yes. Pass `--zod` to generate a `schemas.ts` file with Zod schemas for every named type and operation response, including format-aware refinements (`email`, `uuid`, `date-time`, `uri`).
|
|
1059
|
+
|
|
1060
|
+
**Does it generate MSW mocks?**
|
|
1061
|
+
Yes. Pass `--mock` to generate a complete MSW v2 mock server with request handlers, data factories, and `setupServer`/`setupWorker` setup files.
|
|
1062
|
+
|
|
1063
|
+
**How is this different from orval or openapi-typescript-codegen?**
|
|
1064
|
+
`auto-api-hooks` uniquely combines OpenAPI, Swagger, and GraphQL support in one tool, generates MSW v2 handlers (not v1), produces one hook per file for maximum tree-shakeability, and includes smart pagination detection with automatic infinite query hook generation.
|
|
1065
|
+
|
|
1066
|
+
**Can I use it programmatically in a build script?**
|
|
1067
|
+
Yes. Import `generate()` directly from `auto-api-hooks` and pass a parsed spec object or file path. It returns generated file contents in memory when `outputDir` is omitted.
|
|
1068
|
+
|
|
1030
1069
|
## Support
|
|
1031
1070
|
|
|
1032
1071
|
If you find this package useful, consider buying me a coffee!
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "auto-api-hooks",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Auto-generate type-safe React hooks from OpenAPI, Swagger, and GraphQL specs. Supports fetch, Axios, React Query, and SWR with optional Zod validation and MSW mock generation.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|