poe-api-manager
Version:
poe.ninja and poe.watch API
42 lines (41 loc) • 1.62 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const ApiError_1 = __importDefault(require("../../errors/ApiError"));
const fetchData_1 = __importDefault(require("./fetchData/fetchData"));
/**
* Fetches league names from the provided URL.
*
* This function fetches data from the specified URL which contains information about
* Path of Exile leagues. It then extracts the names of the leagues from the fetched data
* and returns them as an array.
*
* @returns A Promise that resolves with an array of league names.
* @throws If an error occurs during the fetch process or if the response data is empty.
*/
async function getLeagues() {
const url = "https://api.poe.watch/leagues";
try {
const { data } = await (0, fetchData_1.default)(url);
if (!data || !Array.isArray(data)) {
throw new ApiError_1.default("Invalid response format from leagues API", 400, {
url,
});
}
const names = data.map((item) => item.name);
if (names.length === 0) {
throw new ApiError_1.default("No leagues found in response", 404, { url });
}
return names;
}
catch (error) {
// Pass through ApiError instances
if (error instanceof ApiError_1.default) {
throw error;
}
throw new ApiError_1.default(`Error fetching League Names: ${error.message}`, 500, { url });
}
}
exports.default = getLeagues;