swgto-ts 2.0.0 → 3.0.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 +196 -147
- package/dist/chunk-HBBM5HFP.js +64 -0
- package/dist/{chunk-4OK6R2K5.js → chunk-OU5BJNJI.js} +138 -78
- package/dist/cli.js +23 -1
- package/dist/genApiDocsHtml-V7PDR4PW.js +382 -0
- package/dist/genApiDocsMd-WVDJY3ST.js +153 -0
- package/dist/index.d.ts +33 -10
- package/dist/index.js +2 -1
- package/package.json +4 -1
package/README.md
CHANGED
|
@@ -1,147 +1,196 @@
|
|
|
1
|
-
# swgto
|
|
2
|
-
|
|
3
|
-
`swgto` 是一个把 `OpenAPI 3.x` 文档转换成项目内请求函数的 Node.js 库和 CLI。
|
|
4
|
-
|
|
5
|
-
安装后会注册一个 `swgto` 命令。执行时它会在当前目录查找 `.swaggerts.config.ts` 或 `.swaggerts.config.js`,然后生成:
|
|
6
|
-
|
|
7
|
-
- 按路径前缀分组的请求文件
|
|
8
|
-
- 按文档模块输出请求文件。单文档默认输出到 `services/`,多文档时多个模块目录平级
|
|
9
|
-
- `outputDir/index.ts` 或 `outputDir/index.js`
|
|
10
|
-
- 类型文件 `outputDir/types.ts` 或带 JSDoc 的 `outputDir/types.js`
|
|
11
|
-
|
|
12
|
-
## 安装
|
|
13
|
-
|
|
14
|
-
```bash
|
|
15
|
-
npm install swgto-ts
|
|
16
|
-
```
|
|
17
|
-
|
|
18
|
-
## 配置
|
|
19
|
-
|
|
20
|
-
在项目根目录创建 `.swaggerts.config.ts`:
|
|
21
|
-
|
|
22
|
-
```ts
|
|
23
|
-
import type { SwaggerTsConfig } from 'swgto-ts'
|
|
24
|
-
|
|
25
|
-
const config: SwaggerTsConfig = {
|
|
26
|
-
docUrls: 'https://example.com/openapi.json',
|
|
27
|
-
httpClientPath: '@/utils/request',
|
|
28
|
-
outputDir: 'src/api',
|
|
29
|
-
outputType: 'ts',
|
|
30
|
-
typeName: 'types',
|
|
31
|
-
cleanOutput: true,
|
|
32
|
-
renameMethod: (apiPath, method) => `${method}_${apiPath.replace(/[\\/{}]/g, '_')}`,
|
|
33
|
-
resolveRequestPath: (apiPath, method) => `/proxy${apiPath}`,
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export default config
|
|
37
|
-
```
|
|
38
|
-
|
|
39
|
-
多文档模式下需要提供 `moduleName`:
|
|
40
|
-
|
|
41
|
-
```ts
|
|
42
|
-
export default {
|
|
43
|
-
docUrls: ['https://example.com/user-openapi.json', 'https://example.com/order-openapi.json'],
|
|
44
|
-
httpClientPath: '@/utils/request',
|
|
45
|
-
outputDir: 'src/api',
|
|
46
|
-
moduleName: (docUrl) => (docUrl.includes('user') ? 'user' : 'order'),
|
|
47
|
-
}
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
## 使用
|
|
51
|
-
|
|
52
|
-
```bash
|
|
53
|
-
swgto
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
## 输出示例
|
|
57
|
-
|
|
58
|
-
```text
|
|
59
|
-
src/api/
|
|
60
|
-
index.ts
|
|
61
|
-
types.ts
|
|
62
|
-
services/
|
|
63
|
-
get_user_list.ts
|
|
64
|
-
post_user_create.ts
|
|
65
|
-
```
|
|
66
|
-
|
|
67
|
-
## 配置项
|
|
68
|
-
|
|
69
|
-
```ts
|
|
70
|
-
export interface SwaggerTsConfig {
|
|
71
|
-
docUrls: string | string[]
|
|
72
|
-
httpClientPath: string
|
|
73
|
-
renameMethod?: (path: string, method: string) => string
|
|
74
|
-
resolveRequestPath?: (path: string, method: string, docUrl: string) => string
|
|
75
|
-
ignoreUrl?: (path: string, method: string, docUrl: string) => boolean
|
|
76
|
-
outputDir?: string
|
|
77
|
-
moduleName?: (docUrl: string) => string
|
|
78
|
-
outputType?: 'ts' | 'js'
|
|
79
|
-
typeName?: string
|
|
80
|
-
cleanOutput?: boolean
|
|
81
|
-
fileNaming?: 'module' | 'path'
|
|
82
|
-
flattenQueryParam?: boolean
|
|
83
|
-
mergeParams?: boolean
|
|
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
|
-
|
|
1
|
+
# swgto
|
|
2
|
+
|
|
3
|
+
`swgto` 是一个把 `OpenAPI 3.x` 文档转换成项目内请求函数的 Node.js 库和 CLI。
|
|
4
|
+
|
|
5
|
+
安装后会注册一个 `swgto` 命令。执行时它会在当前目录查找 `.swaggerts.config.ts` 或 `.swaggerts.config.js`,然后生成:
|
|
6
|
+
|
|
7
|
+
- 按路径前缀分组的请求文件
|
|
8
|
+
- 按文档模块输出请求文件。单文档默认输出到 `services/`,多文档时多个模块目录平级
|
|
9
|
+
- `outputDir/index.ts` 或 `outputDir/index.js`
|
|
10
|
+
- 类型文件 `outputDir/types.ts` 或带 JSDoc 的 `outputDir/types.js`
|
|
11
|
+
|
|
12
|
+
## 安装
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install swgto-ts
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## 配置
|
|
19
|
+
|
|
20
|
+
在项目根目录创建 `.swaggerts.config.ts`:
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
import type { SwaggerTsConfig } from 'swgto-ts'
|
|
24
|
+
|
|
25
|
+
const config: SwaggerTsConfig = {
|
|
26
|
+
docUrls: 'https://example.com/openapi.json',
|
|
27
|
+
httpClientPath: '@/utils/request',
|
|
28
|
+
outputDir: 'src/api',
|
|
29
|
+
outputType: 'ts',
|
|
30
|
+
typeName: 'types',
|
|
31
|
+
cleanOutput: true,
|
|
32
|
+
renameMethod: (apiPath, method) => `${method}_${apiPath.replace(/[\\/{}]/g, '_')}`,
|
|
33
|
+
resolveRequestPath: (apiPath, method) => `/proxy${apiPath}`,
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export default config
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
多文档模式下需要提供 `moduleName`:
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
export default {
|
|
43
|
+
docUrls: ['https://example.com/user-openapi.json', 'https://example.com/order-openapi.json'],
|
|
44
|
+
httpClientPath: '@/utils/request',
|
|
45
|
+
outputDir: 'src/api',
|
|
46
|
+
moduleName: (docUrl) => (docUrl.includes('user') ? 'user' : 'order'),
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## 使用
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
swgto
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## 输出示例
|
|
57
|
+
|
|
58
|
+
```text
|
|
59
|
+
src/api/
|
|
60
|
+
index.ts
|
|
61
|
+
types.ts
|
|
62
|
+
services/
|
|
63
|
+
get_user_list.ts
|
|
64
|
+
post_user_create.ts
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## 配置项
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
export interface SwaggerTsConfig {
|
|
71
|
+
docUrls: string | string[]
|
|
72
|
+
httpClientPath: string
|
|
73
|
+
renameMethod?: (path: string, method: string) => string
|
|
74
|
+
resolveRequestPath?: (path: string, method: string, docUrl: string) => string
|
|
75
|
+
ignoreUrl?: (path: string, method: string, docUrl: string) => boolean
|
|
76
|
+
outputDir?: string
|
|
77
|
+
moduleName?: (docUrl: string) => string
|
|
78
|
+
outputType?: 'ts' | 'js'
|
|
79
|
+
typeName?: string
|
|
80
|
+
cleanOutput?: boolean
|
|
81
|
+
fileNaming?: 'module' | 'path'
|
|
82
|
+
flattenQueryParam?: boolean
|
|
83
|
+
mergeParams?: boolean
|
|
84
|
+
apiDocs?: ApiDocsConfig
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### 字段说明
|
|
89
|
+
|
|
90
|
+
| 字段 | 类型 | 默认值 | 说明 |
|
|
91
|
+
| -------------------- | ----------------------------------- | ------------ | -------------------------------------------------------------------------------------------------------------------------------------- |
|
|
92
|
+
| `docUrls` | `string \| string[]` | (必填) | OpenAPI 3.x 文档的 URL,支持单文档字符串或多文档数组 |
|
|
93
|
+
| `httpClientPath` | `string` | (必填) | 项目内 HTTP 请求工具的引入路径,生成的文件会从中 `import request` |
|
|
94
|
+
| `outputDir` | `string` | `'src/api'` | 代码输出目录,相对项目根目录 |
|
|
95
|
+
| `outputType` | `'ts' \| 'js'` | `'ts'` | 输出文件类型,`'ts'` 生成 `.ts` 文件,`'js'` 生成 `.js` 文件(带 JSDoc 类型注释) |
|
|
96
|
+
| `typeName` | `string` | `'types'` | 类型文件名(不含后缀),如 `'types'` 生成 `types.ts` 或 `types.js` |
|
|
97
|
+
| `fileNaming` | `'module' \| 'path'` | `'path'` | 文件组织方式:`'path'` 按接口路径每个文件一个函数;`'module'` 按控制器合并到同一个文件 |
|
|
98
|
+
| `moduleName` | `(docUrl) => string` | `'services'` | 多文档时,为每个文档指定模块目录名。多文档模式下**必填** |
|
|
99
|
+
| `renameMethod` | `(path, method) => string` | 见说明 | 自定义函数名生成规则。默认根据路径和方法名自动生成(如 `get_user_list`) |
|
|
100
|
+
| `resolveRequestPath` | `(path, method, docUrl) => string` | 原路径 | 自定义请求路径转换规则,可用于添加统一前缀等,如 `(path) => \`/proxy\${path}\`` |
|
|
101
|
+
| `ignoreUrl` | `(path, method, docUrl) => boolean` | 不过滤 | 过滤不需要生成代码的接口,返回 `true` 跳过当前接口 |
|
|
102
|
+
| `cleanOutput` | `boolean` | `false` | 生成前是否清空输出目录 |
|
|
103
|
+
| `flattenQueryParam` | `boolean` | `false` | 当 query 参数只有一个且为 `$ref` 引用类型时,直接用引用类型代替 `{ query: RefType }` |
|
|
104
|
+
| `mergeParams` | `boolean` | `false` | 合并参数层级:`true` 时展平 `path/query/body` 嵌套,直接使用 `params: { field1, field2 }` 代替 `params: { path: {...}, query: {...} }` |
|
|
105
|
+
| `apiDocs` | `ApiDocsConfig` | 见说明 | 自动生成 API 文档(HTML/Markdown),详细配置见 [API 文档生成](#api-文档生成) |
|
|
106
|
+
|
|
107
|
+
### 函数名生成规则
|
|
108
|
+
|
|
109
|
+
默认函数名格式为 `{method}_{path_segments}`,如:
|
|
110
|
+
|
|
111
|
+
- `GET /user/list` → `get_user_list`
|
|
112
|
+
- `POST /user/create` → `post_user_create`
|
|
113
|
+
|
|
114
|
+
可通过 `renameMethod` 自定义:
|
|
115
|
+
|
|
116
|
+
```ts
|
|
117
|
+
renameMethod: (path, method) => `${method}_${path.replace(/[\\/{}]/g, '_')}`,
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### 文件组织方式
|
|
121
|
+
|
|
122
|
+
**`fileNaming: 'path'`(默认)**— 每个接口生成独立文件:
|
|
123
|
+
|
|
124
|
+
```
|
|
125
|
+
src/api/moduleA/
|
|
126
|
+
get_user_list.ts
|
|
127
|
+
post_user_create.ts
|
|
128
|
+
get_user_detail.ts
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
**`fileNaming: 'module'`**— 按控制器合并文件(自动识别 RESTful 资源路径分组):
|
|
132
|
+
|
|
133
|
+
```
|
|
134
|
+
src/api/moduleA/
|
|
135
|
+
user.ts // 包含 get_user_list, post_user_create, get_user_detail
|
|
136
|
+
order.ts // 包含 get_order_list, post_order_create
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
### 参数合并模式
|
|
140
|
+
|
|
141
|
+
`mergeParams: true` 时展平参数层级,适用于不想嵌套 `path/query/body` 的场景:
|
|
142
|
+
|
|
143
|
+
```ts
|
|
144
|
+
// mergeParams: false(默认),参数按来源嵌套
|
|
145
|
+
getUser({ path: { id: '1' }, query: { name: 'foo' } })
|
|
146
|
+
|
|
147
|
+
// mergeParams: true,参数展平
|
|
148
|
+
getUser({ id: '1', name: 'foo' })
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
### API 文档生成
|
|
152
|
+
|
|
153
|
+
配置 `apiDocs` 可在生成代码的同时,自动输出一份人类可读的 API 文档:
|
|
154
|
+
|
|
155
|
+
```ts
|
|
156
|
+
export default {
|
|
157
|
+
// ... 其他配置
|
|
158
|
+
apiDocs: {
|
|
159
|
+
enable: true,
|
|
160
|
+
format: 'html', // 'html' 或 'markdown'
|
|
161
|
+
output: 'api-docs.html', // 输出文件名
|
|
162
|
+
title: '我的 API 文档', // 文档标题,默认取 OpenAPI info.title
|
|
163
|
+
companyName: 'XX 公司', // 封面公司名称(仅 HTML)
|
|
164
|
+
template: './my-template.html', // 自定义 HTML 模板路径(仅 HTML)
|
|
165
|
+
theme: './my-theme.css', // 自定义样式文件路径(仅 HTML)
|
|
166
|
+
},
|
|
167
|
+
}
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
| 配置项 | 类型 | 默认值 | 说明 |
|
|
171
|
+
| ------------- | --------------------------- | ----------------------- | -------------------------------------------------------------- |
|
|
172
|
+
| `enable` | `boolean` | `false` | 是否开启文档生成 |
|
|
173
|
+
| `format` | `'html' \| 'markdown'` | `'html'` | 输出格式:HTML 适合浏览器打印为 PDF,Markdown 适合仓库内查看 |
|
|
174
|
+
| `output` | `string` | `'api-docs.html'` | 输出文件名,相对 `outputDir` |
|
|
175
|
+
| `title` | `string` | OpenAPI `info.title` | 文档标题 |
|
|
176
|
+
| `companyName` | `string` | 无 | 封面上的公司名称(仅 HTML) |
|
|
177
|
+
| `template` | `string` | 内置模板 | 自定义 HTML 模板路径(仅 HTML)。优先级:`template` 配置 > 项目根目录 `.swagger.docs.html` > 内置默认模板 |
|
|
178
|
+
| `theme` | `string` | 无 | 自定义 CSS 文件路径,覆盖文档样式(仅 HTML) |
|
|
179
|
+
|
|
180
|
+
#### HTML 文档
|
|
181
|
+
|
|
182
|
+
输出为带书本风格的 HTML 页面,包含:
|
|
183
|
+
- **封面页**:A4 整页,显示公司名称(如配置)、API 名称和版本号
|
|
184
|
+
- **目录**:方法 + 路径 | 点线 | 摘要,点击跳转到对应接口
|
|
185
|
+
- **接口卡片**:包含路径参数、查询参数、请求体、响应体的字段表格(名称 / 类型 / 必填 / 描述)
|
|
186
|
+
|
|
187
|
+
可通过项目根目录的 `.swagger.docs.html` 文件自定义文档样式。如果设置了 `apiDocs.template`,则优先使用该路径的模板文件。
|
|
188
|
+
|
|
189
|
+
#### Markdown 文档
|
|
190
|
+
|
|
191
|
+
输出为 `.md` 文件,包含:
|
|
192
|
+
- 封面区域:公司名称(如配置)、API 名称和版本号
|
|
193
|
+
- 目录(Table of Contents):所有接口的锚点链接列表
|
|
194
|
+
- 每个接口的详细说明:HTTP 方法 + 路径、描述、参数表格和响应表格
|
|
195
|
+
|
|
196
|
+
适合直接在代码仓库中查看或用于 GitBook、VuePress 等文档工具。```
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// src/generators/schemaToTs.ts
|
|
2
|
+
function formatPropertyName(name) {
|
|
3
|
+
return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name) ? name : JSON.stringify(name);
|
|
4
|
+
}
|
|
5
|
+
function refToTypeName(ref) {
|
|
6
|
+
const parts = ref.split("/");
|
|
7
|
+
return parts[parts.length - 1] || "unknown";
|
|
8
|
+
}
|
|
9
|
+
function schemaToTs(schema) {
|
|
10
|
+
if (!schema) {
|
|
11
|
+
return "unknown";
|
|
12
|
+
}
|
|
13
|
+
if (schema.$ref) {
|
|
14
|
+
return refToTypeName(schema.$ref);
|
|
15
|
+
}
|
|
16
|
+
if (schema.enum?.length) {
|
|
17
|
+
return schema.enum.map((item) => JSON.stringify(item)).join(" | ");
|
|
18
|
+
}
|
|
19
|
+
if (schema.anyOf?.length) {
|
|
20
|
+
return schema.anyOf.map((item) => schemaToTs(item)).join(" | ");
|
|
21
|
+
}
|
|
22
|
+
if (schema.oneOf?.length) {
|
|
23
|
+
return schema.oneOf.map((item) => schemaToTs(item)).join(" | ");
|
|
24
|
+
}
|
|
25
|
+
if (schema.allOf?.length) {
|
|
26
|
+
return schema.allOf.map((item) => schemaToTs(item)).join(" & ");
|
|
27
|
+
}
|
|
28
|
+
if (schema.type === "array") {
|
|
29
|
+
return `${schemaToTs(schema.items)}[]`;
|
|
30
|
+
}
|
|
31
|
+
if (schema.type === "object" || schema.properties) {
|
|
32
|
+
const requiredSet = new Set(schema.required ?? []);
|
|
33
|
+
const properties = Object.entries(schema.properties ?? {}).map(([key, value]) => {
|
|
34
|
+
const optional = requiredSet.has(key) ? "" : "?";
|
|
35
|
+
return `${formatPropertyName(key)}${optional}: ${schemaToTs(value)};`;
|
|
36
|
+
});
|
|
37
|
+
if (!properties.length && schema.additionalProperties) {
|
|
38
|
+
const valueType = schema.additionalProperties === true ? "unknown" : schemaToTs(schema.additionalProperties);
|
|
39
|
+
return `{ [key: string]: ${valueType} }`;
|
|
40
|
+
}
|
|
41
|
+
return `{ ${properties.join(" ")} }`;
|
|
42
|
+
}
|
|
43
|
+
switch (schema.type) {
|
|
44
|
+
case "integer":
|
|
45
|
+
case "number":
|
|
46
|
+
return "number";
|
|
47
|
+
case "boolean":
|
|
48
|
+
return "boolean";
|
|
49
|
+
case "string":
|
|
50
|
+
return "string";
|
|
51
|
+
case "null":
|
|
52
|
+
return "null";
|
|
53
|
+
default:
|
|
54
|
+
return "unknown";
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
function toTypePropertyName(name) {
|
|
58
|
+
return formatPropertyName(name);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export {
|
|
62
|
+
schemaToTs,
|
|
63
|
+
toTypePropertyName
|
|
64
|
+
};
|