imgbb-uploader
Advanced tools
Comparing version 1.3.0 to 1.3.1
@@ -0,1 +1,17 @@ | ||
# 1.3.1 | ||
- [Updated responseObject interface](https://github.com/TheRealBarenziah/imgbb-uploader/pull/9) | ||
Thanks for PR :) | ||
- While being at it, I had some fun with code coverage, tweaked some `package.json` scripts and gave a cleaner name to "rebuild.js". | ||
Intrigued about it, I tried Jest's built-in coverage report, but it pointed to ugly, transpiled JS. Saw two ways to fix this: a parallel Istanbul setup to handle it, or hacking around to make it work with Jest built-in tool. For funsies I took the latter route using [remap-istanbul](https://www.npmjs.com/package/remap-istanbul). | ||
Their readme says: `We strongly encourage developers to leverage IstanbulJS, and only use this package (remap-istanbul) when needing to support legacy versions of Istanbul.` | ||
In my case, it seem the Istanbul version in Jest's depencency graph is 0.x, so I opted to use their module for now and leave in-depth Istanbul for later. | ||
Note: node kept complaning about undefined "VERSION" and circular dependency while running `npm run remap`, so I ended using `--no-warnings` flag for it. If curious, feel free to make it `--trace-warnings` in `package.json` ;). | ||
In the end, it allows to generate two HTML coverage reports after a test pass, one for transpiled code (`./coverage/lcov-report`), one for source (`./coverage/ts-report`). | ||
- Increased code coverage: 98.46% statements covered. Tests will never enter the ".on('error', () => )" callback in the POST function, but I'll feel safer leaving that block. It's not like it's hurting anybody anyway ^o^ | ||
- Updated [CONTRIBUTING](https://github.com/TheRealBarenziah/imgbb-uploader/blob/master/CONTRIBUTING.md) to reflect that. | ||
# 1.3.0 | ||
@@ -2,0 +18,0 @@ |
@@ -22,1 +22,2 @@ "use strict"; | ||
}; | ||
//# sourceMappingURL=fileToString.js.map |
@@ -0,1 +1,8 @@ | ||
interface IOptions { | ||
apiKey: string; | ||
imagePath?: string; | ||
name?: string; | ||
expiration?: number; | ||
base64string?: string; | ||
} | ||
/** | ||
@@ -16,2 +23,3 @@ * Upload local pictures files to imgbb API and get display URLs in response. | ||
* A promise. Access your data using `.then` as shown in [the README](https://github.com/TheRealBarenziah/imgbb-uploader#use) : | ||
* | ||
* @example | ||
@@ -22,10 +30,3 @@ * imgbbUploader("your-api-key", "/absolute/path/to/file.jpg") | ||
*/ | ||
interface IOptions { | ||
apiKey: string; | ||
imagePath: string | undefined; | ||
name: string | undefined; | ||
expiration: number | undefined; | ||
base64string: string | undefined; | ||
} | ||
declare const imgbbUploader: (...args: string[] | IOptions[]) => Promise<import("./responseInterface").default>; | ||
export = imgbbUploader; |
@@ -52,2 +52,23 @@ "use strict"; | ||
var validateInput_1 = require("./validateInput"); | ||
/** | ||
* Upload local pictures files to imgbb API and get display URLs in response. | ||
* | ||
* @param {string} apiKey - Your imgBB API key | ||
* @param {string} pathToFile - Path to your file | ||
* | ||
* @param {Object} options - OPTIONAL: pass Option object as parameter | ||
* @param {string} options.apiKey - Your imgBB API key | ||
* @param {string} options.imagePath - Path to your image | ||
* @param {string} options.name - Custom name for your file | ||
* @param {string} options.expiration - Expiration value in seconds | ||
* @param {string} options.base64string - Upload a base64 string (alternative to options.imagePath) | ||
* | ||
* @returns {Promise.<ResponseObject>} | ||
* A promise. Access your data using `.then` as shown in [the README](https://github.com/TheRealBarenziah/imgbb-uploader#use) : | ||
* | ||
* @example | ||
* imgbbUploader("your-api-key", "/absolute/path/to/file.jpg") | ||
* .then(res => console.log(res)) | ||
* .catch(err => console.error(err)) | ||
*/ | ||
var imgbbUploader = function () { | ||
@@ -59,13 +80,10 @@ var args = []; | ||
return __awaiter(void 0, void 0, void 0, function () { | ||
var _a, _b, e_1, _c, imagePath, apiKey, _d, name_1, _e, expiration, _f, base64string, _g, _h, _j, e_2; | ||
return __generator(this, function (_k) { | ||
switch (_k.label) { | ||
var _a, _b, _c, imagePath, apiKey, name_1, expiration, base64string, _d, _e, _f, e_1; | ||
return __generator(this, function (_g) { | ||
switch (_g.label) { | ||
case 0: | ||
if (!(args.length === 2)) return [3 /*break*/, 8]; | ||
if (!(args.length === 2)) return [3 /*break*/, 5]; | ||
return [4 /*yield*/, validateInput_1.validateInput(String(args[0]), String(args[1]))]; | ||
case 1: | ||
if (!_k.sent()) return [3 /*break*/, 6]; | ||
_k.label = 2; | ||
case 2: | ||
_k.trys.push([2, 4, , 5]); | ||
if (!_g.sent()) return [3 /*break*/, 3]; | ||
_a = postToImgbb_1.postToImgbb; | ||
@@ -76,40 +94,34 @@ _b = { | ||
return [4 /*yield*/, fileToString_1.fileToString(String(args[1]))]; | ||
case 3: return [2 /*return*/, _a.apply(void 0, [(_b.base64str = _k.sent(), | ||
_b.name = null, | ||
_b.expiration = null, | ||
case 2: return [2 /*return*/, _a.apply(void 0, [(_b.base64str = _g.sent(), | ||
_b)])]; | ||
case 4: | ||
e_1 = _k.sent(); | ||
throw new Error(e_1); | ||
case 5: return [3 /*break*/, 7]; | ||
case 6: 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 7: return [3 /*break*/, 16]; | ||
case 8: | ||
if (!(args.length === 1 && typeof args[0] === "object")) return [3 /*break*/, 15]; | ||
_c = __assign({}, args[0]), imagePath = _c.imagePath, apiKey = _c.apiKey, _d = _c.name, name_1 = _d === void 0 ? null : _d, _e = _c.expiration, expiration = _e === void 0 ? null : _e, _f = _c.base64string, base64string = _f === void 0 ? null : _f; | ||
_k.label = 9; | ||
case 9: | ||
_k.trys.push([9, 13, , 14]); | ||
_g = postToImgbb_1.postToImgbb; | ||
_h = { | ||
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 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; | ||
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*/, 10]; // if base64string is provided, skip fs call | ||
_j = base64string; | ||
return [3 /*break*/, 12]; | ||
case 10: return [4 /*yield*/, fileToString_1.fileToString(String(imagePath))]; | ||
case 11: | ||
_j = _k.sent(); | ||
_k.label = 12; | ||
case 12: return [2 /*return*/, _g.apply(void 0, [(_h.base64str = _j, | ||
_h.name = name_1 ? String(name_1) : null, | ||
_h.expiration = expiration ? Number(expiration) : null, | ||
_h)])]; | ||
case 13: | ||
e_2 = _k.sent(); | ||
throw new Error(e_2); | ||
case 14: return [3 /*break*/, 16]; | ||
case 15: 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 16: return [2 /*return*/]; | ||
) 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))]; | ||
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*/]; | ||
} | ||
@@ -120,1 +132,2 @@ }); | ||
module.exports = imgbbUploader; | ||
//# sourceMappingURL=index.js.map |
@@ -8,2 +8,3 @@ import ResponseObject from "./responseInterface"; | ||
* @param {string} base64string - Typically, the output of fileToString("path") function | ||
* | ||
* @returns A promise. Use `.then` as shown in [the README](https://github.com/TheRealBarenziah/imgbb-uploader#use) : | ||
@@ -14,6 +15,6 @@ */ | ||
base64str: string; | ||
name: string | null; | ||
expiration: number | null; | ||
name?: string; | ||
expiration?: number; | ||
} | ||
export declare const postToImgbb: (params: IPostParams) => Promise<ResponseObject>; | ||
export {}; |
@@ -72,7 +72,8 @@ "use strict"; | ||
.on("error", function (err) { | ||
reject(err); | ||
reject(new Error(err)); | ||
}); | ||
req.write(payload); | ||
req.end(); | ||
return req.end(); | ||
}); | ||
}; | ||
//# sourceMappingURL=postToImgbb.js.map |
@@ -24,3 +24,10 @@ export default interface ResponseObject { | ||
}; | ||
medium: { | ||
filename: string; | ||
name: string; | ||
mime: string; | ||
extension: string; | ||
url: string; | ||
}; | ||
delete_url: string; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
//# sourceMappingURL=responseInterface.js.map |
@@ -6,2 +6,3 @@ /** | ||
* @param {string} path - Should be a valid file path | ||
* | ||
* @returns {Promise.<Boolean>} | ||
@@ -8,0 +9,0 @@ * A promise that resolve to `true` if things are looking good, and to `false` otherwise |
@@ -49,2 +49,3 @@ "use strict"; | ||
* @param {string} path - Should be a valid file path | ||
* | ||
* @returns {Promise.<Boolean>} | ||
@@ -54,24 +55,15 @@ * A promise that resolve to `true` if things are looking good, and to `false` otherwise | ||
exports.validateInput = function (apiKey, path) { return __awaiter(void 0, void 0, void 0, function () { | ||
var presumedFile, e_1; | ||
var file; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
_a.trys.push([0, 2, , 3]); | ||
return [4 /*yield*/, fs_1.default.promises.lstat(path)]; | ||
case 0: return [4 /*yield*/, fs_1.default.promises | ||
.lstat(path) | ||
.then(function (res) { return res.isFile(); }) | ||
.catch(function () { return false; })]; | ||
case 1: | ||
presumedFile = _a.sent(); | ||
if (apiKey.length === 32 && // imgBB API keys being 32 characters long is empiric knowledge | ||
presumedFile.isFile() // Using fs.lstat to ensure there is a file to upload | ||
) { | ||
return [2 /*return*/, true]; | ||
} | ||
else | ||
return [2 /*return*/, false]; | ||
return [3 /*break*/, 3]; | ||
case 2: | ||
e_1 = _a.sent(); | ||
return [2 /*return*/, false]; | ||
case 3: return [2 /*return*/]; | ||
file = _a.sent(); | ||
return [2 /*return*/, file && apiKey.length === 32 ? true : false]; | ||
} | ||
}); | ||
}); }; | ||
//# sourceMappingURL=validateInput.js.map |
{ | ||
"name": "imgbb-uploader", | ||
"version": "1.3.0", | ||
"version": "1.3.1", | ||
"description": "Lightweight module to upload images through Imgbb API.", | ||
@@ -11,6 +11,8 @@ "main": "lib/cjs/index.js", | ||
"scripts": { | ||
"dev": "npm run build && npm run lint && npm run test && npm run remap", | ||
"test": "jest --config jestconfig.json", | ||
"build": "node rebuild.js && tsc -p tsconfig-cjs.json", | ||
"build:cjs": "node rebuild.js && tsc -p tsconfig-cjs.json", | ||
"build:esm": "node rebuild.js && tsc -p tsconfig.json", | ||
"build": "node clean.js && tsc -p tsconfig-cjs.json", | ||
"build:cjs": "node clean.js && tsc -p tsconfig-cjs.json", | ||
"build:esm": "node clean.js && tsc -p tsconfig.json", | ||
"remap": "node --no-warnings ./node_modules/.bin/remap-istanbul -i coverage/coverage-final.json -o coverage/ts-report -t html -e src/__tests__/images", | ||
"format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"", | ||
@@ -26,3 +28,4 @@ "lint": "tslint -p tsconfig.json", | ||
"files": [ | ||
"lib/**/*" | ||
"lib/**/*.js", | ||
"lib/**/*.ts" | ||
], | ||
@@ -36,7 +39,10 @@ "devDependencies": { | ||
"dotenv": "^8.2.0", | ||
"eslint": "^7.26.0", | ||
"eslint-plugin-import": "^2.22.0", | ||
"husky": "^4.2.5", | ||
"intern": "^4.9.0", | ||
"jest": "^26.4.0", | ||
"prettier": "^2.0.5", | ||
"pretty-quick": "^2.0.1", | ||
"remap-istanbul": "^0.13.0", | ||
"rimraf": "^3.0.2", | ||
@@ -43,0 +49,0 @@ "sharp": "^0.26.3", |
@@ -7,5 +7,4 @@ # imgbb-uploader | ||
[![https://nodei.co/npm/imgbb-uploader.png?downloads=true&downloadRank=true&stars=true](https://nodei.co/npm/imgbb-uploader.png?downloads=true&downloadRank=true&stars=true)](https://www.npmjs.com/package/imgbb-uploader) | ||
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier) | ||
[![Known Vulnerabilities](https://snyk.io/test/github/TheRealBarenziah/imgbb-uploader/badge.svg?targetFile=package.json)](https://snyk.io/test/github/TheRealBarenziah/imgbb-uploader?targetFile=package.json) | ||
[![dependencies](https://img.shields.io/badge/dependencies-0-brightgreen)](https://img.shields.io/badge/dependencies-0-brightgreen) | ||
[![dependencies](https://img.shields.io/badge/dependencies-0-brightgreen)](https://www.npmjs.com/package/imgbb-uploader?activeTab=dependencies) | ||
[![Build Status](https://travis-ci.org/TheRealBarenziah/imgbb-uploader.svg?branch=master)](https://travis-ci.org/TheRealBarenziah/imgbb-uploader) | ||
@@ -16,5 +15,5 @@ | ||
**Node >= 8** ( [this module uses async/await](https://node.green/) ) | ||
_Care: this module uses `fs` under the hood. It means **it WON'T work outside the node environment !**_ | ||
_Care: this module uses `fs` under the hood. **It WON'T work outside the node environment !**_ | ||
_You really SHOULDN'T use API keys from your frontend. But, if you're the yolo type, you could use window.File & window.fetch. PLEASE DON'T DO THAT !!!_ | ||
_You really SHOULDN'T use API keys from your frontend. PLEASE DON'T DO THAT !!!_ | ||
[Blah blah blah, my soul is lost already, bring it on](https://stackoverflow.com/a/63669049/11894221) | ||
@@ -29,4 +28,4 @@ | ||
- I) [Get a free API key from imgbb](https://api.imgbb.com/) ( estimated time ~1 minute ) | ||
- II) (facultative) [Put that in an environment variable](https://www.npmjs.com/package/dotenv) | ||
- III) **imgbbUploader takes _exactly two_ String arguments** : your API key, and the absolute path of your image : | ||
- II) [Put that in an environment variable](https://www.npmjs.com/package/dotenv) | ||
- III) **imgbbUploader takes _exactly two_ String arguments** : your API key, and the path to your image : | ||
@@ -36,6 +35,3 @@ ```javascript | ||
imgbbUploader( | ||
"your-imgbb-api-key-string", | ||
"absolute/path/to/your/image/image.png", | ||
) | ||
imgbbUploader("your-imgbb-api-key-string", "path/to/your/image.png") | ||
.then((response) => console.log(response)) | ||
@@ -71,2 +67,9 @@ .catch((error) => console.error(error)); | ||
}, | ||
medium: { | ||
filename: '5e7599f65f27.png', | ||
name: '5e7599f65f27', | ||
mime: 'image/png', | ||
extension: 'png', | ||
url: 'https://i.ibb.co/14kK0tt/5e7599f65f27.png' | ||
}, | ||
delete_url: 'https://ibb.co/26Sy9tM/087a7edaaac26e1c940283df07d0b1d7' | ||
@@ -76,5 +79,5 @@ } | ||
This async function returns a promise, so this is normal : | ||
`console.log(imgbbUploader(myKey, myPath)) // output : Promise { <pending> }` | ||
Your data is available in `.then((response) => response)` as shown above. | ||
This async function returns a promise: | ||
`console.log(imgbbUploader(myKey, myPath)) // Promise { <pending> }` | ||
Use `await` or `.then` as shown above. | ||
@@ -84,8 +87,4 @@ ## Use with options object (more features, yay! ) | ||
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/): | ||
- I) [Get a free API key from imgbb](https://api.imgbb.com/) ( estimated time ~1 minute ) | ||
- II) [Put that in an environment variable](https://www.npmjs.com/package/dotenv) | ||
- III) **pass an option object as argument** : | ||
```javascript | ||
@@ -106,8 +105,9 @@ const imgbbUploader = require("imgbb-uploader"); | ||
base64string: | ||
"data:image/jpeg;base64,blahblahblah" /* OPTIONAL (unless options.imagePath is falsy) | ||
"iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAEklEQVR42mNcLVNbzwAEjDAGACcSA4kB6ARiAAAAAElFTkSuQmCC", | ||
/* OPTIONAL (unless options.imagePath is falsy) | ||
Enable this to upload base64-encoded image directly as string. (available from 1.3.0 onward) | ||
Allows to work with RAM directly for increased performance (skips fs I/O calls). | ||
Beware: options.imagePath will be ignored as long as options.base64string is defined! | ||
*/, | ||
*/ | ||
}; | ||
@@ -141,17 +141,19 @@ | ||
// Your barebone async function | ||
const myFunc = async (name) => { | ||
try { | ||
return await imgbbUploader({ | ||
apiKey: "definitely-not-a-valid-key", | ||
base64string: await base64str(), | ||
name: name, | ||
const myUrl = async (name) => { | ||
return await imgbbUploader({ | ||
apiKey: "definitely-not-a-valid-key", | ||
base64string: await base64str(), | ||
name: name, | ||
}) | ||
.then((res) => { | ||
console.log(`Handle success: ${res.url}`); | ||
return res.url; | ||
}) | ||
.catch((e) => { | ||
console.error(`Handle error: ${e}`); | ||
return "http://placekitten.com/300/300"; | ||
}); | ||
} catch (e) { | ||
throw e; | ||
} | ||
}; | ||
myFunc("Dolunay_Obruk-Sama_<3") | ||
.then((res) => console.log(res)) | ||
.catch((e) => console.error(e)); | ||
myUrl("Dolunay_Obruk-Sama_<3"); | ||
``` | ||
@@ -165,8 +167,5 @@ | ||
```javascript | ||
module.exports = require("path").join(__dirname); | ||
``` | ||
// baseDir.js | ||
const path = require('path'); | ||
const dirPath = path.join(__dirname); | ||
module.exports = dirPath; | ||
``` | ||
@@ -173,0 +172,0 @@ Then you can require this file elsewhere and use something like `path.join(myDirpath, "subfolder")` to dig into directories programmatically. Once there, you can f.e. `fs.readdir` and iterate `forEach` file of that directory. |
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
31300
392
22
166