Add ttm.sh module
This commit is contained in:
parent
2df44132f9
commit
9d4074d6c2
39
cli.js
39
cli.js
|
@ -58,37 +58,18 @@ async function shorten(url) {
|
|||
doRequest("shorten", url);
|
||||
}
|
||||
|
||||
// this is run when we need to contact ttm.sh (via the module index.js)
|
||||
function doRequest(action, data) {
|
||||
// create an spinner to show that we are trying to upload
|
||||
// the spinner will have a funny message from 'funnies'
|
||||
const spinner = ora(funnies.message()).start();
|
||||
|
||||
let formData = {}
|
||||
|
||||
switch (action) {
|
||||
case "file":
|
||||
// in the form data, set file to be a file stream
|
||||
// data is our file path if the action is 'file'
|
||||
formData["file"] = fs.createReadStream(data);
|
||||
break;
|
||||
case "shorten":
|
||||
// in the form data, set shorten to be the url to shorten
|
||||
// data is our url if the action is 'shorten'
|
||||
formData["shorten"] = data;
|
||||
break;
|
||||
default:
|
||||
spinner.fail("Invalid action!");
|
||||
return;
|
||||
}
|
||||
|
||||
// make a post request to ttm.sh with our form data that we made above
|
||||
request.post({
|
||||
url: "https://ttm.sh",
|
||||
formData: formData
|
||||
}, (err, res, body) => {
|
||||
if (res.statusCode !== 200 || err) {
|
||||
spinner.fail(`Whoops! There was a problem while completing your request [HTTP ${res.statusCode}] - ${err.message}`);
|
||||
return;
|
||||
}
|
||||
|
||||
spinner.succeed(body.trim());
|
||||
// use the ttmsh module to run the action with the data
|
||||
ttmsh.do(action, data).then((result) => {
|
||||
// if we were successful, display our result (i.e the url)
|
||||
spinner.succeed(result);
|
||||
}).catch((e) => {
|
||||
// if something went wrong, display an message with the error
|
||||
spinner.fail(`Whoops! There was a problem while completing your request - ${err.message}`);
|
||||
});
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
const rp = require("request-promise");
|
||||
|
||||
/**
|
||||
* A wrapper for communication with ttm.sh
|
||||
*
|
||||
* @param {string} action An action to perform (e.g file, shorten)
|
||||
* @param {string} data The data to upload (e.g path to file, url to shorten)
|
||||
* @returns A promise with the shortened link in a string
|
||||
*/
|
||||
module.exports.do = function(action, data) {
|
||||
if (!action) {
|
||||
throw new Error("Invalid action!");
|
||||
}
|
||||
|
||||
if (!data) {
|
||||
throw new Error("Invalid data!");
|
||||
}
|
||||
|
||||
const formData = {}
|
||||
|
||||
switch (action) {
|
||||
case "file":
|
||||
// in the form data, set file to be a file stream
|
||||
// data is our file path if the action is 'file'
|
||||
formData["file"] = fs.createReadStream(data);
|
||||
break;
|
||||
case "shorten":
|
||||
// in the form data, set shorten to be the url to shorten
|
||||
// data is our url if the action is 'shorten'
|
||||
formData["shorten"] = data;
|
||||
break;
|
||||
default:
|
||||
throw new Error("Invalid action!");
|
||||
}
|
||||
|
||||
return rp({
|
||||
method: "POST",
|
||||
url: "https://ttm.sh",
|
||||
formData: formData
|
||||
}).then(function(body) {
|
||||
return body.trim();
|
||||
});
|
||||
}
|
Reference in New Issue