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.
Files changed (75) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +139 -2
  3. package/cordis.patch.yml +3 -0
  4. package/dist/client.js +5651 -0
  5. package/docs/DETAIL.md +27 -0
  6. package/docs/apifox-design-spec.md +101 -0
  7. package/docs/apifox-research/analysis.md +254 -0
  8. package/docs/apifox-ui-delivery.md +150 -0
  9. package/docs/apifox-visual-parity-spec.md +353 -0
  10. package/docs/feature-matrix.md +112 -0
  11. package/docs/flow-reference/sequence-reference.html +163 -0
  12. package/docs/interface-contract-v2.md +175 -0
  13. package/docs/interface-contract.md +105 -0
  14. package/docs/nav-layout-v2-spec.md +107 -0
  15. package/docs/preview.svg +9 -0
  16. package/docs/probe-client.legacy.js +31 -0
  17. package/docs/rootcause-sidebar-vanish.md +95 -0
  18. package/docs/rootcause.md +86 -0
  19. package/docs/sidebar-notes.md +153 -0
  20. package/docs/ui-audit.md +197 -0
  21. package/docs/ui-notes.md +98 -0
  22. package/docs/v2-delivery.md +99 -0
  23. package/icon.svg +4 -0
  24. package/index.js +5 -0
  25. package/locale/en.json +7 -0
  26. package/locale/zh.json +7 -0
  27. package/package.json +73 -4
  28. package/src/client/index.js +132 -0
  29. package/src/client/jsx-shim.js +9 -0
  30. package/src/client/pages/__tests__/heights.test.js +135 -0
  31. package/src/client/pages/components/AssertTable.jsx +122 -0
  32. package/src/client/pages/components/BodyEditor.jsx +91 -0
  33. package/src/client/pages/components/EmptyState.jsx +52 -0
  34. package/src/client/pages/components/FolderTree.jsx +109 -0
  35. package/src/client/pages/components/JsonEditor.jsx +45 -0
  36. package/src/client/pages/components/ParamTable.jsx +226 -0
  37. package/src/client/pages/components/ResponsePanel.jsx +278 -0
  38. package/src/client/pages/components/ScriptPanel.jsx +99 -0
  39. package/src/client/pages/components/StatusPill.jsx +35 -0
  40. package/src/client/pages/components/__tests__/apifox-ui.test.js +291 -0
  41. package/src/client/pages/components/__tests__/pages-a.test.js +392 -0
  42. package/src/client/pages/components/__tests__/tree.test.js +379 -0
  43. package/src/client/pages/components/headerPresets.js +63 -0
  44. package/src/client/pages/debug.jsx +576 -0
  45. package/src/client/pages/design-tokens.css +2604 -0
  46. package/src/client/pages/doc.jsx +552 -0
  47. package/src/client/pages/env.jsx +378 -0
  48. package/src/client/pages/flow.jsx +349 -0
  49. package/src/client/pages/history.jsx +238 -0
  50. package/src/client/pages/import.jsx +396 -0
  51. package/src/client/pages/mock.jsx +316 -0
  52. package/src/client/pages/overview.jsx +348 -0
  53. package/src/client/pages/schema.jsx +317 -0
  54. package/src/client/pages/search.jsx +216 -0
  55. package/src/client/pages/sequence.jsx +344 -0
  56. package/src/client/pages/settings.jsx +205 -0
  57. package/src/client/pages/test.jsx +470 -0
  58. package/src/client/pages/theme.js +27 -0
  59. package/src/client/pages-entry.js +20 -0
  60. package/src/client/shell/ApiTitle.js +19 -0
  61. package/src/client/shell/Body.js +492 -0
  62. package/src/client/shell/definition.js +57 -0
  63. package/src/client/shell/locales.js +185 -0
  64. package/src/client/shell/menu.js +174 -0
  65. package/src/client/shell/registry.js +530 -0
  66. package/src/host/bridge.js +407 -0
  67. package/src/host/codegen.js +356 -0
  68. package/src/host/import-export.js +1044 -0
  69. package/src/host/index.js +102 -0
  70. package/src/host/mock-server.js +303 -0
  71. package/src/host/request.js +336 -0
  72. package/src/host/script-sandbox.js +272 -0
  73. package/src/host/storage.js +803 -0
  74. package/src/host/test-runner.js +126 -0
  75. package/src/host/variables.js +108 -0
@@ -0,0 +1,126 @@
1
+ /**
2
+ * 自动化测试执行:契约 §4 createTestRunner。
3
+ * runCase 跑单用例;runScenario 跑多步骤场景,步骤间用 extract 提取的局部变量传递。
4
+ * 断言来源两处:endpoint.postProcessors 里 type=assert 的内置断言 + 用例自带 assertions。
5
+ */
6
+ import { pickValue } from './request.js'
7
+
8
+ function endpointAssertions(endpoint) {
9
+ const out = []
10
+ const list = Array.isArray(endpoint.postProcessors) ? endpoint.postProcessors : []
11
+ for (const p of list) {
12
+ if (!p || p.enabled === false || p.type !== 'assert') continue
13
+ for (const a of Array.isArray(p.assertions) ? p.assertions : []) out.push(a)
14
+ }
15
+ return out
16
+ }
17
+
18
+ function firstScript(processors) {
19
+ const list = Array.isArray(processors) ? processors : []
20
+ for (const p of list) {
21
+ if (p && p.enabled !== false && p.type === 'script' && p.code) return String(p.code)
22
+ }
23
+ return ''
24
+ }
25
+
26
+ function extractAll(res, extractors) {
27
+ const out = {}
28
+ for (const ex of Array.isArray(extractors) ? extractors : []) {
29
+ if (!ex || !ex.name) continue
30
+ const v = pickValue(res, ex.source || 'body', ex.path)
31
+ if (v !== undefined) out[String(ex.name)] = v
32
+ }
33
+ return out
34
+ }
35
+
36
+ export function createTestRunner({ storage, request } = {}) {
37
+ function defaultEnv(project) {
38
+ const envs = Array.isArray(project.environments) ? project.environments : []
39
+ return envs.find((e) => e && e.isDefault) || envs[0] || null
40
+ }
41
+
42
+ function endpointOf(project, endpointId) {
43
+ const list = Array.isArray(project.endpoints) ? project.endpoints : []
44
+ return list.find((e) => e && String(e.id) === String(endpointId)) || null
45
+ }
46
+
47
+ /** endpoint + override 合并成 request.send 参数;local 为场景内累积变量。 */
48
+ function buildSendOptions(project, endpoint, override, local) {
49
+ const merged = { ...endpoint, ...(override && typeof override === 'object' ? override : {}) }
50
+ const settings = project.settings && typeof project.settings === 'object' ? project.settings : {}
51
+ return {
52
+ method: merged.method,
53
+ url: merged.url,
54
+ params: merged.params,
55
+ headers: merged.headers,
56
+ body: merged.body,
57
+ auth: merged.auth,
58
+ variables: { globals: project.globals, environment: defaultEnv(project), local },
59
+ timeoutMs: Number(settings.requestTimeoutMs) || 30000,
60
+ assertions: endpointAssertions(endpoint),
61
+ preScript: firstScript(endpoint.preProcessors),
62
+ postScript: firstScript(endpoint.postProcessors),
63
+ }
64
+ }
65
+
66
+ function verdict(res, extra) {
67
+ const results = [...((res && res.tests) || []), ...(extra || [])]
68
+ const error = res && res.error ? `${res.error.kind}: ${res.error.message}` : undefined
69
+ return { results, error, passed: results.every((r) => r.passed) && !res.error }
70
+ }
71
+
72
+ async function runCase(projectId, caseId) {
73
+ const started = Date.now()
74
+ const project = await storage.getProject(projectId)
75
+ // 用例落点:project.testCases 优先,回落 project.settings.testCases
76
+ // (test 页把用例与场景写在 settings 里,运行器必须两处都能读到,否则运行按钮永远报「用例不存在」)
77
+ const tcPool = Array.isArray(project.testCases) && project.testCases.length > 0
78
+ ? project.testCases
79
+ : (Array.isArray(project.settings && project.settings.testCases) ? project.settings.testCases : [])
80
+ const tc = tcPool.find((c) => c && String(c.id) === String(caseId))
81
+ if (!tc) throw new Error(`test case not found: ${caseId}`)
82
+ const endpoint = endpointOf(project, tc.endpointId)
83
+ if (!endpoint) throw new Error(`endpoint not found: ${tc.endpointId}`)
84
+ const options = buildSendOptions(project, endpoint, tc.override, {})
85
+ if (Array.isArray(tc.assertions)) options.assertions = [...options.assertions, ...tc.assertions]
86
+ const res = await request.send(options)
87
+ const { results, passed } = verdict(res)
88
+ return {
89
+ caseId,
90
+ passed,
91
+ results,
92
+ extracted: extractAll(res, tc.extractors),
93
+ durationMs: Date.now() - started,
94
+ }
95
+ }
96
+
97
+ async function runScenario(projectId, scenarioId) {
98
+ const started = Date.now()
99
+ const project = await storage.getProject(projectId)
100
+ const tsPool = Array.isArray(project.testScenarios) && project.testScenarios.length > 0
101
+ ? project.testScenarios
102
+ : (Array.isArray(project.settings && project.settings.testScenarios) ? project.settings.testScenarios : [])
103
+ const ts = tsPool.find((s) => s && String(s.id) === String(scenarioId))
104
+ if (!ts) throw new Error(`test scenario not found: ${scenarioId}`)
105
+ const local = {}
106
+ const steps = []
107
+ for (const step of Array.isArray(ts.steps) ? ts.steps : []) {
108
+ const stepId = step && step.id ? String(step.id) : `step_${steps.length + 1}`
109
+ const name = (step && step.name) || stepId
110
+ const endpoint = step ? endpointOf(project, step.endpointId) : null
111
+ if (!endpoint) {
112
+ steps.push({ id: stepId, name, status: 0, passed: false, extracted: {}, error: `endpoint not found: ${step && step.endpointId}` })
113
+ continue
114
+ }
115
+ // 局部变量按值快照传入,避免后续步骤写入影响已发出的请求
116
+ const res = await request.send(buildSendOptions(project, endpoint, step.override, { ...local }))
117
+ const extracted = extractAll(res, step.extract)
118
+ Object.assign(local, extracted)
119
+ const { results, passed, error } = verdict(res)
120
+ steps.push({ id: stepId, name, status: res.status, passed, extracted, results, error })
121
+ }
122
+ return { scenarioId, passed: steps.every((s) => s.passed), steps, durationMs: Date.now() - started }
123
+ }
124
+
125
+ return { runCase, runScenario }
126
+ }
@@ -0,0 +1,108 @@
1
+ /**
2
+ * 变量解析:契约 §4 createVariableResolver + DYNAMIC_VARS。
3
+ * 优先级 local > environment > globals(feature-matrix 第 7 行);未定义变量保留 {{key}} 原文。
4
+ * 变量集合三种入参形态都吃:数组 [{key,value,enabled}]、{variables:[...]}、普通对象 {k:v}。
5
+ */
6
+ import { randomUUID, randomBytes } from 'node:crypto'
7
+
8
+ const VAR_RE = /\{\{\s*([^{}]+?)\s*\}\}/g
9
+
10
+ function pad2(n) {
11
+ return String(n).padStart(2, '0')
12
+ }
13
+
14
+ function randInt(min, max) {
15
+ return Math.floor(Math.random() * (max - min + 1)) + min
16
+ }
17
+
18
+ function randStr(len) {
19
+ return randomBytes(Math.ceil(len / 2)).toString('hex').slice(0, len)
20
+ }
21
+
22
+ function ymd(d) {
23
+ return `${d.getFullYear()}-${pad2(d.getMonth() + 1)}-${pad2(d.getDate())}`
24
+ }
25
+
26
+ function ymdhms(d) {
27
+ return `${ymd(d)} ${pad2(d.getHours())}:${pad2(d.getMinutes())}:${pad2(d.getSeconds())}`
28
+ }
29
+
30
+ /** 契约 §4 的 11 个动态变量:{{$timestamp}} 形态,每次替换即时生成。 */
31
+ export const DYNAMIC_VARS = {
32
+ $timestamp: () => String(Math.floor(Date.now() / 1000)),
33
+ $isoTimestamp: () => new Date().toISOString(),
34
+ $randomInt: () => String(randInt(0, 1000)),
35
+ $randomString: () => randStr(12),
36
+ $uuid: () => randomUUID(),
37
+ $randomEmail: () => `user_${randStr(6)}@example.com`,
38
+ $randomPhone: () => `1${randInt(3, 9)}${String(randInt(0, 999999999)).padStart(9, '0')}`,
39
+ $randomBoolean: () => (Math.random() < 0.5 ? 'true' : 'false'),
40
+ $randomFloat: () => Math.random().toFixed(6),
41
+ $datetime: () => ymdhms(new Date()),
42
+ $date: () => ymd(new Date()),
43
+ }
44
+
45
+ function collectInto(out, input) {
46
+ if (!input || typeof input !== 'object') return
47
+ const list = Array.isArray(input) ? input : Array.isArray(input.variables) ? input.variables : null
48
+ if (list) {
49
+ for (const item of list) {
50
+ if (!item || item.enabled === false) continue
51
+ const key = item.key == null ? '' : String(item.key)
52
+ if (key === '') continue
53
+ out[key] = item.value == null ? '' : String(item.value)
54
+ }
55
+ // 环境对象的 baseUrl 也作为变量暴露({{baseUrl}} 是最高频用法)
56
+ if (!Array.isArray(input) && input.baseUrl) {
57
+ if (!Object.prototype.hasOwnProperty.call(out, 'baseUrl')) out.baseUrl = String(input.baseUrl)
58
+ }
59
+ return
60
+ }
61
+ for (const [k, v] of Object.entries(input)) {
62
+ if (v && typeof v === 'object') continue
63
+ out[k] = v == null ? '' : String(v)
64
+ }
65
+ }
66
+
67
+ /** 把任意形态的变量集合归一化成 { key: 'value' } 普通对象(request / script 共用)。 */
68
+ export function normalizeVariables(input) {
69
+ const out = {}
70
+ collectInto(out, input)
71
+ return out
72
+ }
73
+
74
+ /**
75
+ * createVariableResolver({ globals, environment, local })
76
+ * .resolve(text) -> string(未定义保留原文)
77
+ * .explain(text) -> { resolved, missing: [] }
78
+ */
79
+ export function createVariableResolver({ globals, environment, local } = {}) {
80
+ // 后写覆盖先写 → local 最强、globals 最弱
81
+ const table = Object.assign({}, normalizeVariables(globals), normalizeVariables(environment), normalizeVariables(local))
82
+
83
+ function resolve(text) {
84
+ if (typeof text !== 'string' || text === '') return text
85
+ return text.replace(VAR_RE, (raw, name) => {
86
+ const key = String(name).trim()
87
+ if (key.startsWith('$') && typeof DYNAMIC_VARS[key] === 'function') return String(DYNAMIC_VARS[key]())
88
+ if (Object.prototype.hasOwnProperty.call(table, key)) return table[key]
89
+ return raw
90
+ })
91
+ }
92
+
93
+ function explain(text) {
94
+ const missing = []
95
+ const resolved = resolve(text)
96
+ if (typeof text === 'string') {
97
+ for (const m of text.matchAll(VAR_RE)) {
98
+ const key = String(m[1]).trim()
99
+ if (key.startsWith('$') && typeof DYNAMIC_VARS[key] === 'function') continue
100
+ if (Object.prototype.hasOwnProperty.call(table, key)) continue
101
+ if (!missing.includes(key)) missing.push(key)
102
+ }
103
+ }
104
+ return { resolved, missing }
105
+ }
106
+
107
+ return { resolve, explain, variables: { ...table } }
108
+ }