dsh-postapi 0.0.0-stage → 0.0.1
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/LICENSE +201 -0
- package/README.md +139 -2
- package/cordis.patch.yml +3 -0
- package/dist/client.js +5651 -0
- package/docs/DETAIL.md +27 -0
- package/docs/apifox-design-spec.md +101 -0
- package/docs/apifox-research/analysis.md +254 -0
- package/docs/apifox-ui-delivery.md +150 -0
- package/docs/apifox-visual-parity-spec.md +353 -0
- package/docs/feature-matrix.md +112 -0
- package/docs/flow-reference/sequence-reference.html +163 -0
- package/docs/interface-contract-v2.md +175 -0
- package/docs/interface-contract.md +105 -0
- package/docs/nav-layout-v2-spec.md +107 -0
- package/docs/preview.svg +9 -0
- package/docs/probe-client.legacy.js +31 -0
- package/docs/rootcause-sidebar-vanish.md +95 -0
- package/docs/rootcause.md +86 -0
- package/docs/sidebar-notes.md +153 -0
- package/docs/ui-audit.md +197 -0
- package/docs/ui-notes.md +98 -0
- package/docs/v2-delivery.md +99 -0
- package/icon.svg +4 -0
- package/index.js +5 -0
- package/locale/en.json +7 -0
- package/locale/zh.json +7 -0
- package/package.json +73 -4
- package/src/client/index.js +132 -0
- package/src/client/jsx-shim.js +9 -0
- package/src/client/pages/__tests__/heights.test.js +135 -0
- package/src/client/pages/components/AssertTable.jsx +122 -0
- package/src/client/pages/components/BodyEditor.jsx +91 -0
- package/src/client/pages/components/EmptyState.jsx +52 -0
- package/src/client/pages/components/FolderTree.jsx +109 -0
- package/src/client/pages/components/JsonEditor.jsx +45 -0
- package/src/client/pages/components/ParamTable.jsx +226 -0
- package/src/client/pages/components/ResponsePanel.jsx +278 -0
- package/src/client/pages/components/ScriptPanel.jsx +99 -0
- package/src/client/pages/components/StatusPill.jsx +35 -0
- package/src/client/pages/components/__tests__/apifox-ui.test.js +291 -0
- package/src/client/pages/components/__tests__/pages-a.test.js +392 -0
- package/src/client/pages/components/__tests__/tree.test.js +379 -0
- package/src/client/pages/components/headerPresets.js +63 -0
- package/src/client/pages/debug.jsx +576 -0
- package/src/client/pages/design-tokens.css +2604 -0
- package/src/client/pages/doc.jsx +552 -0
- package/src/client/pages/env.jsx +378 -0
- package/src/client/pages/flow.jsx +349 -0
- package/src/client/pages/history.jsx +238 -0
- package/src/client/pages/import.jsx +396 -0
- package/src/client/pages/mock.jsx +316 -0
- package/src/client/pages/overview.jsx +348 -0
- package/src/client/pages/schema.jsx +317 -0
- package/src/client/pages/search.jsx +216 -0
- package/src/client/pages/sequence.jsx +344 -0
- package/src/client/pages/settings.jsx +205 -0
- package/src/client/pages/test.jsx +470 -0
- package/src/client/pages/theme.js +27 -0
- package/src/client/pages-entry.js +20 -0
- package/src/client/shell/ApiTitle.js +19 -0
- package/src/client/shell/Body.js +492 -0
- package/src/client/shell/definition.js +57 -0
- package/src/client/shell/locales.js +185 -0
- package/src/client/shell/menu.js +174 -0
- package/src/client/shell/registry.js +530 -0
- package/src/host/bridge.js +407 -0
- package/src/host/codegen.js +356 -0
- package/src/host/import-export.js +1044 -0
- package/src/host/index.js +102 -0
- package/src/host/mock-server.js +303 -0
- package/src/host/request.js +336 -0
- package/src/host/script-sandbox.js +272 -0
- package/src/host/storage.js +803 -0
- package/src/host/test-runner.js +126 -0
- package/src/host/variables.js +108 -0
|
@@ -0,0 +1,576 @@
|
|
|
1
|
+
// debug.jsx — 调试发送页(Apifox 调试台:左接口树 + 中 URL 行/页签请求编辑 + 右响应面板)
|
|
2
|
+
// 布局类全部来自 design-tokens.css:.pp-url-row / .pp-tabs + .pp-tab / .pp-pane / .pp-pill / .pp-pre / .pp-table。
|
|
3
|
+
// 发送走 window.__postapiApi.request.send,接口与环境走 storage;未注入 api 时用文件内 mock 并标注「示例数据」。
|
|
4
|
+
import { useEffect, useMemo, useRef, useState } from 'react'
|
|
5
|
+
import { applyTheme } from './theme.js'
|
|
6
|
+
import { ParamTable } from './components/ParamTable.jsx'
|
|
7
|
+
import { JsonEditor } from './components/JsonEditor.jsx'
|
|
8
|
+
import { EmptyState } from './components/EmptyState.jsx'
|
|
9
|
+
import { StatusPill } from './components/StatusPill.jsx'
|
|
10
|
+
import { COMMON_HEADERS, filterHeaders } from './components/headerPresets.js'
|
|
11
|
+
import { BodyEditor } from './components/BodyEditor.jsx'
|
|
12
|
+
import { ScriptPanel } from './components/ScriptPanel.jsx'
|
|
13
|
+
import { ResponsePanel } from './components/ResponsePanel.jsx'
|
|
14
|
+
|
|
15
|
+
;(window.__postapiQueue = window.__postapiQueue || []).push(['debug', DebugPage]) // 注册表就绪前入队,ensureGlobals 统一重放(模块求值顺序无关)
|
|
16
|
+
|
|
17
|
+
const METHODS = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS']
|
|
18
|
+
const BODY_TYPES = ['none', 'json', 'form', 'urlencoded', 'raw', 'xml', 'graphql']
|
|
19
|
+
const AUTH_TYPES = ['none', 'bearer', 'basic', 'apikey']
|
|
20
|
+
|
|
21
|
+
// —— mock 接口(契约 §2 endpoint 结构;与 doc 页独立副本,避免跨页耦合)——
|
|
22
|
+
const MOCK_PROJECT = {
|
|
23
|
+
id: 'proj_demo01',
|
|
24
|
+
name: '订单中心(示例数据)',
|
|
25
|
+
environments: [
|
|
26
|
+
{ id: 'env_01', name: '开发环境', baseUrl: 'https://api.example.test', isDefault: true,
|
|
27
|
+
variables: [{ key: 'token', value: 'demo-token', enabled: true, secret: false }] },
|
|
28
|
+
{ id: 'env_02', name: '生产环境', baseUrl: 'https://api.example.com', isDefault: false, variables: [] },
|
|
29
|
+
],
|
|
30
|
+
globals: { variables: [{ key: 'apiKey', value: 'demo-key', enabled: true, secret: false }] },
|
|
31
|
+
folders: [{ id: 'fld_01', name: '订单管理', parentId: null, order: 0 }],
|
|
32
|
+
endpoints: [
|
|
33
|
+
{
|
|
34
|
+
id: 'ep_1001', name: '查询订单列表', folderId: 'fld_01', method: 'GET', url: '/api/v1/orders',
|
|
35
|
+
params: [
|
|
36
|
+
{ key: 'page', value: '1', enabled: true, type: 'number', description: '', required: false },
|
|
37
|
+
{ key: 'pageSize', value: '20', enabled: true, type: 'number', description: '', required: false },
|
|
38
|
+
],
|
|
39
|
+
pathParams: [], headers: [{ key: 'Accept', value: 'application/json', enabled: true, type: 'string', description: '', required: false }],
|
|
40
|
+
cookies: [], body: { type: 'none', content: '' },
|
|
41
|
+
auth: { type: 'bearer', token: '{{token}}', username: '', password: '', key: '', value: '', addTo: 'header' },
|
|
42
|
+
responses: [{ id: 'res_a', name: '成功', code: 200, contentType: 'application/json',
|
|
43
|
+
body: '{\n "code": 0,\n "data": { "total": 42, "items": [] }\n}', headers: [], enabled: true, isDefault: true }],
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
id: 'ep_1002', name: '创建订单', folderId: 'fld_01', method: 'POST', url: '/api/v1/orders',
|
|
47
|
+
params: [], pathParams: [],
|
|
48
|
+
headers: [{ key: 'Content-Type', value: 'application/json', enabled: true, type: 'string', description: '', required: true }],
|
|
49
|
+
cookies: [], body: { type: 'json', content: '{\n "skuId": "SKU-1001",\n "quantity": 2\n}' },
|
|
50
|
+
auth: { type: 'bearer', token: '{{token}}', username: '', password: '', key: '', value: '', addTo: 'header' },
|
|
51
|
+
responses: [{ id: 'res_b', name: '创建成功', code: 201, contentType: 'application/json',
|
|
52
|
+
body: '{\n "code": 0,\n "data": { "orderId": "ORD-002" }\n}', headers: [], enabled: true, isDefault: true }],
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
id: 'ep_3001', name: '更新支付状态', folderId: null, method: 'PATCH', url: '/api/v1/payments/{paymentId}',
|
|
56
|
+
params: [], pathParams: [{ key: 'paymentId', value: 'PAY-001', enabled: true, type: 'string', description: '', required: true }],
|
|
57
|
+
headers: [], cookies: [], body: { type: 'json', content: '{\n "status": "PAID"\n}' },
|
|
58
|
+
auth: { type: 'none', token: '', username: '', password: '', key: '', value: '', addTo: 'header' },
|
|
59
|
+
responses: [{ id: 'res_c', name: '成功', code: 200, contentType: 'application/json', body: '{}', headers: [], enabled: true, isDefault: true }],
|
|
60
|
+
},
|
|
61
|
+
],
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const TABS = [
|
|
65
|
+
{ id: 'params', label: 'Params' },
|
|
66
|
+
{ id: 'headers', label: 'Headers' },
|
|
67
|
+
{ id: 'body', label: 'Body' },
|
|
68
|
+
{ id: 'auth', label: 'Auth' },
|
|
69
|
+
{ id: 'script', label: '脚本' },
|
|
70
|
+
{ id: 'settings', label: '设置' },
|
|
71
|
+
]
|
|
72
|
+
|
|
73
|
+
/** 回放快照 key:与 history 页写入的 key 一致,切到调试页即按快照重填。 */
|
|
74
|
+
const REPLAY_KEY = 'postapi.debug.replay'
|
|
75
|
+
|
|
76
|
+
// 参数表列用 ParamTable 的默认列(启用/参数名/类型/必填/示例值/描述 + 操作),
|
|
77
|
+
// 与 doc 页只读表同一套,避免两页列不一致。
|
|
78
|
+
const AUTH_LABEL = { none: '无认证', bearer: 'Bearer', basic: 'Basic', apikey: 'API Key' }
|
|
79
|
+
|
|
80
|
+
function resolveApi() {
|
|
81
|
+
return typeof window !== 'undefined' ? window.__postapiApi : null
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function emptyDraft(ep) {
|
|
85
|
+
return {
|
|
86
|
+
id: ep && ep.id ? ep.id : '',
|
|
87
|
+
name: ep && ep.name ? ep.name : '',
|
|
88
|
+
folderId: ep && ep.folderId ? ep.folderId : null,
|
|
89
|
+
method: (ep && ep.method) || 'GET',
|
|
90
|
+
url: (ep && ep.url) || '',
|
|
91
|
+
params: (ep && Array.isArray(ep.params) ? ep.params : []).map((r) => ({ ...r })),
|
|
92
|
+
pathParams: (ep && Array.isArray(ep.pathParams) ? ep.pathParams : []).map((r) => ({ ...r })),
|
|
93
|
+
headers: (ep && Array.isArray(ep.headers) ? ep.headers : []).map((r) => ({ ...r })),
|
|
94
|
+
cookies: (ep && Array.isArray(ep.cookies) ? ep.cookies : []).map((r) => ({ ...r })),
|
|
95
|
+
body: { type: (ep && ep.body && ep.body.type) || 'none', content: (ep && ep.body && ep.body.content) || '' },
|
|
96
|
+
auth: {
|
|
97
|
+
type: (ep && ep.auth && ep.auth.type) || 'none',
|
|
98
|
+
token: (ep && ep.auth && ep.auth.token) || '',
|
|
99
|
+
username: (ep && ep.auth && ep.auth.username) || '',
|
|
100
|
+
password: (ep && ep.auth && ep.auth.password) || '',
|
|
101
|
+
key: (ep && ep.auth && ep.auth.key) || '',
|
|
102
|
+
value: (ep && ep.auth && ep.auth.value) || '',
|
|
103
|
+
addTo: (ep && ep.auth && ep.auth.addTo) || 'header',
|
|
104
|
+
},
|
|
105
|
+
responses: (ep && Array.isArray(ep.responses) ? ep.responses : []).map((r) => ({ ...r })),
|
|
106
|
+
// 前后置脚本 / 断言 / 发送前等待(Apifox 的「前置操作 / 后置操作」与「等待时间」)
|
|
107
|
+
preScript: (ep && ep.preScript) || '',
|
|
108
|
+
postScript: (ep && ep.postScript) || '',
|
|
109
|
+
assertions: (ep && Array.isArray(ep.assertions) ? ep.assertions : []).map((a) => ({ ...a })),
|
|
110
|
+
waitMs: Number.isFinite(ep && ep.waitMs) ? ep.waitMs : 0,
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** 读取 history 页写入的回放快照(无则 null);结构 { method, url, headers, body }。 */
|
|
115
|
+
export function readReplay() {
|
|
116
|
+
try {
|
|
117
|
+
const raw = typeof localStorage !== 'undefined' ? localStorage.getItem(REPLAY_KEY) : null
|
|
118
|
+
if (!raw) return null
|
|
119
|
+
const parsed = JSON.parse(raw)
|
|
120
|
+
return parsed && typeof parsed === 'object' ? parsed : null
|
|
121
|
+
} catch {
|
|
122
|
+
return null
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** 收集 {{var}} 引用(URL / Params / Headers / Body / Auth),用于提示未定义变量。 */
|
|
127
|
+
export function collectVarRefs(draft) {
|
|
128
|
+
const found = []
|
|
129
|
+
const push = (text) => {
|
|
130
|
+
const re = /\{\{\s*([^}\s]+)\s*\}\}/g
|
|
131
|
+
let m
|
|
132
|
+
const src = String(text == null ? '' : text)
|
|
133
|
+
while ((m = re.exec(src))) if (found.indexOf(m[1]) < 0) found.push(m[1])
|
|
134
|
+
}
|
|
135
|
+
if (!draft) return found
|
|
136
|
+
push(draft.url)
|
|
137
|
+
for (const rows of [draft.params, draft.pathParams, draft.headers, draft.cookies]) {
|
|
138
|
+
for (const r of Array.isArray(rows) ? rows : []) {
|
|
139
|
+
push(r && r.key)
|
|
140
|
+
push(r && r.value)
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
if (draft.body) push(draft.body.content)
|
|
144
|
+
if (draft.auth) {
|
|
145
|
+
push(draft.auth.token)
|
|
146
|
+
push(draft.auth.value)
|
|
147
|
+
}
|
|
148
|
+
return found
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** 当前环境 + 全局变量表(key -> value),只取 enabled !== false 的项。 */
|
|
152
|
+
export function varTable(project, envId) {
|
|
153
|
+
const table = {}
|
|
154
|
+
const globals = project && project.globals && Array.isArray(project.globals.variables) ? project.globals.variables : []
|
|
155
|
+
for (const v of globals) if (v && v.enabled !== false && v.key) table[v.key] = v.value
|
|
156
|
+
const envs = project && Array.isArray(project.environments) ? project.environments : []
|
|
157
|
+
const env = envs.find((e) => e.id === envId) || envs.find((e) => e.isDefault) || envs[0]
|
|
158
|
+
for (const v of env && Array.isArray(env.variables) ? env.variables : []) {
|
|
159
|
+
if (v && v.enabled !== false && v.key) table[v.key] = v.value
|
|
160
|
+
}
|
|
161
|
+
return { table, env }
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function joinUrl(baseUrl, url) {
|
|
165
|
+
const raw = String(url || '')
|
|
166
|
+
if (/^https?:\/\//i.test(raw)) return raw
|
|
167
|
+
const base = String(baseUrl || '').replace(/\/+$/, '')
|
|
168
|
+
if (!base) return raw
|
|
169
|
+
return base + (raw.charAt(0) === '/' ? raw : '/' + raw)
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** 追加常用请求头:跳过已存在(key 大小写不敏感)的项,按清单顺序补最多 limit 条。
|
|
173
|
+
* 行结构与 ParamTable 的 blankParamRow 一致,可直接交给 patch('headers', rows)。 */
|
|
174
|
+
export function appendCommonHeaders(rows, limit = 3) {
|
|
175
|
+
const list = Array.isArray(rows) ? rows.slice() : []
|
|
176
|
+
const exists = (key) => list.some((r) => String((r && r.key) || '').trim().toLowerCase() === String(key).toLowerCase())
|
|
177
|
+
let added = 0
|
|
178
|
+
for (const h of Array.isArray(COMMON_HEADERS) ? COMMON_HEADERS : []) {
|
|
179
|
+
if (added >= limit) break
|
|
180
|
+
if (!h || !h.key || exists(h.key)) continue
|
|
181
|
+
list.push({
|
|
182
|
+
key: h.key,
|
|
183
|
+
value: h.value == null ? '' : h.value,
|
|
184
|
+
enabled: true,
|
|
185
|
+
type: 'string',
|
|
186
|
+
required: false,
|
|
187
|
+
description: h.description == null ? '' : h.description,
|
|
188
|
+
})
|
|
189
|
+
added += 1
|
|
190
|
+
}
|
|
191
|
+
return list
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** JSON 美化;失败返回原文(原文/美化切换用)。 */
|
|
195
|
+
export function prettyJson(text) {
|
|
196
|
+
if (text == null) return ''
|
|
197
|
+
try {
|
|
198
|
+
return JSON.stringify(JSON.parse(text), null, 2)
|
|
199
|
+
} catch {
|
|
200
|
+
return text == null ? '' : String(text)
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export function DebugPage({ route, api }) {
|
|
205
|
+
const rootRef = useRef(null)
|
|
206
|
+
const abortRef = useRef({ cancelled: false })
|
|
207
|
+
useEffect(() => applyTheme(rootRef.current), [])
|
|
208
|
+
const apiRef = resolveApi() || api
|
|
209
|
+
|
|
210
|
+
const [project, setProject] = useState(MOCK_PROJECT)
|
|
211
|
+
const [isMock, setIsMock] = useState(true)
|
|
212
|
+
const [error, setError] = useState('')
|
|
213
|
+
const [envId, setEnvId] = useState(() => {
|
|
214
|
+
const envs = MOCK_PROJECT.environments
|
|
215
|
+
const def = envs.find((e) => e.isDefault) || envs[0]
|
|
216
|
+
return def ? def.id : ''
|
|
217
|
+
})
|
|
218
|
+
const [draft, setDraft] = useState(() => emptyDraft(MOCK_PROJECT.endpoints[0]))
|
|
219
|
+
const [tab, setTab] = useState('params')
|
|
220
|
+
// 左树折叠态:{ folderId: true } 表示该目录已收起
|
|
221
|
+
const [resp, setResp] = useState(null)
|
|
222
|
+
const [sending, setSending] = useState(false)
|
|
223
|
+
const [timeoutMs, setTimeoutMs] = useState(30000)
|
|
224
|
+
const [savedHint, setSavedHint] = useState('')
|
|
225
|
+
|
|
226
|
+
// 载入项目(列表首个)
|
|
227
|
+
useEffect(() => {
|
|
228
|
+
const storage = apiRef && apiRef.storage
|
|
229
|
+
if (!storage || typeof storage.listProjects !== 'function' || typeof storage.getProject !== 'function') return undefined
|
|
230
|
+
let cancelled = false
|
|
231
|
+
Promise.resolve()
|
|
232
|
+
.then(() => storage.listProjects())
|
|
233
|
+
.then((list) => {
|
|
234
|
+
const first = Array.isArray(list) && list.length ? list[0] : null
|
|
235
|
+
return first ? storage.getProject(first.id) : null
|
|
236
|
+
})
|
|
237
|
+
.then((data) => {
|
|
238
|
+
if (cancelled || !data) return
|
|
239
|
+
setProject(data)
|
|
240
|
+
setIsMock(false)
|
|
241
|
+
setError('')
|
|
242
|
+
const envs = Array.isArray(data.environments) ? data.environments : []
|
|
243
|
+
const def = envs.find((e) => e.isDefault) || envs[0]
|
|
244
|
+
setEnvId(def ? def.id : '')
|
|
245
|
+
const list = Array.isArray(data.endpoints) ? data.endpoints : []
|
|
246
|
+
setDraft(emptyDraft(list[0] || null))
|
|
247
|
+
})
|
|
248
|
+
.catch((err) => {
|
|
249
|
+
if (cancelled) return
|
|
250
|
+
setError('读取项目失败:' + (err && err.message ? err.message : String(err)))
|
|
251
|
+
})
|
|
252
|
+
return () => { cancelled = true }
|
|
253
|
+
}, [apiRef])
|
|
254
|
+
|
|
255
|
+
// 历史页「回放」写入的快照:进入本页时读一次,把 method/url/headers/body 填进草稿
|
|
256
|
+
useEffect(() => {
|
|
257
|
+
const snap = readReplay()
|
|
258
|
+
if (!snap) return
|
|
259
|
+
setDraft((prev) => ({
|
|
260
|
+
...prev,
|
|
261
|
+
method: snap.method || prev.method,
|
|
262
|
+
url: snap.url || prev.url,
|
|
263
|
+
headers: Array.isArray(snap.headers) && snap.headers.length
|
|
264
|
+
? snap.headers.map((h) => ({ enabled: true, type: 'string', required: false, description: '', ...h }))
|
|
265
|
+
: prev.headers,
|
|
266
|
+
body: typeof snap.body === 'string' && snap.body
|
|
267
|
+
? { ...prev.body, type: prev.body.type === 'none' ? 'json' : prev.body.type, content: snap.body }
|
|
268
|
+
: prev.body,
|
|
269
|
+
}))
|
|
270
|
+
try { localStorage.removeItem(REPLAY_KEY) } catch { /* 隐私模式忽略 */ }
|
|
271
|
+
}, [])
|
|
272
|
+
|
|
273
|
+
const envs = useMemo(() => (Array.isArray(project && project.environments) ? project.environments : []), [project])
|
|
274
|
+
const { table, env } = useMemo(() => varTable(project, envId), [project, envId])
|
|
275
|
+
const varRefs = useMemo(() => collectVarRefs(draft), [draft])
|
|
276
|
+
const missingVars = useMemo(() => varRefs.filter((k) => !(k in table)), [varRefs, table])
|
|
277
|
+
const finalUrl = useMemo(() => joinUrl(env && env.baseUrl, draft.url), [env, draft.url])
|
|
278
|
+
|
|
279
|
+
const patch = (field, value) => setDraft((prev) => ({ ...prev, [field]: value }))
|
|
280
|
+
const patchAuth = (field, value) => setDraft((prev) => ({ ...prev, auth: { ...prev.auth, [field]: value } }))
|
|
281
|
+
// 「+ 常用请求头」:按 COMMON_HEADERS 顺序补 3 条未出现的头,已存在的跳过(函数式更新避免拿到旧 draft)
|
|
282
|
+
const addCommonHeaders = () => setDraft((prev) => ({ ...prev, headers: appendCommonHeaders(prev.headers, 3) }))
|
|
283
|
+
|
|
284
|
+
const send = () => {
|
|
285
|
+
const requestApi = apiRef && apiRef.request
|
|
286
|
+
const cancelled = { cancelled: false }
|
|
287
|
+
abortRef.current = cancelled
|
|
288
|
+
setSending(true)
|
|
289
|
+
setResp(null)
|
|
290
|
+
const payload = {
|
|
291
|
+
method: draft.method,
|
|
292
|
+
url: finalUrl,
|
|
293
|
+
params: draft.params.filter((p) => p && p.enabled !== false && p.key),
|
|
294
|
+
headers: draft.headers.filter((h) => h && h.enabled !== false && h.key),
|
|
295
|
+
body: draft.body,
|
|
296
|
+
auth: draft.auth,
|
|
297
|
+
timeoutMs: timeoutMs > 0 ? timeoutMs : 30000,
|
|
298
|
+
// 前后置脚本与断言:host request.send 已支持这三个字段(bridge 直通 request.js)
|
|
299
|
+
preScript: draft.preScript || undefined,
|
|
300
|
+
postScript: draft.postScript || undefined,
|
|
301
|
+
assertions: Array.isArray(draft.assertions) && draft.assertions.length ? draft.assertions : undefined,
|
|
302
|
+
variables: {
|
|
303
|
+
globals: project && project.globals ? project.globals.variables || [] : [],
|
|
304
|
+
environment: env && Array.isArray(env.variables) ? env.variables : [],
|
|
305
|
+
local: [],
|
|
306
|
+
},
|
|
307
|
+
projectId: project && project.id ? project.id : '',
|
|
308
|
+
}
|
|
309
|
+
if (!requestApi || typeof requestApi.send !== 'function') {
|
|
310
|
+
// 无 api:本地 mock 响应(结构对齐契约 §4 request.send 返回值)
|
|
311
|
+
setResp({
|
|
312
|
+
status: /DELETE|PATCH/.test(draft.method) ? 204 : 200,
|
|
313
|
+
statusText: 'OK (mock)',
|
|
314
|
+
headers: { 'content-type': 'application/json' },
|
|
315
|
+
body: '{\n "code": 0,\n "data": { "mock": true }\n}',
|
|
316
|
+
bodySize: 128,
|
|
317
|
+
timeMs: 42,
|
|
318
|
+
tests: [{ name: 'status 为 2xx', passed: true }],
|
|
319
|
+
logs: [],
|
|
320
|
+
})
|
|
321
|
+
setSending(false)
|
|
322
|
+
return
|
|
323
|
+
}
|
|
324
|
+
// 发送前等待(Apifox「等待时间」):仅在有值时延后,0 不引入额外 tick
|
|
325
|
+
const wait = Number(draft.waitMs) > 0 ? Number(draft.waitMs) : 0
|
|
326
|
+
Promise.resolve()
|
|
327
|
+
.then(() => (wait > 0 ? new Promise((resolve) => setTimeout(resolve, wait)) : undefined))
|
|
328
|
+
.then(() => requestApi.send(payload))
|
|
329
|
+
.then((result) => {
|
|
330
|
+
if (cancelled.cancelled) return
|
|
331
|
+
setResp(result || {})
|
|
332
|
+
setError('')
|
|
333
|
+
})
|
|
334
|
+
.catch((err) => {
|
|
335
|
+
if (cancelled.cancelled) return
|
|
336
|
+
setResp({ status: 0, statusText: '', headers: {}, body: '', bodySize: 0, timeMs: 0,
|
|
337
|
+
error: { message: err && err.message ? err.message : String(err) } })
|
|
338
|
+
})
|
|
339
|
+
.finally(() => { if (!cancelled.cancelled) setSending(false) })
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
const cancel = () => {
|
|
343
|
+
abortRef.current.cancelled = true
|
|
344
|
+
setSending(false)
|
|
345
|
+
setError('已取消本次发送(响应不再回填)')
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
const saveAsEndpoint = () => {
|
|
349
|
+
const storage = apiRef && apiRef.storage
|
|
350
|
+
if (!storage || typeof storage.saveEndpoint !== 'function' || !project || !project.id) {
|
|
351
|
+
setSavedHint('保存失败:storage.saveEndpoint 不可用')
|
|
352
|
+
return
|
|
353
|
+
}
|
|
354
|
+
const endpoint = {
|
|
355
|
+
...draft,
|
|
356
|
+
name: draft.name || draft.method + ' ' + draft.url,
|
|
357
|
+
responses: draft.responses.length ? draft.responses : [],
|
|
358
|
+
}
|
|
359
|
+
Promise.resolve()
|
|
360
|
+
.then(() => storage.saveEndpoint(project.id, endpoint))
|
|
361
|
+
.then((saved) => {
|
|
362
|
+
setSavedHint('已保存:' + ((saved && saved.name) || endpoint.name))
|
|
363
|
+
setDraft((prev) => ({ ...prev, id: (saved && saved.id) || prev.id }))
|
|
364
|
+
})
|
|
365
|
+
.catch((err) => setSavedHint('保存失败:' + (err && err.message ? err.message : String(err))))
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
const responsePanel = (
|
|
369
|
+
<section className="pp-pane pp-debug-response">
|
|
370
|
+
<ResponsePanel resp={resp} sending={sending} onCancel={cancel} />
|
|
371
|
+
</section>
|
|
372
|
+
)
|
|
373
|
+
return (
|
|
374
|
+
<div className="pp-scope pp-page-flush" data-page="debug" ref={rootRef}>
|
|
375
|
+
{/* Apifox 调试页是单栏:接口树就是左侧全局导航,页面内不再自带一份。
|
|
376
|
+
之前自带 .pp-pane 树面板,窄容器下收成单列后变成一张铺满宽度的空白卡。 */}
|
|
377
|
+
<div className="pp-debug-page pp-debug-split">
|
|
378
|
+
<section className="pp-pane pp-debug-request">
|
|
379
|
+
{/* URL 行(Apifox .main-input-bar):40px 高,方法 + baseUrl 胶囊 + path + 发送按钮组 */}
|
|
380
|
+
<div className="pp-url-row">
|
|
381
|
+
{/* 方法徽标:Apifox .url-col-method 在 URL 行首显示彩色方法块 */}
|
|
382
|
+
<span className="pp-method" data-method={draft.method}>{draft.method}</span>
|
|
383
|
+
<select
|
|
384
|
+
className="pp-select pp-url-method"
|
|
385
|
+
value={draft.method}
|
|
386
|
+
onChange={(e) => patch('method', e.target.value)}
|
|
387
|
+
aria-label="请求方法"
|
|
388
|
+
>
|
|
389
|
+
{METHODS.map((m) => <option key={m} value={m}>{m}</option>)}
|
|
390
|
+
</select>
|
|
391
|
+
<span className="pp-url-pill" title={env && env.baseUrl ? env.baseUrl : '未配置 baseUrl'}>
|
|
392
|
+
{env && env.baseUrl ? env.baseUrl : '未配置 baseUrl'}
|
|
393
|
+
</span>
|
|
394
|
+
<input
|
|
395
|
+
className="pp-input pp-input-mono"
|
|
396
|
+
value={draft.url}
|
|
397
|
+
placeholder="/api/v1/... 或 https://TARGET/endpoint,支持 {{var}}"
|
|
398
|
+
onChange={(e) => patch('url', e.target.value)}
|
|
399
|
+
aria-label="请求路径"
|
|
400
|
+
/>
|
|
401
|
+
<span className="pp-send-group">
|
|
402
|
+
<button className="pp-btn pp-btn-primary" onClick={send} disabled={sending || !String(draft.url || '').trim()}
|
|
403
|
+
title={String(draft.url || '').trim() ? '' : '先填写请求路径'}>
|
|
404
|
+
{sending ? '发送中…' : '发送'}
|
|
405
|
+
</button>
|
|
406
|
+
<button className="pp-btn" onClick={cancel} disabled={!sending} title="取消本次发送">✕</button>
|
|
407
|
+
</span>
|
|
408
|
+
</div>
|
|
409
|
+
|
|
410
|
+
{/* 环境 + 实际地址 + 变量:压成一行元信息,不再各占一行 */}
|
|
411
|
+
<div className="pp-debug-meta">
|
|
412
|
+
<select className="pp-select pp-input-sm" value={envId} onChange={(e) => setEnvId(e.target.value)}
|
|
413
|
+
aria-label="环境">
|
|
414
|
+
{envs.length === 0 ? <option value="">(无环境)</option> : null}
|
|
415
|
+
{envs.map((e) => <option key={e.id} value={e.id}>{e.name}</option>)}
|
|
416
|
+
</select>
|
|
417
|
+
<span className="pp-mono pp-meta pp-ellipsis" title={finalUrl || ''}>
|
|
418
|
+
{finalUrl || '(未填写 URL)'}
|
|
419
|
+
</span>
|
|
420
|
+
{varRefs.length > 0 ? (
|
|
421
|
+
<span className="pp-meta">
|
|
422
|
+
变量 {varRefs.join('、')}
|
|
423
|
+
{missingVars.length > 0 ? '(未定义:' + missingVars.join('、') + ')' : '(均已定义)'}
|
|
424
|
+
</span>
|
|
425
|
+
) : null}
|
|
426
|
+
<a className="pp-meta" href="#/history">请求历史</a>
|
|
427
|
+
</div>
|
|
428
|
+
{/* 空项目引导:真实项目还没有接口时,URL 为空、发送无意义,明确指引去哪里建 */}
|
|
429
|
+
{!isMock && !String(draft.url || '').trim() ? (
|
|
430
|
+
<p className="pp-note">
|
|
431
|
+
当前项目还没有接口,先到 <a href="#/doc">接口文档</a> 或 <a href="#/overview">总览</a> 新建一个,也可以直接在上方填写完整 URL 调试。
|
|
432
|
+
</p>
|
|
433
|
+
) : null}
|
|
434
|
+
{isMock ? <p className="pp-note pp-note-warn">示例数据 — window.__postapiApi 未注入或读取失败,发送将返回 mock 响应</p> : null}
|
|
435
|
+
{error ? <p className="pp-note pp-note-bad">{error}</p> : null}
|
|
436
|
+
|
|
437
|
+
<div className="pp-tabs">
|
|
438
|
+
{TABS.map((t) => (
|
|
439
|
+
<button key={t.id} className="pp-tab" data-active={tab === t.id} onClick={() => setTab(t.id)}>{t.label}</button>
|
|
440
|
+
))}
|
|
441
|
+
</div>
|
|
442
|
+
|
|
443
|
+
<div className="pp-debug-tabbody">
|
|
444
|
+
{tab === 'params' ? (
|
|
445
|
+
<div>
|
|
446
|
+
<div className="pp-group-title">Path 参数</div>
|
|
447
|
+
<div className="pp-table-wrap"><ParamTable rows={draft.pathParams} onChange={(rows) => patch('pathParams', rows)} emptyText="无 Path 参数" /></div>
|
|
448
|
+
<div className="pp-group-title">Query 参数</div>
|
|
449
|
+
<div className="pp-table-wrap"><ParamTable rows={draft.params} onChange={(rows) => patch('params', rows)} emptyText="无 Query 参数" /></div>
|
|
450
|
+
</div>
|
|
451
|
+
) : null}
|
|
452
|
+
{tab === 'headers' ? (
|
|
453
|
+
<div>
|
|
454
|
+
{/* 分组标题行:标题靠左、动作靠右(.pp-group-head 布局类见 design-tokens.css) */}
|
|
455
|
+
<div className="pp-group-head">
|
|
456
|
+
{/* 右对齐由 .pp-group-head 的 space-between 与 > .pp-group-title 的 margin-right:auto 负责 */}
|
|
457
|
+
<span className="pp-group-title">请求头</span>
|
|
458
|
+
<button className="pp-btn pp-btn-link" onClick={addCommonHeaders} title="追加 3 条常用请求头">+ 常用请求头</button>
|
|
459
|
+
</div>
|
|
460
|
+
<div className="pp-table-wrap">
|
|
461
|
+
<ParamTable
|
|
462
|
+
rows={draft.headers}
|
|
463
|
+
onChange={(rows) => patch('headers', rows)}
|
|
464
|
+
suggest={filterHeaders}
|
|
465
|
+
emptyText="无请求头"
|
|
466
|
+
/>
|
|
467
|
+
</div>
|
|
468
|
+
<div className="pp-group-title">Cookies</div>
|
|
469
|
+
<div className="pp-table-wrap"><ParamTable rows={draft.cookies} onChange={(rows) => patch('cookies', rows)} emptyText="无 Cookies" /></div>
|
|
470
|
+
</div>
|
|
471
|
+
) : null}
|
|
472
|
+
{tab === 'body' ? (
|
|
473
|
+
<BodyEditor value={draft.body} onChange={(next) => patch('body', next)} />
|
|
474
|
+
) : null}
|
|
475
|
+
{tab === 'script' ? (
|
|
476
|
+
<ScriptPanel
|
|
477
|
+
value={{ preScript: draft.preScript, postScript: draft.postScript, assertions: draft.assertions, waitMs: draft.waitMs }}
|
|
478
|
+
onChange={(next) => setDraft((prev) => ({ ...prev, ...next }))}
|
|
479
|
+
/>
|
|
480
|
+
) : null}
|
|
481
|
+
{tab === 'settings' ? (
|
|
482
|
+
<div>
|
|
483
|
+
<div className="pp-group-title">发送设置</div>
|
|
484
|
+
<div className="pp-form-row">
|
|
485
|
+
<label className="pp-form-label">超时 (ms)</label>
|
|
486
|
+
<input className="pp-input pp-input-sm" style={{ flex: 1, maxWidth: 160 }} value={String(timeoutMs)}
|
|
487
|
+
onChange={(e) => setTimeoutMs(Number(e.target.value.replace(/[^0-9]/g, '')) || 0)} />
|
|
488
|
+
</div>
|
|
489
|
+
<div className="pp-form-row">
|
|
490
|
+
<label className="pp-form-label">等待 (ms)</label>
|
|
491
|
+
<input className="pp-input pp-input-sm" style={{ flex: 1, maxWidth: 160 }} value={String(draft.waitMs || 0)}
|
|
492
|
+
onChange={(e) => patch('waitMs', Number(e.target.value.replace(/[^0-9]/g, '')) || 0)} />
|
|
493
|
+
</div>
|
|
494
|
+
<div className="pp-group-title">请求概览</div>
|
|
495
|
+
<table className="pp-table">
|
|
496
|
+
<tbody>
|
|
497
|
+
<tr><td className="pp-meta">方法</td><td><span className="pp-method" data-method={draft.method}>{draft.method}</span></td></tr>
|
|
498
|
+
<tr><td className="pp-meta">完整 URL</td><td className="pp-mono pp-ellipsis">{finalUrl || '(未填写)'}</td></tr>
|
|
499
|
+
<tr><td className="pp-meta">环境</td><td>{env ? env.name : '(无环境)'}</td></tr>
|
|
500
|
+
<tr><td className="pp-meta">变量</td><td>{varRefs.length ? varRefs.join('、') + (missingVars.length ? '(未定义:' + missingVars.join('、') + ')' : '(均已定义)') : '无'}</td></tr>
|
|
501
|
+
<tr><td className="pp-meta">已启用参数</td><td>{draft.params.filter((p) => p && p.enabled !== false && p.key).length} 个 Query / {draft.pathParams.filter((p) => p && p.enabled !== false && p.key).length} 个 Path</td></tr>
|
|
502
|
+
<tr><td className="pp-meta">已启用请求头</td><td>{draft.headers.filter((h) => h && h.enabled !== false && h.key).length} 个</td></tr>
|
|
503
|
+
</tbody>
|
|
504
|
+
</table>
|
|
505
|
+
</div>
|
|
506
|
+
) : null}
|
|
507
|
+
{tab === 'auth' ? (
|
|
508
|
+
<div>
|
|
509
|
+
<div className="pp-form-row">
|
|
510
|
+
<label className="pp-form-label">类型</label>
|
|
511
|
+
<select className="pp-select" value={draft.auth.type} onChange={(e) => patchAuth('type', e.target.value)}>
|
|
512
|
+
{AUTH_TYPES.map((t) => <option key={t} value={t}>{AUTH_LABEL[t] || t}</option>)}
|
|
513
|
+
</select>
|
|
514
|
+
</div>
|
|
515
|
+
{draft.auth.type === 'bearer' ? (
|
|
516
|
+
<div className="pp-form-row">
|
|
517
|
+
<label className="pp-form-label">Token</label>
|
|
518
|
+
<input className="pp-input pp-input-mono" style={{ flex: 1 }} value={draft.auth.token} onChange={(e) => patchAuth('token', e.target.value)} />
|
|
519
|
+
</div>
|
|
520
|
+
) : null}
|
|
521
|
+
{draft.auth.type === 'basic' ? (
|
|
522
|
+
<div>
|
|
523
|
+
<div className="pp-form-row">
|
|
524
|
+
<label className="pp-form-label">用户名</label>
|
|
525
|
+
<input className="pp-input pp-input-mono" style={{ flex: 1 }} value={draft.auth.username} onChange={(e) => patchAuth('username', e.target.value)} />
|
|
526
|
+
</div>
|
|
527
|
+
<div className="pp-form-row">
|
|
528
|
+
<label className="pp-form-label">密码</label>
|
|
529
|
+
<input className="pp-input pp-input-mono" style={{ flex: 1 }} value={draft.auth.password} onChange={(e) => patchAuth('password', e.target.value)} />
|
|
530
|
+
</div>
|
|
531
|
+
</div>
|
|
532
|
+
) : null}
|
|
533
|
+
{draft.auth.type === 'apikey' ? (
|
|
534
|
+
<div>
|
|
535
|
+
<div className="pp-form-row">
|
|
536
|
+
<label className="pp-form-label">Key</label>
|
|
537
|
+
<input className="pp-input pp-input-mono" style={{ flex: 1 }} value={draft.auth.key} onChange={(e) => patchAuth('key', e.target.value)} />
|
|
538
|
+
</div>
|
|
539
|
+
<div className="pp-form-row">
|
|
540
|
+
<label className="pp-form-label">Value</label>
|
|
541
|
+
<input className="pp-input pp-input-mono" style={{ flex: 1 }} value={draft.auth.value} onChange={(e) => patchAuth('value', e.target.value)} />
|
|
542
|
+
</div>
|
|
543
|
+
<div className="pp-form-row">
|
|
544
|
+
<label className="pp-form-label">位置</label>
|
|
545
|
+
<select className="pp-select" value={draft.auth.addTo} onChange={(e) => patchAuth('addTo', e.target.value)}>
|
|
546
|
+
<option value="header">header</option>
|
|
547
|
+
<option value="query">query</option>
|
|
548
|
+
</select>
|
|
549
|
+
</div>
|
|
550
|
+
</div>
|
|
551
|
+
) : null}
|
|
552
|
+
{draft.auth.type === 'none' ? <p className="pp-meta">无认证</p> : null}
|
|
553
|
+
</div>
|
|
554
|
+
) : null}
|
|
555
|
+
</div>
|
|
556
|
+
|
|
557
|
+
<div className="pp-toolbar-inline" style={{ marginTop: 'var(--sp-3)' }}>
|
|
558
|
+
<input
|
|
559
|
+
className="pp-input"
|
|
560
|
+
style={{ flex: 1 }}
|
|
561
|
+
placeholder="接口名称"
|
|
562
|
+
value={draft.name}
|
|
563
|
+
onChange={(e) => patch('name', e.target.value)}
|
|
564
|
+
/>
|
|
565
|
+
<button className="pp-btn" onClick={saveAsEndpoint}>保存为接口</button>
|
|
566
|
+
{savedHint ? (
|
|
567
|
+
<span className={'pp-note ' + (savedHint.indexOf('已保存') === 0 ? 'pp-note-ok' : 'pp-note-bad')}>{savedHint}</span>
|
|
568
|
+
) : null}
|
|
569
|
+
</div>
|
|
570
|
+
</section>
|
|
571
|
+
|
|
572
|
+
{responsePanel}
|
|
573
|
+
</div>
|
|
574
|
+
</div>
|
|
575
|
+
)
|
|
576
|
+
}
|