Home  >  Article  >  Web Front-end  >  Something

Something

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-28 04:26:31605browse

Something

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="%7Bpost.url%7D" target="_blank" rel="noopener noreferrer">
                                {post.title}
                            </a>
                        </li>
                    ))
                ) : (
                    <li>No articles found.</li>
                )}
            </ul>
        </div>
    );
};

export default Page;

The above is the detailed content of Something. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn