vvc
Version:
Vivocha Command Line Tools
70 lines • 2.09 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const fs = require("fs");
const request = require("request");
const config_1 = require("./config");
class RequestError extends Error {
constructor(originalError, response, body) {
super();
this.originalError = originalError;
this.response = response;
this.body = body;
}
}
exports.RequestError = RequestError;
async function ws(path, opts, okStatusCodes = [200, 201]) {
const config = await config_1.read();
const url = await wsUrl(path);
return new Promise((resolve, reject) => {
request(Object.assign({
method: 'GET',
url,
auth: {
user: config.user_id,
pass: config.secret,
sendImmediately: true
},
json: true
}, opts), function (err, resp, body) {
if (err || okStatusCodes.indexOf(resp.statusCode) === -1) {
reject(new RequestError(err, resp, body));
}
else {
resolve(body);
}
});
});
}
exports.ws = ws;
async function wsUrl(path) {
const config = await config_1.read();
return `https://${config.server}/a/${config.acct_id}/api/v${config.version || '2'}/${path}`;
}
exports.wsUrl = wsUrl;
async function retriever(url) {
return new Promise(function (resolve, reject) {
request({
url: url,
method: 'GET',
json: true
}, function (err, resp, body) {
if (err || resp.statusCode !== 200) {
reject(new RequestError(err, resp, body));
}
else {
resolve(body);
}
});
});
}
exports.retriever = retriever;
async function download(url, filename) {
return new Promise((resolve, reject) => {
request(url).pipe(fs
.createWriteStream(filename)
.on('close', resolve)
.on('error', reject));
});
}
exports.download = download;
//# sourceMappingURL=ws.js.map