feat(api): return post versions from GET /content/post/:slug - #214
feat(api): return post versions from GET /content/post/:slug#214bbornino wants to merge 3 commits into
Conversation
Adds a self-referential posts.versions relation keyed on groupId, and exposes other versions of a post (slug, versionName, publishedAt) sorted by versionOrder then publishedAt. Posts with no groupId, or versions that aren't published, are simply omitted from the list rather than erroring.
|
Important
This repository does not receive automatic reviews because it has fewer than 10 stars. ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Team Run ID: Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
| .filter( | ||
| (version) => | ||
| version.publishedAt !== null && version.slug !== post.slug, | ||
| ) | ||
| .sort( | ||
| (a, b) => | ||
| a.versionOrder - b.versionOrder || | ||
| a.publishedAt!.getTime() - b.publishedAt!.getTime(), | ||
| ) |
There was a problem hiding this comment.
Could this sort/filter be moved into the drizzle query via where/ordereBy?
| const versions: PostResponse["versions"] = post.versions | ||
| .filter( | ||
| (version) => | ||
| version.publishedAt !== null && version.slug !== post.slug, |
There was a problem hiding this comment.
For version.slug !== post.slug - the versions list should also include the post being queried, so we probably don't want this filter.
All of these versions will be displayed in a dropdown on the frontend, so it needs to have the full list of versions for the post (including the current version).
… post Move the versions publishedAt filter and versionOrder/publishedAt ordering into the versions relation's where/orderBy, matching the pattern posts.ts already uses. Also stop excluding the current post from the versions list, since the frontend uses the full list to populate a version-switcher dropdown.
Closes #192
What changed
GET /content/post/:slugnow returns aversionsarray alongside theexisting
collectionfield — the other versions of a post that shareits
groupId.versionsrelation onpostsinpackages/db/src/relations.ts, keyed on the (nullable)groupIdcolumn. A post with no
groupIdnaturally resolves to zero relationmatches, so no special-casing is needed for that case.
PostResponseSchemagained aversions: { slug, versionName, publishedAt }[]field.slug,versionName,versionOrder, andpublishedAt, then sorts byversionOrderfirst and
publishedAtas a tiebreaker, per the issue.Notes
list — the relation itself returns every post sharing the same
groupId, including the post being requested, so the handlerfilters that entry out by slug before returning the array.
collection chapters are already excluded from
collection.chaptersin this route, to avoid leaking draft slugs through a public
endpoint.
createdAtcolumn onposts(onlyeditedAtandpublishedAt), sopublishedAtis reused as the version's date —same field name and value already exposed for the post itself and
for collection chapters.
Test plan
pnpm test:unitpasses cleanpnpm run build:allpasses clean (fulltsctype-check, notjust vitest's esbuild transpile)
pnpm prettier --checkclean on changed filesversionOrderthen
publishedAt, self excludedgroupIdreturns an emptyversionsarray