imgbb-uploader
Advanced tools
Comparing version 1.3.2 to 1.3.3
@@ -0,1 +1,11 @@ | ||
# 1.3.3 | ||
- [Add encodeURIComponent](https://github.com/TheRealBarenziah/imgbb-uploader/issues/10) on the `name` that is passed. This revealed the weird transformations imgBB API could make on passed name. A future version will come to better check the compability of passed `name` with imgBB, and either warn or reject in case the exact `name` can't be accepted as is by imgBB. | ||
- [Feat: passing an image URL is now feasible](https://github.com/TheRealBarenziah/imgbb-uploader/issues/11) as it should ! | ||
- [Chore: fileToString func now calls fs.promises.readFile instead of wrapping fs.readFile into a Promise](https://github.com/TheRealBarenziah/imgbb-uploader/commit/ee6e6abc2b6af08313f44b9b4e6a2f74aa53dd77): afaik it won't change the behaviour in the end, it's just more readable that way, & makes the async nature of that operation even more obvious (reminder: [the syncronous fs.readFile call blocks the holy Event Loop 'til it is complete](https://nodejs.org/api/fs.html#fs_synchronous_example)) | ||
- TODO: continue the UX work that has been started. This implies, amongst other things, making extra sure (writing a test for it?) that the ResponseObject interface is easily accessible; update the "compatibility" section (I'm transpiling to es5 atm); better input validations (imageUrl regex, name parsing...); audit performance (could it be improved further) | ||
# 1.3.2 | ||
@@ -6,4 +16,4 @@ | ||
- [Update devDependencies](https://github.com/TheRealBarenziah/remap-istanbul/commit/f8a107f7aba3b5224011ffa3d353d5e6c2360a8b) | ||
For sport, I tried to please `npm audit` & GitHub dependabot for dev deps. Fixing the bothering [RCE](https://github.com/TheRealBarenziah/remap-istanbul/commit/f8a107f7aba3b5224011ffa3d353d5e6c2360a8b) vulnerability took a `remap-istanbul` fork since their repo last commit is 3 years ago. | ||
- [Update devDependencies](https://github.com/TheRealBarenziah/imgbb-uploader/commit/5a196ea5d58e5fe654d1dee693c7267fbb75476f) | ||
For sport, I tried to please `npm audit` & GitHub dependabot for dev deps. Fixing the bothering [RCE](https://snyk.io/vuln/SNYK-JS-HANDLEBARS-1056767) vulnerability [took a `remap-istanbul` fork](https://github.com/TheRealBarenziah/remap-istanbul/commit/f8a107f7aba3b5224011ffa3d353d5e6c2360a8b) since their repo last commit is 3 years ago. | ||
@@ -10,0 +20,0 @@ - One day tho, I should setup a proper Istanbul CC setup & take the opportunity to replace `tslint` by `typescript-eslint` since the former [is deprecated since 2019](https://github.com/palantir/tslint/issues/4534). |
@@ -8,2 +8,2 @@ /** | ||
*/ | ||
export declare const fileToString: (imagePath: string) => Promise<string>; | ||
export declare const fileToString: (imagePath: string) => Promise<any>; |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.fileToString = void 0; | ||
var fs_1 = require("fs"); | ||
var fs_1 = __importDefault(require("fs")); | ||
/** | ||
@@ -13,11 +16,4 @@ * Promise to turn an image path into a base64 string | ||
exports.fileToString = function (imagePath) { | ||
return new Promise(function (resolve, reject) { | ||
return fs_1.readFile(imagePath, "base64", function (err, data) { | ||
if (err) { | ||
reject(err); | ||
} | ||
resolve(data); | ||
}); | ||
}); | ||
return fs_1.default.promises.readFile(imagePath, "base64").catch(function (e) { return e; }); | ||
}; | ||
//# sourceMappingURL=fileToString.js.map |
@@ -1,8 +0,2 @@ | ||
interface IOptions { | ||
apiKey: string; | ||
imagePath?: string; | ||
name?: string; | ||
expiration?: number; | ||
base64string?: string; | ||
} | ||
import { IOptionObject, IResponseObject } from "./interfaces"; | ||
/** | ||
@@ -20,2 +14,3 @@ * Upload local pictures files to imgbb API and get display URLs in response. | ||
* @param {string} options.base64string - Upload a base64 string (alternative to options.imagePath) | ||
* @param {string} options.imageUrl - URL of your image (32Mb max) | ||
* | ||
@@ -30,3 +25,3 @@ * @returns {Promise.<ResponseObject>} | ||
*/ | ||
declare const imgbbUploader: (...args: string[] | IOptions[]) => Promise<import("./responseInterface").default>; | ||
declare const imgbbUploader: (...args: string[] | IOptionObject[]) => Promise<IResponseObject>; | ||
export = imgbbUploader; |
@@ -64,2 +64,3 @@ "use strict"; | ||
* @param {string} options.base64string - Upload a base64 string (alternative to options.imagePath) | ||
* @param {string} options.imageUrl - URL of your image (32Mb max) | ||
* | ||
@@ -80,10 +81,10 @@ * @returns {Promise.<ResponseObject>} | ||
return __awaiter(void 0, void 0, void 0, function () { | ||
var _a, _b, _c, imagePath, apiKey, name_1, expiration, base64string, _d, _e, _f, e_1; | ||
return __generator(this, function (_g) { | ||
switch (_g.label) { | ||
var _a, _b, _c, apiKey, name_1, expiration, image, e_1; | ||
return __generator(this, function (_d) { | ||
switch (_d.label) { | ||
case 0: | ||
if (!(args.length === 2)) return [3 /*break*/, 5]; | ||
return [4 /*yield*/, validateInput_1.validateInput(String(args[0]), String(args[1]))]; | ||
return [4 /*yield*/, validateInput_1.validateStringInput(String(args[0]), String(args[1]))]; | ||
case 1: | ||
if (!_g.sent()) return [3 /*break*/, 3]; | ||
if (!_d.sent()) return [3 /*break*/, 3]; | ||
_a = postToImgbb_1.postToImgbb; | ||
@@ -94,34 +95,27 @@ _b = { | ||
return [4 /*yield*/, fileToString_1.fileToString(String(args[1]))]; | ||
case 2: return [2 /*return*/, _a.apply(void 0, [(_b.base64str = _g.sent(), | ||
case 2: return [2 /*return*/, _a.apply(void 0, [(_b.image = _d.sent(), | ||
_b)])]; | ||
case 3: throw new Error("Invalid params: please make sure that first argument is an imgBB API key, and second argument is a valid path to image file."); | ||
case 4: return [3 /*break*/, 13]; | ||
case 4: return [3 /*break*/, 11]; | ||
case 5: | ||
if (!(args.length === 1 && typeof args[0] === "object")) return [3 /*break*/, 12]; | ||
_c = __assign({}, args[0]), imagePath = _c.imagePath, apiKey = _c.apiKey, name_1 = _c.name, expiration = _c.expiration, base64string = _c.base64string; | ||
_g.label = 6; | ||
if (!(args.length === 1 && typeof args[0] === "object")) return [3 /*break*/, 10]; | ||
_c = __assign({}, args[0]), apiKey = _c.apiKey, name_1 = _c.name, expiration = _c.expiration; | ||
_d.label = 6; | ||
case 6: | ||
_g.trys.push([6, 10, , 11]); | ||
_d = postToImgbb_1.postToImgbb; | ||
_e = { | ||
apiKey: String(apiKey) | ||
}; | ||
if (!base64string // if base64string is provided, skip fs call | ||
) return [3 /*break*/, 7]; // if base64string is provided, skip fs call | ||
_f = base64string; | ||
return [3 /*break*/, 9]; | ||
case 7: return [4 /*yield*/, fileToString_1.fileToString(String(imagePath))]; | ||
_d.trys.push([6, 8, , 9]); | ||
return [4 /*yield*/, validateInput_1.validateOptionObject(__assign({}, args[0]))]; | ||
case 7: | ||
image = _d.sent(); | ||
return [2 /*return*/, postToImgbb_1.postToImgbb({ | ||
apiKey: String(apiKey), | ||
image: image, | ||
name: name_1, | ||
expiration: expiration, | ||
})]; | ||
case 8: | ||
_f = _g.sent(); | ||
_g.label = 9; | ||
case 9: return [2 /*return*/, _d.apply(void 0, [(_e.base64str = _f, | ||
_e.name = name_1, | ||
_e.expiration = expiration, | ||
_e)])]; | ||
case 10: | ||
e_1 = _g.sent(); | ||
throw new Error(e_1); | ||
case 11: return [3 /*break*/, 13]; | ||
case 12: throw new Error("It seems you didn't pass your arguments properly! Please check imgbbUploader documentation here:\nhttps://github.com/TheRealBarenziah/imgbb-uploader/tree/master"); | ||
case 13: return [2 /*return*/]; | ||
e_1 = _d.sent(); | ||
throw new Error(String(e_1)); | ||
case 9: return [3 /*break*/, 11]; | ||
case 10: throw new Error("It seems you didn't pass your arguments properly! Please check imgbbUploader documentation here:\nhttps://github.com/TheRealBarenziah/imgbb-uploader/tree/master"); | ||
case 11: return [2 /*return*/]; | ||
} | ||
@@ -128,0 +122,0 @@ }); |
@@ -1,2 +0,2 @@ | ||
import ResponseObject from "./responseInterface"; | ||
import { IResponseObject } from "./interfaces"; | ||
/** | ||
@@ -7,3 +7,3 @@ * Now using the standard 'https' module instead of 'request' deprecated dependency. | ||
* @param {string} apiKey - Your imgBB API key | ||
* @param {string} base64string - Typically, the output of fileToString("path") function | ||
* @param {string} imageing - Typically, the output of fileToString("path") function | ||
* | ||
@@ -14,7 +14,7 @@ * @returns A promise. Use `.then` as shown in [the README](https://github.com/TheRealBarenziah/imgbb-uploader#use) : | ||
apiKey: string; | ||
base64str: string; | ||
image?: string; | ||
name?: string; | ||
expiration?: number; | ||
} | ||
export declare const postToImgbb: (params: IPostParams) => Promise<ResponseObject>; | ||
export declare const postToImgbb: (params: IPostParams) => Promise<IResponseObject>; | ||
export {}; |
@@ -38,9 +38,9 @@ "use strict"; | ||
return new Promise(function (resolve, reject) { | ||
var _a = __assign({}, params), apiKey = _a.apiKey, base64str = _a.base64str, _b = _a.name, name = _b === void 0 ? null : _b, _c = _a.expiration, expiration = _c === void 0 ? null : _c; | ||
var _a = __assign({}, params), apiKey = _a.apiKey, image = _a.image, _b = _a.name, name = _b === void 0 ? null : _b, _c = _a.expiration, expiration = _c === void 0 ? null : _c; | ||
var payload = querystring.stringify({ | ||
image: base64str, | ||
image: image, | ||
}); | ||
var query = "/1/upload?key=" + apiKey; | ||
if (name) | ||
query += "&name=" + name; | ||
query += "&name=" + encodeURIComponent(name); | ||
if (expiration) | ||
@@ -69,3 +69,3 @@ query += "&expiration=" + expiration; | ||
? resolve(output) | ||
: reject(new Error("There was a problem with imgBB, please check your inputs")); | ||
: reject(new Error("There was a problem with imgBB, please check your inputs.\nFaulty payload: " + image)); | ||
}); | ||
@@ -72,0 +72,0 @@ }) |
@@ -0,10 +1,11 @@ | ||
import { IOptionObject } from "./interfaces"; | ||
export declare const validateStringInput: (apiKey: string | null, path: string) => Promise<boolean>; | ||
/** | ||
* Formally validate input params | ||
* Formally validate option object. Either return proper string or throws | ||
* | ||
* @param {string} apiKey - Should be 32-character long string | ||
* @param {string} path - Should be a valid file path | ||
* @param {IOptions} options - The options object as described in the docs | ||
* | ||
* @returns {Promise.<Boolean>} | ||
* A promise that resolve to `true` if things are looking good, and to `false` otherwise | ||
* A promise that resolve to a valid "image" value if things are looking good, and throws otherwise | ||
*/ | ||
export declare const validateInput: (apiKey: string, path: string) => Promise<boolean>; | ||
export declare const validateOptionObject: (options: IOptionObject) => Promise<string | undefined>; |
"use strict"; | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
@@ -42,6 +53,7 @@ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.validateInput = void 0; | ||
exports.validateOptionObject = exports.validateStringInput = void 0; | ||
var fs_1 = __importDefault(require("fs")); | ||
var fileToString_1 = require("./fileToString"); | ||
/** | ||
* Formally validate input params | ||
* Formally validate input 2 strings params | ||
* | ||
@@ -54,3 +66,6 @@ * @param {string} apiKey - Should be 32-character long string | ||
*/ | ||
exports.validateInput = function (apiKey, path) { return __awaiter(void 0, void 0, void 0, function () { | ||
var looksLikeApiKey = function (value) { | ||
return value && value.length === 32 ? true : false; | ||
}; | ||
exports.validateStringInput = function (apiKey, path) { return __awaiter(void 0, void 0, void 0, function () { | ||
var file; | ||
@@ -65,6 +80,62 @@ return __generator(this, function (_a) { | ||
file = _a.sent(); | ||
return [2 /*return*/, file && apiKey.length === 32 ? true : false]; | ||
return [2 /*return*/, file && looksLikeApiKey(apiKey) ? true : false]; | ||
} | ||
}); | ||
}); }; | ||
/** | ||
* Formally validate option object. Either return proper string or throws | ||
* | ||
* @param {IOptions} options - The options object as described in the docs | ||
* | ||
* @returns {Promise.<Boolean>} | ||
* A promise that resolve to a valid "image" value if things are looking good, and throws otherwise | ||
*/ | ||
exports.validateOptionObject = function (options) { return __awaiter(void 0, void 0, void 0, function () { | ||
var _a, _b, imagePath, _c, apiKey, _d, | ||
// name = null, | ||
expiration, _e, base64string, _f, imageUrl, oopsie, e_1; | ||
return __generator(this, function (_g) { | ||
switch (_g.label) { | ||
case 0: | ||
_g.trys.push([0, 7, , 8]); | ||
_a = __assign({}, options), _b = _a.imagePath, imagePath = _b === void 0 ? null : _b, _c = _a.apiKey, apiKey = _c === void 0 ? null : _c, _d = _a.expiration, expiration = _d === void 0 ? null : _d, _e = _a.base64string, base64string = _e === void 0 ? null : _e, _f = _a.imageUrl, imageUrl = _f === void 0 ? null : _f; | ||
oopsie = Error("A single input key must be defined between: 'imagePath', 'imageUrl', 'base64string'."); | ||
if (!looksLikeApiKey(apiKey)) | ||
throw new Error("'apiKey' looks invalid (should be 32 characters long)."); | ||
if (expiration) { | ||
if (Number(expiration) < 60 || Number(expiration) > 15552000) { | ||
throw new Error("'expiration' value must be in 60-15552000 range."); | ||
} | ||
} | ||
if (!imagePath) return [3 /*break*/, 5]; | ||
if (!(base64string || imageUrl)) return [3 /*break*/, 1]; | ||
throw oopsie; | ||
case 1: | ||
if (!!exports.validateStringInput(apiKey, imagePath)) return [3 /*break*/, 2]; | ||
throw Error("'imagePath' seem invalid (" + imagePath + ")"); | ||
case 2: return [4 /*yield*/, fileToString_1.fileToString(imagePath)]; | ||
case 3: return [2 /*return*/, _g.sent()]; | ||
case 4: return [3 /*break*/, 6]; | ||
case 5: | ||
if (base64string) { | ||
if (imageUrl) | ||
throw oopsie; | ||
else | ||
return [2 /*return*/, base64string]; | ||
} | ||
else if (imageUrl) { | ||
// todo: some research on imgBB opinions before pasting a regex | ||
return [2 /*return*/, imageUrl]; | ||
} | ||
else | ||
throw oopsie; | ||
_g.label = 6; | ||
case 6: return [3 /*break*/, 8]; | ||
case 7: | ||
e_1 = _g.sent(); | ||
throw new Error(String(e_1)); | ||
case 8: return [2 /*return*/]; | ||
} | ||
}); | ||
}); }; | ||
//# sourceMappingURL=validateInput.js.map |
{ | ||
"name": "imgbb-uploader", | ||
"version": "1.3.2", | ||
"version": "1.3.3", | ||
"description": "Lightweight module to upload images through Imgbb API.", | ||
@@ -11,3 +11,3 @@ "main": "lib/cjs/index.js", | ||
"scripts": { | ||
"dev": "npm run build && npm run lint && npm run test && npm run remap", | ||
"dev": "npm run build && npm run format && npm run lint && npm run test && npm run remap", | ||
"test": "jest --config jestconfig.json", | ||
@@ -14,0 +14,0 @@ "build": "node clean.js && tsc -p tsconfig-cjs.json", |
@@ -85,4 +85,9 @@ # imgbb-uploader | ||
From version 1.2.0 onward, you can also pass an options object as param. | ||
Use it to customize filename and/or a set duration after which the image will be deleted, [cf their docs](https://api.imgbb.com/): | ||
Use it to customize filename and/or a set duration after which the image will be deleted, [cf their docs](https://api.imgbb.com/). | ||
The key you'll use for your image depends on its nature. One of these must be defined: | ||
- `imagePath` in case of a local file | ||
- `imageUrl` in case of an URL string | ||
- `base64string` in case of base64-encoded image | ||
```javascript | ||
@@ -94,3 +99,3 @@ const imgbbUploader = require("imgbb-uploader"); | ||
imagePath: "./your/image/path", // OPTIONAL (unless options.base64string is falsy) | ||
imagePath: "./your/image/path", // OPTIONAL: pass a local file (max 32Mb) | ||
@@ -103,5 +108,7 @@ name: "yourCustomFilename", // OPTIONAL: pass a custom filename to imgBB API | ||
imageUrl: "https://placekitten.com/500/500", // OPTIONAL: pass an URL to imgBB (max 32Mb) | ||
base64string: | ||
"iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAEklEQVR42mNcLVNbzwAEjDAGACcSA4kB6ARiAAAAAElFTkSuQmCC", | ||
/* OPTIONAL (unless options.imagePath is falsy) | ||
/* OPTIONAL: pass base64-encoded image (max 32Mb) | ||
@@ -108,0 +115,0 @@ Enable this to upload base64-encoded image directly as string. (available from 1.3.0 onward) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
37400
457
177