如果您像我一样,您可能会在多个博客平台上写作。就我而言,我使用 Dev.to 和 Hashnode 来接触不同的受众。但是,当您想在个人网站上显示所有帖子时会发生什么?今天我将向您展示如何将这两个 API 集成到我用 Astro 构建的产品组合中。
主要挑战是:
首先,我们定义输入数据的接口:
interface BlogPost { title: string; brief: string; slug: string; dateAdded: string; rawDate: string; coverImage: string; url: string; source: string; } interface HashnodeEdge { node: { title: string; brief: string; slug: string; dateAdded: string; coverImage?: { url: string; }; url: string; }; }
Dev.to API 是 RESTful 并且相当简单。这是我的实现方式:
async function getDevToPosts() { try { const params = new URLSearchParams({ username: 'tuUsuario', per_page: '20', state: 'all', sort: 'published_at', order: 'desc' }); const headers = { 'Accept': 'application/vnd.forem.api-v1+json' }; // Agregar API key si está disponible if (import.meta.env.DEV_TO_API_KEY) { headers['api-key'] = import.meta.env.DEV_TO_API_KEY; } const response = await fetch(`https://dev.to/api/articles?${params}`, { headers }); if (!response.ok) { throw new Error(`HTTP ${response.status}`); } const posts = await response.json(); return posts.map((post: any) => ({ title: post.title, brief: post.description, slug: post.slug, dateAdded: formatDate(post.published_timestamp), rawDate: post.published_timestamp, coverImage: post.cover_image || '/images/default-post.png', url: post.url, source: 'devto' })); } catch (error) { console.error('Error al obtener posts de Dev.to:', error); return []; } }
Hashnode 使用 GraphQL,这需要稍微不同的方法:
async function getHashnodePosts() { try { const query = ` query { publication(host: "tuBlog.hashnode.dev") { posts(first: 20) { edges { node { title brief slug dateAdded: publishedAt coverImage { url } url } } } } } `; const response = await fetch('https://gql.hashnode.com', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ query }) }); const { data } = await response.json(); return data.publication.posts.edges.map((edge: HashnodeEdge) => ({ title: edge.node.title, brief: edge.node.brief, slug: edge.node.slug, dateAdded: formatDate(edge.node.dateAdded), rawDate: edge.node.dateAdded, coverImage: edge.node.coverImage?.url || '/images/default-post.png', url: edge.node.url, source: 'hashnode' })); } catch (error) { console.error('Error al obtener posts de Hashnode:', error); return []; } }
组合和排序帖子时会发生神奇的事情:
const hashnodePosts = await getHashnodePosts(); const devtoPosts = await getDevToPosts(); const allBlogPosts = [...hashnodePosts, ...devtoPosts] .sort((a, b) => new Date(b.rawDate).getTime() - new Date(a.rawDate).getTime());
为了处理速率限制和错误,我实施了以下策略:
const CACHE_DURATION = 5 * 60 * 1000; // 5 minutos let postsCache = { data: null, timestamp: 0 }; async function getAllPosts() { const now = Date.now(); if (postsCache.data && (now - postsCache.timestamp) < CACHE_DURATION) { return postsCache.data; } // Obtener y combinar posts... postsCache = { data: allBlogPosts, timestamp: now }; return allBlogPosts; }
async function fetchWithRetry(url: string, options: any, retries = 3) { for (let i = 0; i < retries; i++) { try { const response = await fetch(url, options); if (response.status === 429) { // Rate limit const retryAfter = response.headers.get('Retry-After') || '60'; await new Promise(resolve => setTimeout(resolve, parseInt(retryAfter) * 1000)); continue; } return response; } catch (error) { if (i === retries - 1) throw error; await new Promise(resolve => setTimeout(resolve, Math.pow(2, i) * 1000)); } } }
最后,我们在 Astro 组件中渲染帖子:
--- const allBlogPosts = 等待 getAllPosts(); --- <div> <p>这种集成使我们能够:</p> <ul> <li>维护我们帖子的单一事实来源</li> <li>显示来自多个平台的内容</li> <li>优雅地处理错误</li> <li>维护输入的安全代码</li> </ul> <p>完整的代码可以在我的 GitHub 上找到。</p> <p>您是否将其他博客平台集成到您的网站中?在评论中分享您的经验! ?</p> <h2> webdev #astro #typescript #api </h2> </div>
以上是将多个博客 API 集成到 Astro 站点中:Dev.to 和 Hashnode的详细内容。更多信息请关注PHP中文网其他相关文章!