UNPKG

amberflo-metering-typescript

Version:
116 lines 4.93 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; 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 axios_retry_1 = __importDefault(require("axios-retry")); const constants_1 = require("./model/constants"); class BaseClient { /** * Initialize a new `BaseClient` * `name`: Name of the client for logging purposes. * `debug`: Whether to issue debug level logs or not. * `retry`: Whether to retry idempotent requests on 5xx or network errors, or retry configuration (see https://github.com/softonic/axios-retry). */ constructor(apiKey, debug, name, retry = true) { this.signature = `[amberflo-metering ${name}]:`; this.apiKey = apiKey; this.debug = debug; this.axiosInstance = axios_1.default.create({ baseURL: constants_1.amberfloBaseUrl, responseType: 'json', headers: { "X-API-KEY": this.apiKey, "Content-Type": "application/json", "User-Agent": constants_1.userAgent, }, timeout: 30000 }); if (retry) { (0, axios_retry_1.default)(this.axiosInstance, Object.assign({ retries: 3, retryDelay: axios_retry_1.default.exponentialDelay }, toRetryOptions(retry))); } } /* eslint-disable-next-line @typescript-eslint/no-explicit-any */ log(level, message, ...args) { console.log(new Date(), this.signature, level, message, ...args); } /* eslint-disable-next-line @typescript-eslint/no-explicit-any */ logInfo(message, ...args) { this.log('INFO', message, ...args); } /* eslint-disable-next-line @typescript-eslint/no-explicit-any */ logDebug(message, ...args) { if (this.debug) { this.log('DEBUG', message, ...args); } } /* eslint-disable-next-line @typescript-eslint/no-explicit-any */ logError(message, ...args) { this.log('ERROR', message, ...args); } /* eslint-disable-next-line @typescript-eslint/no-explicit-any */ doGet(path, params) { return __awaiter(this, void 0, void 0, function* () { const action = `GET ${path}`; try { this.logDebug(action, params); const response = yield this.axiosInstance.get(path, params ? { params } : undefined); this.logDebug(action, response.status); return response.data; } catch (error) { this.logError(action, error); throw new Error(`${action} failed: ${error}`); } }); } /* eslint-disable-next-line @typescript-eslint/no-explicit-any */ doPost(path, payload, params) { return __awaiter(this, void 0, void 0, function* () { const action = `POST ${path}`; try { this.logDebug(action, payload, params); const response = yield this.axiosInstance.post(path, payload, params ? { params } : undefined); this.logDebug(action, response.status); return response.data; } catch (error) { this.logError(action, error); throw new Error(`${action} failed: ${error}`); } }); } /* eslint-disable-next-line @typescript-eslint/no-explicit-any */ doPut(path, payload) { return __awaiter(this, void 0, void 0, function* () { const action = `PUT ${path}`; try { this.logDebug(action, payload); const response = yield this.axiosInstance.put(path, payload); this.logDebug(action, response.status); return response.data; } catch (error) { this.logError(action, error); throw new Error(`${action} failed: ${error}`); } }); } } exports.default = BaseClient; function toRetryOptions(retry) { if (typeof retry === 'boolean') return {}; return retry; } //# sourceMappingURL=baseClient.js.map