vika-cli 1.4.0

Generate TypeScript types, Zod schemas, and Fetch-based API clients from OpenAPI/Swagger specifications
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
# ๐Ÿš€ vika-cli

### A blazing-fast Rust-powered OpenAPI code generator that produces TypeScript types, Zod schemas, and Fetch clients โ€” designed for real-world DX.

[![CI](https://github.com/MahdiZarrinkolah/vika-cli/workflows/CI/badge.svg)](https://github.com/MahdiZarrinkolah/vika-cli/actions)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)

`vika-cli` is a **blazing-fast**, **production-grade** code generator that transforms any Swagger/OpenAPI specification into fully typed:

- **TypeScript interfaces**
- **Zod validation schemas**
- **Fetch-based API client functions**
- **Module-organized output structure**

Built in Rust for exceptional performance and reliability.

---

# โœจ Features

- ๐Ÿš€ Ultra-fast Rust engine with deep schema resolution
- ๐Ÿ“ฆ Module-based generation (grouped by Swagger tags)
- ๐Ÿงฌ TypeScript interfaces from OpenAPI schemas
- ๐Ÿ›ก๏ธ Zod validation schemas with constraints
- ๐Ÿ”Œ Strongly-typed Fetch API client generation
- ๐ŸŽฏ Interactive module selection
- ๐ŸŽ› Config-driven outputs (`.vika.json`)
- ๐Ÿ’พ Spec caching for fast regeneration
- ๐Ÿ”„ Backup system for generated files
- โš ๏ธ Conflict detection for user-modified files
- ๐Ÿ” Inspect command for analyzing specs
- ๐ŸŽจ Progress indicators and verbose logging
- ๐Ÿง  Handles: oneOf, anyOf, allOf, enums, recursion, circular refs
- ๐ŸŒ Supports HEAD, OPTIONS, PATCH, all HTTP verbs
- ๐ŸŽจ Customizable templates (Tera-based) with user overrides
- ๐Ÿ›  Multi-platform installers + CI/CD automation
- ๐Ÿ”€ Multi-spec support for microservices architectures

---

# โš–๏ธ Comparison With Other Tools

| Tool                 | Types | Zod | Client      | Rust Speed | Module Selection | Inspect | Cache |
| -------------------- | ----- | --- | ----------- | ---------- | ---------------- | ------- | ----- |
| **vika-cli**         | โœ…    | โœ…  | Fetch       | โšกโšกโšก     | โœ…               | โœ…      | โœ…    |
| openapi-typescript   | โœ…    | โŒ  | โŒ          | โŒ         | โŒ               | โŒ      | โŒ    |
| Orval                | โš ๏ธ    | โš ๏ธ  | Axios/Fetch | โŒ         | โŒ               | โŒ      | โŒ    |
| openapi-client-axios | โŒ    | โŒ  | Axios       | โŒ         | โŒ               | โŒ      | โŒ    |

**Why choose vika-cli?**

- **๐Ÿš€ Rust-powered**: Blazing fast schema resolution and code generation
- **๐Ÿ›ก๏ธ Complete validation**: Native Zod schema generation with full constraint support
- **๐Ÿ“ฆ Modular**: Interactive module selection for selective generation
- **๐Ÿ” Built-in inspection**: Analyze specs without generating code
- **๐Ÿ’พ Smart caching**: Fast regeneration with intelligent spec caching
- **๐ŸŽฏ Developer experience**: Conflict detection, backups, and progress indicators

---

# ๐Ÿ“ฆ Installation

### macOS & Linux (recommended)

```bash
curl -fsSL https://github.com/MahdiZarrinkolah/vika-cli/releases/latest/download/install.sh | sh
```

### Windows (PowerShell)

```powershell
irm https://github.com/MahdiZarrinkolah/vika-cli/releases/latest/download/install.ps1 | iex
```

### Cargo

```bash
cargo install vika-cli
```

### From Source

```bash
git clone https://github.com/MahdiZarrinkolah/vika-cli
cd vika-cli
cargo build --release
```

---

# ๐Ÿš€ Quick Start

## 1๏ธ Initialize a project

```bash
vika-cli init
```

`init` now captures your first spec (name, path, schema/api outputs, module preferences) and writes `.vika.json`.  
Need more specs later? Run `vika-cli add` each time you want to register another service.

## 2๏ธ Generate code from your configured specs

```bash
# Interactive selection (default)
vika-cli generate

# Force a specific spec
vika-cli generate --spec ecommerce

# Regenerate everything
vika-cli generate --all-specs
```

Generation always uses the specs defined in `.vika.json`; the CLI no longer accepts raw spec paths. Remote URLs and local files are supported via each specโ€™s `path`.

Flags:

| Flag | Description |
| ----------- | ------------------------------- |
| `--spec <name>` | Generate specific spec (single or multi-spec mode) |
| `--all-specs` | Generate all specs (multi-spec mode only) |
| `--verbose` | Show detailed output |
| `--cache` | Use cached version of the spec |
| `--backup` | Backup files before overwriting |
| `--force` | Force overwrite conflicts |

The generator will:

1. Parse the spec(s)
2. Extract modules (tags)
3. Ask you which modules to generate
4. Produce TypeScript + Zod + Fetch clients
5. Show a detailed generation summary table

## 3๏ธ Update previously generated modules

```bash
vika-cli update
```

## 4๏ธ Inspect a specification (no generation)

### Single Spec Mode

```bash
vika-cli inspect --spec ./swagger.yaml
```

### Multi-Spec Mode

```bash
# Inspect all specs
vika-cli inspect --all-specs

# Inspect specific spec by name
vika-cli inspect --spec ecommerce
```

Examples:

```bash
# Single spec mode
vika-cli inspect --spec ./swagger.yaml --module products
vika-cli inspect --spec ./swagger.yaml --schemas
vika-cli inspect --spec ./swagger.yaml --json

# Multi-spec mode
vika-cli inspect --spec admin --schemas
vika-cli inspect --all-specs --json
```

---

# ๐Ÿง  How It Works

`vika-cli` uses a robust generation pipeline:

### 1. **Spec Parsing**

Reads OpenAPI 3.x JSON/YAML.

### 2. **Module Extraction**

Groups endpoints by Swagger tags.

### 3. **Schema Resolution**

Resolves:

- `$ref`
- Circular dependencies
- Recursive models
- oneOf / anyOf / allOf
- Enum values
- Nullable fields

### 4. **Code Generation**

Produces:

- TypeScript interfaces
- Zod schemas with constraints
- Fetch-based API clients

### 5. **Safe Writing**

- Writes only changed files
- Detects conflicts
- Optional backup mode
- Generates index/barrel files
- Optional Prettier/Biome post-formatting

---

# โš™๏ธ Configuration (`.vika.json`)

`vika-cli` now uses a single, always-multi-spec configuration model. Every spec lives inside the `specs` array and declares its own schema/API/module configuration.

```json
{
  "$schema": "https://raw.githubusercontent.com/vikarno/vika-cli/main/schema/vika-config.schema.json",
  "root_dir": "src",
  "generation": {
    "enable_cache": true,
    "enable_backup": false,
    "conflict_strategy": "ask"
  },
  "specs": [
    {
      "name": "ecommerce",
      "path": "http://localhost:3000/swagger-ecommerce.json",
      "schemas": {
        "output": "src/schemas/ecommerce",
        "naming": "PascalCase"
      },
      "apis": {
        "output": "src/apis/ecommerce",
        "style": "fetch",
        "base_url": "https://api.example.com",
        "header_strategy": "consumerInjected"
      },
      "modules": {
        "ignore": [],
        "selected": ["orders", "payments", "users"]
      }
    }
  ]
}
```

### Global settings

| Key | Description |
| --- | --- |
| `root_dir` | Base directory for generated files (`src` by default). |
| `generation.enable_cache` | Cache parsed specs for faster re-runs (default `true`). |
| `generation.enable_backup` | Create timestamped backups before overwriting (default `false`). |
| `generation.conflict_strategy` | How to handle modified files: `ask`, `force`, or `skip`. |

### Spec entry reference

| Key | Description |
| --- | --- |
| `specs[].name` | Unique spec identifier (kebab-case recommended). |
| `specs[].path` | Local file path or URL to the OpenAPI document. |
| `specs[].schemas.output` | Destination folder for TypeScript + Zod output for this spec. |
| `specs[].schemas.naming` | Naming convention (`PascalCase`, `camelCase`, `snake_case`, `kebab-case`). |
| `specs[].apis.output` | Output folder for this specโ€™s API clients. |
| `specs[].apis.style` | Client style. Currently only `fetch`. |
| `specs[].apis.base_url` | Optional base URL baked into generated clients. |
| `specs[].apis.header_strategy` | `consumerInjected`, `bearerToken`, or `fixed`. |
| `specs[].modules.ignore` | Tags to skip entirely. |
| `specs[].modules.selected` | Tags to generate. Filled automatically after the first run or when using interactive prompts. |

Add new specs via `vika-cli add` (appends to `specs`). `vika-cli init` only creates the first entry, keeping multi-spec workflows predictable.

Full reference: [`docs/configuration.md`](docs/configuration.md)

---

# ๐ŸŽจ Customizing Templates

`vika-cli` uses **Tera templates** for code generation. You can customize the output format by overriding built-in templates.

## Quick Start

1. **Initialize templates** (copies built-in templates to `.vika/templates/`):
   ```bash
   vika-cli templates init
   ```

2. **List available templates**:
   ```bash
   vika-cli templates list
   ```

3. **Customize templates** in `.vika/templates/`:
   ```bash
   # Edit .vika/templates/type-interface.tera
   # Edit .vika/templates/api-client-fetch.tera
   # etc.
   ```

4. **Regenerate code** - your custom templates will be used automatically:
   ```bash
   vika-cli generate --spec your-spec.yaml
   ```

## Template Files

- `type-interface.tera` - TypeScript interface generation
- `type-enum.tera` - TypeScript enum generation
- `zod-schema.tera` - Zod schema generation
- `api-client-fetch.tera` - API client function generation

**Template Resolution**: User templates in `.vika/templates/` override built-in templates automatically.

Full documentation: [`docs/templates.md`](docs/templates.md)

---

# ๐Ÿงฑ Output Structure

## Single Spec Mode

```
๐Ÿ“ src/
โ”‚
โ”œโ”€โ”€ ๐Ÿ“ schemas/
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ ๐Ÿ“ products/
โ”‚   โ”‚   โ”œโ”€โ”€ ๐Ÿ“„ types.ts          # TypeScript interfaces
โ”‚   โ”‚   โ”œโ”€โ”€ ๐Ÿ“„ schemas.ts        # Zod validation schemas
โ”‚   โ”‚   โ””โ”€โ”€ ๐Ÿ“„ index.ts          # Barrel exports
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ ๐Ÿ“ users/
โ”‚   โ”‚   โ”œโ”€โ”€ ๐Ÿ“„ types.ts
โ”‚   โ”‚   โ”œโ”€โ”€ ๐Ÿ“„ schemas.ts
โ”‚   โ”‚   โ””โ”€โ”€ ๐Ÿ“„ index.ts
โ”‚   โ”‚
โ”‚   โ””โ”€โ”€ ๐Ÿ“ orders/
โ”‚       โ”œโ”€โ”€ ๐Ÿ“„ types.ts
โ”‚       โ”œโ”€โ”€ ๐Ÿ“„ schemas.ts
โ”‚       โ””โ”€โ”€ ๐Ÿ“„ index.ts
โ”‚
โ””โ”€โ”€ ๐Ÿ“ apis/
    โ”‚
    โ”œโ”€โ”€ ๐Ÿ“ runtime/               # Runtime HTTP client with ApiResult types
    โ”‚   โ”œโ”€โ”€ ๐Ÿ“„ types.ts           # ApiResult type definitions
    โ”‚   โ”œโ”€โ”€ ๐Ÿ“„ http-client.ts     # VikaClient class with middleware, retries, auth
    โ”‚   โ””โ”€โ”€ ๐Ÿ“„ index.ts           # Runtime exports
    โ”‚
    โ”œโ”€โ”€ ๐Ÿ“ products/
    โ”‚   โ””โ”€โ”€ ๐Ÿ“„ index.ts           # API client functions
    โ”‚
    โ”œโ”€โ”€ ๐Ÿ“ users/
    โ”‚   โ””โ”€โ”€ ๐Ÿ“„ index.ts
    โ”‚
    โ””โ”€โ”€ ๐Ÿ“ orders/
        โ””โ”€โ”€ ๐Ÿ“„ index.ts
```

## Multi-Spec Mode (Microservices)

When using `specs` array, output is organized by spec name:

```
๐Ÿ“ src/
โ”‚
โ”œโ”€โ”€ ๐Ÿ“ schemas/
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ ๐Ÿ“ ecommerce/             # From ecommerce spec
โ”‚   โ”‚   โ”œโ”€โ”€ ๐Ÿ“ products/
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ ๐Ÿ“„ types.ts
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ ๐Ÿ“„ schemas.ts
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ ๐Ÿ“„ index.ts
โ”‚   โ”‚   โ””โ”€โ”€ ๐Ÿ“ orders/
โ”‚   โ”‚       โ””โ”€โ”€ ...
โ”‚   โ”‚
โ”‚   โ”œโ”€โ”€ ๐Ÿ“ admin/                 # From admin spec
โ”‚   โ”‚   โ”œโ”€โ”€ ๐Ÿ“ users/
โ”‚   โ”‚   โ””โ”€โ”€ ๐Ÿ“ permissions/
โ”‚   โ”‚
โ”‚   โ””โ”€โ”€ ๐Ÿ“ public/                # From public spec
โ”‚       โ””โ”€โ”€ ๐Ÿ“ catalog/
โ”‚
โ””โ”€โ”€ ๐Ÿ“ apis/
    โ”‚
    โ”œโ”€โ”€ ๐Ÿ“ runtime/               # Runtime HTTP client with ApiResult types
    โ”‚   โ”œโ”€โ”€ ๐Ÿ“„ types.ts           # ApiResult type definitions
    โ”‚   โ”œโ”€โ”€ ๐Ÿ“„ http-client.ts     # VikaClient class with middleware, retries, auth
    โ”‚   โ””โ”€โ”€ ๐Ÿ“„ index.ts           # Runtime exports
    โ”‚
    โ”œโ”€โ”€ ๐Ÿ“ ecommerce/             # From ecommerce spec
    โ”‚   โ”œโ”€โ”€ ๐Ÿ“ products/
    โ”‚   โ”‚   โ””โ”€โ”€ ๐Ÿ“„ index.ts
    โ”‚   โ””โ”€โ”€ ๐Ÿ“ orders/
    โ”‚       โ””โ”€โ”€ ๐Ÿ“„ index.ts
    โ”‚
    โ”œโ”€โ”€ ๐Ÿ“ admin/                 # From admin spec
    โ”‚   โ””โ”€โ”€ ๐Ÿ“ users/
    โ”‚       โ””โ”€โ”€ ๐Ÿ“„ index.ts
    โ”‚
    โ””โ”€โ”€ ๐Ÿ“ public/                # From public spec
        โ””โ”€โ”€ ๐Ÿ“ catalog/
            โ””โ”€โ”€ ๐Ÿ“„ index.ts
```

This structure ensures:
- โœ… **Isolation**: Each spec's generated code is separated
- โœ… **No conflicts**: Different specs can have modules with the same name
- โœ… **Clear organization**: Easy to identify which service generated which code

**File types:**

- ๐ŸŸฆ **types.ts** - TypeScript type definitions (`ProductDto`, `UserProfile`, etc.)
- ๐ŸŸจ **schemas.ts** - Zod validation schemas (`ProductDtoSchema`, etc.)
- ๐ŸŸฉ **index.ts** - Barrel exports and API client functions
- ๐ŸŸง **http.ts** - Core HTTP client with fetch wrapper

---

# ๐Ÿ“˜ Example Output

### TypeScript Types

```ts
export interface ProductDto {
  id: string;
  price: number;
  title: string;
}
```

### Zod Schema

```ts
export const ProductDtoSchema = z.object({
  id: z.string(),
  price: z.number(),
  title: z.string(),
});
```

### Fetch API Client

```ts
export const getProduct = async (id: string): Promise<ProductDto> =>
  http.get(`/products/${id}`);
```

---

# ๐Ÿงฉ Advanced Features

## Multi-Spec Support (Microservices)

`vika-cli` supports generating code from multiple OpenAPI specifications in a single project. This is ideal for microservices architectures where different services expose separate APIs.

### Benefits

- **๐Ÿ”€ Multiple Services**: Generate code for all your microservices in one command
- **๐Ÿ“ฆ Isolated Output**: Each spec's code is organized in separate directories
- **๐ŸŽฏ Selective Generation**: Generate all specs or target specific ones
- **๐Ÿ”„ Unified Workflow**: Single config file manages all specs

### Usage

1. **Configure multiple specs** in `.vika.json`:
   ```json
   {
     "specs": [
       { "name": "ecommerce", "path": "http://localhost:3000/swagger-ecommerce.json" },
       { "name": "admin", "path": "http://localhost:3000/swagger-admin.json" },
       { "name": "public", "path": "http://localhost:3000/swagger-public.json" }
     ]
   }
   ```

2. **Generate all specs**:
   ```bash
   vika-cli generate --all-specs
   ```

3. **Generate specific spec**:
   ```bash
   vika-cli generate --spec ecommerce
   ```

4. **Interactive selection** (default):
   ```bash
   vika-cli generate
   # Prompts: "Which spec do you want to generate?"
   ```

### Spec Naming Rules

- Use **kebab-case** (e.g., `ecommerce`, `order-service`, `user-api`)
- Names must be **unique** within the `specs` array
- Names are used in directory paths: `apis/{spec_name}/{module}/`

### โšก Caching

```bash
vika-cli generate --cache
```

Each spec is cached independently using its name as part of the cache key.

### ๐Ÿ›ก Backup Mode

```bash
vika-cli generate --backup
```

Backups stored in:

```
.vika-backup/<timestamp>/
```

### โš  Conflict Detection

Warns if manually modified files would be overwritten.

### ๐Ÿงช Snapshot Testing

Using `insta` for generator correctness.

---

# ๐Ÿงฑ Architecture Overview

- **Commands**: init, generate, update, inspect
- **Generator Core**: TS/Zod/API client generation
- **Schema Resolver**: Handles refs, unions, recursion
- **Writer System**: Diffs, backups, conflict detection
- **Config System**: Load & validate `.vika.json`
- **Error System**: Structured typed errors
- **Utilities**: caching, formatting, progress indicators

Details: [`docs/architecture.md`](docs/architecture.md)

---

# ๐Ÿ”„ Development & Release

### CI includes:

- `cargo fmt`
- `cargo clippy`
- Unit tests
- Snapshot tests
- Multi-platform builds

### Releases

```bash
cargo release patch
```

Semantic versioning applies:

- **MAJOR** = breaking changes
- **MINOR** = new features
- **PATCH** = fixes

See: `CHANGELOG.md`

---

# ๐Ÿค Contributing

1. Fork
2. Create a feature branch
3. Make changes with tests
4. Submit a PR

Guide: [`docs/contributing.md`](docs/contributing.md)

---

# ๐Ÿ“œ License

MIT

---

# ๐Ÿ”— Links

- ๐Ÿ”— **GitHub**: [https://github.com/MahdiZarrinkolah/vika-cli]https://github.com/MahdiZarrinkolah/vika-cli
- ๐Ÿ“š **Documentation**: /docs
- ๐Ÿž **Issue Tracker**: [https://github.com/MahdiZarrinkolah/vika-cli/issues]https://github.com/MahdiZarrinkolah/vika-cli/issues

---

# ๐ŸŽ‰ Thank You

`vika-cli` is now a fully production-grade OpenAPI codegen tool.

Enjoy building! ๐Ÿš€