Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | 1x 1x 1x 1x 5x 5x 1x 4x 1x 3x 4x 1x 1x 1x 3x 1x 2x 2x 2x | import type { H3Event } from "h3";
import { readAllPages } from "../utils/readAllPages";
const PER_PAGE = 100;
export interface WordpressComment {
id: number;
parent: number;
author_name: string;
date_gmt: string;
content: { rendered: string };
}
const unavailable = (cause: unknown) =>
createError({ statusCode: 502, statusMessage: "WordPress is unavailable", cause });
const readPage = async <T>(event: H3Event, query: Record<string, string | number>) => {
const { baseURL } = useRuntimeConfig(event).content.vendor;
const response = await $fetch
.raw<T[]>("/wp-json/wp/v2/comments", { baseURL, query: { ...query, per_page: PER_PAGE } })
.catch((cause: unknown) => {
throw unavailable(cause);
});
return {
items: response._data ?? [],
totalPages: Number(response.headers.get("x-wp-totalpages") ?? 1),
};
};
/** Every approved comment on a post, oldest first. */
export const listComments = (event: H3Event, postId: string): Promise<WordpressComment[]> =>
readAllPages((page) =>
readPage<WordpressComment>(event, {
post: postId,
page,
order: "asc",
_fields: "id,parent,author_name,date_gmt,content",
}),
);
/** The post id of every approved comment, once per comment. */
export const listCommentedPosts = async (event: H3Event): Promise<string[]> => {
const comments = await readAllPages((page) =>
readPage<{ post: number }>(event, { page, _fields: "post" }),
);
return comments.map(({ post }) => String(post));
};
export const isOpenForComments = async (event: H3Event, postId: string): Promise<boolean> => {
const { baseURL } = useRuntimeConfig(event).content.vendor;
const post = await $fetch<{ comment_status: string }>(`/wp-json/wp/v2/posts/${postId}`, {
baseURL,
query: { _fields: "comment_status" },
}).catch((cause: unknown) => {
throw unavailable(cause);
});
return post.comment_status === "open";
};
|