cht-sh
Version:
A node based command line tool for cht.sh
56 lines (55 loc) • 1.81 kB
JavaScript
import fetch from 'node-fetch';
export const call = async (topic, subtopic, options) => {
let url = `https://cht.sh/${topic}`;
// trim subtopic and replace all spaces with a +
subtopic = subtopic.trim().replace(/\s+/g, '+');
if (subtopic)
url += `/${subtopic}`;
let style = '';
const optionLetters = [];
Object.keys(options).forEach(key => {
switch (key) {
case 'quiet':
if (options[key])
pushIfNotExists(optionLetters, 'q');
break;
case 'text':
if (options[key])
pushIfNotExists(optionLetters, 'T');
break;
case 'commentDisabled':
if (options[key])
pushIfNotExists(optionLetters, 'c');
break;
case 'commentDisabledShift':
if (options[key])
pushIfNotExists(optionLetters, 'C');
break;
case 'codeOnly':
if (options[key])
pushIfNotExists(optionLetters, 'Q');
break;
case 'style':
style = options.style;
break;
}
});
const queryParams = [];
if (optionLetters.length)
queryParams.push(optionLetters.join(''));
if (style)
queryParams.push(`style=${style}`);
const initialLength = queryParams.length;
while (queryParams.length > 0) {
url += (queryParams.length === initialLength) ? '?' : '&';
url += queryParams.shift();
}
const response = await fetch(url);
const body = await response.text();
return body;
};
const pushIfNotExists = (array, value) => {
if (!array.includes(value)) {
array.push(value);
}
};