如果您像我一樣,您可能會在多個部落格平台上寫作。就我而言,我使用 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中文網其他相關文章!