UNPKG

scriptures-byu-edu-http-client

Version:

TypeScript SDK for querying the BYU Scripture Citation Index API (for Bun)

88 lines (85 loc) 3.35 kB
var __create = Object.create; var __getProtoOf = Object.getPrototypeOf; var __defProp = Object.defineProperty; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __toESM = (mod, isNodeMode, target) => { target = mod != null ? __create(__getProtoOf(mod)) : {}; const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target; for (let key of __getOwnPropNames(mod)) if (!__hasOwnProp.call(to, key)) __defProp(to, key, { get: () => mod[key], enumerable: true }); return to; }; var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports); var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true, configurable: true, set: (newValue) => all[name] = () => newValue }); }; // src/types/index.ts var TalkSourceFlag; ((TalkSourceFlag2) => { TalkSourceFlag2["GeneralConference"] = "g"; TalkSourceFlag2["JournalOfDiscourses"] = "j"; TalkSourceFlag2["TeachingsOfJosephSmith"] = "t"; })(TalkSourceFlag ||= {}); var ScriptureSourceFlag; ((ScriptureSourceFlag2) => { ScriptureSourceFlag2["OldTestament"] = "o"; ScriptureSourceFlag2["NewTestament"] = "n"; ScriptureSourceFlag2["JSTFootnotes"] = "f"; ScriptureSourceFlag2["BookOfMormon"] = "b"; ScriptureSourceFlag2["DoctrineAndCovenants"] = "d"; ScriptureSourceFlag2["PearlOfGreatPrice"] = "p"; ScriptureSourceFlag2["JST"] = "j"; })(ScriptureSourceFlag ||= {}); var resultTitleClass = "resultTitle"; var resultContextClass = "resultContext"; // src/urlBuilder.ts function validateSources(tab, sources) { if (tab === "t") { for (const s of sources) { if (!Object.values(TalkSourceFlag).includes(s)) { throw new Error(`Invalid source flag '${s}' for Talks tab`); } } } else if (tab === "s") { for (const s of sources) { if (!Object.values(ScriptureSourceFlag).includes(s)) { throw new Error(`Invalid source flag '${s}' for Scriptures tab`); } } } return sources.join(""); } function buildCitationIndexUrl(params) { const base = "https://scriptures.byu.edu/citation_index/search_results_ajax"; const tab = encodeURIComponent(params.tab); const sort = encodeURIComponent(params.sort ?? "r"); const pageSize = encodeURIComponent((params.pageSize ?? 30).toString()); const offset = encodeURIComponent((params.offset ?? 0).toString()); const luceneQuery = encodeURIComponent(params.query); if (params.tab === "t") { const speakerId = encodeURIComponent(params.speakerId ?? ""); const startYear = encodeURIComponent(params.startYear?.toString() ?? ""); const endYear = encodeURIComponent(params.endYear?.toString() ?? ""); const sources = encodeURIComponent(validateSources("t", params.sources)); return `${base}/${tab}&${speakerId}&${startYear}&${endYear}&${sources}&${sort}&${pageSize}@${offset}$${luceneQuery}`; } else if (params.tab === "s") { const sources = encodeURIComponent(validateSources("s", params.sources)); return `${base}/${tab}&${sources}&${sort}&${pageSize}@${offset}$${luceneQuery}`; } else { throw new Error(`Invalid tab value: ${params}`); } } export { buildCitationIndexUrl };