@tronsfey/openapi2cli 1.0.10

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.
Files changed (42) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +173 -0
  3. package/README.zh.md +173 -0
  4. package/bin/openapi2cli +2 -0
  5. package/dist/analyzer/schema-analyzer.d.ts +4 -0
  6. package/dist/analyzer/schema-analyzer.d.ts.map +1 -0
  7. package/dist/analyzer/schema-analyzer.js +329 -0
  8. package/dist/analyzer/schema-analyzer.js.map +1 -0
  9. package/dist/auth/auth-provider.d.ts +22 -0
  10. package/dist/auth/auth-provider.d.ts.map +1 -0
  11. package/dist/auth/auth-provider.js +100 -0
  12. package/dist/auth/auth-provider.js.map +1 -0
  13. package/dist/generator/command-generator.d.ts +3 -0
  14. package/dist/generator/command-generator.d.ts.map +1 -0
  15. package/dist/generator/command-generator.js +96 -0
  16. package/dist/generator/command-generator.js.map +1 -0
  17. package/dist/generator/template-engine.d.ts +2 -0
  18. package/dist/generator/template-engine.d.ts.map +1 -0
  19. package/dist/generator/template-engine.js +154 -0
  20. package/dist/generator/template-engine.js.map +1 -0
  21. package/dist/index.d.ts +3 -0
  22. package/dist/index.d.ts.map +1 -0
  23. package/dist/index.js +135 -0
  24. package/dist/index.js.map +1 -0
  25. package/dist/parser/oas-parser.d.ts +3 -0
  26. package/dist/parser/oas-parser.d.ts.map +1 -0
  27. package/dist/parser/oas-parser.js +64 -0
  28. package/dist/parser/oas-parser.js.map +1 -0
  29. package/dist/templates/README.md.hbs +197 -0
  30. package/dist/templates/README.zh.md.hbs +197 -0
  31. package/dist/templates/SKILL.md.hbs +134 -0
  32. package/dist/templates/api-client.ts.hbs +217 -0
  33. package/dist/templates/command.ts.hbs +130 -0
  34. package/dist/templates/flat-commands.ts.hbs +126 -0
  35. package/dist/templates/index.ts.hbs +38 -0
  36. package/dist/templates/package.json.hbs +31 -0
  37. package/dist/templates/tsconfig.json.hbs +16 -0
  38. package/dist/types/index.d.ts +104 -0
  39. package/dist/types/index.d.ts.map +1 -0
  40. package/dist/types/index.js +3 -0
  41. package/dist/types/index.js.map +1 -0
  42. package/package.json +88 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 cli-openapi contributors
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.
package/README.md ADDED
@@ -0,0 +1,173 @@
1
+ English | [中文](./README.zh.md)
2
+
3
+ # openapi2cli
4
+
5
+ Generate a fully typed [Commander.js](https://github.com/tj/commander.js) CLI project from an OpenAPI 3.x specification — in one command.
6
+
7
+ ## Features
8
+
9
+ - Parse OAS 3.x specs from a **file path or URL** (full `$ref` resolution)
10
+ - Group operations by **tag** → Commander subcommand groups; untagged operations become top-level commands
11
+ - Generate **TypeScript source** with correct types, required/optional flags, enum `.choices()` validation
12
+ - **5 auth schemes** auto-detected from OAS security definitions: Bearer, API Key, HTTP Basic, OAuth2 Client Credentials, custom dynamic token provider
13
+ - **SSE streaming** via `eventsource-parser` (Node.js-native, used by Anthropic SDK) — full SSE spec support, `[DONE]` sentinel handling
14
+ - **Pagination** via `--all-pages` (follows `Link: rel="next"` response headers)
15
+ - **JMESPath filtering** via `--query` on every command
16
+ - **CJK command names** — Chinese/Japanese/Korean operationIds auto-converted to pinyin
17
+ - **OpenAPI extensions**: `x-cli-name`, `x-cli-aliases`, `x-cli-ignore`, `x-cli-token-url`
18
+ - Generates **bilingual docs**: `README.md` (English) + `README.zh.md` (Chinese) + `SKILL.md` (Claude Code skill descriptor)
19
+ - Global `--endpoint`, `--format` (json/yaml/table), and `--verbose` on every generated CLI
20
+
21
+ ## Installation
22
+
23
+ ```bash
24
+ npm install -g openapi2cli
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ ```bash
30
+ # From a local file
31
+ openapi2cli --oas ./openapi.yaml --name my-api --output ./my-api-cli
32
+
33
+ # From a URL
34
+ openapi2cli --oas https://petstore3.swagger.io/api/v3/openapi.json --name petstore --output ./petstore-cli
35
+ ```
36
+
37
+ Build and link the generated project:
38
+
39
+ ```bash
40
+ cd my-api-cli
41
+ npm install && npm run build && npm link
42
+ my-api --help
43
+ ```
44
+
45
+ ## Example
46
+
47
+ See [`examples/github-cli/`](./examples/github-cli/) for a complete generated CLI targeting the GitHub REST API:
48
+
49
+ ```bash
50
+ cd examples/github-cli
51
+ npm install && npm run build
52
+ node dist/index.js repos get-repo --owner octocat --repo Hello-World
53
+ node dist/index.js repos list-repo-issues --owner octocat --repo Hello-World --state open
54
+ node dist/index.js users get-user --username octocat
55
+ ```
56
+
57
+ ## Generated Project Structure
58
+
59
+ ```
60
+ <output>/
61
+ ├── bin/<name> # executable shebang wrapper
62
+ ├── src/
63
+ │ ├── index.ts # Commander root program + global options
64
+ │ ├── commands/
65
+ │ │ └── <tag>.ts # one file per OAS tag
66
+ │ └── lib/
67
+ │ └── api-client.ts # axios client with auth + SSE streaming
68
+ ├── README.md # English usage guide
69
+ ├── README.zh.md # Chinese usage guide
70
+ ├── SKILL.md # Claude Code skill descriptor
71
+ ├── package.json
72
+ └── tsconfig.json
73
+ ```
74
+
75
+ Runtime dependencies in the generated project: `axios`, `chalk`, `commander`, `eventsource-parser`, `jmespath`, `yaml`, `ora`.
76
+
77
+ ## Authentication
78
+
79
+ The generator inspects the first security scheme in the OAS document and emits the appropriate auth code. Set the matching environment variable before running the CLI:
80
+
81
+ | Scheme | Env var(s) | Notes |
82
+ |--------|-----------|-------|
83
+ | Bearer | `<NAME>_TOKEN` | `Authorization: Bearer …` |
84
+ | API Key | `<NAME>_API_KEY` | Header or query param name from OAS |
85
+ | HTTP Basic | `<NAME>_CREDENTIALS` | `user:password` → Base64 |
86
+ | OAuth2 Client Credentials | `<NAME>_CLIENT_ID`, `<NAME>_CLIENT_SECRET`, `<NAME>_SCOPES` | Token fetched once at startup, cached for process lifetime |
87
+ | Dynamic (`x-cli-token-url`) | custom env vars defined in the extension | JSON body POSTed to the token URL |
88
+
89
+ `<NAME>` is the CLI name in SCREAMING_SNAKE_CASE (e.g. `my-api` → `MY_API`).
90
+
91
+ ## OpenAPI Extensions
92
+
93
+ Add these vendor extensions to your OAS document to control CLI generation:
94
+
95
+ | Extension | Placement | Effect |
96
+ |-----------|-----------|--------|
97
+ | `x-cli-name: "my-name"` | operation | Override the auto-generated command name |
98
+ | `x-cli-aliases: ["a"]` | operation | Add Commander aliases to the command |
99
+ | `x-cli-ignore: true` | operation | Exclude the operation from the generated CLI |
100
+ | `x-cli-token-url: "https://…"` | securityScheme | Custom token endpoint for dynamic auth |
101
+
102
+ ## Global Options
103
+
104
+ Every generated CLI exposes these options on all commands:
105
+
106
+ | Option | Description | Default |
107
+ |--------|-------------|---------|
108
+ | `--endpoint <url>` | Override the base API URL | spec server URL |
109
+ | `--format <fmt>` | Output format: `json`, `yaml`, `table` | `json` |
110
+ | `--verbose` | Log HTTP method + URL before each request | `false` |
111
+ | `--query <expr>` | JMESPath expression to filter the response | — |
112
+ | `--all-pages` | Auto-paginate via `Link: rel="next"` headers | `false` |
113
+
114
+ ## Request Body
115
+
116
+ For `POST`, `PUT`, `PATCH` commands use `--data`:
117
+
118
+ ```bash
119
+ # Inline JSON
120
+ my-api widgets create --data '{"name": "foo", "color": "blue"}'
121
+
122
+ # From a file
123
+ my-api widgets create --data @./payload.json
124
+ ```
125
+
126
+ ## Output Formats
127
+
128
+ ```bash
129
+ # Default: pretty-printed JSON
130
+ my-api repos list
131
+
132
+ # Table view (great for arrays)
133
+ my-api repos list --format table
134
+
135
+ # YAML
136
+ my-api repos list --format yaml
137
+
138
+ # JMESPath filter
139
+ my-api repos list --query '[].name'
140
+
141
+ # Pipe to jq
142
+ my-api repos list | jq '.[].full_name'
143
+
144
+ # Save to file
145
+ my-api repos list > repos.json
146
+ ```
147
+
148
+ ## Streaming (SSE)
149
+
150
+ Operations that return `text/event-stream` are detected automatically from the OAS response content type. The generated CLI pipes each SSE data payload to stdout, one per line:
151
+
152
+ ```bash
153
+ my-api completions create --model gpt-4o --stream
154
+ ```
155
+
156
+ The generated client uses `eventsource-parser` with the native `fetch` API. It supports multi-line `data:` payloads, named `event:` types, and silently drops `[DONE]` sentinels used by OpenAI-compatible APIs.
157
+
158
+ ## CLI Options
159
+
160
+ ```
161
+ openapi2cli [options]
162
+
163
+ Options:
164
+ --oas <path|url> Path or URL to the OpenAPI 3.x spec (required)
165
+ --name <name> CLI executable name (required)
166
+ --output <dir> Output directory (required)
167
+ --overwrite Overwrite existing output directory
168
+ -h, --help Display help
169
+ ```
170
+
171
+ ## License
172
+
173
+ MIT
package/README.zh.md ADDED
@@ -0,0 +1,173 @@
1
+ [English](./README.md) | 中文
2
+
3
+ # openapi2cli
4
+
5
+ 一条命令,将 OpenAPI 3.x 规范生成为完整的 [Commander.js](https://github.com/tj/commander.js) TypeScript CLI 项目。
6
+
7
+ ## 功能特性
8
+
9
+ - 支持从**文件路径或 URL** 解析 OAS 3.x 规范(完整 `$ref` 解析)
10
+ - 按 **tag** 分组操作,生成 Commander 子命令组;未分组的操作注册为顶层命令
11
+ - 生成带完整类型标注的 **TypeScript 源码**,支持必填/可选参数、枚举 `.choices()` 校验
12
+ - **5 种认证方案**自动从 OAS 安全定义中检测:Bearer、API Key、HTTP Basic、OAuth2 Client Credentials、自定义动态 Token
13
+ - **SSE 流式输出**基于 `eventsource-parser`,支持自动重连和 `[DONE]` 哨兵处理
14
+ - **自动分页**:`--all-pages` 参数,自动跟随 `Link: rel="next"` 响应头翻页
15
+ - **JMESPath 过滤**:每条命令均支持 `--query` 参数
16
+ - **中文命令名支持**:中/日/韩 operationId 自动转换为拼音
17
+ - **OpenAPI 扩展**:`x-cli-name`、`x-cli-aliases`、`x-cli-ignore`、`x-cli-token-url`
18
+ - 自动生成**双语文档**:`README.md`(英文)+ `README.zh.md`(中文)+ `SKILL.md`(Claude Code 技能描述文件)
19
+ - 所有生成的 CLI 均内置全局选项:`--endpoint`、`--format`(json/yaml/table)、`--verbose`
20
+
21
+ ## 安装
22
+
23
+ ```bash
24
+ npm install -g openapi2cli
25
+ ```
26
+
27
+ ## 使用方法
28
+
29
+ ```bash
30
+ # 从本地文件生成
31
+ openapi2cli --oas ./openapi.yaml --name my-api --output ./my-api-cli
32
+
33
+ # 从 URL 生成
34
+ openapi2cli --oas https://petstore3.swagger.io/api/v3/openapi.json --name petstore --output ./petstore-cli
35
+ ```
36
+
37
+ 构建并链接生成的项目:
38
+
39
+ ```bash
40
+ cd my-api-cli
41
+ npm install && npm run build && npm link
42
+ my-api --help
43
+ ```
44
+
45
+ ## 示例
46
+
47
+ 参见 [`examples/github-cli/`](./examples/github-cli/),这是一个针对 GitHub REST API 的完整生成示例:
48
+
49
+ ```bash
50
+ cd examples/github-cli
51
+ npm install && npm run build
52
+ node dist/index.js repos get-repo --owner octocat --repo Hello-World
53
+ node dist/index.js repos list-repo-issues --owner octocat --repo Hello-World --state open
54
+ node dist/index.js users get-user --username octocat
55
+ ```
56
+
57
+ ## 生成项目结构
58
+
59
+ ```
60
+ <output>/
61
+ ├── bin/<name> # 可执行文件 shebang 包装器
62
+ ├── src/
63
+ │ ├── index.ts # Commander 根程序 + 全局选项
64
+ │ ├── commands/
65
+ │ │ └── <tag>.ts # 每个 OAS tag 对应一个文件
66
+ │ └── lib/
67
+ │ └── api-client.ts # 带认证 + SSE 流式支持的 axios 客户端
68
+ ├── README.md # 英文使用文档
69
+ ├── README.zh.md # 中文使用文档
70
+ ├── SKILL.md # Claude Code 技能描述文件
71
+ ├── package.json
72
+ └── tsconfig.json
73
+ ```
74
+
75
+ 生成项目的运行时依赖:`axios`、`chalk`、`commander`、`eventsource-parser`、`jmespath`、`yaml`、`ora`。
76
+
77
+ ## 认证方式
78
+
79
+ 生成器自动检测 OAS 文档中的第一个安全方案,并生成对应的认证代码。运行 CLI 前设置相应的环境变量:
80
+
81
+ | 方案 | 环境变量 | 说明 |
82
+ |------|---------|------|
83
+ | Bearer | `<NAME>_TOKEN` | `Authorization: Bearer …` |
84
+ | API Key | `<NAME>_API_KEY` | Header 或 Query 参数,名称取自 OAS |
85
+ | HTTP Basic | `<NAME>_CREDENTIALS` | `用户名:密码` → Base64 编码 |
86
+ | OAuth2 Client Credentials | `<NAME>_CLIENT_ID`、`<NAME>_CLIENT_SECRET`、`<NAME>_SCOPES` | 启动时自动换取 Token,进程内缓存 |
87
+ | 动态 Token(`x-cli-token-url`) | 扩展字段中定义的自定义环境变量 | 以 JSON Body 请求 Token 端点 |
88
+
89
+ `<NAME>` 为 CLI 名称的大写下划线形式(如 `my-api` → `MY_API`)。
90
+
91
+ ## OpenAPI 扩展
92
+
93
+ 在 OAS 文档中添加以下厂商扩展,可控制 CLI 生成行为:
94
+
95
+ | 扩展字段 | 位置 | 效果 |
96
+ |----------|------|------|
97
+ | `x-cli-name: "my-name"` | operation | 覆盖自动生成的命令名 |
98
+ | `x-cli-aliases: ["a"]` | operation | 为命令添加 Commander 别名 |
99
+ | `x-cli-ignore: true` | operation | 从生成的 CLI 中排除该操作 |
100
+ | `x-cli-token-url: "https://…"` | securityScheme | 自定义动态认证 Token 端点 |
101
+
102
+ ## 全局选项
103
+
104
+ 所有生成的 CLI 的每条命令均支持以下全局选项:
105
+
106
+ | 选项 | 说明 | 默认值 |
107
+ |------|------|--------|
108
+ | `--endpoint <url>` | 覆盖 API 基础地址 | OAS 规范中的 server URL |
109
+ | `--format <fmt>` | 输出格式:`json`、`yaml`、`table` | `json` |
110
+ | `--verbose` | 打印每次请求的 HTTP 方法和 URL | `false` |
111
+ | `--query <expr>` | JMESPath 表达式,过滤响应结果 | — |
112
+ | `--all-pages` | 自动翻页(跟随 `Link: rel="next"` 响应头) | `false` |
113
+
114
+ ## 请求体
115
+
116
+ `POST`、`PUT`、`PATCH` 命令使用 `--data` 传入请求体:
117
+
118
+ ```bash
119
+ # 内联 JSON
120
+ my-api widgets create --data '{"name": "foo", "color": "blue"}'
121
+
122
+ # 从文件读取
123
+ my-api widgets create --data @./payload.json
124
+ ```
125
+
126
+ ## 输出格式
127
+
128
+ ```bash
129
+ # 默认:格式化 JSON
130
+ my-api repos list
131
+
132
+ # 表格视图(适合数组类型数据)
133
+ my-api repos list --format table
134
+
135
+ # YAML 格式
136
+ my-api repos list --format yaml
137
+
138
+ # JMESPath 过滤
139
+ my-api repos list --query '[].name'
140
+
141
+ # 配合 jq 过滤
142
+ my-api repos list | jq '.[].full_name'
143
+
144
+ # 保存到文件
145
+ my-api repos list > repos.json
146
+ ```
147
+
148
+ ## 流式输出(SSE)
149
+
150
+ 响应内容类型为 `text/event-stream` 的操作会被自动识别。生成的 CLI 将每个 SSE 数据载荷输出到 stdout,每行一条:
151
+
152
+ ```bash
153
+ my-api completions create --model gpt-4o --stream
154
+ ```
155
+
156
+ 生成的客户端使用 `eventsource-parser`,支持多行 `data:` 载荷、命名 `event:` 类型,并静默丢弃 OpenAI 兼容 API 常用的 `[DONE]` 哨兵。
157
+
158
+ ## 命令行选项
159
+
160
+ ```
161
+ openapi2cli [options]
162
+
163
+ 选项:
164
+ --oas <path|url> OpenAPI 3.x 规范的文件路径或 URL(必填)
165
+ --name <name> CLI 可执行文件名称(必填)
166
+ --output <dir> 输出目录(必填)
167
+ --overwrite 覆盖已存在的输出目录
168
+ -h, --help 显示帮助信息
169
+ ```
170
+
171
+ ## 许可证
172
+
173
+ MIT
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ require('../dist/index.js');
@@ -0,0 +1,4 @@
1
+ import { OpenAPIV3 } from 'openapi-types';
2
+ import { CommandStructure } from '../types/index';
3
+ export declare function analyzeSchema(api: OpenAPIV3.Document, cliName: string): CommandStructure;
4
+ //# sourceMappingURL=schema-analyzer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema-analyzer.d.ts","sourceRoot":"","sources":["../../src/analyzer/schema-analyzer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAI1C,OAAO,EAGL,gBAAgB,EAQjB,MAAM,gBAAgB,CAAC;AAkBxB,wBAAgB,aAAa,CAAC,GAAG,EAAE,SAAS,CAAC,QAAQ,EAAE,OAAO,EAAE,MAAM,GAAG,gBAAgB,CAkBxF"}