@vigilkids/cms-client 0.1.3 → 0.1.4
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/dist/index.mjs +41 -10
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -32,6 +32,23 @@ function parseApiErrorEnvelope(body) {
|
|
|
32
32
|
}
|
|
33
33
|
return null;
|
|
34
34
|
}
|
|
35
|
+
function normalizeListResponse(body, endpoint) {
|
|
36
|
+
if (Array.isArray(body)) {
|
|
37
|
+
return body;
|
|
38
|
+
}
|
|
39
|
+
if (typeof body === "object" && body !== null && "data" in body) {
|
|
40
|
+
const data = body.data;
|
|
41
|
+
if (Array.isArray(data)) {
|
|
42
|
+
return data;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
throw new CmsApiError(
|
|
46
|
+
500,
|
|
47
|
+
"INVALID_LIST_RESPONSE",
|
|
48
|
+
`CMS API returned invalid list response for ${endpoint}`,
|
|
49
|
+
body
|
|
50
|
+
);
|
|
51
|
+
}
|
|
35
52
|
class CmsClient {
|
|
36
53
|
config;
|
|
37
54
|
fetchFn;
|
|
@@ -54,12 +71,13 @@ class CmsClient {
|
|
|
54
71
|
}
|
|
55
72
|
/** 获取相关文章 */
|
|
56
73
|
async getRelatedArticles(slug, options) {
|
|
74
|
+
const endpoint = `/articles/${encodeURIComponent(slug)}/related`;
|
|
57
75
|
const result = await this.request(
|
|
58
76
|
"GET",
|
|
59
|
-
|
|
77
|
+
endpoint,
|
|
60
78
|
options
|
|
61
79
|
);
|
|
62
|
-
return result
|
|
80
|
+
return normalizeListResponse(result, endpoint);
|
|
63
81
|
}
|
|
64
82
|
// ─── 作者 ───────────────────────────────────────────
|
|
65
83
|
/** 获取作者分页列表 */
|
|
@@ -69,7 +87,7 @@ class CmsClient {
|
|
|
69
87
|
/** 获取所有作者 */
|
|
70
88
|
async getAuthors(options) {
|
|
71
89
|
const result = await this.getAuthorsPage(options);
|
|
72
|
-
return result
|
|
90
|
+
return normalizeListResponse(result, "/authors");
|
|
73
91
|
}
|
|
74
92
|
/** 获取作者详情 */
|
|
75
93
|
async getAuthor(slug, locale) {
|
|
@@ -86,13 +104,19 @@ class CmsClient {
|
|
|
86
104
|
/** 获取作者的文章列表 */
|
|
87
105
|
async getAuthorArticles(slug, options) {
|
|
88
106
|
const result = await this.getAuthorArticlesPage(slug, options);
|
|
89
|
-
return
|
|
107
|
+
return normalizeListResponse(
|
|
108
|
+
result,
|
|
109
|
+
`/authors/${encodeURIComponent(slug)}/articles`
|
|
110
|
+
);
|
|
90
111
|
}
|
|
91
112
|
// ─── 分类 ───────────────────────────────────────────
|
|
92
113
|
/** 获取分类树 */
|
|
93
114
|
async getCategories() {
|
|
94
|
-
const result = await this.request(
|
|
95
|
-
|
|
115
|
+
const result = await this.request(
|
|
116
|
+
"GET",
|
|
117
|
+
"/categories"
|
|
118
|
+
);
|
|
119
|
+
return normalizeListResponse(result, "/categories");
|
|
96
120
|
}
|
|
97
121
|
// ─── 评论 ───────────────────────────────────────────
|
|
98
122
|
/** 获取文章评论树 */
|
|
@@ -111,13 +135,20 @@ class CmsClient {
|
|
|
111
135
|
// ─── Sitemap / 重定向 / 系列 ──────────────────────
|
|
112
136
|
/** 获取 Sitemap 条目 */
|
|
113
137
|
async getSitemap(locale) {
|
|
114
|
-
const result = await this.request(
|
|
115
|
-
|
|
138
|
+
const result = await this.request(
|
|
139
|
+
"GET",
|
|
140
|
+
"/sitemap",
|
|
141
|
+
{ locale }
|
|
142
|
+
);
|
|
143
|
+
return normalizeListResponse(result, "/sitemap");
|
|
116
144
|
}
|
|
117
145
|
/** 获取重定向规则 */
|
|
118
146
|
async getRedirects() {
|
|
119
|
-
const result = await this.request(
|
|
120
|
-
|
|
147
|
+
const result = await this.request(
|
|
148
|
+
"GET",
|
|
149
|
+
"/redirects"
|
|
150
|
+
);
|
|
151
|
+
return normalizeListResponse(result, "/redirects");
|
|
121
152
|
}
|
|
122
153
|
/** 获取文章系列 */
|
|
123
154
|
async getSeries(slug, locale) {
|