biblia-interface
Version:
An interface for interacting with the api.biblia.com
158 lines (157 loc) • 6.08 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Biblia = void 0;
var node_fetch_1 = require("node-fetch");
var Biblia = /** @class */ (function () {
/**
* constructor
* @param apiKey Visit https://bibliaapi.com/docs/API_Keys to active an API key
* @param [bible = "asv"]
*/
function Biblia(apiKey, bible) {
if (bible === void 0) { bible = "asv"; }
this.baseReference = "https://api.biblia.com/v1/bible";
this.apiKey = apiKey;
this.bible = bible;
}
/**
* @param {Object} [options] Optional query parameters
* @return A promise with an object of the different bibles and their descriptions
*/
Biblia.prototype.getBibles = function (options) {
var _this = this;
var params = "";
if (options) {
params = setParams(options);
}
return new Promise(function (resolve, reject) {
node_fetch_1.default(_this.baseReference + "/find.js?" + params + "key=" + _this.apiKey)
.then(function (res) { return resolve(res.json()); })
.catch(function (err) { return reject(err); });
});
};
/**
* @returns A promise with an array of all the available bibles
*/
Biblia.prototype.getBibleNames = function () {
var _this = this;
return new Promise(function (resolve, reject) {
_this.getBibles()
.then(function (res) {
resolve(res.bibles.map(function (bible) { return bible.bible; }));
})
.catch(function (err) { return reject(err); });
});
};
/**
* @param passage - The text to parse (required).
* @param {Option} [options] - Optional params
* @returns A promise. Parses the specified text as one or more Bible passages.
* Can also be used to render a Bible reference in short, medium, or long form.
*/
Biblia.prototype.parseText = function (passage, options) {
var _this = this;
var params = "";
if (options) {
params = setParams(options);
}
passage = encodeURI(passage);
return new Promise(function (resolve, reject) {
node_fetch_1.default(_this.baseReference + "/parse?passage=" + passage + "&" + params + "key=" + _this.apiKey)
.then(function (res) { return resolve(res.json()); })
.catch(function (err) { return reject(err); });
});
};
/**
* @param {String} text - The text to parse
* @param {Object} [options] Optional parameters
* @returns A promise with the comparison of the compared texts
*/
Biblia.prototype.scanText = function (text, options) {
var _this = this;
var params = "";
if (options) {
params = setParams(options);
}
return new Promise(function (resolve, reject) {
node_fetch_1.default(_this.baseReference + "/scan.js?" + params + "text=" + text + "&key=" + _this.apiKey)
.then(function (res) { return resolve(res.json()); })
.catch(function (err) { return reject(err); });
});
};
/**
* @param {String} firstVerse The first verse to compare
* @param {String} secondVerse The second verse to compare
* @returns A promise with the comparison two Bible references
*/
Biblia.prototype.compare = function (firstVerse, secondVerse) {
var _this = this;
firstVerse = encodeURI(firstVerse);
secondVerse = encodeURI(secondVerse);
return new Promise(function (resolve, reject) {
node_fetch_1.default(_this.baseReference + "/compare?first=" + firstVerse + "&second=" + secondVerse + "&key=" + _this.apiKey)
.then(function (res) { return resolve(res.json()); })
.catch(function (err) { return reject(err); });
});
};
/**
* @param {String} query - The query text
* @param {Object} [options] - Optional Parameters
* @return A promise of an object with previews of the query matches
*/
Biblia.prototype.search = function (query, options) {
var _this = this;
var params = "";
if (options) {
params = setParams(options);
}
query = encodeURI(query);
return new Promise(function (resolve, reject) {
node_fetch_1.default(_this.baseReference + "/search/" + _this.bible + ".js?query=" + query + "&" + params + "key=" + _this.apiKey)
.then(function (res) { return resolve(res.json()); })
.catch(function (err) { return reject(err); });
});
};
/**
* @param passage - The Bible passage to return
* @param {Object} [options]
* @returns A promise with contents of a the Bible.
*/
Biblia.prototype.getPassage = function (passage, options) {
var _this = this;
var params = "";
var format = "";
if (options) {
params = setParams(options);
if (options.html) {
format = "html.";
}
}
passage = encodeURI(passage);
return new Promise(function (resolve, reject) {
node_fetch_1.default(_this.baseReference + "/content/" + _this.bible + "." + format + "js?" + params + "passage=" + passage + "&key=" + _this.apiKey)
.then(function (res) { return resolve(res.json()); })
.catch(function (err) { return reject(err); });
});
};
/**
*
* @param {String} bible - The bible version to set the interface to.
*/
Biblia.prototype.setBible = function (bible) {
this.bible = bible;
};
return Biblia;
}());
exports.Biblia = Biblia;
function setParams(options) {
var params = "";
if (options) {
Object.keys(options).forEach(function (key) {
if (key) {
params = params + "&" + key + "=" + options[key] + "&";
}
});
}
return params;
}