UNPKG

poe-api-manager

Version:
58 lines (57 loc) 2.16 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const axios_1 = __importDefault(require("axios")); const ApiError_1 = __importDefault(require("../../../errors/ApiError")); const WatchUrlGenerator_1 = __importDefault(require("../func/WatchUrlGenerator")); /** * Fetches data from the POE Watch API based on the provided query URL. * @param league - The game league for which to fetch currency data. * @param type - The type of data to fetch. * @returns - A Promise that resolves to the fetched data. * @throws - Throws an error if there is an issue with the API response or fetching data. */ async function fetchData(league, type) { try { const url = (0, WatchUrlGenerator_1.default)(league, type); const response = await axios_1.default.get(url, { headers: { "Accept-Encoding": "identity", }, }); if (response.data) { return response.data; } else { throw new ApiError_1.default("Invalid response format from POE Watch API", 400, { league, type, }); } } catch (error) { // If it's already an ApiError, pass it through if (error instanceof ApiError_1.default) { throw error; } // Handle axios errors if (axios_1.default.isAxiosError(error)) { const statusCode = error.response?.status || 500; const errorDetails = error.response?.data || {}; throw new ApiError_1.default(`Error fetching data from poe.watch: ${errorDetails.error || error.message}`, statusCode, { code: errorDetails.code, league, type, url: error.config?.url, }); } // General error case throw new ApiError_1.default(`Error fetching data from poe.watch: ${error}`, 500, { league, type, }); } } exports.default = fetchData;