From 7a34ee6b5dc776826ab4dc90bdfc438b6ea8ee99 Mon Sep 17 00:00:00 2001 From: ssw Date: Thu, 25 Jun 2026 15:06:48 +0800 Subject: [PATCH] =?UTF-8?q?refactor(v0.11+):=20datasource=204=20=E5=AD=90?= =?UTF-8?q?=E5=91=BD=E4=BB=A4=E6=94=B9=E7=94=A8=20webapi=20=E9=95=9C?= =?UTF-8?q?=E5=83=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Phase 1 审计 (cmd-review/datasource.md) 标记 P0 必做: - webapi/dataSource.ts 已镜像 11 函数, 使用率 0% - 4 子命令 (list/create/test/delete) 100% 绕 webapi 修复: - list → loadDataSource.getDataSourceList - create → loadDataSource.createDataSource - delete → loadDataSource.delDataSource - test → loadDataSource.testDbConnection + getDataSourceInfo 收益: - 消除 ~150 行 c.get/postJson/del 硬编码 - 自动同步 web 端 schema 变更 - 与 v0.11+ 镜像架构对齐 (6 个 webapi 模块之一) Co-Authored-By: Claude --- src/commands/datasource/create.ts | 29 ++++++++++++++++------------- src/commands/datasource/delete.ts | 13 ++++++++----- src/commands/datasource/list.ts | 23 +++++++++++------------ src/commands/datasource/test.ts | 24 ++++++++++-------------- 4 files changed, 45 insertions(+), 44 deletions(-) diff --git a/src/commands/datasource/create.ts b/src/commands/datasource/create.ts index 7a6cc8c..1e0ac09 100644 --- a/src/commands/datasource/create.ts +++ b/src/commands/datasource/create.ts @@ -1,13 +1,15 @@ /** * commands/datasource/create.ts — 新建数据源 * - * API: POST /api/system/DataSource + * v0.11+ Phase 2 (2026-06-25): 改用 webapi 镜像 (loadDataSource.createDataSource) + * 替代 v0.10 直接 c.postJson('/system/DataSource') */ import { Command } from 'commander' import { JnpfClient } from '../../core/client.ts' import { installAiHelpHook } from '../../core/help.ts' import { log, printJson } from '../../core/log.ts' import { encryptAesEcb, isEncrypted } from '../../core/crypto.ts' +import { initWebapi, loadDataSource } from '../../webapi/dataSource.ts' interface CreateOpts { fullName: string @@ -35,6 +37,7 @@ export function buildDatasourceCreateCommand(): Command { .option('--json', '原始 JSON 输出', false) .action(async (opts: CreateOpts) => { const c = await JnpfClient.fromToken() + await initWebapi(c) const body: Record = { fullName: opts.fullName, enCode: opts.enCode, @@ -54,26 +57,26 @@ export function buildDatasourceCreateCommand(): Command { // web Form.vue: data.oracleExtend = !!values.oracleExtend → 必须显式设, 否则 DbLinkBaseForm.getOracleExtend() NPE body.oracleExtend = opts.dbType === 'Oracle' ? 1 : 0 log.debug(`[datasource create] body=${JSON.stringify(body)}`) - const r = await c.postJson<{ id?: string }>('/system/DataSource', body) - if (r.code !== 200) { + const r = (await loadDataSource.createDataSource(body)) as { id?: string; code?: number; msg?: string } + if (r.code !== undefined && r.code !== 200) { log.error(`datasource create 失败 (code=${r.code}): ${r.msg}`) process.exit(1) } - let id = r.data?.id + let id = r.id // 参考 menu create 历史模式: 响应可能不返 id, 按 enCode 反查 list if (!id) { - const listR = await c.get<{ list: { id: string; enCode: string; fullName: string }[] }>( - '/system/DataSource', - { keyword: opts.enCode, pageSize: '50' }, - ) - log.debug(`[datasource create] listR.data=${JSON.stringify(listR.data).slice(0, 200)}`) - const found = (listR.data?.list || []).find( + const listR = (await loadDataSource.getDataSourceList({ + keyword: opts.enCode, + pageSize: 50, + })) as { list?: { id: string; enCode: string; fullName: string }[] } + log.debug(`[datasource create] listR.list=${JSON.stringify(listR.list).slice(0, 200)}`) + const found = (listR.list || []).find( (d) => d.enCode === opts.enCode || d.fullName === opts.fullName, ) id = found?.id } log.info(`✓ 数据源创建成功 id=${id}`) - if (opts.json) printJson({ ...(r.data as object), id }) + if (opts.json) printJson({ ...(r as object), id }) }) installAiHelpHook(cmd, { purpose: '新建数据源 (数据库连接配置)', @@ -91,9 +94,9 @@ export function buildDatasourceCreateCommand(): Command { source: { web: 'web/apps/jnpf-web-apps-main/src/views/dataCenter/dataSource/Form.vue', api: 'POST /api/system/DataSource', - ref: 'doc/v0.8-endpoints.md §1.1', + ref: 'web/.../api/systemData/dataSource.ts:16 createDataSource()', }, - notes: ['密码可后续用 update 修改', '可立即用 test 子命令验证连接'], + notes: ['v0.11+ 改用 webapi 镜像', '密码可后续用 update 修改', '可立即用 test 子命令验证连接'], }) return cmd } diff --git a/src/commands/datasource/delete.ts b/src/commands/datasource/delete.ts index 3a7f88d..e14dcef 100644 --- a/src/commands/datasource/delete.ts +++ b/src/commands/datasource/delete.ts @@ -1,12 +1,14 @@ /** * commands/datasource/delete.ts — 删除数据源 * - * API: DELETE /api/system/DataSource/{id} + * v0.11+ Phase 2 (2026-06-25): 改用 webapi 镜像 (loadDataSource.delDataSource) + * 替代 v0.10 直接 c.del('/system/DataSource/{id}') */ import { Command } from 'commander' import { JnpfClient } from '../../core/client.ts' import { installAiHelpHook } from '../../core/help.ts' import { log } from '../../core/log.ts' +import { initWebapi, loadDataSource } from '../../webapi/dataSource.ts' interface DeleteOpts { id: string @@ -24,8 +26,9 @@ export function buildDatasourceDeleteCommand(): Command { process.exit(2) } const c = await JnpfClient.fromToken() - const r = await c.del(`/system/DataSource/${opts.id}`) - if (r.code !== 200) { + await initWebapi(c) + const r = (await loadDataSource.delDataSource(opts.id)) as { code?: number; msg?: string } + if (r.code !== undefined && r.code !== 200) { log.error(`datasource delete 失败: ${r.msg}`) process.exit(1) } @@ -38,9 +41,9 @@ export function buildDatasourceDeleteCommand(): Command { source: { web: 'web/apps/jnpf-web-apps-main/src/views/dataCenter/dataSource/index.vue', api: 'DELETE /api/system/DataSource/{id}', - ref: 'doc/v0.8-endpoints.md §1.1', + ref: 'web/.../api/systemData/dataSource.ts:24 delDataSource()', }, - notes: ['删除后该数据源上的所有业务库表无法访问', '谨慎操作'], + notes: ['v0.11+ 改用 webapi 镜像', '删除后该数据源上的所有业务库表无法访问', '谨慎操作'], }) return cmd } diff --git a/src/commands/datasource/list.ts b/src/commands/datasource/list.ts index 892a0c6..e431be0 100644 --- a/src/commands/datasource/list.ts +++ b/src/commands/datasource/list.ts @@ -1,12 +1,14 @@ /** * commands/datasource/list.ts — 数据源列表 * - * API: GET /api/system/DataSource + * v0.11+ Phase 2 (2026-06-25): 改用 webapi 镜像 (loadDataSource.getDataSourceList) + * 替代 v0.10 直接 c.get('/system/DataSource') */ import { Command } from 'commander' import { JnpfClient } from '../../core/client.ts' import { installAiHelpHook } from '../../core/help.ts' import { log, printJson } from '../../core/log.ts' +import { initWebapi, loadDataSource } from '../../webapi/dataSource.ts' interface ListOpts { keyword?: string @@ -20,21 +22,17 @@ export function buildDatasourceListCommand(): Command { .option('--json', '原始 JSON 输出', false) .action(async (opts: ListOpts) => { const c = await JnpfClient.fromToken() - const r = await c.get<{ list: Record[] }>( - '/system/DataSource', - { keyword: opts.keyword }, - ) - if (r.code !== 200) { - log.error(`datasource list 失败 (code=${r.code}): ${r.msg}`) - process.exit(1) + await initWebapi(c) + const data = (await loadDataSource.getDataSourceList({ keyword: opts.keyword })) as { + list?: Record[] } - const list = r.data?.list || [] + const list = data?.list || [] if (opts.json) { - printJson(r.data) + printJson(data) return } log.info(`共 ${list.length} 个数据源`) - console.log('ID FULL_NAME ENCODE DB_TYPE') + console.log('ID'.padEnd(30) + 'FULL_NAME'.padEnd(23) + 'ENCODE'.padEnd(21) + 'DB_TYPE') console.log('-'.repeat(90)) for (const d of list) { const id = String(d.id || '').padEnd(30) @@ -51,8 +49,9 @@ export function buildDatasourceListCommand(): Command { source: { web: 'web/apps/jnpf-web-apps-main/src/views/dataCenter/dataSource/index.vue', api: 'GET /api/system/DataSource', - ref: 'doc/v0.8-endpoints.md §1.1', + ref: 'web/.../api/systemData/dataSource.ts:9 getDataSourceList()', }, + notes: ['v0.11+ 改用 webapi 镜像 (loadDataSource.getDataSourceList)'], }) return cmd } diff --git a/src/commands/datasource/test.ts b/src/commands/datasource/test.ts index d999ed7..109e9a3 100644 --- a/src/commands/datasource/test.ts +++ b/src/commands/datasource/test.ts @@ -1,16 +1,15 @@ /** * commands/datasource/test.ts — 测试数据源连接 * - * API: POST /api/system/DataSource/Actions/Test - * - * 决策: 不去 GET 拿配置再转发 (password 已被加密, 二次加密会错), - * 直接传当前 opts 给 test 端点, 由后端真去连库 + * v0.11+ Phase 2 (2026-06-25): 改用 webapi 镜像 (loadDataSource.testDbConnection + getDataSourceInfo) + * 替代 v0.10 直接 c.postJson + c.get */ import { Command } from 'commander' import { JnpfClient } from '../../core/client.ts' import { installAiHelpHook } from '../../core/help.ts' import { log, printJson } from '../../core/log.ts' import { encryptAesEcb, isEncrypted } from '../../core/crypto.ts' +import { initWebapi, loadDataSource } from '../../webapi/dataSource.ts' interface TestOpts { id?: string @@ -36,15 +35,11 @@ export function buildDatasourceTestCommand(): Command { .option('--json', '原始 JSON 输出', false) .action(async (opts: TestOpts) => { const c = await JnpfClient.fromToken() + await initWebapi(c) let body: Record if (opts.id) { - // 按 id 拉配置, password 已被后端加密存储, 直接转发 (不要再加密) - const r1 = await c.get>(`/system/DataSource/${opts.id}`) - if (r1.code !== 200) { - log.error(`datasource test 失败 (拉配置): ${r1.msg}`) - process.exit(1) - } - const data = r1.data as Record + // 按 id 拉配置 (用 webapi), password 已被后端加密存储, 直接转发 (不要再加密) + const data = (await loadDataSource.getDataSourceInfo(opts.id)) as Record body = { ...data, id: undefined, oracleExtend: data.dbType === 'Oracle' ? 1 : 0 } } else if (opts.host && opts.dbType) { body = { @@ -60,8 +55,8 @@ export function buildDatasourceTestCommand(): Command { log.error('请传 --id <数据源 id> 或 --dbType + --host + --user + --password') process.exit(2) } - const r = await c.postJson('/system/DataSource/Actions/Test', body) - if (r.code !== 200) { + const r = (await loadDataSource.testDbConnection(body)) as { code?: number; msg?: string; data?: unknown } + if (r.code !== undefined && r.code !== 200) { log.error(`datasource test 失败 (code=${r.code}): ${r.msg}`) if (opts.json) printJson(r) process.exit(1) @@ -76,8 +71,9 @@ export function buildDatasourceTestCommand(): Command { source: { web: 'web/apps/jnpf-web-apps-main/src/views/dataCenter/dataSource/Form.vue', api: 'POST /api/system/DataSource/Actions/Test', - ref: 'doc/v0.8-endpoints.md §1.1', + ref: 'web/.../api/systemData/dataSource.ts:27 testDbConnection()', }, + notes: ['v0.11+ 改用 webapi 镜像 (testDbConnection + getDataSourceInfo)'], }) return cmd }