auto-api-hooks 1.0.1 → 1.0.3

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.
Files changed (2) hide show
  1. package/README.md +57 -12
  2. package/package.json +12 -3
package/README.md CHANGED
@@ -1,21 +1,40 @@
1
- # auto-api-hooks
1
+ # React Hooks Generator from OpenAPI, Swagger & GraphQL | auto-api-hooks
2
2
 
3
- Auto-generate type-safe React hooks from API specifications.
3
+ [![npm version](https://img.shields.io/npm/v/auto-api-hooks)](https://www.npmjs.com/package/auto-api-hooks)
4
+ [![npm downloads](https://img.shields.io/npm/dm/auto-api-hooks)](https://www.npmjs.com/package/auto-api-hooks)
5
+ [![bundle size](https://img.shields.io/bundlephobia/minzip/auto-api-hooks)](https://bundlephobia.com/package/auto-api-hooks)
6
+ [![license](https://img.shields.io/npm/l/auto-api-hooks)](https://github.com/abdallaemadeldin/auto-api-hooks/blob/master/LICENSE)
7
+ [![TypeScript](https://img.shields.io/badge/TypeScript-strict-blue)](https://www.typescriptlang.org/)
4
8
 
5
- `auto-api-hooks` reads your OpenAPI 3.x, Swagger 2.0, or GraphQL schema and produces ready-to-use React hooks, TypeScript types, optional Zod validation schemas, and MSW v2 mock handlers -- all from a single command.
9
+ 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.
10
+
11
+ > `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.
12
+
13
+ `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.
14
+
15
+ ## Why Use an OpenAPI to React Hooks Generator?
16
+
17
+ 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.
18
+
19
+ **Compared to alternatives:**
20
+
21
+ - **vs orval** -- supports GraphQL schemas in addition to OpenAPI/Swagger, generates MSW v2 handlers (not v1) out of the box
22
+ - **vs openapi-codegen** -- single `npx` command with no config file required; programmatic API for CI/CD integration included
23
+ - **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
24
+ - **vs openapi-typescript-codegen** -- generates Zod validation schemas from OpenAPI with format-aware refinements and watch mode for development workflows
6
25
 
7
26
  ## Features
8
27
 
9
- - **Multi-spec support** -- Generate React hooks from OpenAPI 3.x, Swagger 2.0, and GraphQL schemas
10
- - **Multiple fetcher strategies** -- Plain `fetch`, Axios, TanStack React Query v5, and SWR
11
- - **Optional Zod validation** -- Generate Zod schemas with format-aware refinements for runtime response validation
12
- - **Smart pagination detection** -- Automatically detects cursor-based, offset-limit, and page-number pagination patterns and generates infinite query hooks
13
- - **MSW v2 mock server generation** -- Produce request handlers, mock data factories, and server/browser setup files
28
+ - **OpenAPI to TypeScript React hooks** -- Generate React hooks from OpenAPI 3.x, Swagger 2.0, and GraphQL schemas
29
+ - **Generate React Query hooks from OpenAPI** -- TanStack React Query v5, SWR, Axios, or plain `fetch`
30
+ - **Generate Zod schemas from OpenAPI** -- Format-aware refinements (`email`, `uuid`, `date-time`) for runtime response validation
31
+ - **Auto-detect pagination, generate infinite query hooks** -- Cursor-based, offset-limit, and page-number patterns detected automatically
32
+ - **Generate MSW v2 mock server from API spec** -- Request handlers, mock data factories, and server/browser setup files
14
33
  - **CLI tool + programmatic API** -- Use from the terminal or import `generate()` in your build scripts
15
34
  - **Watch mode** -- File-watching with debounced regeneration for development workflows
16
- - **TypeScript-first** -- Fully typed output with per-operation params, body, and response types
35
+ - **Fully typed TypeScript output** -- Per-operation params, body, and response types emitted as interfaces
17
36
  - **One hook per file** -- Maximum tree-shakeability; bundlers only include what you import
18
- - **Cache key factories** -- React Query key factories following TanStack v5 best practices
37
+ - **React Query cache key factories** -- Following TanStack v5 best practices
19
38
 
20
39
  ## Quick Start
21
40
 
@@ -25,7 +44,7 @@ Install the package:
25
44
  npm install auto-api-hooks
26
45
  ```
27
46
 
28
- Generate hooks from your API specification:
47
+ Generate React hooks from your OpenAPI specification:
29
48
 
30
49
  ```bash
31
50
  npx auto-api-hooks generate --spec ./openapi.yaml --fetcher react-query --output ./src/hooks
@@ -58,7 +77,7 @@ function UserList() {
58
77
  }
59
78
  ```
60
79
 
61
- ## CLI Usage
80
+ ## CLI Usage -- Generate React Hooks from OpenAPI, Swagger, or GraphQL
62
81
 
63
82
  ```
64
83
  auto-api-hooks generate [options]
@@ -1027,6 +1046,32 @@ test('renders todo list from mock data', async () => {
1027
1046
 
1028
1047
  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
1048
 
1049
+ ## FAQ
1050
+
1051
+ **Does auto-api-hooks support OpenAPI 3.1?**
1052
+ Yes. Both OpenAPI 3.0 and 3.1 are fully supported, including multi-file specs with external `$ref` references.
1053
+
1054
+ **Can I use it with Swagger 2.0?**
1055
+ Yes. Swagger 2.0 YAML and JSON files are supported alongside OpenAPI 3.x and GraphQL SDL/introspection.
1056
+
1057
+ **Which React data-fetching libraries does it support?**
1058
+ Plain `fetch` (no extra dependencies), Axios, TanStack React Query v5, and SWR. Each generates idiomatic hooks for that library.
1059
+
1060
+ **Does it generate TypeScript types automatically?**
1061
+ Yes. Every generated hook is fully typed -- path params, query params, request body, and response types are all emitted as TypeScript interfaces.
1062
+
1063
+ **Can I generate Zod schemas from my OpenAPI spec?**
1064
+ 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`).
1065
+
1066
+ **Does it generate MSW mocks?**
1067
+ Yes. Pass `--mock` to generate a complete MSW v2 mock server with request handlers, data factories, and `setupServer`/`setupWorker` setup files.
1068
+
1069
+ **How is this different from orval or openapi-typescript-codegen?**
1070
+ `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.
1071
+
1072
+ **Can I use it programmatically in a build script?**
1073
+ 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.
1074
+
1030
1075
  ## Support
1031
1076
 
1032
1077
  If you find this package useful, consider buying me a coffee!
package/package.json CHANGED
@@ -1,8 +1,17 @@
1
1
  {
2
2
  "name": "auto-api-hooks",
3
- "version": "1.0.1",
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.",
3
+ "version": "1.0.3",
4
+ "description": "Generate React Query hooks, SWR hooks, and TypeScript types from OpenAPI 3.x, Swagger 2.0, and GraphQL specs. API client code generator with Zod schema validation and MSW v2 mock server generation.",
5
5
  "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/abdallaemadeldin/auto-api-hooks.git"
9
+ },
10
+ "bugs": {
11
+ "url": "https://github.com/abdallaemadeldin/auto-api-hooks/issues"
12
+ },
13
+ "homepage": "https://github.com/abdallaemadeldin/auto-api-hooks#readme",
14
+ "author": "Abdalla Emad Eldin",
6
15
  "type": "module",
7
16
  "sideEffects": false,
8
17
  "bin": {
@@ -71,4 +80,4 @@
71
80
  "api-client",
72
81
  "hooks-generator"
73
82
  ]
74
- }
83
+ }