UNPKG

bible-kit

Version:

An NPM package to use bible data from the Free Bible Use API. You will be able to get data for all available translations, books for a specific translation, chapters of a specific book and chapter details.

207 lines (153 loc) 6.51 kB
// index.js // The base URL for the Bible API const API_BASE_URL = 'https://bible.helloao.org'; // Get the URL params to get the language const urlParams = new URLSearchParams(window.location.search); const language = urlParams.get('language'); const translation = urlParams.get('translation'); const book = urlParams.get('book'); const chapter = urlParams.get('chapter'); /** * Fetches all available Bible translations. * @returns {Promise<Object>} A promise that resolves to the list of translations. */ export const getAllLanguagesTranslations = async () => { try { const response = await fetch(`${API_BASE_URL}/api/available_translations.json`); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const data = await response.json(); return data.translations; } catch (error) { console.error("Error fetching all translations:", error); throw error; } }; // test the function locally, comment for production // const allTranslations = await getAllLanguagesTranslations(); // console.log("--- All Translations ---"); // console.log("Returns all of the translations available in the API."); // console.log(allTranslations); // console.log(""); /** * Fetches the books for a specific Bible translation. * @param {string} language - The ID of the language (e.g., 'spa', 'eng') * Get the params data from translation.language * @returns {Promise<Object>} A promise that resolves to the list of books. */ export const getTranslationsFromSingleLanguage = async (language) => { try { const response = await fetch(`${API_BASE_URL}/api/available_translations.json`); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const data = await response.json(); const langaugeTranslations = data.translations.filter((translation) => { return translation.language === language; }); return langaugeTranslations; } catch (error) { console.error("Error fetching all translations:", error); throw error; } } // test the function locally, comment for production // const langaugeTranslations = await getTranslationsFromSingleLanguage(language); // console.log("--- Language Translations ---"); // console.log("Returns all of the translations for the selected language."); // console.log(langaugeTranslations); // console.log(""); /** * Fetches the books for a specific Bible translation. * @param {string} translation - The ID of the translation (e.g., 'kjv', 'web'). * Get the params data from translation.id * @returns {Promise<Object>} A promise that resolves to the list of books. */ export const getTranslationBooks = async (translation) => { if (!translation) { throw new Error("translation is required."); } try { const response = await fetch(`${API_BASE_URL}/api/${translation}/books.json`); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const data = await response.json(); return data; } catch (error) { console.error(`Error fetching books for translation ${translation}:`, error); throw error; } }; // test the function locally, comment for production // const translationBooks = await getTranslationBooks(translation); // console.log("--- Translation Books ---"); // console.log("Returns data.books for the translation books and data.translation for the translation general information.") // console.log(translationBooks); // console.log(""); /** * Fetches the chapters for a specific book in a translation. * @param {string} translation - The ID of the translation (e.g., 'kjv', 'web'). * @param {string} book - The ID of the book (e.g., 'GEN', 'JHN'). * Get the params data from translation.id and book.id * @returns {Promise<Object>} A promise that resolves to the list of chapters. */ export const getBookChapters = async (translation, book) => { if (!translation || !book) { throw new Error("translation and book are required."); } try { const response = await fetch(`${API_BASE_URL}/api/${translation}/books.json`); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const data = await response.json(); const chapters = data.books.find((b) => { return b.id === book; }); const totalChapters = chapters.numberOfChapters; const chaptersArray = Array.from({ length: totalChapters }, (_, index) => index + 1); return chaptersArray; } catch (error) { console.error(`Error fetching chapters for book ${book}:`, error); throw error; } }; // test the function locally, comment for production // const bookChapters = await getBookChapters(translation, book); // console.log("--- Book Chapters ---"); // console.log("Returns an arrays of chapter numbers for the selected book.") // console.log(bookChapters); // console.log(""); /** * Fetches the details and verses for a specific chapter. * @param {string} translation - The ID of the translation. * @param {string} book - The ID of the book. * @param {number} chapter - The chapter number. * Get the params data from translation.id, book.id and chapter number * @returns {Promise<Object>} A promise that resolves to the chapter details. */ export const getChapterDetails = async (translation, book, chapter) => { if (!translation || !book || !chapter) { throw new Error("translation, book, and chapter are required."); } try { // The API uses a format like "John+3" for the query const response = await fetch(`${API_BASE_URL}/api/${translation}/${book}/${chapter}.json`); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const data = await response.json(); return data; } catch (error) { console.error(`Error fetching details for chapter ${book} ${chapter}:`, error); throw error; } }; // test the function locally, comment for production // const chapterDetails = await getChapterDetails(translation, book, chapter); // console.log("--- Chapter Details ---"); // console.log("Returns data.book for book general info, data.chapter for the chapter details and more..."); // console.log(chapterDetails); // console.log("");