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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "id": "dsh-external/dsh-skill-harbor",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "main": "./lib/index.js",
5
5
  "description": "技能港湾:技能发现、遮蔽判定、审计与市场管理(基础骨架)",
6
6
  "engines": {
@@ -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 }
@@ -0,0 +1,6 @@
1
+ /** 状态的显示标签(与 locales 分离,供 panel 复用)。 */
2
+ export const FILE_STATE_LABEL = Object.freeze({
3
+ editable: 'editable',
4
+ 'provider-file': 'provider-file',
5
+ virtual: 'virtual',
6
+ })