Skip to content
This repository has been archived by the owner on Dec 30, 2021. It is now read-only.

Commit

Permalink
fix: fix cli utility and incorrect references to .data
Browse files Browse the repository at this point in the history
  • Loading branch information
kaimallea committed Mar 13, 2021
1 parent 9b1d2ee commit 5862a76
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 33 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ imgur.setCredentials('email@domain.com', 'password', 'aCs53GSs4tga0ikp');
imgur
.uploadFile('/home/kai/kittens.png')
.then((json) => {
console.log(json.data.link);
console.log(json.link);
})
.catch((err) => {
console.error(err.message);
Expand All @@ -173,7 +173,7 @@ const albumId = 'F8KTV';
imgur
.uploadFile('/home/kai/*.jpg', albumId)
.then((json) => {
console.log(json.data.link);
console.log(json.link);
})
.catch((err) => {
console.error(err.message);
Expand All @@ -183,7 +183,7 @@ imgur
imgur
.uploadFile('~/*.(jpg|png|gif)')
.then((json) => {
console.log(json.data.link);
console.log(json.link);
})
.catch((err) => {
console.error(err.message);
Expand Down Expand Up @@ -261,7 +261,7 @@ imgur
imgur
.uploadUrl('https://octodex.github.com/images/topguntocat.png')
.then((json) => {
console.log(json.data.link);
console.log(json.link);
})
.catch((err) => {
console.error(err.message);
Expand All @@ -277,7 +277,7 @@ const imgurFavicon =
imgur
.uploadBase64(imgurFavicon)
.then((json) => {
console.log(json.data.link);
console.log(json.link);
})
.catch((err) => {
console.error(err.message);
Expand Down
29 changes: 13 additions & 16 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ commander

imgur
.loadClientId()
.catch(() => {})
.then(imgur.setClientId)
.fin(() => {
.finally(() => {
if (commander.clientId) {
imgur.setClientId(commander.clientId);
}
Expand All @@ -55,7 +56,7 @@ imgur
} else if (commander.credits) {
imgur.getCredits().then(
(json) => {
console.log(json.data);
console.log(json);
},
(err) => {
console.error('Unable to get credit info (%s)', err.message);
Expand All @@ -69,17 +70,13 @@ imgur
let aId, deleteHash;
imgur.createAlbum().then(
(json) => {
aId = json.data.id;
deleteHash = json.data.deletehash;
aId = json.id;
deleteHash = json.deletehash;
console.log('Album -> https://imgur.com/a/%s', aId);
args.forEach((file) => {
imgur.uploadFile(file, deleteHash).then(
(json) => {
const output = util.format(
'%s -> %s',
file,
json.data.link
);
const output = util.format('%s -> %s', file, json.link);
console.log(output);
},
(err) => {
Expand All @@ -98,9 +95,9 @@ imgur
(json) => {
let output;
if (args.length > 1) {
output = util.format('%s -> %s', file, json.data.link);
output = util.format('%s -> %s', file, json.link);
} else {
output = json.data.link;
output = json.link;
}
console.log(output);
},
Expand All @@ -116,7 +113,7 @@ imgur
commander.info.forEach((id) => {
imgur.getInfo(id).then(
(json) => {
console.log(json.data);
console.log(json);
},
(err) => {
console.log(err.message);
Expand All @@ -134,10 +131,10 @@ imgur
output = util.format(
'%s... -> %s',
str.substr(0, 7),
json.data.link
json.link
);
} else {
output = json.data.link;
output = json.link;
}
console.log(output);
},
Expand All @@ -159,9 +156,9 @@ imgur
(json) => {
let output;
if (commander.url.length > 1) {
output = util.format('%s -> %s', url, json.data.link);
output = util.format('%s -> %s', url, json.link);
} else {
output = json.data.link;
output = json.link;
}
console.log(output);
},
Expand Down
27 changes: 15 additions & 12 deletions lib/imgur.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
'use strict';
const imgur = exports;
const got = require('got');
const util = require('util');
const fs = require('fs');
const readFile = util.promisify(fs.readFile);
const FormData = require('form-data');
const VERSION = require('../package.json').version;

Expand Down Expand Up @@ -239,17 +241,18 @@ imgur.setCredentials = (username, password, clientId) => {
imgur.loadClientId = async (path) => {
path = path || DEFAULT_CLIENT_ID_PATH;

fs.readFile(path, { encoding: 'utf8' }, (err, data) => {
if (err) {
throw new Error(err);
}
let data = null;
try {
data = await readFile(path, { encoding: 'utf-8' });
} catch (e) {
throw new Error(e.message);
}

if (!data) {
throw new Error('File is empty');
}
if (!data) {
throw new Error('File is empty');
}

return data;
});
return data;
};

/**
Expand Down Expand Up @@ -443,7 +446,7 @@ imgur.search = async (query, options) => {
delete params.queryStr;

const json = await imgur._imgurRequest('search', queryStr);
return { data: json.data, params };
return { data: json, params };
}
};

Expand Down Expand Up @@ -616,8 +619,8 @@ imgur.uploadAlbum = async (images, uploadType, failSafe) => {
}

const album = await imgur.createAlbum();
const imageArr = await imgur.uploadImages(images, uploadType, album.data.id);
return { data: album.data, images: imageArr };
const imageArr = await imgur.uploadImages(images, uploadType, album.id);
return { data: album, images: imageArr };
};

/**
Expand Down

0 comments on commit 5862a76

Please sign in to comment.