UNPKG

persistent-image-url

Version:

A lightweight Node.js library for persisting image URLs using the [imgbb.com](https://imgbb.com/) or [sm.ms](https://sm.ms/) API. This library allows you to upload images from temporary URLs and get back persistent URLs that can be used to access the imag

36 lines (35 loc) 1.63 kB
"use strict"; 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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.uploadImageToImgbb = void 0; const axios_1 = __importDefault(require("axios")); const IMGBB_URL = "https://api.imgbb.com/1/upload"; function uploadImageToImgbb(imageBase64, token) { return __awaiter(this, void 0, void 0, function* () { try { const formData = new FormData(); formData.append("image", imageBase64); const res = yield axios_1.default.post(IMGBB_URL, formData, { params: { key: token, }, }); return res.data.data.url; } catch (error) { throw new Error("Unable to upload image to Imgbb"); } }); } exports.uploadImageToImgbb = uploadImageToImgbb;