UNPKG

poe-api-manager

Version:
47 lines (46 loc) 2.02 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const watchFetch_1 = __importDefault(require("../fetch/watchFetch")); const ValidationError_1 = __importDefault(require("../../../errors/ValidationError")); /** * Fetches data based on the provided query URL and filters it by category name. * @param league - The league to fetch the data from. * @param type - The type of data to fetch. * @param categoryName - The name of the category to filter the data by. * @returns A promise that resolves to an array of filtered data items. * @throws Throws an error if category name is not provided or if there is an issue fetching or filtering the data. */ async function getCategory(league, type, categoryName) { try { // Validate category name first if (!categoryName && categoryName !== "0") { throw new ValidationError_1.default("Category name is required.", 400, { league, type, }); } const fetchedData = await (0, watchFetch_1.default)(league, type); // Filter data by category name const filteredData = fetchedData.filter((item) => item.group === categoryName); // Check if filtered data is empty if (filteredData.length === 0) { throw new ValidationError_1.default(`No data found for category: ${categoryName}`, 404, { league, type, categoryName, }); } return filteredData; } catch (error) { // If it's already a custom error, pass it through if (error instanceof ValidationError_1.default) { throw error; } throw new ValidationError_1.default(`Error fetching or filtering data for category: ${error.message}`, 400, { league, type, categoryName }); } } exports.default = getCategory;