@nsnanocat/preference-panes 0.7.1 → 0.8.0

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.
@@ -1,43 +1,29 @@
1
1
  import { URL } from "@nsnanocat/url";
2
2
  import assets from "#assets";
3
- import { BoxJS } from "../BoxJS.mjs";
4
3
  import { pageInputs } from "../lib/page-inputs.mjs";
5
4
  import { response } from "../lib/response.mjs";
6
5
  import { Store } from "../Store.mjs";
7
6
  import { complete } from "./response.mjs";
8
7
 
9
8
  /**
10
- * 仅为导入的模块提供页面与存储服务,项目主页由调用方自行托管。
11
- * Serve only the imported module's page and persistence; callers host their own project landing pages.
12
- * @param {unknown} boxjs BoxJS JSON / BoxJS JSON.
13
- * @param {string} [css] 自定义 CSS 正文 / Custom CSS text.
14
- * @returns {Promise<void>} 已提交宿主响应 / Delivered host response.
9
+ * 通用代理入口:提供无鉴权 form 存储 API 及模块页面,不含业务配置。
10
+ * Generic proxy entry serving unauthenticated form storage and module pages without business configuration.
11
+ * @returns {Promise<void>} 已交给代理宿主的响应 / Response delivered to the proxy host.
15
12
  */
16
- export async function run(boxjs, css = "") {
13
+ async function run() {
17
14
  const request = globalThis.$request;
18
15
  let result;
19
16
  try {
20
- if (typeof css !== "string") throw new TypeError("CSS must be a string");
21
- const catalog = new BoxJS(boxjs);
22
- const module = catalog.module.module;
23
17
  const url = new URL(request.url);
24
18
  switch (true) {
25
19
  case url.pathname.startsWith("/api/"):
26
- result = await new Store(catalog).handle(request, url);
20
+ result = await new Store().handle(request, url);
27
21
  break;
28
- case url.pathname.startsWith("/configs/"):
29
- break;
30
- case url.pathname === `/settings/${module}` || url.pathname === `/settings/${module}/`: {
31
- // Header 由代理传入文档,浏览器再下载 JSON/CSS;代理不获取外部资源。
32
- // Carry headers into the document; only the browser downloads external JSON/CSS.
22
+ case /^\/settings\/[a-zA-Z0-9_-]+\/?$/.test(url.pathname): {
33
23
  const inputs = encodeURIComponent(JSON.stringify(pageInputs(url, request.headers)));
34
- const html = assets.page.body.replace("</head>", `<meta name="preference-panes-inputs" content="${inputs}"></head>`);
35
- result = response(request, 200, html, "text/html");
24
+ result = response(request, 200, assets.page.body.replace("</head>", `<meta name="preference-panes-inputs" content="${inputs}"></head>`), "text/html");
36
25
  break;
37
26
  }
38
- case url.pathname === `/settings/assets/${module}.css`:
39
- result = response(request, 200, css, "text/css");
40
- break;
41
27
  default: {
42
28
  const asset = assets[url.pathname];
43
29
  if (asset) result = response(request, 200, asset.body, asset.type);
@@ -46,7 +32,8 @@ export async function run(boxjs, css = "") {
46
32
  if (result && !url.pathname.startsWith("/api/") && !["GET", "HEAD"].includes(request.method)) result = response(request, 405, { error: "Method not allowed" });
47
33
  } catch (error) {
48
34
  console.error(`PreferencePanes: ${error.message}`);
49
- result = response(request, 500, { error: "Settings execution failed" });
35
+ result = response(request, 500, { error: error.message });
50
36
  }
51
37
  complete(result);
52
38
  }
39
+ run();