import type { BasePayload } from 'payload' export type ArchiveEntries> = { docs: T[] hasNextPage: boolean hasPrevPage: boolean /** 1-based. */ page: number totalDocs: number totalPages: number } type GetArchiveEntriesArgs = { /** Collection to list, e.g. 'posts'. */ collection: string /** Relationship depth. Defaults to 1 — enough for a cover image. */ depth?: number /** Preview (draftMode): include unpublished entries and latest drafts. Default false. */ draft?: boolean locale: string /** 1-based. Values below 1 are clamped. */ page?: number payload: BasePayload /** Defaults to 10. */ perPage?: number /** Payload sort string. Defaults to newest first. */ sort?: string /** Extra constraints merged into the query, e.g. { category: { equals: id } }. */ where?: Record } /** * Fetches one page of a collection's entries for an archive listing. * * Only published documents are returned when the collection has drafts enabled * (the `_status` filter is added automatically — only then, since a collection * without drafts has no such field and querying it would throw). Pass * `draft: true` for a preview. * * Returns pagination facts rather than markup — the listing itself belongs to * the client (as a block on the archive page, most likely), because how a list * of posts should look isn't something a plugin can decide. */ export async function getArchiveEntries>({ collection, depth = 1, draft = false, locale, page = 1, payload, perPage = 10, sort = '-createdAt', where, }: GetArchiveEntriesArgs): Promise> { const versions = payload.collections?.[collection as never]?.config?.versions as { drafts?: unknown } | false | undefined const hasDrafts = Boolean(versions && versions.drafts) const published = !draft && hasDrafts ? { _status: { equals: 'published' } } : undefined const fullWhere = published && where ? { and: [published, where] } : (published ?? where) const result = await payload.find({ collection: collection as never, depth, draft, limit: perPage, locale: locale as never, page: Math.max(1, page), sort, ...(fullWhere ? { where: fullWhere as never } : {}), }) return { docs: result.docs as T[], hasNextPage: result.hasNextPage, hasPrevPage: result.hasPrevPage, page: result.page ?? 1, totalDocs: result.totalDocs, totalPages: result.totalPages, } }