dsh-postapi 0.0.0-stage → 0.0.2
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,316 @@
|
|
|
1
|
+
// mock.jsx — Mock 规则与服务页(#/mock)
|
|
2
|
+
// Mock 规则表(字段/规则/值/启用)+ 延迟设置 + 服务启停(api.mock.start/stop/status)+ 命中地址展示。
|
|
3
|
+
// 数据走 window.__postapiApi.storage(getProject / saveEndpoint 写回 endpoint.mock),
|
|
4
|
+
// 服务走 window.__postapiApi.mock;api 缺失时渲染文件内 mock 并可见标注「示例数据」。
|
|
5
|
+
import { useEffect, useMemo, useRef, useState } from 'react'
|
|
6
|
+
import { applyTheme } from './theme.js'
|
|
7
|
+
import { EmptyState } from './components/EmptyState.jsx'
|
|
8
|
+
|
|
9
|
+
;(window.__postapiQueue = window.__postapiQueue || []).push(['mock', MockPage]) // 注册表就绪前入队,ensureGlobals 统一重放(模块求值顺序无关)
|
|
10
|
+
|
|
11
|
+
const RULES = ['fixed', 'regex', 'increment', 'random', 'template']
|
|
12
|
+
const RULE_LABEL = { fixed: '固定值', regex: '正则生成', increment: '自增', random: '随机', template: '模板' }
|
|
13
|
+
const RULE_COLUMNS = ['enabled', 'field', 'rule', 'value']
|
|
14
|
+
|
|
15
|
+
// —— mock 项目(契约 §2:endpoint.mock = { enabled, delayMs, rules[] })——
|
|
16
|
+
const MOCK_PROJECT = {
|
|
17
|
+
id: 'proj_demo01',
|
|
18
|
+
name: '订单中心(示例数据)',
|
|
19
|
+
endpoints: [
|
|
20
|
+
{
|
|
21
|
+
id: 'ep_1001', name: '查询订单列表', method: 'GET', url: '/api/v1/orders',
|
|
22
|
+
mock: {
|
|
23
|
+
enabled: true,
|
|
24
|
+
delayMs: 120,
|
|
25
|
+
rules: [
|
|
26
|
+
{ id: 'mk_01', endpointId: 'ep_1001', field: 'data.total', rule: 'fixed', value: '42', enabled: true },
|
|
27
|
+
{ id: 'mk_02', endpointId: 'ep_1001', field: 'data.items[0].orderId', rule: 'template', value: 'ORD-{{$randomInt}}', enabled: true },
|
|
28
|
+
{ id: 'mk_03', endpointId: 'ep_1001', field: 'data.items[0].status', rule: 'fixed', value: 'PAID', enabled: false },
|
|
29
|
+
],
|
|
30
|
+
},
|
|
31
|
+
responses: [{ id: 'res_a', name: '成功', code: 200, contentType: 'application/json', body: '{}', headers: [], enabled: true, isDefault: true }],
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
id: 'ep_1002', name: '创建订单', method: 'POST', url: '/api/v1/orders',
|
|
35
|
+
mock: { enabled: false, delayMs: 0, rules: [] },
|
|
36
|
+
responses: [{ id: 'res_b', name: '创建成功', code: 201, contentType: 'application/json', body: '{}', headers: [], enabled: true, isDefault: true }],
|
|
37
|
+
},
|
|
38
|
+
],
|
|
39
|
+
mocks: [],
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function resolveApi() {
|
|
43
|
+
return typeof window !== 'undefined' ? window.__postapiApi : null
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function blankRule(endpointId) {
|
|
47
|
+
return { id: '', endpointId: endpointId || '', field: '', rule: 'fixed', value: '', enabled: true }
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** 命中地址:契约 §4 两种形态(/mock/<projectId>/<endpointId> 与 /mock/<projectId><endpointPath>)。 */
|
|
51
|
+
export function hitUrls(projectId, endpoint, port) {
|
|
52
|
+
const base = 'http://127.0.0.1:' + (port || 4010)
|
|
53
|
+
if (!projectId || !endpoint) return []
|
|
54
|
+
return [
|
|
55
|
+
base + '/mock/' + projectId + '/' + endpoint.id,
|
|
56
|
+
base + '/mock/' + projectId + (endpoint.url || ''),
|
|
57
|
+
]
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function MockPage({ route, api }) {
|
|
61
|
+
const rootRef = useRef(null)
|
|
62
|
+
useEffect(() => applyTheme(rootRef.current), [])
|
|
63
|
+
const apiRef = resolveApi() || api
|
|
64
|
+
|
|
65
|
+
const [project, setProject] = useState(MOCK_PROJECT)
|
|
66
|
+
const [isMock, setIsMock] = useState(true)
|
|
67
|
+
const [error, setError] = useState('')
|
|
68
|
+
const [notice, setNotice] = useState('')
|
|
69
|
+
const [currentId, setCurrentId] = useState(MOCK_PROJECT.endpoints[0].id)
|
|
70
|
+
const [delayMs, setDelayMs] = useState(MOCK_PROJECT.endpoints[0].mock.delayMs)
|
|
71
|
+
const [rules, setRules] = useState(MOCK_PROJECT.endpoints[0].mock.rules)
|
|
72
|
+
const [mockEnabled, setMockEnabled] = useState(MOCK_PROJECT.endpoints[0].mock.enabled)
|
|
73
|
+
const [service, setService] = useState({ running: false, port: 4010 })
|
|
74
|
+
const [busy, setBusy] = useState(false)
|
|
75
|
+
|
|
76
|
+
const endpoints = useMemo(() => (Array.isArray(project && project.endpoints) ? project.endpoints : []), [project])
|
|
77
|
+
const current = useMemo(() => endpoints.find((e) => e.id === currentId) || endpoints[0] || null, [endpoints, currentId])
|
|
78
|
+
const port = (project && project.settings && project.settings.mockPort) || 4010
|
|
79
|
+
|
|
80
|
+
// 载入项目 + Mock 服务状态
|
|
81
|
+
useEffect(() => {
|
|
82
|
+
const storage = apiRef && apiRef.storage
|
|
83
|
+
let cancelled = false
|
|
84
|
+
if (storage && typeof storage.listProjects === 'function' && typeof storage.getProject === 'function') {
|
|
85
|
+
Promise.resolve()
|
|
86
|
+
.then(() => storage.listProjects())
|
|
87
|
+
.then((list) => {
|
|
88
|
+
const first = Array.isArray(list) && list.length ? list[0] : null
|
|
89
|
+
return first ? storage.getProject(first.id) : null
|
|
90
|
+
})
|
|
91
|
+
.then((data) => {
|
|
92
|
+
if (cancelled || !data) return
|
|
93
|
+
setProject(data)
|
|
94
|
+
setIsMock(false)
|
|
95
|
+
const list = Array.isArray(data.endpoints) ? data.endpoints : []
|
|
96
|
+
const first = list[0] || null
|
|
97
|
+
setCurrentId(first ? first.id : '')
|
|
98
|
+
setRules(first && first.mock && Array.isArray(first.mock.rules) ? first.mock.rules : [])
|
|
99
|
+
setDelayMs(first && first.mock ? first.mock.delayMs || 0 : 0)
|
|
100
|
+
setMockEnabled(Boolean(first && first.mock && first.mock.enabled))
|
|
101
|
+
})
|
|
102
|
+
.catch((err) => {
|
|
103
|
+
if (cancelled) return
|
|
104
|
+
setError('读取项目失败:' + (err && err.message ? err.message : String(err)))
|
|
105
|
+
})
|
|
106
|
+
}
|
|
107
|
+
const mockApi = apiRef && apiRef.mock
|
|
108
|
+
if (mockApi && typeof mockApi.status === 'function') {
|
|
109
|
+
Promise.resolve()
|
|
110
|
+
.then(() => mockApi.status())
|
|
111
|
+
.then((st) => {
|
|
112
|
+
if (cancelled || !st) return
|
|
113
|
+
setService({ running: Boolean(st.running), port: st.port || port })
|
|
114
|
+
})
|
|
115
|
+
.catch(() => { /* 服务未启动时保持默认 */ })
|
|
116
|
+
}
|
|
117
|
+
return () => { cancelled = true }
|
|
118
|
+
}, [apiRef])
|
|
119
|
+
|
|
120
|
+
const selectEndpoint = (ep) => {
|
|
121
|
+
setCurrentId(ep.id)
|
|
122
|
+
setRules(ep.mock && Array.isArray(ep.mock.rules) ? ep.mock.rules.map((r) => ({ ...r })) : [])
|
|
123
|
+
setDelayMs(ep.mock ? ep.mock.delayMs || 0 : 0)
|
|
124
|
+
setMockEnabled(Boolean(ep.mock && ep.mock.enabled))
|
|
125
|
+
setNotice('')
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const persist = () => {
|
|
129
|
+
const storage = apiRef && apiRef.storage
|
|
130
|
+
if (!storage || typeof storage.saveEndpoint !== 'function' || !project || !project.id || !current) {
|
|
131
|
+
setNotice('保存失败:storage.saveEndpoint 不可用')
|
|
132
|
+
return
|
|
133
|
+
}
|
|
134
|
+
const endpoint = {
|
|
135
|
+
...current,
|
|
136
|
+
mock: { enabled: mockEnabled, delayMs: Number(delayMs) || 0, rules },
|
|
137
|
+
}
|
|
138
|
+
setBusy(true)
|
|
139
|
+
Promise.resolve()
|
|
140
|
+
.then(() => storage.saveEndpoint(project.id, endpoint))
|
|
141
|
+
.then(() => {
|
|
142
|
+
setProject((prev) => ({
|
|
143
|
+
...prev,
|
|
144
|
+
endpoints: (prev.endpoints || []).map((e) => (e.id === endpoint.id ? { ...e, mock: endpoint.mock } : e)),
|
|
145
|
+
}))
|
|
146
|
+
setNotice('已保存 Mock 配置')
|
|
147
|
+
setError('')
|
|
148
|
+
})
|
|
149
|
+
.catch((err) => setNotice('保存失败:' + (err && err.message ? err.message : String(err))))
|
|
150
|
+
.finally(() => setBusy(false))
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const startService = () => {
|
|
154
|
+
const mockApi = apiRef && apiRef.mock
|
|
155
|
+
if (!mockApi || typeof mockApi.start !== 'function') {
|
|
156
|
+
setNotice('启动失败:api.mock.start 不可用')
|
|
157
|
+
return
|
|
158
|
+
}
|
|
159
|
+
setBusy(true)
|
|
160
|
+
Promise.resolve()
|
|
161
|
+
.then(() => mockApi.start({ projectId: project && project.id, port }))
|
|
162
|
+
.then((st) => {
|
|
163
|
+
setService({ running: true, port: (st && st.port) || port })
|
|
164
|
+
setNotice('Mock 服务已启动')
|
|
165
|
+
})
|
|
166
|
+
.catch((err) => setNotice('启动失败:' + (err && err.message ? err.message : String(err))))
|
|
167
|
+
.finally(() => setBusy(false))
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const stopService = () => {
|
|
171
|
+
const mockApi = apiRef && apiRef.mock
|
|
172
|
+
if (!mockApi || typeof mockApi.stop !== 'function') {
|
|
173
|
+
setNotice('停止失败:api.mock.stop 不可用')
|
|
174
|
+
return
|
|
175
|
+
}
|
|
176
|
+
setBusy(true)
|
|
177
|
+
Promise.resolve()
|
|
178
|
+
.then(() => mockApi.stop())
|
|
179
|
+
.then(() => {
|
|
180
|
+
setService({ running: false, port })
|
|
181
|
+
setNotice('Mock 服务已停止')
|
|
182
|
+
})
|
|
183
|
+
.catch((err) => setNotice('停止失败:' + (err && err.message ? err.message : String(err))))
|
|
184
|
+
.finally(() => setBusy(false))
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const patchRule = (index, field, value) => {
|
|
188
|
+
const next = rules.slice()
|
|
189
|
+
next[index] = { ...next[index], [field]: value }
|
|
190
|
+
setRules(next)
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const urls = useMemo(() => hitUrls(project && project.id, current, service.port || port), [project, current, service.port, port])
|
|
194
|
+
|
|
195
|
+
return (
|
|
196
|
+
<div className="pp-scope pp-page" data-page="mock" ref={rootRef}>
|
|
197
|
+
<div className="pp-overview-head">
|
|
198
|
+
<span className="pp-overview-title">Mock</span>
|
|
199
|
+
<div className="pp-overview-actions">
|
|
200
|
+
<span className="pp-pill">{service.running ? '运行中' : '已停止'}</span>
|
|
201
|
+
<button className="pp-btn pp-btn-primary" onClick={startService} disabled={busy || service.running}>启动</button>
|
|
202
|
+
<button className="pp-btn" onClick={stopService} disabled={busy || !service.running}>停止</button>
|
|
203
|
+
</div>
|
|
204
|
+
</div>
|
|
205
|
+
<div className="pp-overview-content">
|
|
206
|
+
<p className="pp-muted">
|
|
207
|
+
{isMock ? '示例数据 — window.__postapiApi 未注入或读取失败,以下为文件内 mock' : '数据来源:postapi.storage + postapi.mock'}
|
|
208
|
+
</p>
|
|
209
|
+
{error ? <p className="pp-muted">{error}</p> : null}
|
|
210
|
+
{notice ? <p className="pp-muted">{notice}</p> : null}
|
|
211
|
+
|
|
212
|
+
{/* 左:接口列表(与文档页目录树同规格)+ 右:配置纵向两卡。
|
|
213
|
+
原来是 3 个子项塞进 2 列网格,第三块被挤到第二行左侧,与右侧形成高度差。 */}
|
|
214
|
+
<div className="pp-mock-layout">
|
|
215
|
+
<nav className="pp-panel" aria-label="接口列表">
|
|
216
|
+
<div className="pp-tree">
|
|
217
|
+
<div className="pp-tree-group">接口</div>
|
|
218
|
+
{endpoints.length === 0 ? <EmptyState text="暂无接口" hint="在总览页新建项目后创建接口" /> : null}
|
|
219
|
+
{endpoints.map((ep) => (
|
|
220
|
+
<div key={ep.id} className="pp-tree-row" data-active={ep.id === currentId} onClick={() => selectEndpoint(ep)}>
|
|
221
|
+
<span className="pp-method" data-method={ep.method}>{ep.method}</span>
|
|
222
|
+
<span className="pp-ellipsis">{ep.name}</span>
|
|
223
|
+
</div>
|
|
224
|
+
))}
|
|
225
|
+
</div>
|
|
226
|
+
</nav>
|
|
227
|
+
<div className="pp-mock-side">
|
|
228
|
+
|
|
229
|
+
{/* 上:Mock 配置(启用 / 延迟 / 字段规则) */}
|
|
230
|
+
<section className="pp-card">
|
|
231
|
+
<div className="pp-card-title">{current ? current.name : '未选择接口'}</div>
|
|
232
|
+
<div className="pp-form-grid" style={{ marginBottom: 'var(--sp-3)' }}>
|
|
233
|
+
<label className="pp-form-label">启用 Mock</label>
|
|
234
|
+
<input type="checkbox" checked={mockEnabled} onChange={(e) => setMockEnabled(e.target.checked)} />
|
|
235
|
+
<label className="pp-form-label">延迟 (ms)</label>
|
|
236
|
+
<input
|
|
237
|
+
className="pp-input pp-input-mono"
|
|
238
|
+
value={String(delayMs)}
|
|
239
|
+
onChange={(e) => setDelayMs(e.target.value.replace(/[^0-9]/g, ''))}
|
|
240
|
+
/>
|
|
241
|
+
</div>
|
|
242
|
+
|
|
243
|
+
<div className="pp-card-title" style={{ marginTop: 'var(--sp-3)' }}>字段规则</div>
|
|
244
|
+
{rules.length === 0 ? <p className="pp-meta">尚无规则;未命中规则时回落默认响应</p> : null}
|
|
245
|
+
{rules.length > 0 ? (
|
|
246
|
+
<div className="pp-table-wrap">
|
|
247
|
+
<table className="pp-table">
|
|
248
|
+
<thead>
|
|
249
|
+
<tr>
|
|
250
|
+
<th style={{ width: '56px' }}>启用</th>
|
|
251
|
+
<th style={{ width: '180px' }}>字段</th>
|
|
252
|
+
<th style={{ width: '110px' }}>规则</th>
|
|
253
|
+
<th>值</th>
|
|
254
|
+
<th style={{ width: '64px' }}>操作</th>
|
|
255
|
+
</tr>
|
|
256
|
+
</thead>
|
|
257
|
+
<tbody>
|
|
258
|
+
{rules.map((rule, index) => (
|
|
259
|
+
<tr key={index}>
|
|
260
|
+
<td><input type="checkbox" checked={rule.enabled !== false} onChange={(e) => patchRule(index, 'enabled', e.target.checked)} /></td>
|
|
261
|
+
<td>
|
|
262
|
+
<input className="pp-input pp-input-mono" style={{ width: '100%' }} value={rule.field || ''}
|
|
263
|
+
placeholder="data.items[0].id" onChange={(e) => patchRule(index, 'field', e.target.value)} />
|
|
264
|
+
</td>
|
|
265
|
+
<td>
|
|
266
|
+
<select className="pp-select" value={rule.rule || 'fixed'} onChange={(e) => patchRule(index, 'rule', e.target.value)}>
|
|
267
|
+
{RULES.map((r) => <option key={r} value={r}>{RULE_LABEL[r] || r}</option>)}
|
|
268
|
+
</select>
|
|
269
|
+
</td>
|
|
270
|
+
<td>
|
|
271
|
+
<input className="pp-input pp-input-mono" style={{ width: '100%' }} value={rule.value || ''}
|
|
272
|
+
onChange={(e) => patchRule(index, 'value', e.target.value)} />
|
|
273
|
+
</td>
|
|
274
|
+
<td>
|
|
275
|
+
<button className="pp-btn" onClick={() => setRules(rules.filter((_, i) => i !== index))}>删除</button>
|
|
276
|
+
</td>
|
|
277
|
+
</tr>
|
|
278
|
+
))}
|
|
279
|
+
</tbody>
|
|
280
|
+
</table>
|
|
281
|
+
</div>
|
|
282
|
+
) : null}
|
|
283
|
+
<div style={{ display: 'flex', gap: 'var(--sp-2)', marginTop: 'var(--sp-2)' }}>
|
|
284
|
+
<button className="pp-btn" onClick={() => setRules(rules.concat([blankRule(current && current.id)]))}>+ 添加规则</button>
|
|
285
|
+
<button className="pp-btn pp-btn-primary" onClick={persist} disabled={busy}>保存配置</button>
|
|
286
|
+
</div>
|
|
287
|
+
</section>
|
|
288
|
+
|
|
289
|
+
{/* 下:本地 Mock 服务(端口 / 命中地址 / 规则优先级) */}
|
|
290
|
+
<section className="pp-card">
|
|
291
|
+
<div className="pp-card-title">本地 Mock 服务</div>
|
|
292
|
+
<div className="pp-form-grid">
|
|
293
|
+
<label className="pp-form-label">端口</label>
|
|
294
|
+
<span className="pp-mono">{service.port || port}</span>
|
|
295
|
+
<label className="pp-form-label">状态</label>
|
|
296
|
+
<span className="pp-meta">{service.running ? '运行中' : '已停止'}</span>
|
|
297
|
+
</div>
|
|
298
|
+
|
|
299
|
+
<div className="pp-card-title" style={{ marginTop: 'var(--sp-3)' }}>命中地址</div>
|
|
300
|
+
{urls.length === 0 ? (
|
|
301
|
+
<p className="pp-meta">选择接口后显示命中地址</p>
|
|
302
|
+
) : (
|
|
303
|
+
urls.map((u) => <pre key={u} className="pp-pre" style={{ marginBottom: 'var(--sp-1)' }}>{u}</pre>)
|
|
304
|
+
)}
|
|
305
|
+
|
|
306
|
+
<p className="pp-meta" style={{ marginTop: 'var(--sp-2)' }}>
|
|
307
|
+
规则优先级:字段规则 → 默认响应(isDefault)→ 200 {'{}'}
|
|
308
|
+
</p>
|
|
309
|
+
</section>
|
|
310
|
+
</div>
|
|
311
|
+
</div>
|
|
312
|
+
|
|
313
|
+
</div>
|
|
314
|
+
</div>
|
|
315
|
+
)
|
|
316
|
+
}
|
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
// overview.jsx — 项目总览页(Apifox 工作台:统计条 + 项目卡片网格 + 最近接口表 + 快捷入口)
|
|
2
|
+
// 自注册:模块求值期入队 __postapiQueue,ensureGlobals 统一重放(注册表就绪顺序无关)
|
|
3
|
+
// 数据:api.storage.listProjects / getProject / createProject / renameProject / deleteProject
|
|
4
|
+
// 失败一律页面内显示错误文案(不弹窗);api 缺失时渲染文件内 mock 并标注「示例数据」
|
|
5
|
+
// 视觉基线:docs/apifox-design-spec.md —— .pp-stats / .pp-overview-grid / .pp-card-hover
|
|
6
|
+
import React, { useEffect, useRef, useState } from 'react'
|
|
7
|
+
import { applyTheme } from './theme.js'
|
|
8
|
+
import { EmptyState } from './components/EmptyState.jsx'
|
|
9
|
+
|
|
10
|
+
;(window.__postapiQueue = window.__postapiQueue || []).push(['overview', OverviewPage]) // 注册表就绪前入队
|
|
11
|
+
|
|
12
|
+
// —— mock(契约 §2 project.json 结构子集)——
|
|
13
|
+
const MOCK_PROJECTS = [
|
|
14
|
+
{ id: 'proj_demo01', name: '订单中心', updatedAt: '2026-11-17T10:20:00.000Z' },
|
|
15
|
+
{ id: 'proj_demo02', name: '用户服务', updatedAt: '2026-11-16T18:40:00.000Z' },
|
|
16
|
+
]
|
|
17
|
+
const MOCK_PROJECT = {
|
|
18
|
+
id: 'proj_demo01',
|
|
19
|
+
name: '订单中心',
|
|
20
|
+
environments: [{ id: 'env_dev', name: '开发环境' }, { id: 'env_prod', name: '生产环境' }],
|
|
21
|
+
endpoints: [
|
|
22
|
+
{ id: 'ep_1001', name: '查询订单列表', method: 'GET', url: '/api/v1/orders', updatedAt: '2026-11-17T10:20:00.000Z' },
|
|
23
|
+
{ id: 'ep_1002', name: '创建订单', method: 'POST', url: '/api/v1/orders', updatedAt: '2026-11-17T09:05:00.000Z' },
|
|
24
|
+
{ id: 'ep_1003', name: '取消订单', method: 'DELETE', url: '/api/v1/orders/{orderId}', updatedAt: '2026-11-16T21:30:00.000Z' },
|
|
25
|
+
],
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const RECENT_LIMIT = 5
|
|
29
|
+
|
|
30
|
+
/** 时间格式化为 yyyy-MM-dd HH:mm(无值回落 -)。 */
|
|
31
|
+
export function fmtTime(iso) {
|
|
32
|
+
if (!iso) return '-'
|
|
33
|
+
return String(iso).slice(0, 16).replace('T', ' ')
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** 项目卡片数据:补接口计数(project 详情缺失时为 undefined)。 */
|
|
37
|
+
export function projectCards(projects) {
|
|
38
|
+
return (Array.isArray(projects) ? projects : []).map((p) => ({
|
|
39
|
+
id: p.id,
|
|
40
|
+
name: p.name || p.id,
|
|
41
|
+
updatedAt: p.updatedAt || '',
|
|
42
|
+
endpointCount: Array.isArray(p.endpoints) ? p.endpoints.length : p.endpointCount,
|
|
43
|
+
}))
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** 最近接口:按 updatedAt 降序取前 limit 条(缺 updatedAt 的排在后面)。 */
|
|
47
|
+
export function recentEndpoints(project, limit = RECENT_LIMIT) {
|
|
48
|
+
const list = Array.isArray(project && project.endpoints) ? project.endpoints.slice() : []
|
|
49
|
+
list.sort((a, b) => String(b.updatedAt || '').localeCompare(String(a.updatedAt || '')))
|
|
50
|
+
return list.slice(0, limit)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** 顶部统计条四块:项目数 / 接口数 / 环境数 / 最近更新时间(取项目与接口的最晚时间)。 */
|
|
54
|
+
export function overviewStats(projects, project) {
|
|
55
|
+
const list = Array.isArray(projects) ? projects : []
|
|
56
|
+
const endpoints = Array.isArray(project && project.endpoints) ? project.endpoints : []
|
|
57
|
+
const envs = Array.isArray(project && project.environments) ? project.environments : []
|
|
58
|
+
let latest = ''
|
|
59
|
+
for (const item of endpoints.concat(list)) {
|
|
60
|
+
const at = item && item.updatedAt ? String(item.updatedAt) : ''
|
|
61
|
+
if (at && at > latest) latest = at
|
|
62
|
+
}
|
|
63
|
+
return {
|
|
64
|
+
projectCount: list.length,
|
|
65
|
+
endpointCount: endpoints.length,
|
|
66
|
+
envCount: envs.length,
|
|
67
|
+
latestUpdated: latest,
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function errText(err) {
|
|
72
|
+
return err && err.message ? err.message : String(err)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const SHORTCUTS = [
|
|
76
|
+
{ hash: '#/debug', label: '调试发送' },
|
|
77
|
+
{ hash: '#/doc', label: '接口文档' },
|
|
78
|
+
{ hash: '#/env', label: '环境与变量' },
|
|
79
|
+
{ hash: '#/test', label: '自动化测试' },
|
|
80
|
+
]
|
|
81
|
+
|
|
82
|
+
export function OverviewPage(props) {
|
|
83
|
+
const rootRef = useRef(null)
|
|
84
|
+
const api = (props && props.api) || (typeof window !== 'undefined' ? window.__postapiApi : undefined)
|
|
85
|
+
const storage = api && api.storage
|
|
86
|
+
const [projects, setProjects] = useState(MOCK_PROJECTS)
|
|
87
|
+
const [project, setProject] = useState(MOCK_PROJECT)
|
|
88
|
+
const [currentId, setCurrentId] = useState(MOCK_PROJECT.id)
|
|
89
|
+
const [isMock, setIsMock] = useState(true)
|
|
90
|
+
const [error, setError] = useState('')
|
|
91
|
+
const [notice, setNotice] = useState('')
|
|
92
|
+
const [newName, setNewName] = useState('')
|
|
93
|
+
const [renameName, setRenameName] = useState(MOCK_PROJECT.name)
|
|
94
|
+
const [pendingDelete, setPendingDelete] = useState('')
|
|
95
|
+
// 新建/重命名表单折叠态:页头只留一个「新建项目」按钮
|
|
96
|
+
const [formOpen, setFormOpen] = useState(false)
|
|
97
|
+
|
|
98
|
+
useEffect(() => applyTheme(rootRef.current), [])
|
|
99
|
+
|
|
100
|
+
const refresh = async () => {
|
|
101
|
+
if (!storage || typeof storage.listProjects !== 'function') {
|
|
102
|
+
setIsMock(true)
|
|
103
|
+
return
|
|
104
|
+
}
|
|
105
|
+
try {
|
|
106
|
+
const list = await storage.listProjects()
|
|
107
|
+
if (Array.isArray(list)) {
|
|
108
|
+
setProjects(list)
|
|
109
|
+
setIsMock(false)
|
|
110
|
+
// 有真实项目时自动打开第一个:避免「重命名/删除」误作用到文件内 mock id。
|
|
111
|
+
if (list.length > 0 && typeof storage.getProject === 'function') {
|
|
112
|
+
const loaded = await storage.getProject(list[0].id)
|
|
113
|
+
if (loaded) {
|
|
114
|
+
setProject(loaded)
|
|
115
|
+
setCurrentId(loaded.id || list[0].id)
|
|
116
|
+
setRenameName(loaded.name || '')
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
} catch (err) {
|
|
121
|
+
setError('加载项目列表失败:' + errText(err))
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
useEffect(() => { refresh() }, [])
|
|
126
|
+
|
|
127
|
+
const openProject = async (projectId) => {
|
|
128
|
+
if (!storage || typeof storage.getProject !== 'function') {
|
|
129
|
+
setError('当前桥未提供 api.storage.getProject')
|
|
130
|
+
return
|
|
131
|
+
}
|
|
132
|
+
setError('')
|
|
133
|
+
try {
|
|
134
|
+
const loaded = await storage.getProject(projectId)
|
|
135
|
+
if (loaded) {
|
|
136
|
+
setProject(loaded)
|
|
137
|
+
setCurrentId(loaded.id || projectId)
|
|
138
|
+
setRenameName(loaded.name || '')
|
|
139
|
+
setNotice('已打开:' + (loaded.name || loaded.id || projectId))
|
|
140
|
+
} else {
|
|
141
|
+
setError('项目不存在:' + projectId)
|
|
142
|
+
}
|
|
143
|
+
} catch (err) {
|
|
144
|
+
setError('打开项目失败:' + errText(err))
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const createProject = async () => {
|
|
149
|
+
if (!storage || typeof storage.createProject !== 'function') {
|
|
150
|
+
setError('当前桥未提供 api.storage.createProject')
|
|
151
|
+
return
|
|
152
|
+
}
|
|
153
|
+
const name = String(newName || '').trim()
|
|
154
|
+
if (!name) {
|
|
155
|
+
setError('项目名不能为空')
|
|
156
|
+
return
|
|
157
|
+
}
|
|
158
|
+
setError('')
|
|
159
|
+
try {
|
|
160
|
+
const created = await storage.createProject(name)
|
|
161
|
+
setNewName('')
|
|
162
|
+
setNotice('已创建:' + (created && created.name ? created.name : name))
|
|
163
|
+
await refresh()
|
|
164
|
+
if (created && created.id) await openProject(created.id)
|
|
165
|
+
} catch (err) {
|
|
166
|
+
setError('创建项目失败:' + errText(err))
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const renameProject = async () => {
|
|
171
|
+
if (!storage || typeof storage.renameProject !== 'function') {
|
|
172
|
+
setError('当前桥未提供 api.storage.renameProject')
|
|
173
|
+
return
|
|
174
|
+
}
|
|
175
|
+
const name = String(renameName || '').trim()
|
|
176
|
+
if (!name) {
|
|
177
|
+
setError('新项目名不能为空')
|
|
178
|
+
return
|
|
179
|
+
}
|
|
180
|
+
if (!currentId) {
|
|
181
|
+
setError('请先打开一个项目再重命名')
|
|
182
|
+
return
|
|
183
|
+
}
|
|
184
|
+
setError('')
|
|
185
|
+
try {
|
|
186
|
+
await storage.renameProject(currentId, name)
|
|
187
|
+
setNotice('已重命名:' + name)
|
|
188
|
+
await refresh()
|
|
189
|
+
} catch (err) {
|
|
190
|
+
setError('重命名失败:' + errText(err))
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// 两段式删除:第一次点标记待确认,第二次点执行(不使用弹窗确认)
|
|
195
|
+
const deleteProject = async (projectId) => {
|
|
196
|
+
if (pendingDelete !== projectId) {
|
|
197
|
+
setPendingDelete(projectId)
|
|
198
|
+
setNotice('再次点击「删除」确认删除项目 ' + projectId)
|
|
199
|
+
return
|
|
200
|
+
}
|
|
201
|
+
if (!projectId) {
|
|
202
|
+
setError('项目 id 为空,无法删除')
|
|
203
|
+
return
|
|
204
|
+
}
|
|
205
|
+
if (!storage || typeof storage.deleteProject !== 'function') {
|
|
206
|
+
setError('当前桥未提供 api.storage.deleteProject')
|
|
207
|
+
return
|
|
208
|
+
}
|
|
209
|
+
setError('')
|
|
210
|
+
setPendingDelete('')
|
|
211
|
+
try {
|
|
212
|
+
await storage.deleteProject(projectId)
|
|
213
|
+
setNotice('已删除:' + projectId)
|
|
214
|
+
await refresh()
|
|
215
|
+
} catch (err) {
|
|
216
|
+
setError('删除项目失败:' + errText(err))
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const recent = recentEndpoints(project)
|
|
221
|
+
const cards = projectCards(projects)
|
|
222
|
+
const stats = overviewStats(projects, project)
|
|
223
|
+
|
|
224
|
+
return (
|
|
225
|
+
<div className="pp-scope pp-page" data-page="overview" ref={rootRef}>
|
|
226
|
+
{/* 页头只留标题与主动作:Apifox 页头是「标题 + 1~2 个动作」,
|
|
227
|
+
把新建/重命名的输入框塞进来会让动作区在 528px 宽的主区里换行堆叠。 */}
|
|
228
|
+
<div className="pp-overview-head">
|
|
229
|
+
<span className="pp-overview-title">项目总览</span>
|
|
230
|
+
<div className="pp-overview-actions">
|
|
231
|
+
<button className="pp-btn pp-btn-primary" onClick={() => setFormOpen((v) => !v)}>
|
|
232
|
+
{formOpen ? '取消' : '新建项目'}
|
|
233
|
+
</button>
|
|
234
|
+
</div>
|
|
235
|
+
</div>
|
|
236
|
+
<div className="pp-overview-content">
|
|
237
|
+
<p className="pp-muted">
|
|
238
|
+
{isMock
|
|
239
|
+
? '示例数据 — api.storage 未注入或不可用,以下为文件内 mock'
|
|
240
|
+
: '数据来源:api.storage(listProjects / getProject)'}
|
|
241
|
+
</p>
|
|
242
|
+
{error ? <p className="pp-error-text">{error}</p> : null}
|
|
243
|
+
{notice ? <p className="pp-muted">{notice}</p> : null}
|
|
244
|
+
|
|
245
|
+
<div className="pp-stats">
|
|
246
|
+
<div className="pp-stat">
|
|
247
|
+
<span className="pp-stat-value">{stats.projectCount}</span>
|
|
248
|
+
<span className="pp-stat-label">项目数</span>
|
|
249
|
+
</div>
|
|
250
|
+
<div className="pp-stat">
|
|
251
|
+
<span className="pp-stat-value">{stats.endpointCount}</span>
|
|
252
|
+
<span className="pp-stat-label">接口数</span>
|
|
253
|
+
</div>
|
|
254
|
+
<div className="pp-stat">
|
|
255
|
+
<span className="pp-stat-value">{stats.envCount}</span>
|
|
256
|
+
<span className="pp-stat-label">环境数</span>
|
|
257
|
+
</div>
|
|
258
|
+
<div className="pp-stat">
|
|
259
|
+
<span className="pp-stat-value">{fmtTime(stats.latestUpdated)}</span>
|
|
260
|
+
<span className="pp-stat-label">最近更新</span>
|
|
261
|
+
</div>
|
|
262
|
+
</div>
|
|
263
|
+
|
|
264
|
+
{/* 项目操作区:新建 / 重命名表单(折叠,避免常驻占位) */}
|
|
265
|
+
{formOpen ? (
|
|
266
|
+
<div className="pp-card">
|
|
267
|
+
<div className="pp-card-title">新建 / 重命名项目</div>
|
|
268
|
+
<div className="pp-form-grid">
|
|
269
|
+
<label className="pp-form-label">新建项目</label>
|
|
270
|
+
<div className="pp-toolbar-inline">
|
|
271
|
+
<input className="pp-input" placeholder="项目名"
|
|
272
|
+
value={newName} onChange={(e) => { setNewName(e.target.value); setNotice('') }} />
|
|
273
|
+
<button className="pp-btn pp-btn-primary" onClick={createProject}>新建</button>
|
|
274
|
+
</div>
|
|
275
|
+
<label className="pp-form-label">重命名当前项目</label>
|
|
276
|
+
<div className="pp-toolbar-inline">
|
|
277
|
+
<input className="pp-input" placeholder="新名称"
|
|
278
|
+
value={renameName} onChange={(e) => { setRenameName(e.target.value); setNotice('') }} />
|
|
279
|
+
<button className="pp-btn" onClick={renameProject}>重命名</button>
|
|
280
|
+
</div>
|
|
281
|
+
</div>
|
|
282
|
+
</div>
|
|
283
|
+
) : null}
|
|
284
|
+
|
|
285
|
+
<div className="pp-toolbar-inline">
|
|
286
|
+
<span className="pp-meta">当前项目</span>
|
|
287
|
+
<input className="pp-input pp-input-mono" style={{ width: '180px' }} readOnly value={currentId || '-'} />
|
|
288
|
+
</div>
|
|
289
|
+
|
|
290
|
+
<div className="pp-overview-grid">
|
|
291
|
+
{cards.map((p) => (
|
|
292
|
+
<div key={p.id} className="pp-card pp-card-hover" data-active={currentId === p.id ? 'true' : 'false'}>
|
|
293
|
+
<div className="pp-card-title pp-ellipsis">{p.name}</div>
|
|
294
|
+
<div className="pp-meta">
|
|
295
|
+
{p.endpointCount === undefined ? '接口数未知' : p.endpointCount + ' 个接口'} · 更新于 {fmtTime(p.updatedAt)}
|
|
296
|
+
</div>
|
|
297
|
+
<div className="pp-toolbar-inline" style={{ marginTop: 'var(--sp-3)' }}>
|
|
298
|
+
<button className="pp-btn" onClick={() => openProject(p.id)}>打开</button>
|
|
299
|
+
<button className="pp-btn pp-btn-danger" onClick={() => deleteProject(p.id)}>
|
|
300
|
+
{pendingDelete === p.id ? '确认删除' : '删除'}
|
|
301
|
+
</button>
|
|
302
|
+
</div>
|
|
303
|
+
</div>
|
|
304
|
+
))}
|
|
305
|
+
{cards.length === 0 ? <EmptyState text="暂无项目" hint="先在上方新建一个项目" /> : null}
|
|
306
|
+
</div>
|
|
307
|
+
|
|
308
|
+
<div className="pp-card" style={{ marginTop: 'var(--sp-5)' }}>
|
|
309
|
+
<div className="pp-card-title">最近接口</div>
|
|
310
|
+
<div className="pp-table-wrap">
|
|
311
|
+
<table className="pp-table">
|
|
312
|
+
<thead>
|
|
313
|
+
<tr>
|
|
314
|
+
<th style={{ width: '90px' }}>方法</th>
|
|
315
|
+
<th>名称</th>
|
|
316
|
+
<th>URL</th>
|
|
317
|
+
<th style={{ width: '150px' }}>更新时间</th>
|
|
318
|
+
</tr>
|
|
319
|
+
</thead>
|
|
320
|
+
<tbody>
|
|
321
|
+
{recent.map((e) => (
|
|
322
|
+
<tr key={e.id} onClick={() => { window.location.hash = '#/doc/' + encodeURIComponent(e.id) }}>
|
|
323
|
+
<td><span className="pp-method" data-method={e.method}>{e.method}</span></td>
|
|
324
|
+
<td>{e.name || '-'}</td>
|
|
325
|
+
<td className="pp-table-mono pp-muted">{e.url || '-'}</td>
|
|
326
|
+
<td className="pp-muted">{fmtTime(e.updatedAt)}</td>
|
|
327
|
+
</tr>
|
|
328
|
+
))}
|
|
329
|
+
{recent.length === 0 ? (
|
|
330
|
+
<tr><td colSpan={4} className="pp-muted">当前项目暂无接口</td></tr>
|
|
331
|
+
) : null}
|
|
332
|
+
</tbody>
|
|
333
|
+
</table>
|
|
334
|
+
</div>
|
|
335
|
+
</div>
|
|
336
|
+
|
|
337
|
+
<div className="pp-toolbar" style={{ marginTop: 'var(--sp-5)' }}>
|
|
338
|
+
<span className="pp-card-title" style={{ margin: 0 }}>快捷入口</span>
|
|
339
|
+
<span className="pp-toolbar-spacer" />
|
|
340
|
+
{SHORTCUTS.map((s) => (
|
|
341
|
+
<a key={s.hash} className="pp-btn" href={s.hash}>{s.label}</a>
|
|
342
|
+
))}
|
|
343
|
+
</div>
|
|
344
|
+
|
|
345
|
+
</div>
|
|
346
|
+
</div>
|
|
347
|
+
)
|
|
348
|
+
}
|