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,356 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 代码生成(契约 §3 / feature-matrix 16):generateCode({ endpoint, env, lang }) → string。
|
|
3
|
+
* 纯函数,不落盘、不发请求。支持 curl / fetch / axios / python / go / java / php / csharp 八种。
|
|
4
|
+
* env 提供 baseUrl 与 variables,用于 {{var}} 替换(未定义变量保留原文,便于用户回填)。
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const LANGS = ['curl', 'fetch', 'axios', 'python', 'go', 'java', 'php', 'csharp']
|
|
8
|
+
|
|
9
|
+
/** 变量替换:{{key}} 优先取 env.variables,其次 env 顶层同名键;未命中保留原文。 */
|
|
10
|
+
function makeReplacer(env) {
|
|
11
|
+
const vars = new Map()
|
|
12
|
+
const e = env && typeof env === 'object' ? env : {}
|
|
13
|
+
const push = (k, v) => {
|
|
14
|
+
if (typeof k === 'string' && k !== '' && v !== undefined && v !== null) vars.set(k, String(v))
|
|
15
|
+
}
|
|
16
|
+
if (Array.isArray(e.variables)) {
|
|
17
|
+
for (const row of e.variables) {
|
|
18
|
+
if (row && row.enabled !== false) push(row.key, row.value)
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
for (const [k, v] of Object.entries(e)) {
|
|
22
|
+
if (k !== 'variables' && typeof v !== 'object') push(k, v)
|
|
23
|
+
}
|
|
24
|
+
return (text) => String(text == null ? '' : text).replace(/\{\{\s*([^{}]+?)\s*\}\}/g, (whole, key) => {
|
|
25
|
+
const hit = vars.get(key)
|
|
26
|
+
return hit === undefined ? whole : hit
|
|
27
|
+
})
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function asArray(v) {
|
|
31
|
+
return Array.isArray(v) ? v : []
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function str(v) {
|
|
35
|
+
return typeof v === 'string' ? v : v == null ? '' : String(v)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** 拼完整 URL:绝对 URL 原样,相对 URL 补 env.baseUrl。 */
|
|
39
|
+
function fullUrl(endpoint, env, replace) {
|
|
40
|
+
const raw = replace(str(endpoint && endpoint.url))
|
|
41
|
+
const base = replace(str(env && env.baseUrl)).replace(/\/+$/, '')
|
|
42
|
+
const url = /^https?:\/\//i.test(raw) || base === '' ? raw : base + (raw.startsWith('/') ? raw : '/' + raw)
|
|
43
|
+
const params = asArray(endpoint && endpoint.params)
|
|
44
|
+
.filter((p) => p && p.enabled !== false && str(p.key) !== '')
|
|
45
|
+
.map((p) => encodeURIComponent(replace(str(p.key))) + '=' + encodeURIComponent(replace(str(p.value))))
|
|
46
|
+
if (params.length === 0) return url
|
|
47
|
+
return url + (url.includes('?') ? '&' : '?') + params.join('&')
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** 有效 header 列表(enabled !== false 且 key 非空),含 auth 注入的 header。 */
|
|
51
|
+
function headersOf(endpoint, replace) {
|
|
52
|
+
const out = []
|
|
53
|
+
for (const h of asArray(endpoint && endpoint.headers)) {
|
|
54
|
+
if (!h || h.enabled === false || str(h.key) === '') continue
|
|
55
|
+
out.push([replace(str(h.key)), replace(str(h.value))])
|
|
56
|
+
}
|
|
57
|
+
const auth = (endpoint && endpoint.auth) || {}
|
|
58
|
+
const type = str(auth.type) || 'none'
|
|
59
|
+
const has = (name) => out.some(([k]) => k.toLowerCase() === name.toLowerCase())
|
|
60
|
+
if (type === 'bearer' && str(auth.token) !== '' && !has('Authorization')) {
|
|
61
|
+
out.push(['Authorization', 'Bearer ' + replace(str(auth.token))])
|
|
62
|
+
}
|
|
63
|
+
if (type === 'basic' && !has('Authorization')) {
|
|
64
|
+
const pair = replace(str(auth.username)) + ':' + replace(str(auth.password))
|
|
65
|
+
out.push(['Authorization', 'Basic ' + Buffer.from(pair, 'utf8').toString('base64')])
|
|
66
|
+
}
|
|
67
|
+
if (type === 'apikey' && str(auth.addTo) !== 'query' && str(auth.key) !== '' && !has(str(auth.key))) {
|
|
68
|
+
out.push([replace(str(auth.key)), replace(str(auth.value))])
|
|
69
|
+
}
|
|
70
|
+
return out
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** 请求体:返回 { text, contentType } 或 null。urlencoded/form 由调用方按语言分别渲染。 */
|
|
74
|
+
function bodyOf(endpoint, replace) {
|
|
75
|
+
const body = (endpoint && endpoint.body) || {}
|
|
76
|
+
const type = str(body.type) || 'none'
|
|
77
|
+
if (type === 'none') return null
|
|
78
|
+
const content = replace(str(body.content))
|
|
79
|
+
if (type === 'json') return { type, text: content === '' ? '{}' : content, contentType: 'application/json' }
|
|
80
|
+
if (type === 'xml') return { type, text: content, contentType: 'application/xml' }
|
|
81
|
+
if (type === 'urlencoded') return { type, text: content, contentType: 'application/x-www-form-urlencoded' }
|
|
82
|
+
if (type === 'form') return { type, text: content, contentType: 'multipart/form-data' }
|
|
83
|
+
if (type === 'graphql') return { type, text: content === '' ? '{"query":""}' : content, contentType: 'application/json' }
|
|
84
|
+
if (type === 'binary') return { type, text: '', contentType: 'application/octet-stream' }
|
|
85
|
+
return { type: 'raw', text: content, contentType: 'text/plain' }
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** urlencoded/form 的 content(JSON 数组字符串)→ [[k,v]];非法 JSON 退化为空表。 */
|
|
89
|
+
function rowsOf(text) {
|
|
90
|
+
try {
|
|
91
|
+
const rows = JSON.parse(text)
|
|
92
|
+
return asArray(rows).filter((r) => r && str(r.key) !== '').map((r) => [str(r.key), str(r.value)])
|
|
93
|
+
} catch {
|
|
94
|
+
return []
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function methodOf(endpoint) {
|
|
99
|
+
const m = str(endpoint && endpoint.method).toUpperCase()
|
|
100
|
+
return m === '' ? 'GET' : m
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/* ── 各语言渲染 ────────────────────────────────────────────────────────── */
|
|
104
|
+
|
|
105
|
+
function genCurl(ctx) {
|
|
106
|
+
const lines = ['curl -X ' + ctx.method + ' \'' + ctx.url + '\'']
|
|
107
|
+
for (const [k, v] of ctx.headers) lines.push(' -H \'' + k + ': ' + v + '\'')
|
|
108
|
+
const b = ctx.body
|
|
109
|
+
if (b) {
|
|
110
|
+
if (b.type === 'urlencoded' || b.type === 'form') {
|
|
111
|
+
for (const [k, v] of rowsOf(b.text)) lines.push(' -d \'' + k + '=' + v + '\'')
|
|
112
|
+
} else if (b.type !== 'binary' && b.text !== '') {
|
|
113
|
+
lines.push(' -d \'' + b.text.replace(/'/g, "'\\''") + '\'')
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return lines.join(' \\\n')
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function genFetch(ctx) {
|
|
120
|
+
const opts = [' method: \'' + ctx.method + '\',']
|
|
121
|
+
opts.push(' headers: {')
|
|
122
|
+
for (const [k, v] of ctx.headers) opts.push(' ' + JSON.stringify(k) + ': ' + JSON.stringify(v) + ',')
|
|
123
|
+
opts.push(' },')
|
|
124
|
+
if (ctx.body && ctx.body.type !== 'binary' && ctx.body.text !== '') {
|
|
125
|
+
opts.push(' body: ' + JSON.stringify(ctx.body.text) + ',')
|
|
126
|
+
}
|
|
127
|
+
return [
|
|
128
|
+
'const res = await fetch(' + JSON.stringify(ctx.url) + ', {',
|
|
129
|
+
...opts,
|
|
130
|
+
'})',
|
|
131
|
+
'const data = await res.json()',
|
|
132
|
+
].join('\n')
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function genAxios(ctx) {
|
|
136
|
+
const lines = ['import axios from \'axios\'', '', 'const res = await axios({']
|
|
137
|
+
lines.push(' method: ' + JSON.stringify(ctx.method.toLowerCase()) + ',')
|
|
138
|
+
lines.push(' url: ' + JSON.stringify(ctx.url) + ',')
|
|
139
|
+
if (ctx.headers.length > 0) {
|
|
140
|
+
lines.push(' headers: {')
|
|
141
|
+
for (const [k, v] of ctx.headers) lines.push(' ' + JSON.stringify(k) + ': ' + JSON.stringify(v) + ',')
|
|
142
|
+
lines.push(' },')
|
|
143
|
+
}
|
|
144
|
+
const b = ctx.body
|
|
145
|
+
if (b) {
|
|
146
|
+
if (b.type === 'urlencoded' || b.type === 'form') {
|
|
147
|
+
lines.push(' data: new URLSearchParams({')
|
|
148
|
+
for (const [k, v] of rowsOf(b.text)) lines.push(' ' + JSON.stringify(k) + ': ' + JSON.stringify(v) + ',')
|
|
149
|
+
lines.push(' }),')
|
|
150
|
+
} else if (b.type !== 'binary' && b.text !== '') {
|
|
151
|
+
lines.push(' data: ' + b.text + ',')
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
lines.push('})')
|
|
155
|
+
lines.push('console.log(res.data)')
|
|
156
|
+
return lines.join('\n')
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function pyQuote(v) {
|
|
160
|
+
return JSON.stringify(v)
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function genPython(ctx) {
|
|
164
|
+
const lines = ['import requests', '', 'url = ' + pyQuote(ctx.url)]
|
|
165
|
+
if (ctx.headers.length > 0) {
|
|
166
|
+
lines.push('headers = {')
|
|
167
|
+
for (const [k, v] of ctx.headers) lines.push(' ' + pyQuote(k) + ': ' + pyQuote(v) + ',')
|
|
168
|
+
lines.push('}')
|
|
169
|
+
} else {
|
|
170
|
+
lines.push('headers = {}')
|
|
171
|
+
}
|
|
172
|
+
const b = ctx.body
|
|
173
|
+
if (b && (b.type === 'urlencoded' || b.type === 'form')) {
|
|
174
|
+
lines.push('data = {')
|
|
175
|
+
for (const [k, v] of rowsOf(b.text)) lines.push(' ' + pyQuote(k) + ': ' + pyQuote(v) + ',')
|
|
176
|
+
lines.push('}')
|
|
177
|
+
} else if (b && b.type !== 'binary' && b.text !== '') {
|
|
178
|
+
lines.push('data = ' + pyQuote(b.text))
|
|
179
|
+
} else {
|
|
180
|
+
lines.push('data = None')
|
|
181
|
+
}
|
|
182
|
+
lines.push('')
|
|
183
|
+
lines.push('res = requests.request(' + pyQuote(ctx.method) + ', url, headers=headers, data=data, timeout=30)')
|
|
184
|
+
lines.push('print(res.status_code, res.text)')
|
|
185
|
+
return lines.join('\n')
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function goQuote(v) {
|
|
189
|
+
return JSON.stringify(v)
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function genGo(ctx) {
|
|
193
|
+
const lines = [
|
|
194
|
+
'package main',
|
|
195
|
+
'',
|
|
196
|
+
'import (',
|
|
197
|
+
'\t"fmt"',
|
|
198
|
+
'\t"io"',
|
|
199
|
+
'\t"net/http"',
|
|
200
|
+
'\t"strings"',
|
|
201
|
+
')',
|
|
202
|
+
'',
|
|
203
|
+
'func main() {',
|
|
204
|
+
]
|
|
205
|
+
const b = ctx.body
|
|
206
|
+
const hasBody = !!b && b.type !== 'binary' && b.text !== ''
|
|
207
|
+
const payload = hasBody ? goQuote(b.text) : '""'
|
|
208
|
+
lines.push('\tpayload := strings.NewReader(' + payload + ')')
|
|
209
|
+
lines.push('\treq, err := http.NewRequest(' + goQuote(ctx.method) + ', ' + goQuote(ctx.url) + ', payload)')
|
|
210
|
+
lines.push('\tif err != nil {')
|
|
211
|
+
lines.push('\t\tpanic(err)')
|
|
212
|
+
lines.push('\t}')
|
|
213
|
+
for (const [k, v] of ctx.headers) lines.push('\treq.Header.Set(' + goQuote(k) + ', ' + goQuote(v) + ')')
|
|
214
|
+
lines.push('\tres, err := http.DefaultClient.Do(req)')
|
|
215
|
+
lines.push('\tif err != nil {')
|
|
216
|
+
lines.push('\t\tpanic(err)')
|
|
217
|
+
lines.push('\t}')
|
|
218
|
+
lines.push('\tdefer res.Body.Close()')
|
|
219
|
+
lines.push('\tbody, _ := io.ReadAll(res.Body)')
|
|
220
|
+
lines.push('\tfmt.Println(res.Status)')
|
|
221
|
+
lines.push('\tfmt.Println(string(body))')
|
|
222
|
+
lines.push('}')
|
|
223
|
+
return lines.join('\n')
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function javaQuote(v) {
|
|
227
|
+
return '"' + String(v).replace(/\\/g, '\\\\').replace(/"/g, '\\"') + '"'
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function genJava(ctx) {
|
|
231
|
+
const lines = [
|
|
232
|
+
'import java.net.URI;',
|
|
233
|
+
'import java.net.http.HttpClient;',
|
|
234
|
+
'import java.net.http.HttpRequest;',
|
|
235
|
+
'import java.net.http.HttpResponse;',
|
|
236
|
+
'',
|
|
237
|
+
'public class Main {',
|
|
238
|
+
' public static void main(String[] args) throws Exception {',
|
|
239
|
+
' HttpClient client = HttpClient.newHttpClient();',
|
|
240
|
+
]
|
|
241
|
+
const b = ctx.body
|
|
242
|
+
const hasBody = !!b && b.type !== 'binary' && b.text !== ''
|
|
243
|
+
lines.push(' HttpRequest.Builder builder = HttpRequest.newBuilder()')
|
|
244
|
+
lines.push(' .uri(URI.create(' + javaQuote(ctx.url) + '))')
|
|
245
|
+
for (const [k, v] of ctx.headers) {
|
|
246
|
+
lines.push(' .header(' + javaQuote(k) + ', ' + javaQuote(v) + ')')
|
|
247
|
+
}
|
|
248
|
+
const bodyArg = hasBody
|
|
249
|
+
? 'HttpRequest.BodyPublishers.ofString(' + javaQuote(b.text) + ')'
|
|
250
|
+
: 'HttpRequest.BodyPublishers.noBody()'
|
|
251
|
+
lines.push(' .method(' + javaQuote(ctx.method) + ', ' + bodyArg + ');')
|
|
252
|
+
lines.push(' HttpResponse<String> res = client.send(builder.build(), HttpResponse.BodyHandlers.ofString());')
|
|
253
|
+
lines.push(' System.out.println(res.statusCode());')
|
|
254
|
+
lines.push(' System.out.println(res.body());')
|
|
255
|
+
lines.push(' }')
|
|
256
|
+
lines.push('}')
|
|
257
|
+
return lines.join('\n')
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function phpQuote(v) {
|
|
261
|
+
return "'" + String(v).replace(/\\/g, '\\\\').replace(/'/g, "\\'") + "'"
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function genPhp(ctx) {
|
|
265
|
+
const lines = ['<?php', '$ch = curl_init();']
|
|
266
|
+
lines.push('curl_setopt($ch, CURLOPT_URL, ' + phpQuote(ctx.url) + ');')
|
|
267
|
+
lines.push('curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);')
|
|
268
|
+
lines.push('curl_setopt($ch, CURLOPT_CUSTOMREQUEST, ' + phpQuote(ctx.method) + ');')
|
|
269
|
+
if (ctx.headers.length > 0) {
|
|
270
|
+
lines.push('curl_setopt($ch, CURLOPT_HTTPHEADER, [')
|
|
271
|
+
for (const [k, v] of ctx.headers) lines.push(' ' + phpQuote(k + ': ' + v) + ',')
|
|
272
|
+
lines.push(']);')
|
|
273
|
+
}
|
|
274
|
+
const b = ctx.body
|
|
275
|
+
if (b && (b.type === 'urlencoded' || b.type === 'form')) {
|
|
276
|
+
lines.push('curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([')
|
|
277
|
+
for (const [k, v] of rowsOf(b.text)) lines.push(' ' + phpQuote(k) + ' => ' + phpQuote(v) + ',')
|
|
278
|
+
lines.push(']));')
|
|
279
|
+
} else if (b && b.type !== 'binary' && b.text !== '') {
|
|
280
|
+
lines.push('curl_setopt($ch, CURLOPT_POSTFIELDS, ' + phpQuote(b.text) + ');')
|
|
281
|
+
}
|
|
282
|
+
lines.push('$response = curl_exec($ch);')
|
|
283
|
+
lines.push('$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);')
|
|
284
|
+
lines.push('curl_close($ch);')
|
|
285
|
+
lines.push('echo $status . PHP_EOL;')
|
|
286
|
+
lines.push('echo $response . PHP_EOL;')
|
|
287
|
+
return lines.join('\n')
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
function csQuote(v) {
|
|
291
|
+
return '"' + String(v).replace(/\\/g, '\\\\').replace(/"/g, '\\"') + '"'
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
function genCsharp(ctx) {
|
|
295
|
+
const lines = [
|
|
296
|
+
'using System;',
|
|
297
|
+
'using System.Net.Http;',
|
|
298
|
+
'using System.Text;',
|
|
299
|
+
'using System.Threading.Tasks;',
|
|
300
|
+
'',
|
|
301
|
+
'class Program',
|
|
302
|
+
'{',
|
|
303
|
+
' static async Task Main()',
|
|
304
|
+
' {',
|
|
305
|
+
' using var client = new HttpClient();',
|
|
306
|
+
]
|
|
307
|
+
lines.push(' var request = new HttpRequestMessage(new HttpMethod(' + csQuote(ctx.method) + '), ' + csQuote(ctx.url) + ');')
|
|
308
|
+
for (const [k, v] of ctx.headers) {
|
|
309
|
+
lines.push(' request.Headers.TryAddWithoutValidation(' + csQuote(k) + ', ' + csQuote(v) + ');')
|
|
310
|
+
}
|
|
311
|
+
const b = ctx.body
|
|
312
|
+
if (b && b.type !== 'binary' && b.text !== '') {
|
|
313
|
+
lines.push(' request.Content = new StringContent(' + csQuote(b.text) + ', Encoding.UTF8, ' + csQuote(b.contentType) + ');')
|
|
314
|
+
}
|
|
315
|
+
lines.push(' var response = await client.SendAsync(request);')
|
|
316
|
+
lines.push(' var body = await response.Content.ReadAsStringAsync();')
|
|
317
|
+
lines.push(' Console.WriteLine((int)response.StatusCode);')
|
|
318
|
+
lines.push(' Console.WriteLine(body);')
|
|
319
|
+
lines.push(' }')
|
|
320
|
+
lines.push('}')
|
|
321
|
+
return lines.join('\n')
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
const GENERATORS = {
|
|
325
|
+
curl: genCurl,
|
|
326
|
+
fetch: genFetch,
|
|
327
|
+
axios: genAxios,
|
|
328
|
+
python: genPython,
|
|
329
|
+
go: genGo,
|
|
330
|
+
java: genJava,
|
|
331
|
+
php: genPhp,
|
|
332
|
+
csharp: genCsharp,
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* 生成请求代码。lang 缺省 'curl';未知语言抛错(bridge 侧转 400)。
|
|
337
|
+
* 返回字符串,末尾无换行。
|
|
338
|
+
*/
|
|
339
|
+
export function generateCode({ endpoint, env, lang } = {}) {
|
|
340
|
+
const language = str(lang) === '' ? 'curl' : str(lang).toLowerCase()
|
|
341
|
+
const gen = GENERATORS[language]
|
|
342
|
+
if (!gen) throw new Error('generateCode: unsupported lang ' + lang)
|
|
343
|
+
const replace = makeReplacer(env)
|
|
344
|
+
const ctx = {
|
|
345
|
+
method: methodOf(endpoint),
|
|
346
|
+
url: fullUrl(endpoint, env, replace),
|
|
347
|
+
headers: headersOf(endpoint, replace),
|
|
348
|
+
body: bodyOf(endpoint, replace),
|
|
349
|
+
}
|
|
350
|
+
return gen(ctx)
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/** 供 bridge/页面列出可选语言。 */
|
|
354
|
+
export function listLanguages() {
|
|
355
|
+
return [...LANGS]
|
|
356
|
+
}
|