sdk-imagebb
Version:
Modern, lightweight TypeScript library for uploading images to ImgBB API. Features React 19 & Node 24 support, promise-based async/await interface, automatic error handling, and configurable expiration. Perfect for building image upload functionality in R
127 lines • 5.02 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.imgbbUpload = void 0;
const IMGBB_API_URL = "https://api.imgbb.com/1/upload";
/**
* Upload an image to ImgBB
* @param options - Upload configuration options
* @returns Promise resolving to the upload response
* @throws {Error} When upload fails or API returns an error
* @example
* ```typescript
* const result = await imgbbUpload({
* key: 'your-api-key',
* image: fileFromInput,
* name: 'my-image',
* expiration: 600
* });
* console.log(result.data.url);
* ```
*/
const imgbbUpload = (_a) => __awaiter(void 0, [_a], void 0, function* ({ key, image, expiration, name, }) {
// Input validation
if (!key || typeof key !== "string" || key.trim() === "") {
throw new Error("ImgBB API key is required and must be a non-empty string");
}
if (!image || !(image instanceof File)) {
throw new Error("Image must be a valid File object");
}
// Validate image file
const validImageTypes = [
"image/jpeg",
"image/jpg",
"image/png",
"image/gif",
"image/bmp",
"image/webp",
];
if (validImageTypes.indexOf(image.type) === -1) {
throw new Error(`Invalid image type: ${image.type}. Supported types: JPEG, PNG, GIF, BMP, WEBP`);
}
// Validate expiration (60 to 15552000 seconds according to ImgBB docs)
if (expiration !== undefined && expiration !== null) {
if (typeof expiration !== "number" || expiration < 60 || expiration > 15552000) {
throw new Error("Expiration must be a number between 60 and 15552000 seconds");
}
}
try {
// Build query parameters
const params = {
key: key.trim(),
};
if (expiration) {
params.expiration = expiration.toString();
}
if (name && typeof name === "string" && name.trim() !== "") {
params.name = name.trim();
}
// Create FormData
const formData = new FormData();
formData.append("image", image);
// Make API request with timeout
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 30000); // 30 second timeout
let response;
try {
response = yield fetch(`${IMGBB_API_URL}?${new URLSearchParams(params)}`, {
method: "POST",
body: formData,
signal: controller.signal,
});
}
finally {
clearTimeout(timeoutId);
}
// Handle HTTP errors
if (!response.ok) {
let errorMessage = `HTTP ${response.status}: ${response.statusText}`;
try {
const errorData = yield response.json();
if (errorData.error && errorData.error.message) {
errorMessage = errorData.error.message;
}
}
catch (_b) {
// If parsing error response fails, use default message
}
throw new Error(`ImgBB API error: ${errorMessage}`);
}
// Parse response
const data = yield response.json();
// Validate response structure
if (!data.success || !data.data) {
throw new Error("Invalid response from ImgBB API");
}
return data;
}
catch (error) {
// Handle network errors
if (error instanceof Error) {
if (error.name === "AbortError") {
throw new Error("Upload timed out after 30 seconds");
}
// Re-throw validation errors and API errors as-is
if (error.message.indexOf("ImgBB") !== -1 || error.message.indexOf("Invalid") !== -1 ||
error.message.indexOf("required") !== -1 || error.message.indexOf("Expiration") !== -1) {
throw error;
}
// Wrap network errors
throw new Error(`Failed to upload image: ${error.message}`);
}
// Unknown error type
console.error("Unexpected ImgBB API error:", error);
throw new Error("An unexpected error occurred during image upload");
}
});
exports.imgbbUpload = imgbbUpload;
exports.default = imgbbUpload;
//# sourceMappingURL=index.js.map