wraith4081 / bunnycdn

Use the unofficial BunnyCDN library quickly and easily with Javascript
9 stars 1 forks source link
api bunny bunnycdn cdn javascript typescript

Documentation

Overview

This project consists of several modules:

  1. Types and Definitions: Defining ErrorResponse, StatusResponse, Video related types and other types, and utility types.
  2. EdgeStorage.ts: Provides functionality for interacting with BunnyCDN storage endpoints.
  3. Collection.ts: Represents a video collection in Bunny Stream and provides functions for modifying the collection.
  4. Stream.ts: Provides the main functionalities and interacts with Bunny Stream API's - Video and Collection management operations.

Quick Start

Global

import BunnyCDN, { StorageEndpoints } from "bunnycdn";

const cdn = new BunnyCDN({
    AccessKey: "access-key",
    StorageZone: StorageEndpoints.Falkenstein
});

Spesific

EdgeStorage
import { EdgeStorage, StorageEndpoints } from "bunnycdn";

const edgeStorage = new EdgeStorage("access-key", StorageEndpoints.Falkenstein);
const storageZone = edgeStorage.CreateClient("storage-zone-name");
const files = await storageZone.ListFiles('.')
for (let file of files) {
    console.log(`A file was found with the name ${file.ObjectName} and the guid ${file.Guid} with ${file.Length} bytes.`)
}
Stream
import { Stream } from "bunnycdn";

const stream = new Stream();
const library = stream.GetLibrary(1234, 'access-key');
const MyCollection = await library.GetCollection("collection-guid");

const result = (
    await library.ListVideos({
        collection: MyCollection.data?.collectionId,
        page: 1,
        itemsPerPage: 100,
        orderBy: 'date',
        search: 'My Video'
    })
).data || {};

console.log(
    result.itemsPerPage,
    result.currentPage,
    result.totalItems,
);

for (let video of result) {
    console.log(`${video.title}} has ${video.views} views and is ${video.length} seconds long`);
}

Types and Definitions

ErrorResponse

Type: interface

interface ErrorResponse {
    HttpCode: number;
    Message: string;
}

StatusResponse

Type: enum

enum StatusResponse {
    OK = 200,
    CREATED = 201,

    BAD_REQUEST = 400,
    UNAUTHORIZED = 401,
    NOT_FOUND = 404,

    INTERNAL_SERVER_ERROR = 500,

    // Undefined
    UNDEFINED = 0
}

Video related types

Declares VideoCaption, VideoChapter, VideoMoment, VideoMetaTag, VideoTranscodingMessageLevel, VideoTranscodingMessage, VideoStatus, Video, APIVideo, VideoStatics, and VideoList.

Please refer to the provided typings for their definitions.

EdgeStorage.ts

Class: EdgeStorage

To create an instance of EdgeStorage, you need to provide AccessKey and optionally, StorageZone as parameters.

Example:

const edgeStorage = new EdgeStorage('your-access-key', StorageEndpoints.Falkenstein);

Methods:

Class: EdgeStorageClient

Extends from EdgeStorage.

Methods:

Collection.ts

Interface: APICollection

interface APICollection {
    videoLibraryId: number;
    guid?: string;
    name?: string;
    videoCount: number;
    totalSize: number;
    previewVideoIds?: string;
}

Class: Collection

This class represents a video collection with methods to update and delete itself.

Constructor parameters:

Methods:

Stream.ts

Class: Stream

Main class for working with Bunny Stream APIs.

Constructor:

const stream = new Stream();

Methods:

Class: Library

This class represents a Bunny Stream library.

Constructor parameters:

Methods:

Video.ts

Interface: UpdateParams

export interface UpdateParams {
    title?: string;
    collectionId?: string;
    chapters?: VideoChapter[];
    moments?: VideoMoment[];
    metaTags?: VideoMetaTag[];
}

Class: Video

This class encapsulates a video in Bunny Stream.

Constructor parameters:

Methods:

By using the Video class, you can manage individual videos in your Bunny Stream library.

By using these classes and methods, you can perform various operations related to BunnyCDN Storage and Bunny Stream.