@hyper-fetch/cli 8.1.0 → 8.2.0
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 +237 -26
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
# 🪄 Hyper Fetch CLI
|
|
2
2
|
|
|
3
|
-
<p>
|
|
3
|
+
<p align="center">
|
|
4
|
+
<b>Generate typed API clients from OpenAPI/Swagger schemas. Your SDK is one command away.</b>
|
|
5
|
+
</p>
|
|
6
|
+
|
|
7
|
+
<p align="center">
|
|
4
8
|
<a href="https://bettertyped.com/">
|
|
5
9
|
<img src="https://custom-icon-badges.demolab.com/static/v1?label=&message=BetterTyped&color=333&logo=BT" />
|
|
6
10
|
</a>
|
|
@@ -10,51 +14,258 @@
|
|
|
10
14
|
<a href="https://github.com/BetterTyped/hyper-fetch/blob/main/License.md">
|
|
11
15
|
<img src="https://custom-icon-badges.demolab.com/github/license/BetterTyped/hyper-fetch?logo=law&color=yellow" />
|
|
12
16
|
</a>
|
|
13
|
-
<a href="https://www.npmjs.com/package
|
|
14
|
-
<img src="https://custom-icon-badges.demolab.com/npm/v
|
|
17
|
+
<a href="https://www.npmjs.com/package/@hyper-fetch/cli">
|
|
18
|
+
<img src="https://custom-icon-badges.demolab.com/npm/v/@hyper-fetch/cli.svg?logo=npm&color=e76f51" />
|
|
19
|
+
</a>
|
|
20
|
+
<a href="https://www.npmjs.com/package/@hyper-fetch/cli">
|
|
21
|
+
<img src="https://custom-icon-badges.demolab.com/npm/dm/@hyper-fetch/cli?logoColor=fff&logo=trending-up" />
|
|
15
22
|
</a>
|
|
16
23
|
<a href="https://github.com/BetterTyped/hyper-fetch">
|
|
17
24
|
<img src="https://custom-icon-badges.demolab.com/badge/typescript-%23007ACC.svg?logo=typescript&logoColor=white" />
|
|
18
25
|
</a>
|
|
19
26
|
<a href="https://github.com/BetterTyped/hyper-fetch">
|
|
20
|
-
<img src="https://
|
|
21
|
-
</a>
|
|
22
|
-
<a href="https://www.npmjs.com/package/hyper-fetch">
|
|
23
|
-
<img src="https://custom-icon-badges.demolab.com/bundlephobia/minzip/hyper-fetch?color=64BC4B&logo=package" />
|
|
27
|
+
<img src="https://img.shields.io/badge/openapi-green?logo=openapiinitiative&logoColor=white" alt="OpenAPI" />
|
|
24
28
|
</a>
|
|
25
29
|
</p>
|
|
26
30
|
|
|
27
|
-
## About
|
|
31
|
+
## 📖 About
|
|
32
|
+
|
|
33
|
+
The HyperFetch CLI reads your OpenAPI 3.x schema and generates a complete, fully typed SDK built on `@hyper-fetch/core`. Every endpoint with an `operationId` becomes a typed request — params, payloads, query params, and response types are all extracted from the schema. The generated code is standard HyperFetch you can customize freely.
|
|
34
|
+
|
|
35
|
+
## 🎯 Key Capabilities
|
|
36
|
+
|
|
37
|
+
- 🪄 **One command, full SDK** — Point at an OpenAPI schema and get a complete typed client
|
|
38
|
+
- 🔮 **Types extracted from your schema** — Params, payloads, query params, and responses — you write none of them
|
|
39
|
+
- 🔓 **No lock-in** — Generated code is standard `@hyper-fetch/core` you can customize, extend, and override
|
|
40
|
+
- 📡 **Remote or local schemas** — Pass a URL or a local `.json` file path
|
|
41
|
+
- 🧭 **Interactive mode** — Run without flags and the CLI walks you through every option
|
|
42
|
+
- ✏️ **Your code, your rules** — Edit the generated client, add auth, override individual requests
|
|
43
|
+
|
|
44
|
+
## 🚀 Quick Start
|
|
45
|
+
|
|
46
|
+
### 1. Initialize your project
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
# Creates api.json config and the api/ directory structure
|
|
50
|
+
npx @hyper-fetch/cli init
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
This creates an `api.json` config file at your project root and sets up the output directory (default: `src/api/`).
|
|
54
|
+
|
|
55
|
+
### 2. Generate your SDK
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
# Generate from a remote OpenAPI schema
|
|
59
|
+
npx @hyper-fetch/cli generate --url https://api.example.com/openapi.json
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### 3. Use the generated SDK
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
import { sdk } from "./src/api/api-openapi.sdk";
|
|
66
|
+
|
|
67
|
+
// Every endpoint is typed — params, payloads, and responses
|
|
68
|
+
const { data } = await sdk.users.list.send();
|
|
69
|
+
const { data: user } = await sdk.users.get.setParams({ userId: 1 }).send();
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## 📋 Commands
|
|
73
|
+
|
|
74
|
+
### `init`
|
|
75
|
+
|
|
76
|
+
Sets up the project directory structure and creates the `api.json` config file.
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
npx @hyper-fetch/cli init
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
| Flag | Description |
|
|
83
|
+
| --- | --- |
|
|
84
|
+
| `-y`, `--yes` | Skip prompts and use defaults (`src/api/`) |
|
|
85
|
+
| `-c`, `--cwd <path>` | Set working directory (default: current directory) |
|
|
86
|
+
|
|
87
|
+
Without `--yes`, you'll be asked to choose:
|
|
88
|
+
- **Main directory** — `src`, `app`, or a custom path
|
|
89
|
+
- **API subdirectory** — name of the folder for generated files (default: `api`)
|
|
90
|
+
|
|
91
|
+
### `generate`
|
|
92
|
+
|
|
93
|
+
Reads an OpenAPI schema and generates a typed SDK file.
|
|
94
|
+
|
|
95
|
+
```bash
|
|
96
|
+
npx @hyper-fetch/cli generate --url <schema> [options]
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
| Flag | Description |
|
|
100
|
+
| --- | --- |
|
|
101
|
+
| `-u`, `--url <url>` | Schema source — a URL or local file path (relative to cwd) |
|
|
102
|
+
| `-t`, `--template <type>` | Schema type: `openapi` or `swagger` (default: interactive prompt) |
|
|
103
|
+
| `-f`, `--fileName <name>` | Output filename (default: `api-openapi.sdk.ts`) |
|
|
104
|
+
| `-o`, `--overwrite` | Overwrite existing file without asking |
|
|
105
|
+
| `-c`, `--cwd <path>` | Set working directory (default: current directory) |
|
|
106
|
+
|
|
107
|
+
Without flags, the CLI runs in **interactive mode** — it prompts for every option.
|
|
108
|
+
|
|
109
|
+
## ⚙️ Configuration — `api.json`
|
|
110
|
+
|
|
111
|
+
The `init` command creates an `api.json` file at your project root. This is the only config file the CLI uses.
|
|
112
|
+
|
|
113
|
+
```json
|
|
114
|
+
{
|
|
115
|
+
"tsx": true,
|
|
116
|
+
"aliases": {
|
|
117
|
+
"api": "@/api",
|
|
118
|
+
"hooks": "@/hooks",
|
|
119
|
+
"ui": "@/ui",
|
|
120
|
+
"components": "@/components",
|
|
121
|
+
"lib": "@/lib"
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
| Field | Description |
|
|
127
|
+
| --- | --- |
|
|
128
|
+
| `tsx` | When `true`, generates `.ts` files. When `false`, generates `.js` |
|
|
129
|
+
| `aliases` | Path aliases resolved from your `tsconfig.json` paths. Controls where the SDK file is written |
|
|
130
|
+
|
|
131
|
+
The CLI auto-detects your `tsconfig.json` path aliases (e.g. `@/*` → `./src/*`) to resolve output directories.
|
|
132
|
+
|
|
133
|
+
## 📁 Generated Output
|
|
134
|
+
|
|
135
|
+
The CLI generates a **single TypeScript file** under your configured API directory:
|
|
136
|
+
|
|
137
|
+
```
|
|
138
|
+
src/
|
|
139
|
+
api/
|
|
140
|
+
api-openapi.sdk.ts ← generated SDK file
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
The generated file contains:
|
|
28
144
|
|
|
29
|
-
|
|
145
|
+
1. **Schema types** — TypeScript interfaces for every schema component
|
|
146
|
+
2. **Typed requests** — One `createRequest` call per endpoint (with `operationId`)
|
|
147
|
+
3. **SDK tree** — Nested object mirroring your API paths: `sdk.users.get`, `sdk.posts.list`, etc.
|
|
148
|
+
4. **`createSdk` function** — Factory to create the SDK with your custom client
|
|
30
149
|
|
|
31
|
-
|
|
150
|
+
```ts
|
|
151
|
+
// What gets generated (simplified)
|
|
152
|
+
import { createSdk as coreCreateSdk, ClientInstance, Request } from "@hyper-fetch/core";
|
|
32
153
|
|
|
33
|
-
|
|
154
|
+
// Schema types extracted from your OpenAPI document
|
|
155
|
+
export interface Components {
|
|
156
|
+
schemas: {
|
|
157
|
+
User: { id: number; name: string; email: string };
|
|
158
|
+
// ... all your schema types
|
|
159
|
+
};
|
|
160
|
+
}
|
|
34
161
|
|
|
35
|
-
|
|
162
|
+
// Typed request for each endpoint with operationId
|
|
163
|
+
export const getUsers = client.createRequest<{
|
|
164
|
+
response: Components["schemas"]["User"][];
|
|
165
|
+
}>()({
|
|
166
|
+
endpoint: "/users",
|
|
167
|
+
method: "GET",
|
|
168
|
+
});
|
|
36
169
|
|
|
37
|
-
|
|
170
|
+
// SDK tree you import and use
|
|
171
|
+
export const createSdk = <Client extends ClientInstance>(client: Client) =>
|
|
172
|
+
coreCreateSdk<Client, SdkSchema<Client>>(client);
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
## ⚠️ Important Notes
|
|
176
|
+
|
|
177
|
+
- **`operationId` is required** — Only endpoints with an `operationId` in the schema are included. Endpoints without one are silently skipped.
|
|
178
|
+
- **JSON schemas only for local files** — Local file paths must point to `.json` files. YAML is not supported for local files.
|
|
179
|
+
- **Remote schemas are fetched with GET** — No auth headers are sent. If your schema requires authentication, download it locally first and pass the file path.
|
|
180
|
+
- **Path params are converted** — `{userId}` in OpenAPI becomes `:userId` in HyperFetch endpoints.
|
|
181
|
+
- **Path segments are camelCased** — Kebab-case path segments become camelCase keys in the SDK tree. `{param}` segments become `$param`.
|
|
182
|
+
|
|
183
|
+
## 📚 Documentation
|
|
184
|
+
|
|
185
|
+
- [Generate Command](https://hyperfetch.bettertyped.com/docs/cli/commands/generate)
|
|
186
|
+
- [Getting Started](https://hyperfetch.bettertyped.com/docs/getting-started/quick-start)
|
|
187
|
+
- [API Reference](https://hyperfetch.bettertyped.com/api/)
|
|
38
188
|
|
|
39
|
-
|
|
40
|
-
|
|
189
|
+
## 💡 Examples
|
|
190
|
+
|
|
191
|
+
### Generate from a remote schema
|
|
192
|
+
|
|
193
|
+
```bash
|
|
194
|
+
# OpenAPI 3.x schema from a URL
|
|
195
|
+
npx @hyper-fetch/cli generate --url https://petstore3.swagger.io/api/v3/openapi.json
|
|
41
196
|
```
|
|
42
197
|
|
|
43
|
-
|
|
198
|
+
### Generate from a local file
|
|
44
199
|
|
|
45
|
-
|
|
200
|
+
```bash
|
|
201
|
+
# Local JSON file relative to your project root
|
|
202
|
+
npx @hyper-fetch/cli generate --url ./schemas/openapi.json --fileName my-api.sdk.ts
|
|
203
|
+
```
|
|
46
204
|
|
|
47
|
-
|
|
205
|
+
### Skip all prompts (CI/scripts)
|
|
48
206
|
|
|
49
|
-
|
|
207
|
+
```bash
|
|
208
|
+
# Non-interactive: all flags provided, overwrite existing file
|
|
209
|
+
npx @hyper-fetch/cli generate \
|
|
210
|
+
--url https://api.example.com/openapi.json \
|
|
211
|
+
--template openapi \
|
|
212
|
+
--fileName api.sdk.ts \
|
|
213
|
+
--overwrite
|
|
214
|
+
```
|
|
50
215
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
216
|
+
### Use the generated SDK
|
|
217
|
+
|
|
218
|
+
```ts
|
|
219
|
+
import { createSdk } from "./src/api/api-openapi.sdk";
|
|
220
|
+
import { createClient } from "@hyper-fetch/core";
|
|
221
|
+
|
|
222
|
+
// Create your client with any configuration you need
|
|
223
|
+
const client = createClient({ url: "https://api.example.com" });
|
|
224
|
+
|
|
225
|
+
// Create the typed SDK from your client
|
|
226
|
+
const sdk = createSdk(client);
|
|
227
|
+
|
|
228
|
+
// Every method is fully typed — params, payloads, and responses
|
|
229
|
+
const { data: users } = await sdk.users.list.send();
|
|
230
|
+
const { data: user } = await sdk.users.get.setParams({ userId: 1 }).send();
|
|
231
|
+
const { data: newUser } = await sdk.pets.create.send({
|
|
232
|
+
data: { name: "Buddy", species: "dog" },
|
|
233
|
+
});
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
### Add authentication to the generated client
|
|
237
|
+
|
|
238
|
+
```ts
|
|
239
|
+
import { createSdk } from "./src/api/api-openapi.sdk";
|
|
240
|
+
import { createClient } from "@hyper-fetch/core";
|
|
241
|
+
|
|
242
|
+
const client = createClient({ url: "https://api.example.com" });
|
|
243
|
+
const sdk = createSdk(client);
|
|
244
|
+
|
|
245
|
+
// Add auth header to every request through the client
|
|
246
|
+
client.onAuth((request) => {
|
|
247
|
+
return request.setHeaders({ Authorization: `Bearer ${getToken()}` });
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
// All SDK requests now include the auth header automatically
|
|
251
|
+
const { data } = await sdk.users.me.send();
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
### Programmatic usage (no CLI)
|
|
255
|
+
|
|
256
|
+
```ts
|
|
257
|
+
import { OpenapiRequestGenerator } from "@hyper-fetch/cli";
|
|
258
|
+
|
|
259
|
+
// Use the generator directly in scripts or build tools
|
|
260
|
+
const schema = await OpenapiRequestGenerator.getSchemaFromUrl(url, config);
|
|
261
|
+
const generator = new OpenapiRequestGenerator(schema, config);
|
|
262
|
+
const output = await generator.generateFile(fileName);
|
|
263
|
+
```
|
|
56
264
|
|
|
57
265
|
## Disclaimer
|
|
58
266
|
|
|
59
|
-
This package is
|
|
60
|
-
|
|
267
|
+
This package is inspired by [openapi-client-axios](https://www.npmjs.com/package/openapi-client-axios).
|
|
268
|
+
|
|
269
|
+
## License
|
|
270
|
+
|
|
271
|
+
[MIT](https://github.com/BetterTyped/hyper-fetch/blob/main/License.md)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hyper-fetch/cli",
|
|
3
|
-
"version": "8.
|
|
3
|
+
"version": "8.2.0",
|
|
4
4
|
"description": "Hyper Fetch cli for code generation and utilities",
|
|
5
5
|
"author": "Maciej Pyrc <maciekpyrc@gmail.com>, Kacper Skawina <kacper.skawina@gmail.com>",
|
|
6
6
|
"homepage": "https://hyperfetch.bettertyped.com/",
|