@karry.sun/yapi-gen 0.1.0 → 0.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 +28 -21
- package/dist/resolve-config.d.ts.map +1 -1
- package/dist/resolve-config.js +21 -8
- package/dist/run.d.ts.map +1 -1
- package/dist/run.js +43 -8
- package/package.json +1 -9
package/README.md
CHANGED
|
@@ -5,36 +5,48 @@
|
|
|
5
5
|
## 安装
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
|
-
pnpm add -D yapi-gen
|
|
8
|
+
pnpm add -D @karry.sun/yapi-gen
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
+
本包已内置 yapi-to-typescript,无需单独安装。
|
|
12
|
+
|
|
11
13
|
## 快速开始
|
|
12
14
|
|
|
13
|
-
|
|
15
|
+
1. **安装依赖**(在要生成接口的项目根目录)
|
|
16
|
+
```bash
|
|
17
|
+
pnpm add -D @karry.sun/yapi-gen
|
|
18
|
+
```
|
|
14
19
|
|
|
15
|
-
|
|
20
|
+
2. **生成配置文件**(若还没有 `yapi-gen.config.ts`)
|
|
21
|
+
```bash
|
|
22
|
+
pnpm yapi-gen init
|
|
23
|
+
```
|
|
24
|
+
会生成 `yapi-gen.config.ts`,用编辑器打开,按需修改:
|
|
25
|
+
- `serverUrl`:YApi 服务器地址
|
|
26
|
+
- `projects[0].token`:YApi 项目 token(建议用 `process.env.YAPI_TOKEN`)
|
|
27
|
+
- `requestAdapter`:`'axios'`(Vue/defHttp)或 `'nuxt-bbm'`(Nuxt),或 `'custom'` 并填写 `requestFunctionFilePath`
|
|
28
|
+
- `outputFilePath`:生成文件的目录规则(默认按分类生成到 `src/yapi/cat_xxx/index.ts`)
|
|
16
29
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
30
|
+
3. **生成接口代码**
|
|
31
|
+
```bash
|
|
32
|
+
pnpm yapi-gen
|
|
33
|
+
```
|
|
34
|
+
或在 `package.json` 的 `scripts` 里加 `"gen:api": "yapi-gen"`,然后执行:
|
|
35
|
+
```bash
|
|
36
|
+
pnpm gen:api
|
|
37
|
+
```
|
|
20
38
|
|
|
21
|
-
|
|
39
|
+
4. **在业务代码中引用**
|
|
40
|
+
生成的文件在 `outputFilePath` 配置的路径下,按分类或单文件导出请求函数与类型,直接从对应路径 import 使用即可。
|
|
22
41
|
|
|
23
|
-
|
|
42
|
+
|
|
43
|
+
5. 选择请求适配方式
|
|
24
44
|
|
|
25
45
|
- **内置预设(推荐)**:在配置中设置 `requestAdapter: 'axios'` 或 `requestAdapter: 'nuxt-bbm'`,无需在各仓库维护 `request.ts`,运行 `yapi-gen` 时会自动生成并写入 `src/yapi/request.ts`(路径可配)。
|
|
26
46
|
- **axios**:适用于封装了 axios 的项目(如 defHttp)。可选 `requestAdapter: { type: 'axios', axiosInstancePath: '/@/utils/http/axios' }` 指定实例路径,默认 `'/@/utils/http/axios'`(defHttp 常见路径)。
|
|
27
47
|
- **nuxt-bbm**:适用于 Nuxt 项目,使用 Layer 的 `#imports` 或 `@bitunix/bbm` 的 request。可选 `requestAdapter: { type: 'nuxt-bbm', requestImportPath: '#imports' }`,默认 `'#imports'`。
|
|
28
48
|
- **自定义**:设置 `requestAdapter: 'custom'` 并配置 `requestFunctionFilePath`,在对应文件中自行实现适配逻辑;或参考包内 `templates/request-*.ts` 复制后修改。
|
|
29
49
|
|
|
30
|
-
### 3. 生成接口代码
|
|
31
|
-
|
|
32
|
-
```bash
|
|
33
|
-
pnpm yapi-gen
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
会在当前目录查找 `yapi-gen.config.ts`(或 `.yapi-genrc.ts` 等),生成临时 ytt 配置并执行 `ytt`,输出到你在 `outputFilePath` 中配置的目录。
|
|
37
|
-
|
|
38
50
|
## 配置说明
|
|
39
51
|
|
|
40
52
|
`yapi-gen.config.ts` 使用 `defineYapiGenConfig` 导出单条配置(或数组,多条会合并为 `defineConfig([...])`)。主要字段与 [yapi-to-typescript 配置](https://fjc0k.github.io/yapi-to-typescript/handbook/config.html) 对齐:
|
|
@@ -57,11 +69,6 @@ pnpm yapi-gen
|
|
|
57
69
|
|
|
58
70
|
YApi 的 **token** 建议通过环境变量注入,例如在 `projects[].token` 中写 `process.env.YAPI_TOKEN || 'fallback'`。
|
|
59
71
|
|
|
60
|
-
## 在现有项目中使用
|
|
61
|
-
|
|
62
|
-
- **web-finance-admin(defHttp)**:在 `yapi-gen.config.ts` 中设置 `requestAdapter: 'axios'`(或 `{ type: 'axios', axiosInstancePath: '/@/utils/http/axios' }`),无需再维护 `request.ts`;或继续使用 `requestAdapter: 'custom'` 并保留现有 `requestFunctionFilePath`。
|
|
63
|
-
- **pc(Nuxt / @bitunix/bbm)**:设置 `requestAdapter: 'nuxt-bbm'`(或 `{ type: 'nuxt-bbm', requestImportPath: '#imports' }`),无需再维护 `request.ts`;或使用 `custom` 并指向现有文件。
|
|
64
|
-
|
|
65
72
|
## 配置文件查找顺序
|
|
66
73
|
|
|
67
74
|
CLI 会按以下顺序查找配置(找到即用):
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resolve-config.d.ts","sourceRoot":"","sources":["../src/resolve-config.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"resolve-config.d.ts","sourceRoot":"","sources":["../src/resolve-config.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,iBAAiB,EAAyB,MAAM,iBAAiB,CAAC;AA4FhF;;;GAGG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,iBAAiB,EAAE,GAAG,EAAE,MAAM,GAAG,iBAAiB,CA2CvF"}
|
package/dist/resolve-config.js
CHANGED
|
@@ -1,8 +1,20 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* 在运行 ytt 前解析配置:处理 requestAdapter 预设,生成并写入 request 文件,得到 ytt 可用的配置
|
|
3
3
|
*/
|
|
4
|
-
import { existsSync, mkdirSync, writeFileSync } from 'fs';
|
|
5
|
-
import { resolve, dirname } from 'path';
|
|
4
|
+
import { existsSync, mkdirSync, writeFileSync, readFileSync } from 'fs';
|
|
5
|
+
import { resolve, dirname, join } from 'path';
|
|
6
|
+
import { fileURLToPath } from 'url';
|
|
7
|
+
function getPackageName() {
|
|
8
|
+
try {
|
|
9
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
10
|
+
const pkgPath = join(__dirname, '..', 'package.json');
|
|
11
|
+
const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'));
|
|
12
|
+
return (pkg && pkg.name) || 'yapi-gen';
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
return 'yapi-gen';
|
|
16
|
+
}
|
|
17
|
+
}
|
|
6
18
|
const DEFAULT_REQUEST_FILE_PATH = 'src/yapi/request.ts';
|
|
7
19
|
const DEFAULT_AXIOS_INSTANCE_PATH = "/@/utils/http/axios";
|
|
8
20
|
const DEFAULT_NUXT_REQUEST_IMPORT_PATH = '#imports';
|
|
@@ -46,7 +58,7 @@ function normalizeRequestAdapter(adapter) {
|
|
|
46
58
|
}
|
|
47
59
|
return { type: 'custom' };
|
|
48
60
|
}
|
|
49
|
-
function getAxiosAdapterFileContent(axiosInstancePath, exportName) {
|
|
61
|
+
function getAxiosAdapterFileContent(packageName, axiosInstancePath, exportName) {
|
|
50
62
|
const importLine = exportName === 'default'
|
|
51
63
|
? `import defHttp from '${axiosInstancePath}';`
|
|
52
64
|
: `import { defHttp } from '${axiosInstancePath}';`;
|
|
@@ -54,18 +66,18 @@ function getAxiosAdapterFileContent(axiosInstancePath, exportName) {
|
|
|
54
66
|
* 由 yapi-gen 预设 "axios" 自动生成,请勿直接修改。
|
|
55
67
|
* 如需自定义请求逻辑,请将配置改为 requestAdapter: 'custom' 并维护 requestFunctionFilePath 指向的文件。
|
|
56
68
|
*/
|
|
57
|
-
import { createAxiosAdapter } from '
|
|
69
|
+
import { createAxiosAdapter } from '${packageName}/adapters/axios';
|
|
58
70
|
${importLine}
|
|
59
71
|
|
|
60
72
|
export default createAxiosAdapter(defHttp);
|
|
61
73
|
`;
|
|
62
74
|
}
|
|
63
|
-
function getNuxtBbmAdapterFileContent(requestImportPath) {
|
|
75
|
+
function getNuxtBbmAdapterFileContent(packageName, requestImportPath) {
|
|
64
76
|
return `/**
|
|
65
77
|
* 由 yapi-gen 预设 "nuxt-bbm" 自动生成,请勿直接修改。
|
|
66
78
|
* 如需自定义请求逻辑,请将配置改为 requestAdapter: 'custom' 并维护 requestFunctionFilePath 指向的文件。
|
|
67
79
|
*/
|
|
68
|
-
import { createNuxtAdapter } from '
|
|
80
|
+
import { createNuxtAdapter } from '${packageName}/adapters/nuxt-bbm';
|
|
69
81
|
import { request } from '${requestImportPath}';
|
|
70
82
|
|
|
71
83
|
export default createNuxtAdapter(request);
|
|
@@ -79,8 +91,9 @@ export function resolveConfig(config, cwd) {
|
|
|
79
91
|
const resolved = { ...config };
|
|
80
92
|
const adapter = normalizeRequestAdapter(resolved.requestAdapter);
|
|
81
93
|
const requestFilePath = resolved.requestFunctionFilePath ?? adapter.requestFunctionFilePath ?? DEFAULT_REQUEST_FILE_PATH;
|
|
94
|
+
const packageName = getPackageName();
|
|
82
95
|
if (adapter.type === 'axios') {
|
|
83
|
-
const content = getAxiosAdapterFileContent(adapter.axiosInstancePath, adapter.axiosExportName ?? 'defHttp');
|
|
96
|
+
const content = getAxiosAdapterFileContent(packageName, adapter.axiosInstancePath, adapter.axiosExportName ?? 'defHttp');
|
|
84
97
|
const absolutePath = resolve(cwd, requestFilePath);
|
|
85
98
|
const dir = dirname(absolutePath);
|
|
86
99
|
if (!existsSync(dir)) {
|
|
@@ -92,7 +105,7 @@ export function resolveConfig(config, cwd) {
|
|
|
92
105
|
return resolved;
|
|
93
106
|
}
|
|
94
107
|
if (adapter.type === 'nuxt-bbm') {
|
|
95
|
-
const content = getNuxtBbmAdapterFileContent(adapter.requestImportPath);
|
|
108
|
+
const content = getNuxtBbmAdapterFileContent(packageName, adapter.requestImportPath);
|
|
96
109
|
const absolutePath = resolve(cwd, requestFilePath);
|
|
97
110
|
const dir = dirname(absolutePath);
|
|
98
111
|
if (!existsSync(dir)) {
|
package/dist/run.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"run.d.ts","sourceRoot":"","sources":["../src/run.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"run.d.ts","sourceRoot":"","sources":["../src/run.ts"],"names":[],"mappings":"AAmIA,wBAAgB,GAAG,CAAC,OAAO,GAAE;IAAE,GAAG,CAAC,EAAE,MAAM,CAAC;IAAC,UAAU,CAAC,EAAE,MAAM,CAAC;IAAC,IAAI,CAAC,EAAE,OAAO,CAAA;CAAO,GAAG,MAAM,CAmC/F"}
|
package/dist/run.js
CHANGED
|
@@ -1,11 +1,25 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* CLI 执行逻辑:解析用户配置、生成 ytt 包装配置并执行 ytt
|
|
3
|
+
* 从本包依赖的 yapi-to-typescript 调用 ytt,无需使用方单独安装
|
|
3
4
|
*/
|
|
4
5
|
import { spawnSync } from 'child_process';
|
|
5
6
|
import { existsSync, mkdirSync, writeFileSync } from 'fs';
|
|
6
|
-
import { resolve, dirname, basename, relative } from 'path';
|
|
7
|
+
import { resolve, dirname, basename, relative, join } from 'path';
|
|
7
8
|
import { fileURLToPath } from 'url';
|
|
9
|
+
import { createRequire } from 'module';
|
|
10
|
+
import { readFileSync } from 'fs';
|
|
8
11
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
12
|
+
const require = createRequire(import.meta.url);
|
|
13
|
+
function getPackageName() {
|
|
14
|
+
try {
|
|
15
|
+
const pkgPath = join(__dirname, '..', 'package.json');
|
|
16
|
+
const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'));
|
|
17
|
+
return (pkg && pkg.name) || 'yapi-gen';
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
return 'yapi-gen';
|
|
21
|
+
}
|
|
22
|
+
}
|
|
9
23
|
const CONFIG_NAMES = [
|
|
10
24
|
'yapi-gen.config.ts',
|
|
11
25
|
'yapi-gen.config.mts',
|
|
@@ -33,6 +47,7 @@ function findConfig(cwd) {
|
|
|
33
47
|
* 该文件会 import 用户配置并传入 defineConfig
|
|
34
48
|
*/
|
|
35
49
|
function getWrapperContent(cwd, configPath) {
|
|
50
|
+
const packageName = getPackageName();
|
|
36
51
|
const absoluteConfig = resolve(cwd, configPath);
|
|
37
52
|
const configDir = dirname(absoluteConfig);
|
|
38
53
|
const configBase = basename(absoluteConfig).replace(/\.[^.]+$/, '') || 'yapi-gen.config';
|
|
@@ -41,7 +56,7 @@ function getWrapperContent(cwd, configPath) {
|
|
|
41
56
|
const relativeImport = (rel ? rel + '/' : '') + configBase;
|
|
42
57
|
return `/* 由 yapi-gen 自动生成,请勿修改 */
|
|
43
58
|
import { defineConfig } from 'yapi-to-typescript';
|
|
44
|
-
import { resolveConfig } from '
|
|
59
|
+
import { resolveConfig } from '${packageName}';
|
|
45
60
|
|
|
46
61
|
const userConfig = await import('${relativeImport}');
|
|
47
62
|
const raw = userConfig.default ?? userConfig;
|
|
@@ -57,19 +72,38 @@ function runInit(cwd) {
|
|
|
57
72
|
console.log('[yapi-gen] 已存在 yapi-gen.config.ts,跳过创建');
|
|
58
73
|
}
|
|
59
74
|
else {
|
|
60
|
-
const
|
|
75
|
+
const packageName = getPackageName();
|
|
76
|
+
const tpl = `/**
|
|
77
|
+
* YApi 接口代码生成配置
|
|
78
|
+
* 生成后执行: pnpm yapi-gen 或 pnpm gen:api
|
|
79
|
+
*/
|
|
80
|
+
import { defineYapiGenConfig } from '${packageName}';
|
|
61
81
|
|
|
62
82
|
export default defineYapiGenConfig({
|
|
63
83
|
serverUrl: 'https://yapi.example.com',
|
|
64
84
|
serverType: 'yapi',
|
|
65
|
-
// 请求适配:'axios' | 'nuxt-bbm' | 'custom'
|
|
85
|
+
// 请求适配:'axios' | 'nuxt-bbm' | 'custom'
|
|
66
86
|
requestAdapter: 'axios',
|
|
67
|
-
//
|
|
87
|
+
// axios 时可选指定实例路径,默认 '/@/utils/http/axios'
|
|
88
|
+
// requestAdapter: { type: 'axios', axiosInstancePath: '/@/utils/http/axios' },
|
|
68
89
|
outputFilePath: (interfaceInfo, changeCase) => {
|
|
69
90
|
const catId = interfaceInfo.catid ?? 0;
|
|
70
91
|
return \`src/yapi/cat_\${catId}/index.ts\`;
|
|
71
92
|
},
|
|
72
93
|
dataKey: 'result',
|
|
94
|
+
preproccessInterface(interfaceInfo) {
|
|
95
|
+
// 可选:按环境变量过滤接口。YTT_INTERFACE_IDS=1,2,3 或 YTT_CATEGORY_IDS=401,413
|
|
96
|
+
const ids = process.env.YTT_INTERFACE_IDS;
|
|
97
|
+
const catIds = process.env.YTT_CATEGORY_IDS;
|
|
98
|
+
if (ids) {
|
|
99
|
+
const allowList = ids.split(',').map((s) => parseInt(s.trim(), 10));
|
|
100
|
+
if (!allowList.includes(interfaceInfo._id)) return false;
|
|
101
|
+
} else if (catIds) {
|
|
102
|
+
const allowList = catIds.split(',').map((s) => s.trim());
|
|
103
|
+
if (!allowList.includes(String(interfaceInfo.catid))) return false;
|
|
104
|
+
}
|
|
105
|
+
return interfaceInfo;
|
|
106
|
+
},
|
|
73
107
|
projects: [
|
|
74
108
|
{
|
|
75
109
|
token: process.env.YAPI_TOKEN || 'your-project-token',
|
|
@@ -86,7 +120,7 @@ export default defineYapiGenConfig({
|
|
|
86
120
|
});
|
|
87
121
|
`;
|
|
88
122
|
writeFileSync(configPath, tpl, 'utf-8');
|
|
89
|
-
console.log('[yapi-gen] 已创建 yapi-gen.config.ts
|
|
123
|
+
console.log('[yapi-gen] 已创建 yapi-gen.config.ts,请修改 serverUrl、token 后执行 pnpm yapi-gen 生成接口');
|
|
90
124
|
}
|
|
91
125
|
return 0;
|
|
92
126
|
}
|
|
@@ -108,10 +142,11 @@ export function run(options = {}) {
|
|
|
108
142
|
const wrapperPath = resolve(wrapperDir, WRAPPER_FILENAME);
|
|
109
143
|
const content = getWrapperContent(cwd, configPath);
|
|
110
144
|
writeFileSync(wrapperPath, content, 'utf-8');
|
|
111
|
-
|
|
145
|
+
// 使用本包依赖的 yapi-to-typescript 的 CLI,不依赖使用方安装
|
|
146
|
+
const yttCliPath = require.resolve('yapi-to-typescript/lib/cjs/cli.js');
|
|
147
|
+
const result = spawnSync(process.execPath, [yttCliPath, '-c', wrapperPath], {
|
|
112
148
|
cwd,
|
|
113
149
|
stdio: 'inherit',
|
|
114
|
-
shell: true,
|
|
115
150
|
});
|
|
116
151
|
return result.status ?? 1;
|
|
117
152
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@karry.sun/yapi-gen",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "基于 yapi-to-typescript 的 YApi 前端代码生成 CLI,支持可配置的请求适配器与多项目复用",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -34,14 +34,6 @@
|
|
|
34
34
|
"dependencies": {
|
|
35
35
|
"yapi-to-typescript": "^3.38.0"
|
|
36
36
|
},
|
|
37
|
-
"peerDependencies": {
|
|
38
|
-
"yapi-to-typescript": ">=3.0.0"
|
|
39
|
-
},
|
|
40
|
-
"peerDependenciesMeta": {
|
|
41
|
-
"yapi-to-typescript": {
|
|
42
|
-
"optional": true
|
|
43
|
-
}
|
|
44
|
-
},
|
|
45
37
|
"devDependencies": {
|
|
46
38
|
"typescript": "^5.0.0"
|
|
47
39
|
},
|