auto-api-hooks 1.0.0 → 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 +55 -15
- package/dist/cli.js +8 -3
- package/dist/cli.js.map +1 -1
- package/dist/index.cjs +8 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +8 -3
- package/dist/index.js.map +1 -1
- 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]
|
|
@@ -564,7 +577,7 @@ await generate({
|
|
|
564
577
|
|
|
565
578
|
Full support for OpenAPI 3.0 and 3.1 specifications, including:
|
|
566
579
|
|
|
567
|
-
- `$ref` resolution across the document
|
|
580
|
+
- `$ref` resolution across the document, including multi-file specs with external `$ref` paths (e.g. `$ref: './paths/users.yaml'`)
|
|
568
581
|
- `components.schemas` mapped to TypeScript types and optional Zod schemas
|
|
569
582
|
- `servers[0].url` used as the default base URL
|
|
570
583
|
- All HTTP methods: GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS
|
|
@@ -575,7 +588,7 @@ Full support for OpenAPI 3.0 and 3.1 specifications, including:
|
|
|
575
588
|
- Tag-based grouping of generated hooks
|
|
576
589
|
- `x-pagination` vendor extension for explicit pagination hints
|
|
577
590
|
|
|
578
|
-
**Supported file formats:** `.yaml`, `.yml`, `.json`
|
|
591
|
+
**Supported file formats:** `.yaml`, `.yml`, `.json` (single-file or multi-file with relative `$ref` references)
|
|
579
592
|
|
|
580
593
|
### Swagger 2.0
|
|
581
594
|
|
|
@@ -584,8 +597,9 @@ Full support for Swagger 2.0 specifications, including:
|
|
|
584
597
|
- `definitions` mapped to TypeScript types
|
|
585
598
|
- `host` + `basePath` combined into the base URL
|
|
586
599
|
- All standard Swagger features (parameters, responses, tags)
|
|
600
|
+
- Multi-file specs with external `$ref` paths
|
|
587
601
|
|
|
588
|
-
**Supported file formats:** `.yaml`, `.yml`, `.json`
|
|
602
|
+
**Supported file formats:** `.yaml`, `.yml`, `.json` (single-file or multi-file with relative `$ref` references)
|
|
589
603
|
|
|
590
604
|
### GraphQL
|
|
591
605
|
|
|
@@ -1026,6 +1040,32 @@ test('renders todo list from mock data', async () => {
|
|
|
1026
1040
|
|
|
1027
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.
|
|
1028
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
|
+
|
|
1029
1069
|
## Support
|
|
1030
1070
|
|
|
1031
1071
|
If you find this package useful, consider buying me a coffee!
|
package/dist/cli.js
CHANGED
|
@@ -454,7 +454,7 @@ var openApiParser = {
|
|
|
454
454
|
},
|
|
455
455
|
async parse(input, options) {
|
|
456
456
|
const derefed = await SwaggerParser.dereference(
|
|
457
|
-
input,
|
|
457
|
+
options?.filePath ?? input,
|
|
458
458
|
{ dereference: { circular: "ignore" } }
|
|
459
459
|
);
|
|
460
460
|
const doc = derefed;
|
|
@@ -789,7 +789,7 @@ var swaggerParser = {
|
|
|
789
789
|
},
|
|
790
790
|
async parse(input, options) {
|
|
791
791
|
const derefed = await SwaggerParser.dereference(
|
|
792
|
-
input,
|
|
792
|
+
options?.filePath ?? input,
|
|
793
793
|
{ dereference: { circular: "ignore" } }
|
|
794
794
|
);
|
|
795
795
|
const doc = derefed;
|
|
@@ -1215,9 +1215,14 @@ async function parseSpec(input, options) {
|
|
|
1215
1215
|
} else {
|
|
1216
1216
|
resolved = input;
|
|
1217
1217
|
}
|
|
1218
|
+
const resolvedOptions = { ...options };
|
|
1219
|
+
if (typeof input === "string" && /\.[a-z]{2,10}$/i.test(input) && !input.includes("\n")) {
|
|
1220
|
+
const { resolve } = await import('path');
|
|
1221
|
+
resolvedOptions.filePath = resolve(input);
|
|
1222
|
+
}
|
|
1218
1223
|
for (const parser of parsers) {
|
|
1219
1224
|
if (parser.canParse(resolved)) {
|
|
1220
|
-
const spec = await parser.parse(resolved,
|
|
1225
|
+
const spec = await parser.parse(resolved, resolvedOptions);
|
|
1221
1226
|
return applyPaginationDetection(spec);
|
|
1222
1227
|
}
|
|
1223
1228
|
}
|