zenko 0.1.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/LICENSE +21 -0
- package/README.md +243 -0
- package/dist/cli.cjs +898 -0
- package/dist/cli.cjs.map +1 -0
- package/dist/cli.d.cts +1 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.mjs +875 -0
- package/dist/cli.mjs.map +1 -0
- package/dist/index.cjs +729 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +16 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.mjs +702 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +73 -0
package/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
echo "MIT License
|
2
|
+
|
3
|
+
Copyright (c) $(date +%Y) RawToast
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the \"Software\"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE." > LICENSE
|
package/README.md
ADDED
@@ -0,0 +1,243 @@
|
|
1
|
+
# Zenko
|
2
|
+
|
3
|
+
[](https://github.com/RawToast/zenko/actions/workflows/ci.yml)
|
4
|
+
|
5
|
+
A TypeScript generator for OpenAPI specifications that creates **Zod schemas**, **type-safe path functions**, and **operation objects**.
|
6
|
+
|
7
|
+
Unlike most OpenAPI generators, Zenko does not create a client. Instead you are free to use your own fetch wrapper or library of choice.
|
8
|
+
|
9
|
+
## Features
|
10
|
+
|
11
|
+
- 🔧 **Zod Schema Generation** - Generates runtime-validated Zod schemas from OpenAPI schemas
|
12
|
+
- 🛣️ **Type-safe Path Functions** - Creates functions to build API paths with proper TypeScript types
|
13
|
+
- 📋 **Operation Objects** - Generates objects containing path functions, request validation, and response types
|
14
|
+
- 🔄 **Dependency Resolution** - Automatically resolves schema dependencies with topological sorting
|
15
|
+
- ⚡ **CLI & Programmatic API** - Use via command line or import as a library
|
16
|
+
|
17
|
+
## Installation
|
18
|
+
|
19
|
+
### One-time Usage
|
20
|
+
|
21
|
+
```bash
|
22
|
+
# Use directly with npx (no installation required)
|
23
|
+
npx zenko input.yaml output.ts
|
24
|
+
|
25
|
+
# Or with bunx
|
26
|
+
bunx zenko input.yaml output.ts
|
27
|
+
```
|
28
|
+
|
29
|
+
### Install for Repeated Use
|
30
|
+
|
31
|
+
```bash
|
32
|
+
# Install globally
|
33
|
+
npm install -g zenko
|
34
|
+
bun install -g zenko
|
35
|
+
|
36
|
+
# Or install locally
|
37
|
+
npm install zenko
|
38
|
+
bun add zenko
|
39
|
+
yarn add zenko
|
40
|
+
pnpm add zenko
|
41
|
+
```
|
42
|
+
|
43
|
+
## Usage
|
44
|
+
|
45
|
+
### Command Line
|
46
|
+
|
47
|
+
```bash
|
48
|
+
# Generate TypeScript types from OpenAPI spec
|
49
|
+
zenko input.yaml output.ts
|
50
|
+
zenko petstore.json api-types.ts
|
51
|
+
|
52
|
+
# Enable strict guards (date + numeric metadata only; validation coming soon)
|
53
|
+
zenko input.yaml output.ts --strict-dates --strict-numeric
|
54
|
+
|
55
|
+
# Drive multiple specs from a config file
|
56
|
+
zenko --config zenko.config.json
|
57
|
+
|
58
|
+
# Show help
|
59
|
+
zenko --help
|
60
|
+
zenko -h
|
61
|
+
|
62
|
+
# One-time usage (no installation)
|
63
|
+
npx zenko input.yaml output.ts
|
64
|
+
bunx zenko input.yaml output.ts
|
65
|
+
```
|
66
|
+
|
67
|
+
### Config File
|
68
|
+
|
69
|
+
The config file is a JSON file that contains an array of schema objects. Each schema object contains the input and output files, and the strict dates and numeric options.
|
70
|
+
|
71
|
+
```json
|
72
|
+
{
|
73
|
+
"schemas": [
|
74
|
+
{
|
75
|
+
"input": "my-api.yaml",
|
76
|
+
"output": "my-api.gen.ts"
|
77
|
+
},
|
78
|
+
{
|
79
|
+
"input": "my-strict-api.yaml",
|
80
|
+
"output": "my-strict-api.gen.ts",
|
81
|
+
"strictDates": true,
|
82
|
+
"strictNumeric": true
|
83
|
+
}
|
84
|
+
]
|
85
|
+
}
|
86
|
+
```
|
87
|
+
|
88
|
+
### Programmatic Usage
|
89
|
+
|
90
|
+
```typescript
|
91
|
+
import { generate, type OpenAPISpec } from "zenko"
|
92
|
+
import * as fs from "fs"
|
93
|
+
import { load } from "js-yaml"
|
94
|
+
|
95
|
+
// Load your OpenAPI spec
|
96
|
+
const spec = load(fs.readFileSync("api.yaml", "utf8")) as OpenAPISpec
|
97
|
+
|
98
|
+
// Generate TypeScript code
|
99
|
+
const output = generate(spec)
|
100
|
+
|
101
|
+
// Write to file
|
102
|
+
fs.writeFileSync("types.ts", output)
|
103
|
+
```
|
104
|
+
|
105
|
+
#### ES Modules
|
106
|
+
|
107
|
+
```typescript
|
108
|
+
import { generate } from "zenko"
|
109
|
+
```
|
110
|
+
|
111
|
+
#### CommonJS
|
112
|
+
|
113
|
+
```javascript
|
114
|
+
const { generate } = require("zenko")
|
115
|
+
```
|
116
|
+
|
117
|
+
## Generated Output
|
118
|
+
|
119
|
+
Zenko generates three main types of code:
|
120
|
+
|
121
|
+
### 1. Zod Schemas
|
122
|
+
|
123
|
+
```typescript
|
124
|
+
import { z } from "zod"
|
125
|
+
|
126
|
+
// Enum schemas
|
127
|
+
export const OtpDispatchMethod = z.enum(["SMS", "VOICE"])
|
128
|
+
export type OtpDispatchMethod = z.infer<typeof OtpDispatchMethod>
|
129
|
+
|
130
|
+
// Object schemas with dependencies resolved
|
131
|
+
export const Recaptcha = z.object({
|
132
|
+
recaptcha_token: z.string(),
|
133
|
+
recaptcha_platform: z.enum(["Web", "IOS", "ANDROID", "CHECKOUT"]),
|
134
|
+
})
|
135
|
+
export type Recaptcha = z.infer<typeof Recaptcha>
|
136
|
+
|
137
|
+
// Complex request/response schemas
|
138
|
+
export const AuthenticateRequest = z.object({
|
139
|
+
recaptcha: Recaptcha,
|
140
|
+
otp_dispatch_method: OtpDispatchMethod,
|
141
|
+
})
|
142
|
+
export type AuthenticateRequest = z.infer<typeof AuthenticateRequest>
|
143
|
+
```
|
144
|
+
|
145
|
+
### 2. Path Functions
|
146
|
+
|
147
|
+
```typescript
|
148
|
+
// Path Functions
|
149
|
+
export const paths = {
|
150
|
+
// Simple paths
|
151
|
+
getUser: () => "/user",
|
152
|
+
|
153
|
+
// Parameterized paths with TypeScript types
|
154
|
+
getUserById: ({ userId }: { userId: string }) => `/users/${userId}`,
|
155
|
+
updatePost: ({ userId, postId }: { userId: string; postId: string }) =>
|
156
|
+
`/users/${userId}/posts/${postId}`,
|
157
|
+
} as const
|
158
|
+
```
|
159
|
+
|
160
|
+
### 3. Operation Objects
|
161
|
+
|
162
|
+
```typescript
|
163
|
+
// Operation Objects
|
164
|
+
export const authenticateUser = {
|
165
|
+
path: paths.authenticateUser,
|
166
|
+
request: AuthenticateRequest.safeParse,
|
167
|
+
response: AuthenticateResponse,
|
168
|
+
} as const
|
169
|
+
|
170
|
+
export const getUserById = {
|
171
|
+
path: paths.getUserById,
|
172
|
+
response: UserResponse,
|
173
|
+
} as const
|
174
|
+
```
|
175
|
+
|
176
|
+
## Example Usage in Your App
|
177
|
+
|
178
|
+
```typescript
|
179
|
+
import { paths, authenticateUser, AuthenticateRequest } from "./generated-types"
|
180
|
+
|
181
|
+
// Type-safe path building
|
182
|
+
const userPath = paths.getUserById({ userId: "123" })
|
183
|
+
// → "/users/123"
|
184
|
+
|
185
|
+
// Request validation with Zod
|
186
|
+
const requestData = {
|
187
|
+
recaptcha: {
|
188
|
+
recaptcha_token: "token123",
|
189
|
+
recaptcha_platform: "Web" as const,
|
190
|
+
},
|
191
|
+
otp_dispatch_method: "SMS" as const,
|
192
|
+
}
|
193
|
+
|
194
|
+
const validation = authenticateUser.request(requestData)
|
195
|
+
if (validation.success) {
|
196
|
+
// Make API call with validated data
|
197
|
+
const response = await fetch("/api" + authenticateUser.path(), {
|
198
|
+
method: "POST",
|
199
|
+
body: JSON.stringify(validation.data),
|
200
|
+
})
|
201
|
+
}
|
202
|
+
```
|
203
|
+
|
204
|
+
## Key Improvements
|
205
|
+
|
206
|
+
### Dependency Resolution
|
207
|
+
|
208
|
+
Zenko automatically resolves schema dependencies using topological sorting, ensuring that referenced types are defined before they're used. This eliminates "used before declaration" errors.
|
209
|
+
|
210
|
+
### Zod Integration
|
211
|
+
|
212
|
+
Runtime validation with Zod schemas provides:
|
213
|
+
|
214
|
+
- Type safety at compile time
|
215
|
+
- Runtime validation for API requests/responses
|
216
|
+
- Automatic type inference with `z.infer<>`
|
217
|
+
- Integration with form libraries and validation flows
|
218
|
+
|
219
|
+
## Development
|
220
|
+
|
221
|
+
```bash
|
222
|
+
# Install dependencies
|
223
|
+
bun install
|
224
|
+
|
225
|
+
# Run tests
|
226
|
+
bun test
|
227
|
+
|
228
|
+
# Build the package
|
229
|
+
bun run build
|
230
|
+
|
231
|
+
# Test with example spec
|
232
|
+
zenko src/resources/petstore.yaml output.ts
|
233
|
+
|
234
|
+
# Format code
|
235
|
+
bun run format
|
236
|
+
```
|
237
|
+
|
238
|
+
## Architecture
|
239
|
+
|
240
|
+
- **`src/zenko.ts`** - Main OpenAPI → TypeScript generator
|
241
|
+
- **`src/cli.ts`** - Command-line interface
|
242
|
+
- **`dist/`** - Bundled outputs (CJS + ESM)
|
243
|
+
- **Topological Sort** - Ensures proper dependency ordering for schema generation
|