talizen 0.2.1 → 0.2.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 (2) hide show
  1. package/cms.js +24 -2
  2. package/package.json +1 -1
package/cms.js CHANGED
@@ -1,4 +1,18 @@
1
1
  import { requestJson } from "./core.js";
2
+ import { useLocale } from "./i18n.js";
3
+ // 字段级本地化解码:把 body._i18n[当前语言] 覆盖到同级字段并删除 _i18n。
4
+ // 线上渲染引擎已在服务端解码(body 无 _i18n)→ 此处 no-op;编辑器预览未解码 → 此处按当前语言解码。
5
+ function decodeBodyLocalization(body) {
6
+ if (!body || typeof body !== "object" || !("_i18n" in body))
7
+ return body;
8
+ const { _i18n, ...base } = body;
9
+ const locale = useLocale().locale;
10
+ const over = locale && _i18n ? _i18n[locale] : undefined;
11
+ return over && typeof over === "object" ? { ...base, ...over } : base;
12
+ }
13
+ function decodeItem(item) {
14
+ return item && item.body ? { ...item, body: decodeBodyLocalization(item.body) } : item;
15
+ }
2
16
  export async function getContentCollection(key, options) {
3
17
  return requestJson(`/cms/${key}`, undefined, options);
4
18
  }
@@ -14,6 +28,8 @@ export async function listContents(key, params = {}, options) {
14
28
  filter: params.filter,
15
29
  }),
16
30
  }, options);
31
+ if (response.list)
32
+ response.list = response.list.map(decodeItem);
17
33
  return response;
18
34
  }
19
35
  export async function getContent(key, slug, params, options) {
@@ -22,10 +38,11 @@ export async function getContent(key, slug, params, options) {
22
38
  if (params?.builtinRef != null) {
23
39
  url.searchParams.set("builtin_ref", String(params.builtinRef));
24
40
  }
25
- return requestJson(url.pathname + url.search, undefined, options);
41
+ const item = await requestJson(url.pathname + url.search, undefined, options);
42
+ return decodeItem(item);
26
43
  }
27
44
  export async function getContentWithPrevNext(key, slug, params = {}, options) {
28
- return requestJson(`/cms/${key}/content_with_prev_next`, {
45
+ const res = await requestJson(`/cms/${key}/content_with_prev_next`, {
29
46
  method: "POST",
30
47
  body: JSON.stringify({
31
48
  slug,
@@ -37,4 +54,9 @@ export async function getContentWithPrevNext(key, slug, params = {}, options) {
37
54
  filter: params.filter,
38
55
  }),
39
56
  }, options);
57
+ return {
58
+ current: res.current ? decodeItem(res.current) : res.current,
59
+ next: res.next ? decodeItem(res.next) : res.next,
60
+ prev: res.prev ? decodeItem(res.prev) : res.prev,
61
+ };
40
62
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "talizen",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "Talizen frontend SDK types for cms, form and core.",
5
5
  "type": "module",
6
6
  "license": "MIT",