Skip to content

Commit

Permalink
fix: use draftId and respective endpoints (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
raisedadead authored May 21, 2024
1 parent 108e8ea commit 2dddd8f
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 68 deletions.
1 change: 0 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
CMS_HOST=
PORT=3000
8 changes: 0 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,3 @@ of a post as it will appear when published live on `/news`.
docker compose build
docker compose up -d
```

> [!NOTE]
> Need to set the following environment variables
**Environment Variables**

- `CMS_HOST`: The Hashnode Publication URL
- `PORT`: The port to run the server on
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,5 @@ services:
ports:
- ${PORT:-3000}:3000
environment:
- CMS_HOST=${CMS_HOST}
- NODE_ENV=production
restart: unless-stopped
70 changes: 32 additions & 38 deletions src/fetch-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,44 @@ import { request, gql } from 'graphql-request';
const endpoint = 'https://gql.hashnode.com/';

const query = gql`
query GetDraftFromPublication($host: String!, $slug: String!) {
publication(host: $host) {
post(slug: $slug) {
query GetDraft($id: ObjectId!) {
draft(id: $id) {
id
slug
author {
id
slug
author {
id
username
name
bio {
text
}
profilePicture
socialMediaLinks {
website
twitter
facebook
}
location
username
name
bio {
text
}
title
tags {
id
name
slug
profilePicture
socialMediaLinks {
website
twitter
facebook
}
brief
readTimeInMinutes
coverImage {
url
attribution
}
content {
html
}
publishedAt
updatedAt
location
}
title
# tagsV2 {
# __typename
# }
readTimeInMinutes
coverImage {
url
attribution
}
content {
html
}
updatedAt
}
}
`;

export async function fetchContent(host, slug) {
const variables = { host, slug };
const { publication } = await request(endpoint, query, variables);
return publication.post;
export async function fetchContent(id) {
const variables = { id };
const { draft } = await request(endpoint, query, variables);
return draft;
}
49 changes: 29 additions & 20 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,39 @@ const noCache = (req, res, next) => {
app.use(noCache);

// Main route
app.get('/', async (req, res) => {
const { slug } = req.query;
if (!slug) {
logger.warn('Request without slug attempted.');
return res
.status(400)
.render('error.njk', { error: 'Slug not found in URI' });
}
app.get(
'/:id',
async (req, res, next) => {
const { id } = req.params;
const isValidObjectId = id.length === 24 && /^[0-9a-fA-F]{24}$/.test(id);

try {
const post = await fetchContent(process.env.CMS_HOST, slug);
if (!post) {
logger.warn(`Post not found for slug: ${slug}`);
if (!isValidObjectId) {
logger.warn(`Invalid ObjectId: ${id}`);
return res
.status(404)
.render('error.njk', { error: 'Post not found in CMS' });
.status(400)
.render('error.njk', { error: 'Invalid ObjectId in URI' });
}
next();
},
async (req, res) => {
const { id } = req.params;

try {
const post = await fetchContent(id);
if (!post) {
logger.warn(`Post not found for id: ${id}`);
return res
.status(404)
.render('error.njk', { error: 'Post not found in CMS' });
}
logger.info(`Post retrieved successfully for id: ${id}`);
res.render('index.njk', { post });
} catch (error) {
logger.error(`Error retrieving content for id ${id}: ${error.message}`);
res.status(500).render('error.njk', { error: 'Internal Server Error' });
}
logger.info(`Post retrieved successfully for slug: ${slug}`);
res.render('index.njk', { post });
} catch (error) {
logger.error(`Error retrieving content for slug ${slug}: ${error.message}`);
res.status(500).render('error.njk', { error: 'Internal Server Error' });
}
});
);

// Handle all other routes
app.all('*', (req, res) => {
Expand Down

0 comments on commit 2dddd8f

Please sign in to comment.