@aronnaxlin/bgm-cli 1.0.0 → 1.1.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.
@@ -12,6 +12,7 @@ import {
12
12
  import {
13
13
  EPISODE_COLLECTION_STATUS_MAP,
14
14
  EPISODE_TYPE_MAP,
15
+ SUBJECT_TYPE_MAP,
15
16
  normalizeEpisodeCollectionStatusValue,
16
17
  normalizeEpisodeTypeFilter,
17
18
  } from "../utils/validators.js";
@@ -135,6 +136,15 @@ export async function executeEpisodeListCommand(args) {
135
136
  handleEpisodeListError(error, subjectId);
136
137
  }
137
138
 
139
+ if (filtered.length === 0) {
140
+ const subject = await client.getSubject(subjectId);
141
+ if (Number(subject?.type) === SUBJECT_TYPE_MAP.book) {
142
+ throw new CommandError(
143
+ `Subject ${subjectId} is a book-type entry. Bangumi does not provide episode lists for books. Use \`bgm book ep ${subjectId} <chapter_number>\` to track reading progress.`,
144
+ );
145
+ }
146
+ }
147
+
138
148
  return {
139
149
  ...result,
140
150
  resource: "episode-list",
@@ -200,6 +210,12 @@ export async function executeEpisodeWatchCommand(args) {
200
210
  const episodes = await fetchAllEpisodes(client, subjectId, { type: EPISODE_TYPE_MAP.main });
201
211
  const episode = episodes.find((item) => Number(item?.type) === EPISODE_TYPE_MAP.main && Number(item?.ep) === episodeNumber);
202
212
  if (!episode) {
213
+ const subject = await client.getSubject(subjectId);
214
+ if (Number(subject?.type) === SUBJECT_TYPE_MAP.book) {
215
+ throw new CommandError(
216
+ `Subject ${subjectId} is a book-type entry. Bangumi does not support episode-level tracking for books. Use \`bgm book ep ${subjectId} ${episodeNumber}\` to update reading progress.`,
217
+ );
218
+ }
203
219
  throw new CommandError(`Could not find main episode ${episodeNumber} under subject ${subjectId}.`);
204
220
  }
205
221
 
@@ -372,6 +388,12 @@ async function resolveEpisodeByNumber(client, subjectId, episodeNumber, typeFilt
372
388
 
373
389
  if (!episode) {
374
390
  const typeLabel = typeFilter.label ?? "main";
391
+ const subject = await client.getSubject(subjectId);
392
+ if (Number(subject?.type) === SUBJECT_TYPE_MAP.book) {
393
+ throw new CommandError(
394
+ `Subject ${subjectId} is a book-type entry. Bangumi does not support episode-level tracking for books. Use \`bgm book ep ${subjectId} ${episodeNumber}\` to update reading progress.`,
395
+ );
396
+ }
375
397
  throw new CommandError(`Could not find ${typeLabel} episode ${episodeNumber} under subject ${subjectId}.`);
376
398
  }
377
399
 
@@ -0,0 +1,114 @@
1
+ /**
2
+ * Search command group — powered by the SearchEncore API.
3
+ *
4
+ * ⚠️ All commands in this group use the SearchEncore service at bgmdb.ry.mk.
5
+ * Results are crawled/aggregated data, NOT from bangumi.tv official sources.
6
+ * Every response carries _meta.isSearchEncore = true.
7
+ *
8
+ * This module is intentionally isolated from official API command groups
9
+ * (subject, user, group, etc.) to avoid path collisions.
10
+ */
11
+
12
+ import {
13
+ searchSubjects,
14
+ searchUsers,
15
+ searchGroups,
16
+ searchReplies,
17
+ searchGroupTopics,
18
+ searchSubjectTopics,
19
+ searchIndexes,
20
+ searchBlogs,
21
+ } from "../core/community-api.js";
22
+ import { CommandError, printResult } from "../core/output.js";
23
+ import { firstPositional, parseFlags } from "../utils/args.js";
24
+ import { normalizeNonNegativeInteger, normalizePageSize } from "../utils/helpers.js";
25
+
26
+ export async function runSearchCommand(command, args, context) {
27
+ switch (command) {
28
+ case "subject":
29
+ case "subjects": {
30
+ const result = await executeEntitySearch("subjects", args, searchSubjects);
31
+ printResult(result, context);
32
+ return;
33
+ }
34
+ case "user":
35
+ case "users": {
36
+ const result = await executeEntitySearch("users", args, searchUsers);
37
+ printResult(result, context);
38
+ return;
39
+ }
40
+ case "group":
41
+ case "groups": {
42
+ const result = await executeEntitySearch("groups", args, searchGroups);
43
+ printResult(result, context);
44
+ return;
45
+ }
46
+ case "topic":
47
+ case "topics":
48
+ case "group-topic":
49
+ case "group-topics": {
50
+ const result = await executeEntitySearch("group-topics", args, searchGroupTopics);
51
+ printResult(result, context);
52
+ return;
53
+ }
54
+ case "subject-topic":
55
+ case "subject-topics": {
56
+ const result = await executeEntitySearch("subject-topics", args, searchSubjectTopics);
57
+ printResult(result, context);
58
+ return;
59
+ }
60
+ case "reply":
61
+ case "replies": {
62
+ const result = await executeEntitySearch("replies", args, searchReplies);
63
+ printResult(result, context);
64
+ return;
65
+ }
66
+ case "index":
67
+ case "indexes": {
68
+ const result = await executeEntitySearch("indexes", args, searchIndexes);
69
+ printResult(result, context);
70
+ return;
71
+ }
72
+ case "blog":
73
+ case "blogs": {
74
+ const result = await executeEntitySearch("blogs", args, searchBlogs);
75
+ printResult(result, context);
76
+ return;
77
+ }
78
+ default:
79
+ throw new CommandError(
80
+ "Usage: bgm search <subject|user|group|topic|subject-topic|reply|index|blog> <keyword> [--limit n] [--offset n] [--sort <sort>]",
81
+ );
82
+ }
83
+ }
84
+
85
+ async function executeEntitySearch(resource, args, searchFn) {
86
+ const options = parseFlags(args);
87
+ const keyword = firstPositional(options);
88
+ if (!keyword) {
89
+ throw new CommandError(`Usage: bgm search ${resource} <keyword> [--limit n] [--offset n] [--sort <sort>]`);
90
+ }
91
+
92
+ const query = {
93
+ q: keyword,
94
+ limit: normalizePageSize(options.limit),
95
+ offset: normalizeNonNegativeInteger(options.offset, "offset"),
96
+ };
97
+ if (options.sort) {
98
+ query.sort = options.sort;
99
+ }
100
+
101
+ const result = await searchFn(query);
102
+
103
+ return {
104
+ ...result,
105
+ resource: `community-${resource}`,
106
+ title: `SearchEncore: ${resource}`,
107
+ filters: {
108
+ keyword,
109
+ limit: query.limit,
110
+ offset: query.offset,
111
+ sort: query.sort,
112
+ },
113
+ };
114
+ }
@@ -12,6 +12,8 @@ import {
12
12
  import {
13
13
  normalizeCollectionStatusValue,
14
14
  normalizeSubjectType,
15
+ normalizeTagFilter,
16
+ normalizeTagsCat,
15
17
  } from "../utils/validators.js";
16
18
  import { fetchAllSubjects, sortSubjectsByRank } from "../utils/collection.js";
17
19
  import { resolveTurnstileTokenForMutation } from "../utils/turnstile-flow.js";
@@ -131,6 +133,9 @@ export async function executeSubjectListCommand(args) {
131
133
 
132
134
  const limit = parseOptionalInteger(options.limit);
133
135
  const offset = parseOptionalInteger(options.offset);
136
+ const tagFilter = normalizeTagFilter(options.tag);
137
+ const tagsCat = normalizeTagsCat(options.tagsCat);
138
+
134
139
  const query = {
135
140
  type,
136
141
  cat: options.cat,
@@ -139,6 +144,8 @@ export async function executeSubjectListCommand(args) {
139
144
  sort: options.sort,
140
145
  year: parseOptionalInteger(options.year),
141
146
  month: parseOptionalInteger(options.month),
147
+ tags: tagFilter.length > 0 ? tagFilter : undefined,
148
+ tagsCat,
142
149
  };
143
150
 
144
151
  let result;
@@ -166,6 +173,8 @@ export async function executeSubjectListCommand(args) {
166
173
  cat: options.cat,
167
174
  series: parseOptionalBoolean(options.series),
168
175
  platform: options.platform,
176
+ tag: tagFilter,
177
+ tagsCat,
169
178
  },
170
179
  };
171
180
  }
@@ -0,0 +1,108 @@
1
+ /**
2
+ * URL command: turn a pasted Bangumi link into the equivalent bgm CLI command.
3
+ *
4
+ * Resolution is read-only. A link never triggers a write operation.
5
+ */
6
+
7
+ import { CommandError, printResult } from "../core/output.js";
8
+ import {
9
+ formatResolvedCommand,
10
+ looksLikeBangumiUrl,
11
+ resolveBangumiUrl,
12
+ } from "../utils/bangumi-url.js";
13
+ import { runBlogCommand } from "./blog.js";
14
+ import { runCalendarCommand } from "./calendar.js";
15
+ import { runCharacterCommand } from "./character.js";
16
+ import { runCollectionCommand } from "./collection.js";
17
+ import { runEpisodeCommand } from "./episode.js";
18
+ import { runGroupCommand } from "./group.js";
19
+ import { runIndexCommand } from "./index.js";
20
+ import { runNotifyCommand } from "./notify.js";
21
+ import { runPersonCommand } from "./person.js";
22
+ import { runSubjectCommand } from "./subject.js";
23
+ import { runTimelineCommand } from "./timeline.js";
24
+ import { runUserCommand } from "./user.js";
25
+
26
+ const COMMAND_RUNNERS = {
27
+ blog: runBlogCommand,
28
+ calendar: runCalendarCommand,
29
+ character: runCharacterCommand,
30
+ collection: runCollectionCommand,
31
+ episode: runEpisodeCommand,
32
+ group: runGroupCommand,
33
+ index: runIndexCommand,
34
+ notify: runNotifyCommand,
35
+ person: runPersonCommand,
36
+ subject: runSubjectCommand,
37
+ timeline: runTimelineCommand,
38
+ user: runUserCommand,
39
+ };
40
+
41
+ const USAGE = "Usage: bgm [--json] url <bangumi_url> [--dry-run] [extra flags passed to the resolved command]";
42
+
43
+ export async function runUrlCommand(args, context = {}) {
44
+ const { url, dryRun, passthrough } = splitUrlArgs(args);
45
+ if (!url) {
46
+ throw new CommandError(USAGE);
47
+ }
48
+
49
+ const resolved = resolveBangumiUrl(url);
50
+ const commandArgs = [...resolved.args, ...passthrough];
51
+
52
+ if (dryRun) {
53
+ printResult(buildDryRunPayload(resolved, commandArgs), context);
54
+ return;
55
+ }
56
+
57
+ const runner = COMMAND_RUNNERS[resolved.group];
58
+ if (!runner) {
59
+ throw new CommandError(`No CLI runner for command group: ${resolved.group}`);
60
+ }
61
+
62
+ await runner(resolved.command, commandArgs, {
63
+ ...context,
64
+ resolvedFrom: {
65
+ url: resolved.url,
66
+ site: resolved.site,
67
+ command: `${resolved.group} ${resolved.command}`,
68
+ args: commandArgs,
69
+ },
70
+ });
71
+ }
72
+
73
+ export function buildDryRunPayload(resolved, commandArgs = resolved.args) {
74
+ return {
75
+ resource: "url-resolve",
76
+ url: resolved.url,
77
+ site: resolved.site,
78
+ path: resolved.path,
79
+ command: `${resolved.group} ${resolved.command}`,
80
+ args: commandArgs,
81
+ commandLine: formatResolvedCommand({ ...resolved, args: commandArgs }),
82
+ };
83
+ }
84
+
85
+ /**
86
+ * Split raw args into the URL, the --dry-run switch, and flags forwarded to the
87
+ * resolved command. Done by hand so `--dry-run <url>` cannot swallow the URL.
88
+ */
89
+ export function splitUrlArgs(args) {
90
+ const tokens = args.filter((token) => token !== undefined && token !== null).map(String);
91
+ const rest = tokens.filter((token) => token !== "--dry-run");
92
+ const dryRun = tokens.length !== rest.length;
93
+
94
+ let urlIndex = rest.findIndex((token) => looksLikeBangumiUrl(token));
95
+ if (urlIndex === -1) {
96
+ urlIndex = rest.findIndex((token) => !token.startsWith("-"));
97
+ }
98
+
99
+ if (urlIndex === -1) {
100
+ return { url: undefined, dryRun, passthrough: rest };
101
+ }
102
+
103
+ return {
104
+ url: rest[urlIndex],
105
+ dryRun,
106
+ passthrough: [...rest.slice(0, urlIndex), ...rest.slice(urlIndex + 1)],
107
+ };
108
+ }
@@ -1172,7 +1172,23 @@ export class BangumiClient {
1172
1172
  throw new CommandError("Missing username. Pass a username or log in first.");
1173
1173
  }
1174
1174
 
1175
- const result = await this.request(`/p1/users/${encodeURIComponent(String(username))}/collections/subjects`, {
1175
+ let isMe = false;
1176
+ try {
1177
+ const me = await this.getMe();
1178
+ const meUsername = typeof me?.username === "string" ? me.username.trim() : "";
1179
+ if (meUsername && meUsername.toLowerCase() === String(username).trim().toLowerCase()) {
1180
+ isMe = true;
1181
+ }
1182
+ } catch {
1183
+ isMe = false;
1184
+ }
1185
+
1186
+ const path = isMe
1187
+ ? "/p1/collections/subjects"
1188
+ : `/p1/users/${encodeURIComponent(String(username))}/collections/subjects`;
1189
+
1190
+ const result = await this.request(path, {
1191
+ auth: true,
1176
1192
  query: normalizeSubjectCollectionQuery(query),
1177
1193
  });
1178
1194
  return normalizeSubjectCollectionPage(result);
@@ -0,0 +1,372 @@
1
+ /**
2
+ * Bangumi SearchEncore!! Community API Client
3
+ *
4
+ * ⚠️ SearchEncore API
5
+ * 本模块封装的是 Bangumi SearchEncore 服务(bgmdb.ry.mk)。
6
+ * 所有数据均来自 SearchEncore 爬取/聚合,非 bangumi.tv 官方提供。
7
+ *
8
+ * 设计原则:
9
+ * - 与 src/core/client.js 的 BangumiClient 保持 API 风格一致。
10
+ * - 响应格式统一适配为官方 API 的扁平结构({ data, total, limit, offset }),
11
+ * 抹平第三方 envelope 差异(pagination / meta)。
12
+ * - 不携带任何认证信息(该服务为公开 API)。
13
+ */
14
+
15
+ import { requestJson, BangumiApiError } from "./http.js";
16
+
17
+ const COMMUNITY_API_BASE_URL = "https://bgmdb.ry.mk/v1";
18
+
19
+ export class BangumiCommunityApiError extends Error {
20
+ constructor(message, { status, details, source = "community-api" } = {}) {
21
+ super(message);
22
+ this.name = "BangumiCommunityApiError";
23
+ this.status = status;
24
+ this.details = details;
25
+ this.source = source;
26
+ }
27
+ }
28
+
29
+ /**
30
+ * 标识常量:用于 UI 侧判断数据来源。
31
+ * 所有通过本模块返回的结果都会带有此标记。
32
+ */
33
+ export const COMMUNITY_API_SOURCE_TAG = "bgmdb.ry.mk";
34
+
35
+ /* ------------------------------------------------------------------ */
36
+ /* 通用请求与适配 */
37
+ /* ------------------------------------------------------------------ */
38
+
39
+ async function communityRequest(path, options = {}) {
40
+ const url = `${COMMUNITY_API_BASE_URL}${path}`;
41
+
42
+ try {
43
+ const raw = await requestJson(url, {
44
+ method: options.method ?? "GET",
45
+ query: options.query,
46
+ body: options.body,
47
+ });
48
+
49
+ return adaptEnvelope(raw);
50
+ } catch (error) {
51
+ if (error instanceof BangumiApiError) {
52
+ throw new BangumiCommunityApiError(error.message, {
53
+ status: error.status,
54
+ details: error.details,
55
+ });
56
+ }
57
+ throw new BangumiCommunityApiError(
58
+ `Community API request failed: ${error?.message ?? "unknown error"}`,
59
+ {
60
+ details: { url, path, originalError: error?.message },
61
+ },
62
+ );
63
+ }
64
+ }
65
+
66
+ /**
67
+ * 将第三方的 envelope 格式适配为官方风格:
68
+ * 原始: { data, pagination: { total, limit, offset }, meta: { executionMs } }
69
+ * 输出: { data, total, limit, offset, _meta: { executionMs, source } }
70
+ *
71
+ * 若原始已经是扁平结构,则直接透传并追加 source 标记。
72
+ */
73
+ function adaptEnvelope(raw) {
74
+ if (!raw || typeof raw !== "object") {
75
+ return raw;
76
+ }
77
+
78
+ const pagination = raw.pagination;
79
+ const isPaginated =
80
+ pagination &&
81
+ typeof pagination === "object" &&
82
+ ("total" in pagination || "limit" in pagination || "offset" in pagination);
83
+
84
+ const base = isPaginated
85
+ ? {
86
+ data: Array.isArray(raw.data) ? raw.data : [],
87
+ total: pagination.total ?? 0,
88
+ limit: pagination.limit ?? 20,
89
+ offset: pagination.offset ?? 0,
90
+ }
91
+ : { ...raw };
92
+
93
+ // 注入来源标记,供 UI / CLI 输出层识别
94
+ base._meta = {
95
+ ...(raw.meta ?? {}),
96
+ source: COMMUNITY_API_SOURCE_TAG,
97
+ isSearchEncore: true,
98
+ };
99
+
100
+ return base;
101
+ }
102
+
103
+ function encode(key) {
104
+ return encodeURIComponent(String(key));
105
+ }
106
+
107
+ function buildSearchQuery(params = {}) {
108
+ const q = {};
109
+ if (params.q !== undefined && params.q !== "") q.q = params.q;
110
+ if (params.limit !== undefined) q.limit = params.limit;
111
+ if (params.offset !== undefined) q.offset = params.offset;
112
+ if (params.sort !== undefined) q.sort = params.sort;
113
+ return q;
114
+ }
115
+
116
+ /* ------------------------------------------------------------------ */
117
+ /* 搜索接口 */
118
+ /* ------------------------------------------------------------------ */
119
+
120
+ /**
121
+ * 搜索条目(番剧、游戏、书籍等)。
122
+ * 对应官方搜索能力的增强版,支持更灵活的排序和过滤。
123
+ */
124
+ export async function searchSubjects(params = {}) {
125
+ return communityRequest("/search/subjects", {
126
+ query: buildSearchQuery(params),
127
+ });
128
+ }
129
+
130
+ /**
131
+ * 搜索用户。
132
+ */
133
+ export async function searchUsers(params = {}) {
134
+ return communityRequest("/search/users", {
135
+ query: buildSearchQuery(params),
136
+ });
137
+ }
138
+
139
+ /**
140
+ * 搜索小组。
141
+ */
142
+ export async function searchGroups(params = {}) {
143
+ return communityRequest("/search/groups", {
144
+ query: buildSearchQuery(params),
145
+ });
146
+ }
147
+
148
+ /**
149
+ * 搜索回复内容。
150
+ */
151
+ export async function searchReplies(params = {}) {
152
+ return communityRequest("/search/replies", {
153
+ query: buildSearchQuery(params),
154
+ });
155
+ }
156
+
157
+ /**
158
+ * 搜索小组话题。
159
+ */
160
+ export async function searchGroupTopics(params = {}) {
161
+ return communityRequest("/search/group-topics", {
162
+ query: buildSearchQuery(params),
163
+ });
164
+ }
165
+
166
+ /**
167
+ * 搜索条目话题。
168
+ */
169
+ export async function searchSubjectTopics(params = {}) {
170
+ return communityRequest("/search/subject-topics", {
171
+ query: buildSearchQuery(params),
172
+ });
173
+ }
174
+
175
+ /**
176
+ * 搜索目录。
177
+ */
178
+ export async function searchIndexes(params = {}) {
179
+ return communityRequest("/search/indexes", {
180
+ query: buildSearchQuery(params),
181
+ });
182
+ }
183
+
184
+ /**
185
+ * 搜索日志。
186
+ */
187
+ export async function searchBlogs(params = {}) {
188
+ return communityRequest("/search/blogs", {
189
+ query: buildSearchQuery(params),
190
+ });
191
+ }
192
+
193
+ /* ------------------------------------------------------------------ */
194
+ /* 列表接口 */
195
+ /* ------------------------------------------------------------------ */
196
+
197
+ /**
198
+ * 获取最新的小组话题列表。
199
+ */
200
+ export async function listRecentGroupTopics(query = {}) {
201
+ return communityRequest("/groups/-/topics", { query });
202
+ }
203
+
204
+ /**
205
+ * 获取指定小组的话题列表。
206
+ */
207
+ export async function listGroupTopics(groupKey, query = {}) {
208
+ return communityRequest(`/groups/${encode(groupKey)}/topics`, { query });
209
+ }
210
+
211
+ /**
212
+ * 获取指定用户的所有小组话题。
213
+ */
214
+ export async function listUserGroupTopics(userKey, query = {}) {
215
+ return communityRequest(`/users/${encode(userKey)}/group-topics`, { query });
216
+ }
217
+
218
+ /**
219
+ * 获取最新的条目话题列表。
220
+ */
221
+ export async function listRecentSubjectTopics(query = {}) {
222
+ return communityRequest("/subjects/-/topics", { query });
223
+ }
224
+
225
+ /**
226
+ * 获取指定条目的话题列表。
227
+ */
228
+ export async function listSubjectTopics(subjectId, query = {}) {
229
+ return communityRequest(`/subjects/${encode(subjectId)}/topics`, { query });
230
+ }
231
+
232
+ /**
233
+ * 获取指定用户的所有条目话题。
234
+ */
235
+ export async function listUserSubjectTopics(userKey, query = {}) {
236
+ return communityRequest(`/users/${encode(userKey)}/subject-topics`, { query });
237
+ }
238
+
239
+ /**
240
+ * 获取指定用户的目录列表。
241
+ */
242
+ export async function listUserIndexes(userKey, query = {}) {
243
+ return communityRequest(`/users/${encode(userKey)}/indexes`, { query });
244
+ }
245
+
246
+ /**
247
+ * 获取指定用户的日志列表。
248
+ */
249
+ export async function listUserBlogs(userKey, query = {}) {
250
+ return communityRequest(`/users/${encode(userKey)}/blogs`, { query });
251
+ }
252
+
253
+ /* ------------------------------------------------------------------ */
254
+ /* 详情接口 */
255
+ /* ------------------------------------------------------------------ */
256
+
257
+ /**
258
+ * 获取单个用户信息。
259
+ */
260
+ export async function getUser(userKey) {
261
+ if (!userKey) {
262
+ throw new BangumiCommunityApiError("Missing userKey.");
263
+ }
264
+ return communityRequest(`/users/${encode(userKey)}`);
265
+ }
266
+
267
+ /**
268
+ * 获取用户头像历史记录。
269
+ */
270
+ export async function getUserAvatarHistory(userKey) {
271
+ if (!userKey) {
272
+ throw new BangumiCommunityApiError("Missing userKey.");
273
+ }
274
+ return communityRequest(`/users/${encode(userKey)}/avatars`);
275
+ }
276
+
277
+ /**
278
+ * 获取单个小组信息。
279
+ */
280
+ export async function getGroup(groupKey) {
281
+ if (!groupKey) {
282
+ throw new BangumiCommunityApiError("Missing groupKey.");
283
+ }
284
+ return communityRequest(`/groups/${encode(groupKey)}`);
285
+ }
286
+
287
+ /**
288
+ * 获取单个小组话题详情。
289
+ */
290
+ export async function getGroupTopic(topicId) {
291
+ if (!topicId) {
292
+ throw new BangumiCommunityApiError("Missing topicId.");
293
+ }
294
+ return communityRequest(`/groups/-/topics/${encode(topicId)}`);
295
+ }
296
+
297
+ /**
298
+ * 获取小组话题的回复列表。
299
+ */
300
+ export async function listGroupTopicReplies(topicId, query = {}) {
301
+ if (!topicId) {
302
+ throw new BangumiCommunityApiError("Missing topicId.");
303
+ }
304
+ return communityRequest(`/groups/-/topics/${encode(topicId)}/replies`, { query });
305
+ }
306
+
307
+ /**
308
+ * 获取单个条目话题详情。
309
+ */
310
+ export async function getSubjectTopic(topicId) {
311
+ if (!topicId) {
312
+ throw new BangumiCommunityApiError("Missing topicId.");
313
+ }
314
+ return communityRequest(`/subjects/-/topics/${encode(topicId)}`);
315
+ }
316
+
317
+ /**
318
+ * 获取条目话题的回复列表。
319
+ */
320
+ export async function listSubjectTopicReplies(topicId, query = {}) {
321
+ if (!topicId) {
322
+ throw new BangumiCommunityApiError("Missing topicId.");
323
+ }
324
+ return communityRequest(`/subjects/-/topics/${encode(topicId)}/replies`, { query });
325
+ }
326
+
327
+ /**
328
+ * 获取单个目录详情。
329
+ */
330
+ export async function getIndex(indexId) {
331
+ if (!indexId) {
332
+ throw new BangumiCommunityApiError("Missing indexId.");
333
+ }
334
+ return communityRequest(`/indexes/${encode(indexId)}`);
335
+ }
336
+
337
+ /**
338
+ * 获取单篇日志详情。
339
+ */
340
+ export async function getBlog(blogId) {
341
+ if (!blogId) {
342
+ throw new BangumiCommunityApiError("Missing blogId.");
343
+ }
344
+ return communityRequest(`/blogs/${encode(blogId)}`);
345
+ }
346
+
347
+ /* ------------------------------------------------------------------ */
348
+ /* 健康检查 */
349
+ /* ------------------------------------------------------------------ */
350
+
351
+ /**
352
+ * 快速探测社区 API 可用性。
353
+ * 返回 { ok: boolean, latencyMs?: number, error?: string }
354
+ */
355
+ export async function probeCommunityApi() {
356
+ const start = performance.now();
357
+ try {
358
+ const result = await requestJson("https://bgmdb.ry.mk/health", {
359
+ method: "GET",
360
+ });
361
+ return {
362
+ ok: result?.status === "ok",
363
+ latencyMs: Math.round(performance.now() - start),
364
+ };
365
+ } catch (error) {
366
+ return {
367
+ ok: false,
368
+ latencyMs: Math.round(performance.now() - start),
369
+ error: error?.message ?? "unreachable",
370
+ };
371
+ }
372
+ }