Retrieve posts from wordpress
This commit is contained in:
1 parent
09ac787260
commit
5b68b1278c
6 files changed
+66
-9
No files matched your search
+45
-5
@@ -1,14 +1,54 @@
|
||||
const wordpressPassword = process.env.WORDPRESS_PASSWORD;
|
||||
const auth = Buffer.from(`lewis:${wordpressPassword}`).toString('base64');
|
||||
|
||||
const mapComment = comment => ({
|
||||
author: {
|
||||
name: comment.author_name,
|
||||
avatars: comment.author_avatar_urls,
|
||||
url: comment.meta.semantic_linkbacks_author_url,
|
||||
},
|
||||
content: comment.content.rendered,
|
||||
canonical: comment.meta.semantic_linkbacks_canonical,
|
||||
date: comment.data,
|
||||
});
|
||||
|
||||
const comments = async pid => {
|
||||
const response = await fetch(`https://lewisdale.dev/wp-json/wp/v2/comments?per_page=100&post=${pid}`);
|
||||
return (await response.json()).map(mapComment);
|
||||
}
|
||||
|
||||
const webmentions = async (pid) => {
|
||||
const response = await fetch(`https://lewisdale.dev/wp-json/wp/v2/comments?per_page=100&post=${pid}&type=Webmention`, {
|
||||
headers: {
|
||||
"Authorization": `Basic ${auth}`
|
||||
}
|
||||
});
|
||||
|
||||
return (await response.json())
|
||||
.reduce((comments, comment) => {
|
||||
if (!comments[comment.meta.semantic_linkbacks_type]) {
|
||||
comments[comment.meta.semantic_linkbacks_type] = [];
|
||||
}
|
||||
|
||||
comments[comment.meta.semantic_linkbacks_type].push(mapComment(comment));
|
||||
return comments;
|
||||
}, { like: [], repost: [] });
|
||||
}
|
||||
|
||||
const getComments = async pid => {
|
||||
return {
|
||||
comments: await comments(pid),
|
||||
...(await webmentions(pid))
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = async () => {
|
||||
const response = await fetch("https://lewisdale.dev/wp-json/wp/v2/posts?per_page=100", {
|
||||
headers: {
|
||||
"Authorization": `Basic lewis:${wordpressPassword}`
|
||||
"Authorization": `Basic ${auth}`
|
||||
}
|
||||
});
|
||||
|
||||
const posts = await response.json();
|
||||
|
||||
console.log(posts.map(post => Buffer.from(post.title.rendered).toString("utf-8")));
|
||||
return posts;
|
||||
const posts = (await response.json());
|
||||
return Promise.all(posts.map(async post => ({...post, comments: await getComments(post.id) })));
|
||||
};
|
||||
Reference in new issue
Block a user