imgbb-client
v1.0.0
Published
A push client for the ImgBB service
Downloads
5
Maintainers
Readme
Claro! Aqui está um exemplo de um arquivo README.md
para a sua biblioteca de upload de imagens no ImgBB:
ImgBB Client Library
A simple and easy-to-use client library for uploading images to ImgBB.
Features
- Upload images to ImgBB using a Base64 string, Buffer, or Blob.
- Supports optional expiration time for images.
- Supports custom filenames for uploaded images.
- Provides callback functionality for handling responses.
Installation
Install the package via npm:
bun add imgbb-client
npm i imgbb-client
yarn add imgbb-client
Usage
Importing the library
import { ImgBBClient } from 'imgbb-client';
Creating an ImgBBClient instance
const apiKey = 'your-imgbb-api-key';
const imgbbClient = new ImgBBClient(apiKey);
Uploading an image
You can upload an image using a Base64 string, Buffer, or Blob.
Using a Base64 string
const options: ImgBBOptions = { image: 'data:image/png;base64,iVBORw0...' };
imgbbClient.upload(options)
.then((response: ImgbbResponse) => {
console.log('Upload successful:', response);
})
.catch((error) => {
console.error('Upload failed:', error);
});
Using a Buffer
const fs = require('fs');
const imageBuffer = fs.readFileSync('path/to/your/image.png');
const options: ImgBBOptions = { image: imageBuffer };
imgbbClient.upload(options)
.then((response: ImgbbResponse) => {
console.log('Upload successful:', response);
})
.catch((error) => {
console.error('Upload failed:', error);
});
Using a Blob
const fileInput = document.querySelector('input[type="file"]');
const file = fileInput.files[0];
const options: ImgBBOptions = { image: file, filename: file.name };
imgbbClient.upload(options)
.then((response: ImgbbResponse) => {
console.log('Upload successful:', response);
})
.catch((error) => {
console.error('Upload failed:', error);
});
Using a callback
You can provide a callback function to handle the response.
const options: ImgBBOptions = { image: 'data:image/png;base64,iVBORw0...' };
imgbbClient.upload(options, (response: ImgbbResponse) => {
console.log('Upload callback:', response);
});
Handling errors
Errors can be caught using the .catch
method.
const options: ImgBBOptions = { image: 'data:image/png;base64,iVBORw0...' };
imgbbClient.upload(options)
.then((response: ImgbbResponse) => {
console.log('Upload successful:', response);
})
.catch((error) => {
console.error('Upload failed:', error);
});
API
ImgBBClient
constructor(apiKey: string)
Creates a new instance of the ImgBBClient
class.
apiKey
: Your ImgBB API key.
upload(options: ImgBBOptions, callback?: (response: ImgbbResponse) => void): Promise<ImgbbResponse>
Uploads an image to ImgBB.
options
: An object containing the image and optional expiration time.callback
: An optional callback function that will be called with the response.
ImgBBOptions
An object containing the image data and optional expiration time.
image
: The image to upload. Can be a Base64 string, Buffer, or Blob.expiration?
: Optional. The expiration time for the image in seconds.filename?
: Optional. The filename for the uploaded image.
ImgbbResponse
The response object from ImgBB.
Contributing
Contributions are welcome! Please open an issue or submit a pull request for any changes or improvements.
License
This project is licensed under the MIT License.
This README provides an overview of the library, including installation instructions, usage examples, and API documentation. It should help users get started with your ImgBB client library quickly and easily.