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 ${auth}` } }); const posts = (await response.json()); return Promise.all(posts.map(async post => ({...post, comments: await getComments(post.id) }))); };