UNPKG

@thebadams/5e-srd-sdk

Version:

An NPM module that links into the Dungeons and Dragons 5e SRD API found at https://www.dnd5eapi.co, built using typescript

93 lines 2.92 kB
import axios from 'axios'; export default class Spell { static #BASE_URL = 'https://www.dnd5eapi.co/api/spells'; static async FindAll() { try { const response = await axios.get(this.#BASE_URL); return response.data; } catch (error) { if (error) { return 'There was an error connecting to the API'; } } } static async GetByIndex(index) { try { const response = await axios.get(`${this.#BASE_URL}/${index}`); return response.data; } catch (error) { if (error) { return 'There was an error connecting to the API'; } } } static async FindByLevels(levelsArray) { const levelsString = levelsArray.join(','); // console.log(levelsString); const url = `${this.#BASE_URL}?level=${levelsString}`; //console.log(url) try { const response = await axios.get(url); return response.data; } catch (error) { if (error) { return 'There was an error connecting to the API'; } } } static async FindBySchool(schoolsArray) { const schoolString = schoolsArray.join(','); const url = `${this.#BASE_URL}?school=${schoolString}`; try { const response = await axios.get(url); return response.data; } catch (error) { if (error) { return 'There was an error connecting to the API'; } } } static async Find(queryConfig) { const queryArray = Object.entries(queryConfig); console.log(queryArray); if (queryArray.length === 0) { return this.FindAll(); } let url = this.#BASE_URL; let queryString = '?'; let queries = []; for (const [key, value] of queryArray) { if (key === 'index') { return this.GetByIndex(value); } else if (key === 'level' || key === 'school') { if (Array.isArray(value)) { const string = value.join(','); const query = `${key}=${string}`; queries.push(query); } else { const query = `${key}=${value}`; queries.push(query); } } } queryString = `${queryString}${queries.join('&')}`; url = `${url}${queryString}`; console.log(url); try { const response = await axios.get(url); return response.data; } catch (error) { if (error) { return 'There was an error connecting to the API'; } } } } //# sourceMappingURL=Spell.js.map