dsh-skill-harbor 0.3.0 → 0.3.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/dsh.plugin.json +1 -1
- package/lib/client/api.js +79 -0
- package/lib/client/labels.js +6 -0
- package/lib/client/locales.js +688 -0
- package/lib/client/panel.js +284 -0
- package/lib/client.js +13 -4
- package/lib/config.js +42 -0
- package/lib/index.js +14 -6
- package/lib/market/install.js +169 -0
- package/lib/market/zip.js +201 -0
- package/lib/routes/index.js +68 -0
- package/package.json +1 -1
- package/src/client/api.js +79 -0
- package/src/client/labels.js +6 -0
- package/src/client/locales.js +688 -0
- package/src/client/panel.js +284 -0
- package/src/client.js +13 -4
- package/src/config.js +42 -0
- package/src/index.js +14 -6
- package/src/market/install.js +169 -0
- package/src/market/zip.js +201 -0
- package/src/routes/index.js +68 -0
package/dsh.plugin.json
CHANGED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 客户端唯一 fetch 出口。
|
|
3
|
+
*
|
|
4
|
+
* 必须按 `document.baseURI` 解析 —— 反代带路径前缀(如 `/app/dsh/`)部署时,
|
|
5
|
+
* 写死 `/api/...` 会打到站点根而不是应用根,全部 404。
|
|
6
|
+
*/
|
|
7
|
+
import { LOCALES } from './locales.js'
|
|
8
|
+
|
|
9
|
+
export const API_PREFIX = '/dsh-skill-harbor/api'
|
|
10
|
+
export const CSRF_HEADER = 'x-dsh-skill-harbor'
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 相对路径 → 绝对 URL,锚定到 document.baseURI。
|
|
14
|
+
* 非 `/` 开头或无 document 环境时原样返回。
|
|
15
|
+
*/
|
|
16
|
+
export function resolveApiUrl(path) {
|
|
17
|
+
if (typeof path !== 'string') return String(path)
|
|
18
|
+
try {
|
|
19
|
+
if (path.startsWith('/') && typeof document !== 'undefined' && document.baseURI) {
|
|
20
|
+
return new URL(path.slice(1), document.baseURI).href
|
|
21
|
+
}
|
|
22
|
+
} catch {
|
|
23
|
+
/* 落到原样返回 */
|
|
24
|
+
}
|
|
25
|
+
return path
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async function request(path, options = {}) {
|
|
29
|
+
const url = resolveApiUrl(`${API_PREFIX}${path}`)
|
|
30
|
+
const method = options.method ?? 'GET'
|
|
31
|
+
|
|
32
|
+
const headers = { ...options.headers }
|
|
33
|
+
if (method !== 'GET' && method !== 'HEAD') {
|
|
34
|
+
// 写方法必须带自定义头;跨站表单设不了这个头
|
|
35
|
+
headers[CSRF_HEADER] = '1'
|
|
36
|
+
if (options.body !== undefined) headers['content-type'] = 'application/json'
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const response = await fetch(url, {
|
|
40
|
+
method,
|
|
41
|
+
headers,
|
|
42
|
+
credentials: 'same-origin',
|
|
43
|
+
...options.body === undefined ? {} : { body: JSON.stringify(options.body) },
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
let payload = null
|
|
47
|
+
try {
|
|
48
|
+
payload = await response.json()
|
|
49
|
+
} catch {
|
|
50
|
+
payload = null
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (!response.ok) {
|
|
54
|
+
const error = new Error(payload?.message ?? payload?.error ?? `HTTP ${response.status}`)
|
|
55
|
+
error.status = response.status
|
|
56
|
+
error.payload = payload
|
|
57
|
+
throw error
|
|
58
|
+
}
|
|
59
|
+
return payload
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export const api = {
|
|
63
|
+
list: () => request('/list'),
|
|
64
|
+
read: path => request(`/read?path=${encodeURIComponent(path)}`),
|
|
65
|
+
audit: () => request('/audit'),
|
|
66
|
+
budget: () => request('/budget'),
|
|
67
|
+
setEnabled: (path, enabled) => request('/set-enabled', { method: 'POST', body: { path, enabled } }),
|
|
68
|
+
update: (path, content) => request('/update', { method: 'POST', body: { path, content } }),
|
|
69
|
+
remove: path => request('/delete', { method: 'POST', body: { path } }),
|
|
70
|
+
importArchive: (archive, targetSource) =>
|
|
71
|
+
request('/import', { method: 'POST', body: { archive, targetSource } }),
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** 取某语言的字典,缺省回落 zh。 */
|
|
75
|
+
export function dict(lang) {
|
|
76
|
+
return LOCALES[lang] ?? LOCALES.zh
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export { request }
|