Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion apps/docs/content/docs/api/page.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Page
description: These are APIs that can be used to fetch page data
---

### Get the latest post
### Get latest post

This endpoint will return a JSON response of the latest post in the page.

Expand Down Expand Up @@ -70,3 +70,25 @@ GET https://yourpage.changes.page/changes.json
}
]
```

### Get a posts by id

This endpoint will return a JSON response of a post by id.

```text title="Request"
GET https://yourpage.changes.page/changes.json/:id
```

```json title="Response: 200 OK"
{
"id": "cf8c52a9-27b3-47df-9f22-0c0af15cfaca",
"title": "Unlock Teamwork with Our Latest Feature Update",
"content": "We're kicking off 2025 by introducing one of our most highly requested features!",
"tags": ["announcement", "new"],
"publication_date": "2025-02-14T12:22:16.571+00:00",
"updated_at": "2025-02-14T12:22:17.887817+00:00",
"created_at": "2025-02-14T09:42:52.934045+00:00",
"url": "https://hey.changes.page/post/cf8c52a9-27b3-47df-9f22-0c0af15cfaca/unlock-teamwork-with-our-latest-feature-update",
"plain_text_content": "We're kicking off 2025 by introducing one of our most highly requested features!"
}
```
4 changes: 4 additions & 0 deletions apps/page/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ const moduleExports = {
source: "/changes.json",
destination: "/api/json",
},
{
source: "/changes.json/:id",
destination: "/api/post/:id",
},
{
source: "/latest.json",
destination: "/api/latest",
Expand Down
6 changes: 3 additions & 3 deletions apps/page/pages/api/json.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { IPost } from "@changes-page/supabase/types/page";
import { convertMarkdownToPlainText } from "@changes-page/utils";
import type { NextApiRequest, NextApiResponse } from "next";
import { allowCors } from "../../lib/cors";
import {
fetchPosts,
fetchRenderData,
translateHostToPageIdentifier,
} from "../../lib/data";
import { convertMarkdownToPlainText } from "@changes-page/utils";
import { getPageUrl, getPostUrl } from "../../lib/url";

async function handler(
Expand Down Expand Up @@ -43,7 +43,7 @@ async function handler(

res.status(200).json(postsWithUrl);
} catch (e: Error | any) {
console.log("changes.md [Error]", e);
console.log("Failed to fetch posts [Error]", e);
res.status(200).json([]);
}
}
Expand Down
2 changes: 1 addition & 1 deletion apps/page/pages/api/latest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ async function handler(

res.status(200).json(postsWithUrl[0] ?? null);
} catch (e: Error | any) {
console.log("changes.md [Error]", e);
console.log("Failed to fetch latest post [Error]", e);
res.status(404).json(null);
}
}
Expand Down
2 changes: 1 addition & 1 deletion apps/page/pages/api/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ ${post.content}

res.status(200).send(markdown);
} catch (e: Error | any) {
console.log("changes.md [Error]", e);
console.log("Failed to fetch posts [changes.md] [Error]", e);
res.status(200).send("## No posts Found");
}
}
Expand Down
2 changes: 1 addition & 1 deletion apps/page/pages/api/pinned.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ async function handler(

res.status(200).json(postsWithUrl[0]);
} catch (e: Error | any) {
console.log("changes.md [Error]", e);
console.log("Failed to fetch pinned post [Error]", e);
res.status(404).json(null);
}
}
Expand Down
76 changes: 76 additions & 0 deletions apps/page/pages/api/post/[id].ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { supabaseAdmin } from "@changes-page/supabase/admin";
import { IPost } from "@changes-page/supabase/types/page";
import type { NextApiRequest, NextApiResponse } from "next";
import { allowCors } from "../../../lib/cors";
import {
fetchRenderData,
translateHostToPageIdentifier,
} from "../../../lib/data";
import { getPageUrl, getPostUrl } from "../../../lib/url";

type IPostWithUrl = Pick<
IPost,
"id" | "title" | "content" | "tags" | "created_at"
> & { url: string };

async function handler(
req: NextApiRequest,
res: NextApiResponse<IPostWithUrl | null>
) {
const { id } = req.query;
if (!id) {
res.status(404).json(null);
return;
}

await allowCors(req, res);

const hostname = String(req?.headers?.host);
const { domain, page: url_slug } = translateHostToPageIdentifier(hostname);

try {
const { page, settings } = await fetchRenderData(
String(domain || url_slug)
);

if (!page) throw new Error("Page not found");
if (!settings) throw new Error("Settings not found");

const pageUrl = getPageUrl(page, settings, hostname);

const { data, error: postsError } = await supabaseAdmin
.from("posts")
.select("id,title,content,tags,publication_date,updated_at,created_at")
.eq("id", String(id))
.eq("page_id", String(page?.id))
.eq("status", "published")
.order("publication_date", { ascending: false })
.limit(1);

if (postsError) {
console.error("Fetch post error", postsError);
throw new Error("Failed to fetch posts");
}

const posts = data as Array<IPost>;

if (!posts?.length) {
res.status(404).json(null);
return;
}
Comment on lines +56 to +61
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Verification agent

🧩 Analysis chain

Add plain_text_content to response

Based on the example response in the documentation, the API should include a plain_text_content field, but it's not selected in the query or added to the response.


🏁 Script executed:

#!/bin/bash
# Check if the IPost type includes plain_text_content field
rg -A 15 "export type IPost" packages/supabase/types/page.ts

# Check if other post endpoints include plain_text_content
rg -A 5 "plain_text_content" apps/page/pages/api

Length of output: 1050


Include plain_text_content in the post detail response

The GET /api/post/[id] handler currently returns the raw IPost rows, but the example response in our docs and the /api/json endpoint both include a computed plain_text_content field. Since IPost is just a direct mapping of your DB row, you need to append this field yourself.

• File: apps/page/pages/api/post/[id].ts
After you’ve fetched and validated posts (around where you call res.status(200).json(...)), map each post to include the plain‐text content:

import { convertMarkdownToPlainText } from 'path/to/markdown-utils';

const postsWithPlainText = posts.map(post => ({
  ...post,
  plain_text_content: convertMarkdownToPlainText(post.content),
}));

res.status(200).json(postsWithPlainText);

This will keep your detail endpoint consistent with the docs and the JSON list endpoint.

🤖 Prompt for AI Agents
In apps/page/pages/api/post/[id].ts around lines 55 to 60, the API response
currently returns posts without the plain_text_content field required by the
documentation. To fix this, after validating the posts array, map each post to
add a plain_text_content property by converting the post's content from markdown
to plain text using a utility function like convertMarkdownToPlainText. Then
return this mapped array in the JSON response to ensure the API output matches
the documented format.


const postsWithUrl = posts.map((post) => {
return {
...post,
url: getPostUrl(pageUrl, post),
};
});

res.status(200).json(postsWithUrl[0] ?? null);
} catch (e: Error | any) {
console.log("Failed to fetch post [Error]", e);
res.status(404).json(null);
}
}

export default handler;