@bazo/openapi-client-generator 3.0.13

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 ADDED
@@ -0,0 +1,167 @@
1
+ # @bazo/openapi-client-generator
2
+
3
+ Generate type-safe API clients from an OpenAPI / Swagger JSON spec. One CLI, four output targets:
4
+
5
+ | Mode | Output | Stack |
6
+ |------|--------|-------|
7
+ | `full` *(default)* | `.ts` | React Query hooks + [Wretch](https://github.com/elbywan/wretch) HTTP client + [Zod](https://zod.dev) validation |
8
+ | `playwright` | `.ts` | Class wrapping Playwright's `APIRequestContext` for E2E tests + Zod validation |
9
+ | `swift` | `.swift` | `Codable` structs + `async/await` client (Foundation only) |
10
+ | `kotlin` | `.kt` | `@Serializable` data classes + Ktor-based suspend client |
11
+
12
+ Schemas are normalized, sorted by dependency, and emitted with full types; unused declarations are stripped from TypeScript output automatically.
13
+
14
+ ## Installation
15
+
16
+ ```bash
17
+ npm install -D @bazo/openapi-client-generator
18
+ # or: bun add -d @bazo/openapi-client-generator
19
+ ```
20
+
21
+ You can also run it without installing:
22
+
23
+ ```bash
24
+ npx @bazo/openapi-client-generator api.json src/api -n MyClient
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ ```
30
+ openapi-client-generator <spec.json> <output-dir> -n <ClientName> [-m <mode>]
31
+ ```
32
+
33
+ | Option | Description |
34
+ |--------|-------------|
35
+ | `<spec.json>` | Path to the OpenAPI/Swagger JSON spec (positional, required) |
36
+ | `<output-dir>` | Directory to write the generated file into (positional, required) |
37
+ | `-n, --name` | Client/class name — must be a valid identifier (required) |
38
+ | `-m, --mode` | `full` \| `playwright` \| `swift` \| `kotlin` (default: `full`) |
39
+ | `-v, --version` | Print version |
40
+ | `-h, --help` | Print help |
41
+
42
+ The output file is named after `--name` with the extension chosen by the mode (`MyClient.ts`, `ApiClient.swift`, `ApiClient.kt`).
43
+
44
+ ```bash
45
+ # React Query client
46
+ openapi-client-generator api.json src/api -n MyClient
47
+
48
+ # Playwright E2E client
49
+ openapi-client-generator api.json tests/api -n TestApi -m playwright
50
+
51
+ # Swift client
52
+ openapi-client-generator api.json Sources/Api -n ApiClient -m swift
53
+
54
+ # Kotlin client
55
+ openapi-client-generator api.json src/main/kotlin -n ApiClient -m kotlin
56
+ ```
57
+
58
+ A handy `package.json` script:
59
+
60
+ ```json
61
+ {
62
+ "scripts": {
63
+ "gen:api": "openapi-client-generator ./openapi.json ./src/api -n MyClient"
64
+ }
65
+ }
66
+ ```
67
+
68
+ ## Generated client examples
69
+
70
+ ### `full` — React Query
71
+
72
+ Peer dependencies: `zod`, `wretch`, `@tanstack/react-query`.
73
+
74
+ ```typescript
75
+ import { QueryClient } from "@tanstack/react-query";
76
+ import { MyClient } from "./api/MyClient";
77
+
78
+ const queryClient = new QueryClient();
79
+ const api = new MyClient("https://api.example.com", queryClient);
80
+
81
+ function TaskList() {
82
+ const { data, isLoading } = api.listTasks.useQuery({ page: 1, limit: 10 });
83
+ const create = api.createTask.useMutation();
84
+ // ...
85
+ }
86
+ ```
87
+
88
+ Constructor: `new MyClient(baseUrl, queryClient, options?, onHttpConfigure?)`. Each operation exposes `query`/`mutation` (raw) and `useQuery`/`useMutation` (hooks); responses are typed as `ApiResponse<T>` and validated with Zod.
89
+
90
+ ### `playwright` — E2E testing
91
+
92
+ Peer dependencies: `zod`, `@playwright/test`.
93
+
94
+ ```typescript
95
+ import { test, expect } from "@playwright/test";
96
+ import { TestApi } from "./api/TestApi";
97
+
98
+ test("list tasks returns 200", async ({ request }) => {
99
+ const api = new TestApi(request);
100
+ const result = await api.listTasks({ page: 1, limit: 10 });
101
+ expect(result.status).toBe(200);
102
+ expect(result.data.items.length).toBeGreaterThan(0);
103
+ });
104
+ ```
105
+
106
+ Constructor takes Playwright's `APIRequestContext` (the `{ request }` fixture). Operations are `async` methods returning a Zod-validated `ApiResponse<T>`.
107
+
108
+ ### `swift` — iOS / macOS
109
+
110
+ No external dependencies (Foundation only). Add the `.swift` file to your Xcode project.
111
+
112
+ ```swift
113
+ let api = ApiClient(baseURL: URL(string: "https://api.example.com")!)
114
+ let response = try await api.listTasks(page: 1, limit: 10)
115
+ ```
116
+
117
+ Constructor: `init(baseURL: URL, session: URLSession = .shared)`. Schemas are `Codable` structs; failures throw an `APIError` enum.
118
+
119
+ ### `kotlin` — Android / JVM
120
+
121
+ Uses Ktor + kotlinx.serialization.
122
+
123
+ ```kotlin
124
+ val httpClient = HttpClient(CIO) {
125
+ install(ContentNegotiation) { json() }
126
+ }
127
+ val api = ApiClient(client = httpClient, baseUrl = "https://api.example.com")
128
+ val response = api.listTasks(page = 1, limit = 10)
129
+ ```
130
+
131
+ Constructor: `ApiClient(client: HttpClient, baseUrl: String)`. Schemas are `@Serializable` data classes; operations are `suspend` functions returning `ApiResponse<T>` and throw `ApiException` on error.
132
+
133
+ ```kotlin
134
+ // build.gradle.kts
135
+ plugins { kotlin("plugin.serialization") version "2.1.0" }
136
+ dependencies {
137
+ implementation("io.ktor:ktor-client-core:3.1.0")
138
+ implementation("io.ktor:ktor-client-cio:3.1.0")
139
+ implementation("io.ktor:ktor-client-content-negotiation:3.1.0")
140
+ implementation("io.ktor:ktor-serialization-kotlinx-json:3.1.0")
141
+ implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.8.0")
142
+ }
143
+ ```
144
+
145
+ ## Agent skills
146
+
147
+ The package ships [agent skills](skills/) — Markdown playbooks for AI coding agents covering CLI usage (`generate-client`) and per-mode integration (`integrate-{full,playwright,swift,kotlin}-client`). Compatible tools discover them automatically once the package is installed.
148
+
149
+ ## Development
150
+
151
+ Requires [Bun](https://bun.sh) and [Task](https://taskfile.dev/).
152
+
153
+ ```bash
154
+ bun install
155
+ task test # run the vitest suite
156
+ task lint # oxlint
157
+ task build # bundle src/bin.ts -> dist/bin.js
158
+ task run # generate a sample client from fixtures/api.json into out/
159
+ ```
160
+
161
+ Run a single test file: `bun vitest run src/__tests__/converter/primitives.test.ts`
162
+
163
+ See [CLAUDE.md](CLAUDE.md) for architecture details and the full task list. `task run-pw`, `task run-swift`, and `task run-kotlin` generate sample clients for the other modes. Runtime integration tests (`task test-runtime`) require a Java/Gradle toolchain (Kotlin) or a Swift toolchain (Swift) and are skipped otherwise.
164
+
165
+ ## License
166
+
167
+ No license has been declared for this package yet. Add a `LICENSE` file and a `license` field to `package.json` / `package.json.dist` before publishing publicly.