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

cloudflare-images-kit

v1.0.3

Published

A simple Cloudflare Images API client

Downloads

7

Readme

Cloudflare Image API Wrapper 🌐

A simple and effective JavaScript library to interact with the Cloudflare Image API. This wrapper simplifies tasks such as uploading, listing, and managing images in your Cloudflare account.

While this library covers a broad range of Cloudflare Image API functionalities, there are certain features not included. These features are more efficiently managed directly through the Cloudflare Dashboard rather than through code.

Features 🌟

  • Image Uploads: Upload images directly from URLs or files.
  • Image Management: List images, delete images, and update image metadata.
  • Image Retrieval: Retrieve details about specific images and download them.
  • Batch Requests: Supports batch token management for efficient API usage.
  • Custom ID Validation: Includes methods to validate and handle custom IDs.
  • UUID Encoding/Decoding: Encode UUIDs to Base64 and decode them back.

Environment Compatibility 🚨

Please note that this tool is designed to be used in a Node.js environment only. Due to CORS (Cross-Origin Resource Sharing) restrictions, the Cloudflare Image API cannot be directly accessed from web browsers.

Installation 💾

Install the library using npm:

npm install zod cloudflare-images-kit

Usage 🛠

Configuration

First, configure your Cloudflare account details:

import CloudflareImages from "cloudflare-images-kit";

const cloudflareImages = new CloudflareImages({
  accountId: process.env.ACCOUNT_ID,
  apiKey: process.env.API_KEY,
});

Validating Custom IDs

const valid = cloudflareImages.isValidCustomId("example-id-123"); // Boolean

This function will throw an error if the custom ID:

  • Is an empty string.
  • Starts or ends with a slash.
  • Is a full UUIDv4.
  • Exceeds the maximum length of 1024 characters.

Encoding and Decoding UUIDs

The encodeUUID and decodeUUID functions provide utilities for encoding a UUID to a Base64 string and decoding it back to a UUID format. Note that UUIDs cannot be used as custom IDs directly in Cloudflare due to their format restrictions.

const uuid = uuidv4();
const encoded = cloudflareImages.encodeUUID(uuid); // Outputs the Base64 encoded version of the UUID
const decoded = cloudflareImages.decodeUUID(encoded); // Outputs the original UUID

Uploading an Image

You can upload images directly from URLs or from files using one of the following methods:

Upload from URL

To upload an image from a URL, use the uploadImage method. You can optionally specify a custom ID, metadata, and whether to require signed URLs for access:

async () => {
  const imageUrl = "https://picsum.photos/id/237/1280/720";
  const customId = cloudflareImages.encodeUUID(uuidv4()); // Optional
  const metadata = { key1: "value1", key2: "value2" }; // Optional
  const requireSignedURLs = false; // Optional
  const res = await cloudflareImages.uploadImage(
    imageUrl,
    customId,
    metadata,
    requireSignedURLs
  );
};

Upload from File

To upload an image from a file, use the uploadImageWithFile method.

async () => {
  const customId = cloudflareImages.encodeUUID(uuidv4()); // Optional
  const metadata = { key1: "value1", key2: "value2" }; // Optional
  const requireSignedURLs = false; // Optional
  const res = await cloudflareImages.uploadImageWithFile(
    file, // Image File
    customId,
    metadata,
    requireSignedURLs
  );
};

Upload from Buffer

To upload an image from a buffer, use the uploadImageWithBuffer method.

async () => {
  const customId = cloudflareImages.encodeUUID(uuidv4()); // Optional
  const metadata = { key1: "value1", key2: "value2" }; // Optional
  const requireSignedURLs = false; // Optional
  const res = await cloudflareImages.uploadImageWithBuffer(
    buffer, // Image Buffer
    "image.jpg", // Image Filename
    customId,
    metadata,
    requireSignedURLs
  );
};

Image Management

For managing images, you can list, update, or delete images as follows:

List Images

async () => {
  const res = await cloudflareImages.listImages();
};

Listing All Images

async () => {
  const images = await cloudflareImages.getFullListImages();
};

Update Image Metadata

async () => {
  const imageId = "your-image-id";
  const metadata = { key1: "new-value1", key2: "new-value2" }; // New metadata
  const requireSignedURLs = false; // Optional
  const res = await cloudflareImages.updateImage(
    imageId,
    metadata,
    requireSignedURLs
  );
};

Delete an Image

async () => {
  const imageId = "your-image-id";
  const res = await cloudflareImages.deleteImage(imageId);
};

Retrieving Image Details

async () => {
  const imageId = "your-image-id";
  const res = await cloudflareImages.getImageDetails(imageId);
};

Downloading Base Image

If you need to download the original image (as a Blob object), use the getBaseImage method:

async () => {
  const imageId = "your-image-id";
  const blob = await cloudflareImages.getBaseImage(imageId);
};

Managing Batch Operations

For efficient management of large numbers of requests, the library supports batch operations. You can refresh the batch token and perform batch operations like uploading, updating, or deleting images:

async () => {
  const success = await cloudflareImages.refreshBatchToken();
  if (success) {
    // Batch operations turn on, 200 requests per batch
    // Batch operations:
    // uploadImage
    // uploadImageWithFile
    // uploadImageWithBuffer
    // updateImage
    // getImageDetails
    // deleteImage

    const promises = Array.from({ length: 200 }, () =>
      cloudflareImages.uploadImage(
        "https://picsum.photos/id/237/1280/720",
        cloudflareImages.encodeUUID(uuidv4())
      )
    );
    await Promise.all(promises);
  }
};

Retrieving Usage Statistics

You can retrieve your Cloudflare Image API usage statistics, including the allowed and current upload counts:

async () => {
  const res = await cloudflareImages.getUsageStats();
};

Additional Resources 📚

For more detailed examples and usage patterns, you can refer to the /demo/index.ts file in the repository.

Contributing 🤝

Contributions are welcome! Please fork the repository and submit pull requests with your proposed changes. For major changes, please open an issue first to discuss what you would like to change.

License 📄

This project is licensed under the MIT License - see the LICENSE file for details.

Contact 📬

If you have any questions or need support with the library, please open an issue in the repository.