DEV Community

Cover image for Something
Sukhrob Tech
Sukhrob Tech

Posted on

Something

Image description

async function getMediumPosts() {
    try {
        const response = await fetch('https://api.medium.com/v1/users/@sukhrobtech/posts', {
            headers: {
                Authorization: `Bearer 29a6e098e7413bb577279281bb650edd904329dfc1561bfc687600f4ca656609b`,
            },
        });

        if (!response.ok) {
            throw new Error(`Error: ${response.status} ${response.statusText}`);
        }

        const data = await response.json();
        return data.data; // Assuming that posts are in the `data` field of the response
    } catch (error) {
        console.error("Failed to fetch Medium posts:", error);
        return []; // Return an empty array if there's an error
    }
}

const Page = async () => {
    const posts = await getMediumPosts();

    return (
        <div>
            <h2>My Medium Articles</h2>
            <ul>
                {posts.length > 0 ? (
                    posts.map((post) => (
                        <li key={post.id}>
                            <a href={post.url} target="_blank" rel="noopener noreferrer">
                                {post.title}
                            </a>
                        </li>
                    ))
                ) : (
                    <li>No articles found.</li>
                )}
            </ul>
        </div>
    );
};

export default Page;

Enter fullscreen mode Exit fullscreen mode

Top comments (0)