swgto-ts 3.0.2 → 3.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 +218 -196
- package/dist/{chunk-GTJIYNPK.js → chunk-4K4VW2PJ.js} +86 -68
- package/dist/{chunk-HBBM5HFP.js → chunk-EAGONRXV.js} +45 -1
- package/dist/cli.js +2 -2
- package/dist/{genApiDocsHtml-V7PDR4PW.js → genApiDocsHtml-ZM2XY5P2.js} +1 -1
- package/dist/{genApiDocsMd-WVDJY3ST.js → genApiDocsMd-UOK4S7BK.js} +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -2
- package/package.json +43 -42
package/README.md
CHANGED
|
@@ -1,196 +1,218 @@
|
|
|
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
|
-
|
|
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
|
-
|
|
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
|
+
flattenOnGet?: boolean
|
|
85
|
+
apiDocs?: ApiDocsConfig
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### 字段说明
|
|
90
|
+
|
|
91
|
+
| 字段 | 类型 | 默认值 | 说明 |
|
|
92
|
+
| -------------------- | ----------------------------------- | ------------ | -------------------------------------------------------------------------------------------------------------------------------------- |
|
|
93
|
+
| `docUrls` | `string \| string[]` | (必填) | OpenAPI 3.x 文档的 URL,支持单文档字符串或多文档数组 |
|
|
94
|
+
| `httpClientPath` | `string` | (必填) | 项目内 HTTP 请求工具的引入路径,生成的文件会从中 `import request` |
|
|
95
|
+
| `outputDir` | `string` | `'src/api'` | 代码输出目录,相对项目根目录 |
|
|
96
|
+
| `outputType` | `'ts' \| 'js'` | `'ts'` | 输出文件类型,`'ts'` 生成 `.ts` 文件,`'js'` 生成 `.js` 文件(带 JSDoc 类型注释) |
|
|
97
|
+
| `typeName` | `string` | `'types'` | 类型文件名(不含后缀),如 `'types'` 生成 `types.ts` 或 `types.js` |
|
|
98
|
+
| `fileNaming` | `'module' \| 'path'` | `'path'` | 文件组织方式:`'path'` 按接口路径每个文件一个函数;`'module'` 按控制器合并到同一个文件 |
|
|
99
|
+
| `moduleName` | `(docUrl) => string` | `'services'` | 多文档时,为每个文档指定模块目录名。多文档模式下**必填** |
|
|
100
|
+
| `renameMethod` | `(path, method) => string` | 见说明 | 自定义函数名生成规则。默认根据路径和方法名自动生成(如 `get_user_list`) |
|
|
101
|
+
| `resolveRequestPath` | `(path, method, docUrl) => string` | 原路径 | 自定义请求路径转换规则,可用于添加统一前缀等,如 `(path) => \`/proxy\${path}\`` |
|
|
102
|
+
| `ignoreUrl` | `(path, method, docUrl) => boolean` | 不过滤 | 过滤不需要生成代码的接口,返回 `true` 跳过当前接口 |
|
|
103
|
+
| `cleanOutput` | `boolean` | `false` | 生成前是否清空输出目录 |
|
|
104
|
+
| `flattenQueryParam` | `boolean` | `false` | 当 query 参数只有一个且为 `$ref` 引用类型时,直接用引用类型代替 `{ query: RefType }` |
|
|
105
|
+
| `mergeParams` | `boolean` | `false` | 合并参数层级:`true` 时展平 `path/query/body` 嵌套,直接使用 `params: { field1, field2 }` 代替 `params: { path: {...}, query: {...} }` |
|
|
106
|
+
| `flattenOnGet` | `boolean` | `false` | GET 请求专用展平:将所有 body/query/path 中的 `$ref` 参数展开为顶层交叉类型,彻底消除嵌套。详见 [GET 参数展平](#get-参数展平) |
|
|
107
|
+
| `apiDocs` | `ApiDocsConfig` | 见说明 | 自动生成 API 文档(HTML/Markdown),详细配置见 [API 文档生成](#api-文档生成) |
|
|
108
|
+
|
|
109
|
+
### 函数名生成规则
|
|
110
|
+
|
|
111
|
+
默认函数名格式为 `{method}_{path_segments}`,如:
|
|
112
|
+
|
|
113
|
+
- `GET /user/list` → `get_user_list`
|
|
114
|
+
- `POST /user/create` → `post_user_create`
|
|
115
|
+
|
|
116
|
+
可通过 `renameMethod` 自定义:
|
|
117
|
+
|
|
118
|
+
```ts
|
|
119
|
+
renameMethod: (path, method) => `${method}_${path.replace(/[\\/{}]/g, '_')}`,
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### 文件组织方式
|
|
123
|
+
|
|
124
|
+
**`fileNaming: 'path'`(默认)**— 每个接口生成独立文件:
|
|
125
|
+
|
|
126
|
+
```
|
|
127
|
+
src/api/moduleA/
|
|
128
|
+
get_user_list.ts
|
|
129
|
+
post_user_create.ts
|
|
130
|
+
get_user_detail.ts
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
**`fileNaming: 'module'`**— 按控制器合并文件(自动识别 RESTful 资源路径分组):
|
|
134
|
+
|
|
135
|
+
```
|
|
136
|
+
src/api/moduleA/
|
|
137
|
+
user.ts // 包含 get_user_list, post_user_create, get_user_detail
|
|
138
|
+
order.ts // 包含 get_order_list, post_order_create
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### 参数合并模式
|
|
142
|
+
|
|
143
|
+
`mergeParams: true` 时展平参数层级,适用于不想嵌套 `path/query/body` 的场景:
|
|
144
|
+
|
|
145
|
+
```ts
|
|
146
|
+
// mergeParams: false(默认),参数按来源嵌套
|
|
147
|
+
getUser({ path: { id: '1' }, query: { name: 'foo' } })
|
|
148
|
+
|
|
149
|
+
// mergeParams: true,参数展平
|
|
150
|
+
getUser({ id: '1', name: 'foo' })
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### GET 参数展平
|
|
154
|
+
|
|
155
|
+
`flattenOnGet: true` 专门解决 GET 请求中 body 和 query 参数嵌套的问题。当 GET 请求同时包含 body 参数(如 `userDTO`)和 query 参数(如 `pageQuery`)时,会将所有 `$ref` 类型的参数展开为顶层交叉类型,实现 `{...userDTO, ...pageQuery}` 的效果。
|
|
156
|
+
|
|
157
|
+
**背景**:部分后端定义 GET 请求时,参数在 body 和 query 中分散定义,但实际入参是合并平铺的。仅靠 `mergeParams` 会保留 body 内部的属性名作为嵌套字段。
|
|
158
|
+
|
|
159
|
+
**示例**:一个 GET 请求的 body 中有 `adminUserVO: AdminUserVO`,query 中有 `pageRequest: SessionPageRequestDTO`
|
|
160
|
+
|
|
161
|
+
```ts
|
|
162
|
+
// flattenOnGet: false(默认),body 属性名保留为嵌套字段
|
|
163
|
+
get_ai_sessions({ adminUserVO: {...}, pageRequest: {...} })
|
|
164
|
+
// 类型:{ adminUserVO: AdminUserVO; pageRequest: SessionPageRequestDTO; }
|
|
165
|
+
|
|
166
|
+
// flattenOnGet: true,$ref 参数全部展开到顶层
|
|
167
|
+
get_ai_sessions({ ...adminUserVOFields, ...pageRequestFields })
|
|
168
|
+
// 类型:AdminUserVO & SessionPageRequestDTO
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
> **注意**:`flattenOnGet` 仅对 GET 请求生效,且通常与 `mergeParams` 和 `flattenQueryParam` 一起使用效果最佳。该选项会同时影响生成的类型签名和函数体实现。
|
|
172
|
+
|
|
173
|
+
### API 文档生成
|
|
174
|
+
|
|
175
|
+
配置 `apiDocs` 可在生成代码的同时,自动输出一份人类可读的 API 文档:
|
|
176
|
+
|
|
177
|
+
```ts
|
|
178
|
+
export default {
|
|
179
|
+
// ... 其他配置
|
|
180
|
+
apiDocs: {
|
|
181
|
+
enable: true,
|
|
182
|
+
format: 'html', // 'html' 或 'markdown'
|
|
183
|
+
output: 'api-docs.html', // 输出文件名
|
|
184
|
+
title: '我的 API 文档', // 文档标题,默认取 OpenAPI info.title
|
|
185
|
+
companyName: 'XX 公司', // 封面公司名称(仅 HTML)
|
|
186
|
+
template: './my-template.html', // 自定义 HTML 模板路径(仅 HTML)
|
|
187
|
+
theme: './my-theme.css', // 自定义样式文件路径(仅 HTML)
|
|
188
|
+
},
|
|
189
|
+
}
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
| 配置项 | 类型 | 默认值 | 说明 |
|
|
193
|
+
| ------------- | --------------------------- | ----------------------- | -------------------------------------------------------------- |
|
|
194
|
+
| `enable` | `boolean` | `false` | 是否开启文档生成 |
|
|
195
|
+
| `format` | `'html' \| 'markdown'` | `'html'` | 输出格式:HTML 适合浏览器打印为 PDF,Markdown 适合仓库内查看 |
|
|
196
|
+
| `output` | `string` | `'api-docs.html'` | 输出文件名,相对 `outputDir` |
|
|
197
|
+
| `title` | `string` | OpenAPI `info.title` | 文档标题 |
|
|
198
|
+
| `companyName` | `string` | 无 | 封面上的公司名称(仅 HTML) |
|
|
199
|
+
| `template` | `string` | 内置模板 | 自定义 HTML 模板路径(仅 HTML)。优先级:`template` 配置 > 项目根目录 `.swagger.docs.html` > 内置默认模板 |
|
|
200
|
+
| `theme` | `string` | 无 | 自定义 CSS 文件路径,覆盖文档样式(仅 HTML) |
|
|
201
|
+
|
|
202
|
+
#### HTML 文档
|
|
203
|
+
|
|
204
|
+
输出为带书本风格的 HTML 页面,包含:
|
|
205
|
+
- **封面页**:A4 整页,显示公司名称(如配置)、API 名称和版本号
|
|
206
|
+
- **目录**:方法 + 路径 | 点线 | 摘要,点击跳转到对应接口
|
|
207
|
+
- **接口卡片**:包含路径参数、查询参数、请求体、响应体的字段表格(名称 / 类型 / 必填 / 描述)
|
|
208
|
+
|
|
209
|
+
可通过项目根目录的 `.swagger.docs.html` 文件自定义文档样式。如果设置了 `apiDocs.template`,则优先使用该路径的模板文件。
|
|
210
|
+
|
|
211
|
+
#### Markdown 文档
|
|
212
|
+
|
|
213
|
+
输出为 `.md` 文件,包含:
|
|
214
|
+
- 封面区域:公司名称(如配置)、API 名称和版本号
|
|
215
|
+
- 目录(Table of Contents):所有接口的锚点链接列表
|
|
216
|
+
- 每个接口的详细说明:HTTP 方法 + 路径、描述、参数表格和响应表格
|
|
217
|
+
|
|
218
|
+
适合直接在代码仓库中查看或用于 GitBook、VuePress 等文档工具。```
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import {
|
|
2
|
+
buildDefaultMethodName,
|
|
3
|
+
buildTypeName,
|
|
4
|
+
sanitizeIdentifier,
|
|
5
|
+
sanitizeSchemaTypeName,
|
|
2
6
|
schemaToTs,
|
|
3
7
|
toTypePropertyName
|
|
4
|
-
} from "./chunk-
|
|
8
|
+
} from "./chunk-EAGONRXV.js";
|
|
5
9
|
|
|
6
10
|
// src/generate.ts
|
|
7
11
|
import path4 from "path";
|
|
@@ -52,6 +56,7 @@ async function loadConfig(cwd) {
|
|
|
52
56
|
fileNaming: rawConfig.fileNaming ?? "path",
|
|
53
57
|
flattenQueryParam: rawConfig.flattenQueryParam ?? false,
|
|
54
58
|
mergeParams: rawConfig.mergeParams ?? false,
|
|
59
|
+
flattenOnGet: rawConfig.flattenOnGet ?? false,
|
|
55
60
|
apiDocs: {
|
|
56
61
|
enable: rawConfig.apiDocs?.enable ?? false,
|
|
57
62
|
output: rawConfig.apiDocs?.output ?? defaultOutput,
|
|
@@ -160,28 +165,6 @@ function groupByController(operations) {
|
|
|
160
165
|
}, {});
|
|
161
166
|
}
|
|
162
167
|
|
|
163
|
-
// src/utils/naming.ts
|
|
164
|
-
function toPascalCase(value) {
|
|
165
|
-
return value.split(/[^a-zA-Z0-9]+/).filter(Boolean).map((segment) => segment[0].toUpperCase() + segment.slice(1)).join("");
|
|
166
|
-
}
|
|
167
|
-
function sanitizePathSegment(value) {
|
|
168
|
-
return value.replace(/^\//, "").replace(/\{|\}/g, "").replace(/[^a-zA-Z0-9/_-]/g, "").replace(/\/+/g, "/");
|
|
169
|
-
}
|
|
170
|
-
function sanitizeIdentifier(value) {
|
|
171
|
-
const normalized = value.replace(/[^a-zA-Z0-9_$]+/g, "_").replace(/_+/g, "_").replace(/^_+|_+$/g, "");
|
|
172
|
-
if (!normalized) {
|
|
173
|
-
return "generated_api";
|
|
174
|
-
}
|
|
175
|
-
return /^[0-9]/.test(normalized) ? `api_${normalized}` : normalized;
|
|
176
|
-
}
|
|
177
|
-
function buildDefaultMethodName(apiPath, method) {
|
|
178
|
-
const cleaned = sanitizePathSegment(apiPath).replace(/\//g, "_").replace(/_+/g, "_");
|
|
179
|
-
return sanitizeIdentifier([method.toLowerCase(), cleaned || "root"].join("_"));
|
|
180
|
-
}
|
|
181
|
-
function buildTypeName(functionName, suffix) {
|
|
182
|
-
return `${toPascalCase(functionName)}${suffix}`;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
168
|
// src/core/parsePaths.ts
|
|
186
169
|
var HTTP_METHODS = ["get", "post", "put", "patch", "delete", "options", "head"];
|
|
187
170
|
function collectReferencedTypeNames(schema, collected = /* @__PURE__ */ new Set()) {
|
|
@@ -191,7 +174,7 @@ function collectReferencedTypeNames(schema, collected = /* @__PURE__ */ new Set(
|
|
|
191
174
|
if (schema.$ref) {
|
|
192
175
|
const typeName = schema.$ref.split("/").pop();
|
|
193
176
|
if (typeName) {
|
|
194
|
-
collected.add(typeName);
|
|
177
|
+
collected.add(sanitizeSchemaTypeName(typeName));
|
|
195
178
|
}
|
|
196
179
|
}
|
|
197
180
|
for (const child of schema.anyOf ?? []) {
|
|
@@ -259,28 +242,60 @@ function parsePaths(document, docUrl, config) {
|
|
|
259
242
|
const pathFields = pathParams.map((param) => `${param.name}: ${schemaToTs(param.schema)};`);
|
|
260
243
|
const queryFields = queryParams.map((param) => `${param.name}${param.required ? "" : "?"}: ${schemaToTs(param.schema)};`);
|
|
261
244
|
const requestTypeParts = [];
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
245
|
+
const isFlattenOnGet = config.flattenOnGet && method === "get";
|
|
246
|
+
if (isFlattenOnGet) {
|
|
247
|
+
const flatFields = [];
|
|
248
|
+
if (requestBodySchema) {
|
|
249
|
+
if (!requestBodySchema.$ref && requestBodySchema.properties) {
|
|
250
|
+
for (const [name, schema] of Object.entries(requestBodySchema.properties)) {
|
|
251
|
+
if (schema.$ref) {
|
|
252
|
+
requestTypeParts.push(schemaToTs(schema));
|
|
253
|
+
} else {
|
|
254
|
+
const required = requestBodySchema.required?.includes(name) ?? false;
|
|
255
|
+
flatFields.push(`${name}${required ? "" : "?"}: ${schemaToTs(schema)}`);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
} else {
|
|
259
|
+
requestTypeParts.push(bodyType);
|
|
260
|
+
}
|
|
270
261
|
}
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
if (config.mergeParams) {
|
|
275
|
-
requestTypeParts.push(schemaToTs(queryParams[0].schema));
|
|
262
|
+
for (const param of queryParams) {
|
|
263
|
+
if (param.schema?.$ref) {
|
|
264
|
+
requestTypeParts.push(schemaToTs(param.schema));
|
|
276
265
|
} else {
|
|
277
|
-
|
|
266
|
+
flatFields.push(`${param.name}${param.required ? "" : "?"}: ${schemaToTs(param.schema)}`);
|
|
278
267
|
}
|
|
279
|
-
}
|
|
268
|
+
}
|
|
269
|
+
for (const field of pathFields) {
|
|
270
|
+
flatFields.push(field);
|
|
271
|
+
}
|
|
272
|
+
if (flatFields.length) {
|
|
273
|
+
requestTypeParts.push(`{ ${flatFields.join(" ")} }`);
|
|
274
|
+
}
|
|
275
|
+
} else {
|
|
276
|
+
if (bodyType) {
|
|
277
|
+
requestTypeParts.push(bodyType);
|
|
278
|
+
}
|
|
279
|
+
if (pathFields.length) {
|
|
280
280
|
if (config.mergeParams) {
|
|
281
|
-
requestTypeParts.push(`{ ${
|
|
281
|
+
requestTypeParts.push(`{ ${pathFields.join(" ")} }`);
|
|
282
|
+
} else {
|
|
283
|
+
requestTypeParts.push(`{ path: { ${pathFields.join(" ")} } }`);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
if (queryFields.length) {
|
|
287
|
+
if (config.flattenQueryParam && queryParams.length === 1 && queryParams[0].schema?.$ref) {
|
|
288
|
+
if (config.mergeParams) {
|
|
289
|
+
requestTypeParts.push(schemaToTs(queryParams[0].schema));
|
|
290
|
+
} else {
|
|
291
|
+
requestTypeParts.push(`{ query: ${schemaToTs(queryParams[0].schema)} }`);
|
|
292
|
+
}
|
|
282
293
|
} else {
|
|
283
|
-
|
|
294
|
+
if (config.mergeParams) {
|
|
295
|
+
requestTypeParts.push(`{ ${queryFields.join(" ")} }`);
|
|
296
|
+
} else {
|
|
297
|
+
requestTypeParts.push(`{ query: { ${queryFields.join(" ")} } }`);
|
|
298
|
+
}
|
|
284
299
|
}
|
|
285
300
|
}
|
|
286
301
|
}
|
|
@@ -369,18 +384,19 @@ function buildFunctionDoc(operation, typeImportPath, requestParamType) {
|
|
|
369
384
|
}
|
|
370
385
|
return ["/**", ...lines.map((line) => ` * ${line}`), " */"].join("\n");
|
|
371
386
|
}
|
|
372
|
-
function renderFunction(operation, useGenericUrl, mergeParams) {
|
|
373
|
-
const
|
|
387
|
+
function renderFunction(operation, useGenericUrl, mergeParams, flattenOnGet = false) {
|
|
388
|
+
const effectiveMerge = mergeParams || flattenOnGet && operation.method === "get";
|
|
389
|
+
const queryLine = operation.queryParams.length ? ` params: ${effectiveMerge ? "params" : "params?.query"},
|
|
374
390
|
` : "";
|
|
375
391
|
const bodyOnly = Boolean(operation.requestBodySchema) && !operation.queryParams.length && !operation.pathParams.length;
|
|
376
392
|
const hasBodySchema = Boolean(operation.requestBodySchema);
|
|
377
|
-
const needsCleanData =
|
|
393
|
+
const needsCleanData = effectiveMerge && hasBodySchema && operation.pathParams.length > 0;
|
|
378
394
|
const destructureLine = needsCleanData ? ` const { ${operation.pathParams.map((p) => p.name).join(", ")}, ...requestData } = params;
|
|
379
395
|
` : "";
|
|
380
|
-
const dataValue = needsCleanData ? "requestData" :
|
|
396
|
+
const dataValue = needsCleanData ? "requestData" : effectiveMerge ? "params" : bodyOnly ? "params" : "params?.body";
|
|
381
397
|
const bodyLine = hasBodySchema ? ` data: ${dataValue},
|
|
382
398
|
` : "";
|
|
383
|
-
const pathArg =
|
|
399
|
+
const pathArg = effectiveMerge ? "params" : "params?.path";
|
|
384
400
|
const urlValue = useGenericUrl && operation.pathParams.length ? `buildUrl(${JSON.stringify(operation.requestPath)}, ${pathArg})` : operation.pathParams.length ? `buildUrl(${pathArg})` : JSON.stringify(operation.requestPath);
|
|
385
401
|
const pathLine = ` url: ${urlValue},
|
|
386
402
|
`;
|
|
@@ -391,19 +407,20 @@ ${queryLine}${bodyLine} ...config,
|
|
|
391
407
|
});
|
|
392
408
|
}`;
|
|
393
409
|
}
|
|
394
|
-
function generateJsRequestFile(operation, httpClientPath, typeImportPath, mergeParams = false) {
|
|
410
|
+
function generateJsRequestFile(operation, httpClientPath, typeImportPath, mergeParams = false, flattenOnGet = false) {
|
|
411
|
+
const effectiveMerge = mergeParams || flattenOnGet && operation.method === "get";
|
|
395
412
|
const requestParamType = operation.requestTypeExpression ?? "void";
|
|
396
|
-
const queryLine = operation.queryParams.length ? ` params: ${
|
|
413
|
+
const queryLine = operation.queryParams.length ? ` params: ${effectiveMerge ? "params" : "params?.query"},
|
|
397
414
|
` : "";
|
|
398
415
|
const bodyOnly = Boolean(operation.requestBodySchema) && !operation.queryParams.length && !operation.pathParams.length;
|
|
399
416
|
const hasBodySchema = Boolean(operation.requestBodySchema);
|
|
400
|
-
const needsCleanData =
|
|
417
|
+
const needsCleanData = effectiveMerge && hasBodySchema && operation.pathParams.length > 0;
|
|
401
418
|
const destructureLine = needsCleanData ? ` const { ${operation.pathParams.map((p) => p.name).join(", ")}, ...requestData } = params;
|
|
402
419
|
` : "";
|
|
403
|
-
const dataValue = needsCleanData ? "requestData" :
|
|
420
|
+
const dataValue = needsCleanData ? "requestData" : effectiveMerge ? "params" : bodyOnly ? "params" : "params?.body";
|
|
404
421
|
const bodyLine = hasBodySchema ? ` data: ${dataValue},
|
|
405
422
|
` : "";
|
|
406
|
-
const pathArg =
|
|
423
|
+
const pathArg = effectiveMerge ? "params" : "params?.path";
|
|
407
424
|
const pathLine = operation.pathParams.length ? ` url: buildUrl(${pathArg}),
|
|
408
425
|
` : ` url: ${JSON.stringify(operation.requestPath)},
|
|
409
426
|
`;
|
|
@@ -426,7 +443,7 @@ ${queryLine}${bodyLine} ...config,
|
|
|
426
443
|
}
|
|
427
444
|
`;
|
|
428
445
|
}
|
|
429
|
-
function generateJsModuleFile(operations, httpClientPath, typeImportPath, mergeParams = false) {
|
|
446
|
+
function generateJsModuleFile(operations, httpClientPath, typeImportPath, mergeParams = false, flattenOnGet = false) {
|
|
430
447
|
const needsBuildUrl = operations.some((op) => op.pathParams.length > 0);
|
|
431
448
|
const buildUrlHelper = needsBuildUrl ? `
|
|
432
449
|
function buildUrl(url, path) {
|
|
@@ -437,7 +454,7 @@ function buildUrl(url, path) {
|
|
|
437
454
|
const doc = buildFunctionDoc(op, typeImportPath, op.requestTypeExpression ?? "void");
|
|
438
455
|
return `${doc}
|
|
439
456
|
|
|
440
|
-
${renderFunction(op, true, mergeParams)}`;
|
|
457
|
+
${renderFunction(op, true, mergeParams, flattenOnGet)}`;
|
|
441
458
|
}).join("\n\n");
|
|
442
459
|
return `/* eslint-disable */
|
|
443
460
|
// Auto-generated by swgto.
|
|
@@ -461,20 +478,21 @@ function buildFunctionDoc2(operation) {
|
|
|
461
478
|
}
|
|
462
479
|
return ["/**", ...lines.map((line) => ` * ${line}`), " */", ""].join("\n");
|
|
463
480
|
}
|
|
464
|
-
function renderFunction2(operation, useGenericUrl, mergeParams) {
|
|
481
|
+
function renderFunction2(operation, useGenericUrl, mergeParams, flattenOnGet = false) {
|
|
482
|
+
const effectiveMerge = mergeParams || flattenOnGet && operation.method === "get";
|
|
465
483
|
const requestArg = operation.requestTypeExpression ? `params: ${operation.requestTypeExpression}, config?: RequestConfig` : "params?: void, config?: RequestConfig";
|
|
466
484
|
const defaultResponseType = operation.responseTypeName ?? "unknown";
|
|
467
485
|
const bodyOnly = Boolean(operation.requestBodySchema) && !operation.queryParams.length && !operation.pathParams.length;
|
|
468
486
|
const hasBodySchema = Boolean(operation.requestBodySchema);
|
|
469
|
-
const needsCleanData =
|
|
487
|
+
const needsCleanData = effectiveMerge && hasBodySchema && operation.pathParams.length > 0;
|
|
470
488
|
const destructureLine = needsCleanData ? ` const { ${operation.pathParams.map((p) => p.name).join(", ")}, ...requestData } = params;
|
|
471
489
|
` : "";
|
|
472
|
-
const dataValue = needsCleanData ? "requestData" :
|
|
490
|
+
const dataValue = needsCleanData ? "requestData" : effectiveMerge ? "params" : bodyOnly ? "params" : "params?.body";
|
|
473
491
|
const bodyLine = hasBodySchema ? ` data: ${dataValue},
|
|
474
492
|
` : "";
|
|
475
|
-
const queryLine = operation.queryParams.length ? ` params: ${
|
|
493
|
+
const queryLine = operation.queryParams.length ? ` params: ${effectiveMerge ? "params" : "params?.query"},
|
|
476
494
|
` : "";
|
|
477
|
-
const pathArg =
|
|
495
|
+
const pathArg = effectiveMerge ? "params" : "params?.path";
|
|
478
496
|
const urlValue = useGenericUrl && operation.pathParams.length ? `buildUrl(${JSON.stringify(operation.requestPath)}, ${pathArg})` : operation.pathParams.length ? `buildUrl(${pathArg})` : JSON.stringify(operation.requestPath);
|
|
479
497
|
const pathLine = ` url: ${urlValue},
|
|
480
498
|
`;
|
|
@@ -485,7 +503,7 @@ ${queryLine}${bodyLine} ...config,
|
|
|
485
503
|
});
|
|
486
504
|
}`;
|
|
487
505
|
}
|
|
488
|
-
function generateTsRequestFile(operation, httpClientPath, typeImportPath, mergeParams = false) {
|
|
506
|
+
function generateTsRequestFile(operation, httpClientPath, typeImportPath, mergeParams = false, flattenOnGet = false) {
|
|
489
507
|
const importTypes = [...operation.requestImportTypes, operation.responseTypeName, "RequestConfig"].filter(
|
|
490
508
|
(value, index, array) => Boolean(value) && array.indexOf(value) === index
|
|
491
509
|
);
|
|
@@ -501,10 +519,10 @@ function buildUrl(path?: Record<string, any>): string {
|
|
|
501
519
|
import request from ${JSON.stringify(httpClientPath)};
|
|
502
520
|
${importLine}
|
|
503
521
|
${buildUrlHelper}
|
|
504
|
-
${renderFunction2(operation, false, mergeParams)}
|
|
522
|
+
${renderFunction2(operation, false, mergeParams, flattenOnGet)}
|
|
505
523
|
`;
|
|
506
524
|
}
|
|
507
|
-
function generateTsModuleFile(operations, httpClientPath, typeImportPath, mergeParams = false) {
|
|
525
|
+
function generateTsModuleFile(operations, httpClientPath, typeImportPath, mergeParams = false, flattenOnGet = false) {
|
|
508
526
|
const importTypeSet = /* @__PURE__ */ new Set();
|
|
509
527
|
for (const op of operations) {
|
|
510
528
|
for (const t of op.requestImportTypes) importTypeSet.add(t);
|
|
@@ -519,7 +537,7 @@ function buildUrl(url: string, path?: Record<string, unknown>): string {
|
|
|
519
537
|
return url.replace(/\\{([^}]+)\\}/g, (_, key) => String(path?.[key] ?? ''));
|
|
520
538
|
}
|
|
521
539
|
` : "";
|
|
522
|
-
const functions = operations.map((op) => renderFunction2(op, true, mergeParams)).join("\n\n");
|
|
540
|
+
const functions = operations.map((op) => renderFunction2(op, true, mergeParams, flattenOnGet)).join("\n\n");
|
|
523
541
|
return `/* eslint-disable */
|
|
524
542
|
// Auto-generated by swgto.
|
|
525
543
|
import request from ${JSON.stringify(httpClientPath)};
|
|
@@ -592,7 +610,7 @@ function renderOperationTypes(operation) {
|
|
|
592
610
|
}
|
|
593
611
|
function renderComponentSchemas(document) {
|
|
594
612
|
return Object.entries(document.components?.schemas ?? {}).map(([name, schema]) => {
|
|
595
|
-
return renderComponentSchema(name, schema);
|
|
613
|
+
return renderComponentSchema(sanitizeSchemaTypeName(name), schema);
|
|
596
614
|
});
|
|
597
615
|
}
|
|
598
616
|
function toJSDocType(typeText) {
|
|
@@ -609,7 +627,7 @@ function generateTypesFile(documentMap, operations, config) {
|
|
|
609
627
|
const moduleName = config.moduleName?.(docUrl) ?? "services";
|
|
610
628
|
parts2.push(`// Types from ${moduleName}`);
|
|
611
629
|
for (const [name, schema] of Object.entries(document.components?.schemas ?? {})) {
|
|
612
|
-
parts2.push(`/** @typedef {${toJSDocType(schemaToTs(schema))}} ${name} */`);
|
|
630
|
+
parts2.push(`/** @typedef {${toJSDocType(schemaToTs(schema))}} ${sanitizeSchemaTypeName(name)} */`);
|
|
613
631
|
}
|
|
614
632
|
}
|
|
615
633
|
for (const operation of operations) {
|
|
@@ -743,7 +761,7 @@ async function generateFromConfig(cwd = process.cwd()) {
|
|
|
743
761
|
for (const [controllerName, controllerOperations] of Object.entries(controllerMap)) {
|
|
744
762
|
const relativeFile = path4.join(config.outputDir, moduleName, `${controllerName}.${config.outputType}`);
|
|
745
763
|
const absoluteFile = path4.join(cwd, relativeFile);
|
|
746
|
-
const content = config.outputType === "ts" ? generateTsModuleFile(controllerOperations, config.httpClientPath, getRootImportPath(config.typeName), config.mergeParams) : generateJsModuleFile(controllerOperations, config.httpClientPath, getRootImportPath(config.typeName), config.mergeParams);
|
|
764
|
+
const content = config.outputType === "ts" ? generateTsModuleFile(controllerOperations, config.httpClientPath, getRootImportPath(config.typeName), config.mergeParams, config.flattenOnGet) : generateJsModuleFile(controllerOperations, config.httpClientPath, getRootImportPath(config.typeName), config.mergeParams, config.flattenOnGet);
|
|
747
765
|
for (const op of controllerOperations) {
|
|
748
766
|
op.fileBaseName = controllerName;
|
|
749
767
|
}
|
|
@@ -754,7 +772,7 @@ async function generateFromConfig(cwd = process.cwd()) {
|
|
|
754
772
|
for (const operation of moduleOperations) {
|
|
755
773
|
const relativeFile = path4.join(config.outputDir, moduleName, `${operation.fileBaseName}.${config.outputType}`);
|
|
756
774
|
const absoluteFile = path4.join(cwd, relativeFile);
|
|
757
|
-
const content = config.outputType === "ts" ? generateTsRequestFile(operation, config.httpClientPath, getRootImportPath(config.typeName), config.mergeParams) : generateJsRequestFile(operation, config.httpClientPath, getRootImportPath(config.typeName), config.mergeParams);
|
|
775
|
+
const content = config.outputType === "ts" ? generateTsRequestFile(operation, config.httpClientPath, getRootImportPath(config.typeName), config.mergeParams, config.flattenOnGet) : generateJsRequestFile(operation, config.httpClientPath, getRootImportPath(config.typeName), config.mergeParams, config.flattenOnGet);
|
|
758
776
|
await writeTextFile(absoluteFile, content);
|
|
759
777
|
files.push(relativeFile);
|
|
760
778
|
}
|
|
@@ -774,10 +792,10 @@ async function generateFromConfig(cwd = process.cwd()) {
|
|
|
774
792
|
let content;
|
|
775
793
|
const docFile = path4.join(cwd, config.outputDir, config.apiDocs.output);
|
|
776
794
|
if (config.apiDocs.format === "markdown") {
|
|
777
|
-
const { generateApiDocsMd } = await import("./genApiDocsMd-
|
|
795
|
+
const { generateApiDocsMd } = await import("./genApiDocsMd-UOK4S7BK.js");
|
|
778
796
|
content = generateApiDocsMd(documentMap, operations, config);
|
|
779
797
|
} else {
|
|
780
|
-
const { generateApiDocsHtml, DEFAULT_TEMPLATE } = await import("./genApiDocsHtml-
|
|
798
|
+
const { generateApiDocsHtml, DEFAULT_TEMPLATE } = await import("./genApiDocsHtml-ZM2XY5P2.js");
|
|
781
799
|
const templateFile = path4.join(cwd, ".swagger.docs.html");
|
|
782
800
|
if (!existsSync2(templateFile)) {
|
|
783
801
|
await writeTextFile(templateFile, DEFAULT_TEMPLATE);
|
|
@@ -1,10 +1,50 @@
|
|
|
1
|
+
// src/utils/naming.ts
|
|
2
|
+
import { pinyin } from "pinyin-pro";
|
|
3
|
+
function toPascalCase(value) {
|
|
4
|
+
return value.split(/[^a-zA-Z0-9]+/).filter(Boolean).map((segment) => segment[0].toUpperCase() + segment.slice(1)).join("");
|
|
5
|
+
}
|
|
6
|
+
function sanitizePathSegment(value) {
|
|
7
|
+
return value.replace(/^\//, "").replace(/\{|\}/g, "").replace(/[^a-zA-Z0-9/_-]/g, "").replace(/\/+/g, "/");
|
|
8
|
+
}
|
|
9
|
+
function sanitizeIdentifier(value) {
|
|
10
|
+
const normalized = value.replace(/[^a-zA-Z0-9_$]+/g, "_").replace(/_+/g, "_").replace(/^_+|_+$/g, "");
|
|
11
|
+
if (!normalized) {
|
|
12
|
+
return "generated_api";
|
|
13
|
+
}
|
|
14
|
+
return /^[0-9]/.test(normalized) ? `api_${normalized}` : normalized;
|
|
15
|
+
}
|
|
16
|
+
function buildDefaultMethodName(apiPath, method) {
|
|
17
|
+
const cleaned = sanitizePathSegment(apiPath).replace(/\//g, "_").replace(/_+/g, "_");
|
|
18
|
+
return sanitizeIdentifier([method.toLowerCase(), cleaned || "root"].join("_"));
|
|
19
|
+
}
|
|
20
|
+
function buildTypeName(functionName, suffix) {
|
|
21
|
+
return `${toPascalCase(functionName)}${suffix}`;
|
|
22
|
+
}
|
|
23
|
+
function sanitizeSchemaTypeName(raw) {
|
|
24
|
+
let result = "";
|
|
25
|
+
for (const ch of raw) {
|
|
26
|
+
if ("\xAB\xBB\uFF08\uFF09()".includes(ch)) {
|
|
27
|
+
result += "_";
|
|
28
|
+
} else if (/[一-鿿]/.test(ch)) {
|
|
29
|
+
const py = pinyin(ch, { toneType: "none" });
|
|
30
|
+
result += py[0].toUpperCase() + py.slice(1);
|
|
31
|
+
} else {
|
|
32
|
+
result += ch;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
result = result.replace(/[^A-Za-z0-9_]/g, "");
|
|
36
|
+
result = result.replace(/_+/g, "_").replace(/^_+|_+$/g, "");
|
|
37
|
+
return result || "GeneratedType";
|
|
38
|
+
}
|
|
39
|
+
|
|
1
40
|
// src/generators/schemaToTs.ts
|
|
2
41
|
function formatPropertyName(name) {
|
|
3
42
|
return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name) ? name : JSON.stringify(name);
|
|
4
43
|
}
|
|
5
44
|
function refToTypeName(ref) {
|
|
6
45
|
const parts = ref.split("/");
|
|
7
|
-
|
|
46
|
+
const raw = parts[parts.length - 1] || "unknown";
|
|
47
|
+
return sanitizeSchemaTypeName(raw);
|
|
8
48
|
}
|
|
9
49
|
function schemaToTs(schema) {
|
|
10
50
|
if (!schema) {
|
|
@@ -59,6 +99,10 @@ function toTypePropertyName(name) {
|
|
|
59
99
|
}
|
|
60
100
|
|
|
61
101
|
export {
|
|
102
|
+
sanitizeIdentifier,
|
|
103
|
+
buildDefaultMethodName,
|
|
104
|
+
buildTypeName,
|
|
105
|
+
sanitizeSchemaTypeName,
|
|
62
106
|
schemaToTs,
|
|
63
107
|
toTypePropertyName
|
|
64
108
|
};
|
package/dist/cli.js
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,42 +1,43 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "swgto-ts",
|
|
3
|
-
"version": "3.0
|
|
4
|
-
"description": "Generate API request files from OpenAPI 3.x documents.",
|
|
5
|
-
"license": "MIT",
|
|
6
|
-
"type": "module",
|
|
7
|
-
"bin": {
|
|
8
|
-
"swgto": "./dist/cli.js"
|
|
9
|
-
},
|
|
10
|
-
"main": "./dist/index.js",
|
|
11
|
-
"module": "./dist/index.js",
|
|
12
|
-
"types": "./dist/index.d.ts",
|
|
13
|
-
"files": [
|
|
14
|
-
"dist"
|
|
15
|
-
],
|
|
16
|
-
"scripts": {
|
|
17
|
-
"build": "tsup src/index.ts src/cli.ts --format esm --dts --clean",
|
|
18
|
-
"dev": "tsup src/index.ts src/cli.ts --format esm --dts --watch",
|
|
19
|
-
"test": "tsx ./src/cli.ts"
|
|
20
|
-
},
|
|
21
|
-
"keywords": [
|
|
22
|
-
"openapi",
|
|
23
|
-
"swagger",
|
|
24
|
-
"typescript",
|
|
25
|
-
"generator",
|
|
26
|
-
"cli"
|
|
27
|
-
],
|
|
28
|
-
"engines": {
|
|
29
|
-
"node": ">=18"
|
|
30
|
-
},
|
|
31
|
-
"dependencies": {
|
|
32
|
-
"jiti": "^2.4.2",
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
|
|
42
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "swgto-ts",
|
|
3
|
+
"version": "3.2.0",
|
|
4
|
+
"description": "Generate API request files from OpenAPI 3.x documents.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"bin": {
|
|
8
|
+
"swgto": "./dist/cli.js"
|
|
9
|
+
},
|
|
10
|
+
"main": "./dist/index.js",
|
|
11
|
+
"module": "./dist/index.js",
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"files": [
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
16
|
+
"scripts": {
|
|
17
|
+
"build": "tsup src/index.ts src/cli.ts --format esm --dts --clean",
|
|
18
|
+
"dev": "tsup src/index.ts src/cli.ts --format esm --dts --watch",
|
|
19
|
+
"test": "tsx ./src/cli.ts"
|
|
20
|
+
},
|
|
21
|
+
"keywords": [
|
|
22
|
+
"openapi",
|
|
23
|
+
"swagger",
|
|
24
|
+
"typescript",
|
|
25
|
+
"generator",
|
|
26
|
+
"cli"
|
|
27
|
+
],
|
|
28
|
+
"engines": {
|
|
29
|
+
"node": ">=18"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"jiti": "^2.4.2",
|
|
33
|
+
"pinyin-pro": "^3.29.4",
|
|
34
|
+
"ts-node": "^10.9.2",
|
|
35
|
+
"tsx": "^4.21.0"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@types/node": "^24.6.0",
|
|
39
|
+
"tsup": "^8.5.0",
|
|
40
|
+
"typescript": "^5.9.3",
|
|
41
|
+
"vitest": "^3.2.4"
|
|
42
|
+
}
|
|
43
|
+
}
|