Update getContentItems to return total count and items

The changes involved modifying the implementation of `getContentItems` across multiple files, specifically in the CMS-related codebase. This method now returns an object containing the total count of items and the items themselves. The updates also included necessary adjustments in the code where `getContentItems` is called to accommodate the new structure of the returned result.
This commit is contained in:
giancarlo
2024-04-10 16:29:19 +08:00
parent 44373c0372
commit f94557e333
8 changed files with 89 additions and 71 deletions

View File

@@ -59,9 +59,10 @@ export abstract class CmsClient {
* @param options - Options for filtering and pagination.
* @returns A promise that resolves to an array of content items.
*/
abstract getContentItems(
options?: Cms.GetContentItemsOptions,
): Promise<Cms.ContentItem[]>;
abstract getContentItems(options?: Cms.GetContentItemsOptions): Promise<{
total: number;
items: Cms.ContentItem[];
}>;
/**
* Retrieves a content item by its ID and type.

View File

@@ -22,38 +22,40 @@ export class KeystaticClient implements CmsClient {
const startOffset = options?.offset ?? 0;
const endOffset = startOffset + (options?.limit ?? 10);
return Promise.all(
docs
.filter((item) => {
const categoryMatch = options?.categories
? options.categories.find((category) =>
item.entry.categories.includes(category),
)
: true;
const filtered = docs.filter((item) => {
const categoryMatch = options?.categories
? options.categories.find((category) =>
item.entry.categories.includes(category),
)
: true;
if (!categoryMatch) {
return false;
}
if (!categoryMatch) {
return false;
}
const tagMatch = options?.tags
? options.tags.find((tag) => item.entry.tags.includes(tag))
: true;
const tagMatch = options?.tags
? options.tags.find((tag) => item.entry.tags.includes(tag))
: true;
if (!tagMatch) {
return false;
}
if (!tagMatch) {
return false;
}
return true;
})
.slice(startOffset, endOffset)
.map(async (item) => {
const children = docs.filter(
(item) => item.entry.parent === item.slug,
);
return true;
});
return this.mapPost(item, children);
}),
const items = await Promise.all(
filtered.slice(startOffset, endOffset).map(async (item) => {
const children = docs.filter((item) => item.entry.parent === item.slug);
return this.mapPost(item, children);
}),
);
return {
total: filtered.length,
items,
};
}
async getContentItemBySlug(params: { slug: string; collection: string }) {

View File

@@ -79,12 +79,14 @@ export class WordpressClient implements CmsClient {
const url = `${this.apiUrl}${endpoint}`;
const posts = await fetch(url).then(
(value) => value.json() as Promise<WP_REST_API_Post[]>,
);
const response = await fetch(url);
const totalHeader = response.headers.get('X-WP-Total');
return Promise.all(
posts.map(async (item: WP_REST_API_Post) => {
const total = totalHeader ? Number(totalHeader) : 0;
const results = (await response.json()) as WP_REST_API_Post[];
const posts = await Promise.all(
results.map(async (item: WP_REST_API_Post) => {
let parentId: string | undefined;
if (!item) {
@@ -117,6 +119,11 @@ export class WordpressClient implements CmsClient {
};
}),
);
return {
total,
items: posts,
};
}
async getContentItemBySlug({