/** * _defhttp_shim.ts — 用 JnpfClient 实现 web 端的 defHttp (v0.11+ Phase 1) * * 设计: * - web 端 request.ts 用 Vben 的 RequestClient, 依赖 axios + Vite + Vue ecosystem * - dacli 是 Node CLI, 没 Vue/Vite, 但 defHttp.get/post/put/delete 签名是稳定的 * - 此 shim 用 JnpfClient.fetch 实现, 签名与 web 完全一致 * - 调用方 (visualDev.ts) 不感知 shim, 仍调 defHttp.x({ url, data, ...opts }) * * 注意: * - 签名严格匹配 web Vben defHttp: * defHttp.get({ url, params?, data? }) → GET (data 当 query) * defHttp.post({ url, data }) → POST JSON * defHttp.put({ url, data }) → PUT JSON * defHttp.delete({ url, data? }) → DELETE * - 响应统一返回 res.data (已经过 Vben transformResponseHook 解构) * 即: { code: 200, msg: 'ok', data: ... } 解出 → 直接拿 .data * - 不支持: useCipher 加密 / 上传下载进度 / axios 拦截器 * (dacli 不需要这些, web 也只在 useCipher=true 时启用加密) * - 用 globalThis 共享 client, 避免 tsx/动态 import 导致的多实例问题 */ import { JnpfClient } from '../core/client.ts' const GLOBAL_KEY = '__dacli_defhttp_shim_client__' type GlobalWithClient = typeof globalThis & { [GLOBAL_KEY]?: JnpfClient } function getClient(): JnpfClient { const client = (globalThis as GlobalWithClient)[GLOBAL_KEY] if (!client) throw new Error('defHttp shim 未初始化, 请先调 initDefHttpShim(client)') return client } /** 初始化 shim, 必须先调一次 (visualDev / dataModel 模块加载前) */ export function initDefHttpShim(client: JnpfClient): void { ;(globalThis as GlobalWithClient)[GLOBAL_KEY] = client } /** 把 undefined/null 值从 query 里去掉 (与 JnpfClient.get 行为一致) */ function cleanQuery(data: Record | undefined): Record { if (!data) return {} const out: Record = {} for (const [k, v] of Object.entries(data)) { if (v === undefined || v === null) continue if (typeof v === 'object') out[k] = JSON.stringify(v) else out[k] = v as string | number } return out } /** * 统一响应包装: web 的 transformResponseHook 已经解出 {code,data,msg} → 取 data * * 关键行为 (v0.11e+ Bug B-1 修复, 2026-06-27): * JNPF create-type POST API (POST /system/DictionaryType, /system/DataModel, * /visualdev/Base, /visualdev/Base/{id}/Actions/Release, .../Actions/Module 等) * 成功时返回 {"code":200,"msg":"New created successfully","data":null}. * 旧 unwrap 返 null → 调用方 `tr.code` 抛 * "Cannot read properties of undefined (reading 'code')". * * 现当 data 为 null/undefined 时, 返 stub `{code:resp.code, msg:resp.msg, id:undefined}`, * 调用方可安全访问 r.code (== 200 表示成功) / r.msg (i18n 提示) / r.id (无). * 调用方应根据 msg='New created successfully' 判断成功, 或用 GET API 反查实际 id/状态 * (避免假 PASS — 镜像 web 端 "创建后弹窗提示成功 + 列表刷新" 流程). * * 参考: web/api/request.ts:36-97 transformResponseHook (返回 res.data 完整信封) * jnpf/.../MsgCode.java SU001 "New created successfully" (i18n) <<<<<<< Updated upstream * tds 9 bug 复盘 2026-06-27 (B-1 webapi shim unwrap null TypeError) ======= * tds 9 bug 复盘 2026-06-27 (b-1 webapi shim unwrap null TypeError) >>>>>>> Stashed changes */ async function unwrap(resp: { code: number; msg?: string; data: T | null }): Promise { if (resp.code !== 200) { throw new Error(`API code=${resp.code}: ${resp.msg ?? ''}`) } if (resp.data === null || resp.data === undefined) { // JNPF POST create-type 成功无 data → 返 stub 防 TypeError. // 兼容所有调用方 cast `{code?, msg?, id?, data?}` 类型断言. return { code: resp.code, msg: resp.msg, id: undefined } as unknown as T } return resp.data as T } /** * 把 web 端 url 里的 /api 前缀去掉, 因为 JnpfClient.baseUrl 已含 /api * * web 端 url: '/api/visualdev/Base' → JNPF base 'http://nginx/api' * dacli 期望: '/visualdev/Base' → 'http://nginx/api' + '/visualdev/Base' = 'http://nginx/api/visualdev/Base' * * 不改 baseUrl (会破坏现有命令), 仅在 shim 里 strip */ function stripApiPrefix(url: string): string { if (url.startsWith('/api/')) return url.slice(4) if (url === '/api') return '' return url } /** * shim of web defHttp — 与 Vben defHttp 签名一致 * 用法 (web 端 visualDev.ts 不感知): * defHttp.get({ url: '/api/visualdev/Base', data: { keyword: 'foo' } }) */ export const defHttp = { async get(config: { url: string; data?: Record }): Promise { const c = getClient() const r = await c.get(stripApiPrefix(config.url), cleanQuery(config.data)) return unwrap(r) }, async post(config: { url: string; data?: unknown }): Promise { const c = getClient() const r = await c.postJson(stripApiPrefix(config.url), config.data ?? {}) return unwrap(r) }, async put(config: { url: string; data?: unknown }): Promise { const c = getClient() const r = await c.putJson(stripApiPrefix(config.url), config.data ?? {}) return unwrap(r) }, async delete(config: { url: string; data?: unknown }): Promise { const c = getClient() // 注意: web 端 defHttp.delete 也可能带 data (axios 支持), 我们忽略 const r = await c.del(stripApiPrefix(config.url)) return unwrap(r) }, } /** * shim of web reportHttp (jxcj 2026-07-01 镜像 web 端 report.ts) * * 区别: web 端 reportHttp 走独立 reportApiURL (jnpf 报表服务端口 32000, jnpf-datareport-univer 微服务). * dacli 没多 client 机制, 但 nginx 已经把 /api/Report/ 代理到 32000, 走主 30000 client 等价. * 镜像目的: 跟 web 端 report.ts 同 schema (formData cells snapshot 等), 不与 web 端 schema 漂移. */ export const reportHttp = { async get(config: { url: string; data?: Record }): Promise { const c = getClient() const r = await c.get(stripApiPrefix(config.url), cleanQuery(config.data)) return unwrap(r) }, async post(config: { url: string; data?: unknown }): Promise { const c = getClient() const r = await c.postJson(stripApiPrefix(config.url), config.data ?? {}) return unwrap(r) }, async put(config: { url: string; data?: unknown }): Promise { const c = getClient() const r = await c.putJson(stripApiPrefix(config.url), config.data ?? {}) return unwrap(r) }, async delete(config: { url: string; data?: unknown }): Promise { const c = getClient() const r = await c.del(stripApiPrefix(config.url)) return unwrap(r) }, }