npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

beespeed-client

v1.0.12

Published

JavaScript SDK for fetching blog posts

Downloads

41

Readme

Beespeed Client SDK

The Beespeed Client SDK provides a simple interface to interact with the Beespeed blog API. It allows you to perform CRUD operations on blog posts.

Installation

To install the Beespeed Client SDK, you can use npm:

npm install beespeed-client

Usage

The SDK is compiled to JavaScript and can be used in both JavaScript and TypeScript projects. It uses ES6 modules, so make sure your environment supports ES6 import/export syntax.

Initialize the client

First, import and initialize the Beespeed client:

import { buildBeespeedClient } from 'beespeed-client';

const client = buildBeespeedClient({
  baseUrl: 'https://www.beespeed.dev/api' 
});

Get all blogs

To retrieve all blog posts:

try {
  const blogs = await client.getBlogs();
  console.log('All blogs:', blogs);
} catch (error) {
  console.error('Error fetching blogs:', error);
}

Get blog by ID

To get a specific blog post by its ID:

try {
  const blogId = 'your-blog-id';
  const blog = await client.getBlog(blogId);
  console.log('Blog:', blog);
} catch (error) {
  console.error('Error fetching blog:', error);
}

Create a blog

Remember to wrap your content in HTML. To create a new blog post:

try {
  const newBlog = {
    title: 'My New Blog Post',
    authorName: 'John Doe',
    content: '<p>This is the content of my new blog post.</p>',
    userEmail: '[email protected]'
  };
  const createdBlogId = await client.createBlog(newBlog);
  console.log('Created blog ID:', createdBlogId);
} catch (error) {
  console.error('Error creating blog:', error);
}

Rendering Blog Content and Images

Blog content returned from the API may include HTML tags such as <p> and <img>. When rendering the blog content in your application, use the following method to safely insert the HTML in React:

<div dangerouslySetInnerHTML={{ __html: blog.content }} />

Additionally, author images are provided as URLs, which can be displayed with standard tags


<img src={blog.authorImage} alt={blog.authorName} style={{ width: '50px', borderRadius: '50%' }} />

Update a blog

To update an existing blog post: Your content is wrapped in HTML

try {
  const blogId = 'existing-blog-id';
  const updatedBlog = {
    title: 'Updated Blog Title',
    content: '<p>This is the updated content of my blog post.</p>' // Wrap content in HTML tags
  };
  const updatedBlogId = await client.updateBlog(blogId, updatedBlog);
  console.log('Updated blog ID:', updatedBlogId);
} catch (error) {
  console.error('Error updating blog:', error);
}

Delete a blog

To delete a blog post:

try {
  const blogId = 'blog-id-to-delete';
  await client.deleteBlog(blogId);
  console.log('Blog deleted successfully');
} catch (error) {
  console.error('Error deleting blog:', error); 
}

For detailed overview here is an exammple in React.js

import React, { useEffect, useState } from 'react';
import { buildBeespeedClient, BlogClient } from 'beespeed-client';

const client = buildBeespeedClient({
  baseUrl: 'https://www.beespeed.dev/api', // Use the correct base URL  
});

const BeeBlogs = () => {
  const [blog, setBlog] = useState<BlogClient | null>(null);
  const [error, setError] = useState<string | null>(null);
  const blogId = 'Your ID'; // Your specific blog ID. Each Blog you create and publish has a unique I.D 

  useEffect(() => {
    const fetchBlog = async () => {
      try {
        const fetchedBlog = await client.getBlog(blogId);
        setBlog(fetchedBlog);
      } catch (err) {
        console.error('Error fetching blog:', err);
        setError('Failed to fetch blog.');
      }
    };

    fetchBlog();
  }, []);

  // Function to format the Firebase timestamp
  const formatDate = (timestamp: string) => {
    const date = new Date(timestamp);
    return date.toLocaleString(); // Adjust formatting as needed 
  };

  return (
    <div style={{ textAlign: 'center', padding: '20px' }}>
      <h1>Blog Details</h1>
      {error && <p className="error">{error}</p>}
      {blog ? (
        <div style={{ position: 'relative', border: '1px solid #ccc', borderRadius: '5px', padding: '10px', background: '#f9f9f9' }}>
          {/* Author Image in Top Right */}
          {blog.authorImage && (
            <img 
              src={blog.authorImage} 
              alt={blog.authorName}
              style={{
                position: 'absolute',
                top: '10px',
                right: '10px',
                width: '50px',
                height: '50px',
                borderRadius: '50%',
              }}
            />
          )}
          <h3>{blog.title}</h3>
          <p><strong>Author:</strong> {blog.authorName}</p>
          {/* Render content safely, handling images and HTML */}
          <div dangerouslySetInnerHTML={{ __html: blog.content }} />
          <p><em>Created at: {formatDate(blog.createdAt)}</em></p>
        </div>
      ) : (
        <p>No blog found.</p>
      )}
    </div>
  );
};

export default BeeBlogs; 

Error Handling

All methods in the Beespeed Client SDK throw errors if the API request fails. It's recommended to wrap your calls in try-catch blocks to handle these errors gracefully.

TypeScript Support

While the SDK is compiled to JavaScript, it includes TypeScript declaration files (.d.ts) for better development experience in TypeScript projects. The types are automatically available when you import the SDK in a TypeScript project.

Contributing

Contributions are welcome! Please feel free to email me at [email protected].

Building the SDK

If you want to build the SDK from source:

  1. Clone the repository
  2. Run npm install to install dependencies
  3. Run npm run build to compile the TypeScript code to JavaScript

The compiled output will be in the dist/lib directory.

License

This project is licensed under the MIT License.

Author

Warren Chisasa