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,407 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* webServer prefix 桥:官方 web client 无法注入插件自定义 face(样板 inject 全为宿主内置 face),
|
|
3
|
+
* 契约 §3 的 inject face 方案作废,改走 dsh-db-tool 同款 prefix 桥(0.2.x 生产验证)。
|
|
4
|
+
* handlePostapiRequest(req, res, { storage, request, services }):全量路由 + {ok,data}/{ok:false,error,code} 响应形态。
|
|
5
|
+
* 路由键统一为剥离 /api 前缀后的形式(GET /health、POST /storage/saveEndpoint ...),
|
|
6
|
+
* /api/... 与 /dsh-postapi/api/... 两种请求形态归一化命中同一批路由。
|
|
7
|
+
*
|
|
8
|
+
* 契约 §5 全表:既有 11 条路由行为不变;新增 storage 扩展 / request.preview / script / mock /
|
|
9
|
+
* schema / import / export / codegen / test / db 共 8 组。第三参 services 承载 host-data 与
|
|
10
|
+
* host-runtime 的扩展服务(variables/sandbox/mock/runner/importExport/codegen/schema);
|
|
11
|
+
* 服务方法缺失时该路由返回 501 NOT_WIRED,绝不抛栈崩 handler——两个半边并行开发期必须如此。
|
|
12
|
+
*/
|
|
13
|
+
const MAX_JSON_BODY_BYTES = 2 * 1024 * 1024
|
|
14
|
+
const PREFIX = '/dsh-postapi/api'
|
|
15
|
+
|
|
16
|
+
function send(res, status, body) {
|
|
17
|
+
if (!res.headersSent) res.writeHead(status, { 'Content-Type': 'application/json; charset=utf-8' })
|
|
18
|
+
res.end(JSON.stringify(body))
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function sendOk(res, data) {
|
|
22
|
+
send(res, 200, { ok: true, data })
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function sendFail(res, status, code, error) {
|
|
26
|
+
send(res, status, { ok: false, error, code })
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** JSON body 读取:2MB 上限、空 body 视为 {}、必须是 JSON 对象(形态同 db-tool readBody)。 */
|
|
30
|
+
function readBody(req) {
|
|
31
|
+
return new Promise((resolve, reject) => {
|
|
32
|
+
const chunks = []
|
|
33
|
+
let size = 0
|
|
34
|
+
req.on('data', (c) => {
|
|
35
|
+
size += c.length
|
|
36
|
+
if (size > MAX_JSON_BODY_BYTES) {
|
|
37
|
+
reject(new Error('request body exceeds 2MB limit'))
|
|
38
|
+
req.destroy()
|
|
39
|
+
return
|
|
40
|
+
}
|
|
41
|
+
chunks.push(c)
|
|
42
|
+
})
|
|
43
|
+
req.on('end', () => {
|
|
44
|
+
const text = Buffer.concat(chunks).toString('utf8').trim()
|
|
45
|
+
if (text === '') return resolve({})
|
|
46
|
+
try {
|
|
47
|
+
const parsed = JSON.parse(text)
|
|
48
|
+
if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) resolve(parsed)
|
|
49
|
+
else reject(new Error('request body must be a JSON object'))
|
|
50
|
+
} catch {
|
|
51
|
+
reject(new Error('request body is not valid JSON'))
|
|
52
|
+
}
|
|
53
|
+
})
|
|
54
|
+
req.on('error', reject)
|
|
55
|
+
})
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function str(v) {
|
|
59
|
+
return typeof v === 'string' ? v : ''
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** 项目 id 字段兼容:client 桥(registry.js)发 { id },历史/文档形态发 { projectId }。 */
|
|
63
|
+
function projectIdOf(b) {
|
|
64
|
+
return str(b.projectId) || str(b.id)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** 兼容 /api/... 与 /dsh-postapi/api/... 两种请求形态,归一化出无 /api 前缀的 path 与 query。 */
|
|
68
|
+
function splitPath(url) {
|
|
69
|
+
const u = new URL(url || '/', 'http://bridge.local')
|
|
70
|
+
let p = u.pathname
|
|
71
|
+
if (p.startsWith('/api/')) p = p.slice(4)
|
|
72
|
+
else if (p.startsWith(PREFIX + '/')) p = p.slice(PREFIX.length)
|
|
73
|
+
else if (p === '/api' || p === PREFIX) p = '/'
|
|
74
|
+
return { path: p, search: u.searchParams }
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/* ---------------- 桥级错误:带 HTTP 状态 + 错误码,由最外层 catch 统一转封套 ---------------- */
|
|
78
|
+
|
|
79
|
+
class BridgeError extends Error {
|
|
80
|
+
constructor(status, code, message) {
|
|
81
|
+
super(message)
|
|
82
|
+
this.name = 'BridgeError'
|
|
83
|
+
this.status = status
|
|
84
|
+
this.code = code
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const badRequest = (message) => new BridgeError(400, 'INVALID_ARGUMENT', message)
|
|
89
|
+
const notWired = (message) => new BridgeError(501, 'NOT_WIRED', message)
|
|
90
|
+
const notImplemented = (message) => new BridgeError(501, 'NOT_IMPLEMENTED', message)
|
|
91
|
+
|
|
92
|
+
/** 取服务方法:对象或方法缺失一律 501 NOT_WIRED(并行开发期 storage/services 尚未补齐也不崩)。 */
|
|
93
|
+
function need(target, name, label) {
|
|
94
|
+
if (!target || typeof target[name] !== 'function') {
|
|
95
|
+
throw notWired((label || name) + ' 未接线(服务方法缺失)')
|
|
96
|
+
}
|
|
97
|
+
return target[name].bind(target)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** projectId 取值:body 优先(POST),回落 query(GET 形态)。 */
|
|
101
|
+
function projectIdFrom(b, search) {
|
|
102
|
+
const fromQuery = search && typeof search.get === 'function' ? str(search.get('projectId')) : ''
|
|
103
|
+
return projectIdOf(b) || fromQuery
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** 必填 projectId 校验:缺失返回 400 INVALID_ARGUMENT(契约 §5)。 */
|
|
107
|
+
function requireProject(b, search) {
|
|
108
|
+
const id = projectIdFrom(b, search)
|
|
109
|
+
if (id === '') throw badRequest('projectId is required')
|
|
110
|
+
return id
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export async function handlePostapiRequest(req, res, { storage, request, services } = {}) {
|
|
114
|
+
const svc = services || {}
|
|
115
|
+
try {
|
|
116
|
+
const { path: p, search } = splitPath(req.url)
|
|
117
|
+
const method = (req.method || 'GET').toUpperCase()
|
|
118
|
+
const key = method + ' ' + p
|
|
119
|
+
|
|
120
|
+
if (key === 'GET /health') {
|
|
121
|
+
return sendOk(res, { status: 'up' })
|
|
122
|
+
}
|
|
123
|
+
if (key === 'GET /storage/listProjects') {
|
|
124
|
+
return sendOk(res, await storage.listProjects())
|
|
125
|
+
}
|
|
126
|
+
// POST 形态兼容(client 桥映射 POST /storage/getProject,body 携 projectId),与 GET query 形态同行为同返回
|
|
127
|
+
if (key === 'GET /storage/getProject' || key === 'POST /storage/getProject') {
|
|
128
|
+
const projectId = key.startsWith('POST ')
|
|
129
|
+
? projectIdOf(await readBody(req))
|
|
130
|
+
: search.get('projectId') || ''
|
|
131
|
+
if (projectId === '') return sendFail(res, 400, 'INVALID_ARGUMENT', 'projectId is required')
|
|
132
|
+
return sendOk(res, await storage.getProject(projectId))
|
|
133
|
+
}
|
|
134
|
+
if (key === 'POST /request/send') {
|
|
135
|
+
const body = await readBody(req)
|
|
136
|
+
const result = await request.send(body)
|
|
137
|
+
// 契约 §4:request 只回 raw,历史由 bridge 负责追加;追加失败不阻断本次发送结果。
|
|
138
|
+
const projectId = projectIdOf(body)
|
|
139
|
+
if (projectId !== '' && typeof storage.appendHistory === 'function') {
|
|
140
|
+
try {
|
|
141
|
+
await storage.appendHistory(projectId, {
|
|
142
|
+
method: body.method || 'GET',
|
|
143
|
+
url: (result && result.request && result.request.url) || body.url || '',
|
|
144
|
+
status: result && result.status,
|
|
145
|
+
timeMs: result && result.timeMs,
|
|
146
|
+
bodySize: result && result.bodySize,
|
|
147
|
+
request: (result && result.request) || {},
|
|
148
|
+
response: {
|
|
149
|
+
status: result && result.status,
|
|
150
|
+
headers: (result && result.headers) || {},
|
|
151
|
+
body: (result && result.body) || '',
|
|
152
|
+
},
|
|
153
|
+
})
|
|
154
|
+
} catch {
|
|
155
|
+
// 历史落盘失败不改变发送结果(契约 §4 只要求 raw 响应)
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return sendOk(res, result)
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const body = method === 'POST' ? await readBody(req) : {}
|
|
162
|
+
const routes = {
|
|
163
|
+
'POST /storage/createProject': (b) => storage.createProject(str(b.name)),
|
|
164
|
+
'POST /storage/renameProject': (b) => storage.renameProject(projectIdOf(b), str(b.name)),
|
|
165
|
+
'POST /storage/deleteProject': (b) => storage.deleteProject(projectIdOf(b)),
|
|
166
|
+
'POST /storage/saveEndpoint': (b) => storage.saveEndpoint(projectIdOf(b), b.endpoint),
|
|
167
|
+
'POST /storage/deleteEndpoint': (b) => storage.deleteEndpoint(projectIdOf(b), str(b.endpointId)),
|
|
168
|
+
'POST /storage/saveFolder': (b) => storage.saveFolder(projectIdOf(b), b.folder),
|
|
169
|
+
'POST /storage/deleteFolder': (b) => storage.deleteFolder(projectIdOf(b), str(b.folderId)),
|
|
170
|
+
}
|
|
171
|
+
const route = routes[key]
|
|
172
|
+
if (route) return sendOk(res, await route(body))
|
|
173
|
+
|
|
174
|
+
/* ---------------- feature-matrix §4 新增路由(第三参 services 注入) ---------------- */
|
|
175
|
+
|
|
176
|
+
// Mock 服务的响应转发入口:/mock/serve/<projectId>/<endpointId> 形态由 mock 服务内部处理。
|
|
177
|
+
if (p.startsWith('/mock/serve/')) {
|
|
178
|
+
const serve = need(svc.mock, 'serve', 'mock.serve')
|
|
179
|
+
return sendOk(res, await serve({ path: p.slice('/mock/serve'.length), search, method, req }))
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* storage 扩展路由统一入口:先校验 projectId(缺 → 400),再取服务方法(缺 → 501)。
|
|
184
|
+
* @param name - storage 方法名(与 client 桥同名)。
|
|
185
|
+
* @param argsOf - 由 projectId 拼出剩余实参(缺省无额外参数)。
|
|
186
|
+
*/
|
|
187
|
+
const store = (name, b, s, argsOf) => {
|
|
188
|
+
const projectId = requireProject(b, s)
|
|
189
|
+
const rest = typeof argsOf === 'function' ? argsOf(projectId) : []
|
|
190
|
+
return need(storage, name)(projectId, ...(Array.isArray(rest) ? rest : [rest]))
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** 导出类路由的 project 取用:body 直接带 project 优先,否则按 projectId 读盘。 */
|
|
194
|
+
const projectOf = async (b, s) => {
|
|
195
|
+
if (b.project && typeof b.project === 'object') return b.project
|
|
196
|
+
return need(storage, 'getProject')(requireProject(b, s))
|
|
197
|
+
}
|
|
198
|
+
/** import 类入参:client 发 { json } 或 { text },兼容旧形态 { data }。 */
|
|
199
|
+
const payloadOf = (b) => (b.json !== undefined ? b.json : b.data !== undefined ? b.data : str(b.text))
|
|
200
|
+
|
|
201
|
+
const newRoutes = {
|
|
202
|
+
/* ---- storage 扩展(契约 §3,host-data 并行交付) ---- */
|
|
203
|
+
// 校验顺序固定:先 requireProject(缺 → 400),再 need 取服务方法(缺 → 501),
|
|
204
|
+
// 否则 callee 先求值会让「缺 projectId + 服务未接线」错报成 501。
|
|
205
|
+
'POST /storage/moveNode': (b, s) => store('moveNode', b, s, (id) => ({
|
|
206
|
+
kind: str(b.kind),
|
|
207
|
+
id: str(b.id),
|
|
208
|
+
parentId: b.parentId === undefined ? null : b.parentId,
|
|
209
|
+
order: b.order,
|
|
210
|
+
})),
|
|
211
|
+
// 导入 bundle 原子落盘:body { projectId, bundle: { folders, endpoints, schemas } }
|
|
212
|
+
'POST /storage/importBundle': (b, s) => store('importBundle', b, s, () => [b.bundle]),
|
|
213
|
+
'POST /storage/saveSchema': (b, s) => store('saveSchema', b, s, (id) => [b.schema]),
|
|
214
|
+
'POST /storage/deleteSchema': (b, s) => store('deleteSchema', b, s, () => [str(b.schemaId)]),
|
|
215
|
+
'POST /storage/saveEnvironment': (b, s) => store('saveEnvironment', b, s, () => [b.environment]),
|
|
216
|
+
'POST /storage/deleteEnvironment': (b, s) => store('deleteEnvironment', b, s, () => [str(b.envId)]),
|
|
217
|
+
'POST /storage/saveGlobals': (b, s) => store('saveGlobals', b, s, () => [b.globals]),
|
|
218
|
+
'POST /storage/saveProjectSettings': (b, s) => store('saveProjectSettings', b, s, () => [b.settings]),
|
|
219
|
+
'POST /storage/saveFlow': (b, s) => store('saveFlow', b, s, () => [b.flow]),
|
|
220
|
+
'POST /storage/saveResponse': (b, s) => store('saveResponse', b, s, () => [str(b.endpointId), b.response]),
|
|
221
|
+
'POST /storage/deleteResponse': (b, s) => store('deleteResponse', b, s, () => [str(b.endpointId), str(b.responseId)]),
|
|
222
|
+
// 历史:GET query 形态与 POST body 形态同行为(client 桥用 GET)
|
|
223
|
+
'GET /storage/getHistory': (b, s) => store('getHistory', b, s),
|
|
224
|
+
'POST /storage/getHistory': (b, s) => store('getHistory', b, s),
|
|
225
|
+
'POST /storage/appendHistory': (b, s) => store('appendHistory', b, s, () => [b.entry]),
|
|
226
|
+
'POST /storage/clearHistory': (b, s) => store('clearHistory', b, s),
|
|
227
|
+
'GET /storage/search': (b, s) => store('search', b, s, () => [str(s.get('keyword') || s.get('q'))]),
|
|
228
|
+
'POST /storage/search': (b, s) => store('search', b, s, () => [str(b.keyword)]),
|
|
229
|
+
|
|
230
|
+
/* ---- request 预览(契约 §4:只渲染变量不发送) ---- */
|
|
231
|
+
'POST /request/preview': async (b, s) => {
|
|
232
|
+
if (request && typeof request.preview === 'function') return request.preview(b)
|
|
233
|
+
const makeResolver = need(svc.variables, 'createVariableResolver', 'variables.createVariableResolver')
|
|
234
|
+
const projectId = projectIdFrom(b, s)
|
|
235
|
+
const project = projectId !== '' ? await need(storage, 'getProject')(projectId) : null
|
|
236
|
+
const environments = (project && project.environments) || []
|
|
237
|
+
const environment = b.environment
|
|
238
|
+
|| environments.find((e) => e && e.id === str(b.environmentId))
|
|
239
|
+
|| environments.find((e) => e && e.isDefault)
|
|
240
|
+
|| null
|
|
241
|
+
const resolver = makeResolver({
|
|
242
|
+
globals: b.globals || (project && project.globals) || null,
|
|
243
|
+
environment,
|
|
244
|
+
local: b.local || null,
|
|
245
|
+
})
|
|
246
|
+
const resolveText = (v) => resolver.resolve(v == null ? '' : String(v))
|
|
247
|
+
const explainText = (v) => (typeof resolver.explain === 'function'
|
|
248
|
+
? resolver.explain(v == null ? '' : String(v))
|
|
249
|
+
: { resolved: resolveText(v), missing: [] })
|
|
250
|
+
return {
|
|
251
|
+
url: explainText(b.url),
|
|
252
|
+
params: (Array.isArray(b.params) ? b.params : []).map((x) => (
|
|
253
|
+
x && typeof x === 'object' ? { ...x, value: resolveText(x.value) } : x
|
|
254
|
+
)),
|
|
255
|
+
headers: (Array.isArray(b.headers) ? b.headers : []).map((x) => (
|
|
256
|
+
x && typeof x === 'object' ? { ...x, value: resolveText(x.value) } : x
|
|
257
|
+
)),
|
|
258
|
+
body: b.body && typeof b.body === 'object'
|
|
259
|
+
? { ...b.body, content: resolveText(b.body.content) }
|
|
260
|
+
: (b.body === undefined ? null : b.body),
|
|
261
|
+
}
|
|
262
|
+
},
|
|
263
|
+
|
|
264
|
+
/* ---- 脚本沙箱(契约 §4) ---- */
|
|
265
|
+
'POST /script/run': (b) => need(svc.sandbox, 'runScript')(str(b.code), {
|
|
266
|
+
context: b.context && typeof b.context === 'object' ? b.context : {},
|
|
267
|
+
timeoutMs: Number.isFinite(b.timeoutMs) ? b.timeoutMs : 3000,
|
|
268
|
+
}),
|
|
269
|
+
|
|
270
|
+
/* ---- 本地 Mock 服务(契约 §4) ---- */
|
|
271
|
+
'POST /mock/start': (b) => need(svc.mock, 'start')(Number.isFinite(b.port) ? b.port : undefined),
|
|
272
|
+
'POST /mock/stop': () => need(svc.mock, 'stop')(),
|
|
273
|
+
'POST /mock/status': () => need(svc.mock, 'status')(),
|
|
274
|
+
|
|
275
|
+
/* ---- Schema 示例生成与校验(services.schema 优先,缺失走内置最小实现) ---- */
|
|
276
|
+
'POST /schema/example': (b) => (svc.schema && typeof svc.schema.example === 'function'
|
|
277
|
+
? svc.schema.example(b)
|
|
278
|
+
: { example: schemaExampleOf(b.schema) }),
|
|
279
|
+
'POST /schema/validate': (b) => (svc.schema && typeof svc.schema.validate === 'function'
|
|
280
|
+
? svc.schema.validate(b)
|
|
281
|
+
: schemaValidateOf(b.schema, b.data)),
|
|
282
|
+
|
|
283
|
+
/* ---- 导入(契约 §4,host-data import-export.js) ---- */
|
|
284
|
+
'POST /import/openapi': (b) => need(svc.importExport, 'importOpenApi')(payloadOf(b)),
|
|
285
|
+
'POST /import/postman': (b) => need(svc.importExport, 'importPostman')(payloadOf(b)),
|
|
286
|
+
'POST /import/har': (b) => need(svc.importExport, 'importHar')(payloadOf(b)),
|
|
287
|
+
'POST /import/curl': (b) => need(svc.importExport, 'importCurl')(str(b.text) || str(b.curl)),
|
|
288
|
+
|
|
289
|
+
/* ---- 导出(契约 §4) ---- */
|
|
290
|
+
'POST /export/openapi': async (b, s) => need(svc.importExport, 'exportOpenApi')(await projectOf(b, s)),
|
|
291
|
+
'POST /export/postman': async (b, s) => need(svc.importExport, 'exportPostman')(await projectOf(b, s)),
|
|
292
|
+
'POST /export/har': async (b, s) => need(svc.importExport, 'exportHar')(await projectOf(b, s)),
|
|
293
|
+
'POST /export/markdown': async (b, s) => need(svc.importExport, 'exportMarkdown')(await projectOf(b, s)),
|
|
294
|
+
|
|
295
|
+
/* ---- 代码生成(契约 §4) ---- */
|
|
296
|
+
'POST /codegen/run': (b) => need(svc.codegen, 'generateCode')({
|
|
297
|
+
endpoint: b.endpoint,
|
|
298
|
+
env: b.env || b.environment || null,
|
|
299
|
+
lang: str(b.lang) || 'curl',
|
|
300
|
+
}),
|
|
301
|
+
|
|
302
|
+
/* ---- 自动化测试(契约 §4) ---- */
|
|
303
|
+
'POST /test/runCase': (b, s) => {
|
|
304
|
+
const projectId = requireProject(b, s)
|
|
305
|
+
return need(svc.runner, 'runCase')(projectId, str(b.caseId))
|
|
306
|
+
},
|
|
307
|
+
'POST /test/runScenario': (b, s) => {
|
|
308
|
+
const projectId = requireProject(b, s)
|
|
309
|
+
return need(svc.runner, 'runScenario')(projectId, str(b.scenarioId))
|
|
310
|
+
},
|
|
311
|
+
|
|
312
|
+
/* ---- 数据库操作(feature-matrix P2:只留路由位,固定 501) ---- */
|
|
313
|
+
'POST /db/test': () => { throw notImplemented('数据库操作规划中(feature-matrix P2)') },
|
|
314
|
+
'POST /db/query': () => { throw notImplemented('数据库操作规划中(feature-matrix P2)') },
|
|
315
|
+
'POST /db/close': () => { throw notImplemented('数据库操作规划中(feature-matrix P2)') },
|
|
316
|
+
}
|
|
317
|
+
const newRoute = newRoutes[key]
|
|
318
|
+
if (newRoute) return sendOk(res, await newRoute(body, search))
|
|
319
|
+
|
|
320
|
+
return sendFail(res, 404, 'NOT_FOUND', 'no route: ' + key)
|
|
321
|
+
} catch (e) {
|
|
322
|
+
// 契约 §5:400 参数错误 / 501 服务未接线 / 500 其余异常,统一转 JSON 封套
|
|
323
|
+
if (e instanceof BridgeError) return sendFail(res, e.status, e.code, e.message)
|
|
324
|
+
return sendFail(res, 500, 'INTERNAL', e && e.message ? e.message : String(e))
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/* ---------------- schema 内置最小实现(services.schema 未接线时兜底,页面不因此报错) ---------------- */
|
|
329
|
+
|
|
330
|
+
/** 由 JSON Schema 生成示例值(example/default/enum/oneOf/anyOf + 七种 type,深度上限 12)。 */
|
|
331
|
+
export function schemaExampleOf(schema, depth = 0) {
|
|
332
|
+
if (!schema || typeof schema !== 'object' || Array.isArray(schema) || depth > 12) return null
|
|
333
|
+
if (schema.example !== undefined) return schema.example
|
|
334
|
+
if (schema.default !== undefined) return schema.default
|
|
335
|
+
if (Array.isArray(schema.enum) && schema.enum.length > 0) return schema.enum[0]
|
|
336
|
+
if (Array.isArray(schema.oneOf) && schema.oneOf.length > 0) return schemaExampleOf(schema.oneOf[0], depth + 1)
|
|
337
|
+
if (Array.isArray(schema.anyOf) && schema.anyOf.length > 0) return schemaExampleOf(schema.anyOf[0], depth + 1)
|
|
338
|
+
const type = Array.isArray(schema.type) ? schema.type[0] : schema.type
|
|
339
|
+
if (type === 'object' || (type === undefined && schema.properties)) {
|
|
340
|
+
const out = {}
|
|
341
|
+
for (const [k, sub] of Object.entries(schema.properties || {})) out[k] = schemaExampleOf(sub, depth + 1)
|
|
342
|
+
return out
|
|
343
|
+
}
|
|
344
|
+
if (type === 'array' || (type === undefined && schema.items)) return [schemaExampleOf(schema.items || {}, depth + 1)]
|
|
345
|
+
if (type === 'string') {
|
|
346
|
+
if (schema.format === 'date-time') return '2024-01-01T00:00:00.000Z'
|
|
347
|
+
if (schema.format === 'date') return '2024-01-01'
|
|
348
|
+
if (schema.format === 'email') return 'user@example.com'
|
|
349
|
+
if (schema.format === 'uri' || schema.format === 'url') return 'https://example.com'
|
|
350
|
+
return ''
|
|
351
|
+
}
|
|
352
|
+
if (type === 'integer' || type === 'number') return 0
|
|
353
|
+
if (type === 'boolean') return true
|
|
354
|
+
if (type === 'null') return null
|
|
355
|
+
return null
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/** 轻量 JSON Schema 校验:未给 data 只校验 schema 结构;给了 data 追加 type/required 命中。 */
|
|
359
|
+
export function schemaValidateOf(schema, data) {
|
|
360
|
+
if (!schema || typeof schema !== 'object' || Array.isArray(schema)) {
|
|
361
|
+
return { ok: false, message: 'schema 必须是 JSON 对象' }
|
|
362
|
+
}
|
|
363
|
+
if (data === undefined) {
|
|
364
|
+
const known = [undefined, 'object', 'array', 'string', 'number', 'integer', 'boolean', 'null']
|
|
365
|
+
const list = Array.isArray(schema.type) ? schema.type : [schema.type]
|
|
366
|
+
const bad = list.find((t) => !known.includes(t))
|
|
367
|
+
if (bad !== undefined) return { ok: false, message: '未知 type: ' + bad }
|
|
368
|
+
if (schema.properties !== undefined
|
|
369
|
+
&& (typeof schema.properties !== 'object' || schema.properties === null || Array.isArray(schema.properties))) {
|
|
370
|
+
return { ok: false, message: 'properties 必须是对象' }
|
|
371
|
+
}
|
|
372
|
+
if (schema.required !== undefined && !Array.isArray(schema.required)) {
|
|
373
|
+
return { ok: false, message: 'required 必须是数组' }
|
|
374
|
+
}
|
|
375
|
+
return { ok: true, message: 'schema 结构合法' }
|
|
376
|
+
}
|
|
377
|
+
const check = (sch, value, at) => {
|
|
378
|
+
if (!sch || typeof sch !== 'object') return null
|
|
379
|
+
const type = Array.isArray(sch.type) ? sch.type[0] : sch.type
|
|
380
|
+
const actual = value === null ? 'null' : Array.isArray(value) ? 'array' : typeof value
|
|
381
|
+
const okType = type === undefined
|
|
382
|
+
|| type === actual
|
|
383
|
+
|| (type === 'number' && actual === 'number')
|
|
384
|
+
|| (type === 'integer' && actual === 'number' && Number.isInteger(value))
|
|
385
|
+
if (!okType) return at + ': 期望 ' + type + ',实际 ' + actual
|
|
386
|
+
if (type === 'object' || sch.properties) {
|
|
387
|
+
for (const key of Array.isArray(sch.required) ? sch.required : []) {
|
|
388
|
+
if (value === null || value === undefined || value[key] === undefined) return at + '.' + key + ': 必填缺失'
|
|
389
|
+
}
|
|
390
|
+
for (const [key, sub] of Object.entries(sch.properties || {})) {
|
|
391
|
+
if (value && value[key] !== undefined) {
|
|
392
|
+
const err = check(sub, value[key], at + '.' + key)
|
|
393
|
+
if (err) return err
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
if ((type === 'array' || sch.items) && Array.isArray(value) && sch.items) {
|
|
398
|
+
for (let i = 0; i < value.length; i += 1) {
|
|
399
|
+
const err = check(sch.items, value[i], at + '[' + i + ']')
|
|
400
|
+
if (err) return err
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
return null
|
|
404
|
+
}
|
|
405
|
+
const err = check(schema, data, '$')
|
|
406
|
+
return err ? { ok: false, message: err } : { ok: true, message: '校验通过' }
|
|
407
|
+
}
|