UNPKG

geonet

Version:

A Node.js API wrapper for GeoNet — Aotearoa's geological hazard monitoring system.

50 lines (49 loc) 1.88 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseService = void 0; const axios_1 = __importDefault(require("axios")); /** * BaseService class to handle common HTTP requests to the GeoNet API. * @abstract * @since 1.0.0 */ class BaseService { baseURL = "https://api.geonet.org.nz"; /** * Performs a GET request to the specified endpoint. * * @param {BaseServiceGETRequest} req - The request object containing the endpoint and format. * @returns {Promise<any>} - A promise that resolves to the response data. * @throws {Error} - Throws an error if the endpoint is not provided, is not a string, or does not start with a backslash (/). * @since 1.0.0 */ async GET(req) { const url = `${this.baseURL}${req.endpoint}`; if (!req.endpoint || typeof (req.endpoint) !== "string") throw new Error("Endpoint not provided or is not a string."); if (!req.endpoint.startsWith("/")) throw new Error("Endpoint must start with a backslash (/)."); try { const response = await axios_1.default.get(url, { headers: { "Content-Type": req.format, "Accept": req.format } }); return response.data; } catch (error) { if (axios_1.default.isAxiosError(error)) { console.error("Response Error: ", { status: error.response?.status, data: error.response?.data }); } throw new Error(error instanceof Error ? error.message : String(error)); } } } exports.BaseService = BaseService;