UNPKG

@akashicpay/sdk

Version:

SDK to interact with the Akashic ecosystem

66 lines 2.39 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.FetchHttpClient = void 0; const node_fetch_1 = __importDefault(require("node-fetch")); const package_json_1 = require("../package.json"); class FetchHttpClient { constructor(logger) { this.client = 'js-sdk'; this.version = package_json_1.version; this.logger = logger; } async post(url, payload) { const response = await (0, node_fetch_1.default)(url, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Ap-Version': this.version, 'Ap-Client': this.client, }, body: JSON.stringify(payload), }); await this.checkResponseForErrors(response); this.checkApiWarning(response.headers); return { data: (await response.json()), status: response.status, }; } async get(url) { const response = await (0, node_fetch_1.default)(url, { method: 'GET', headers: { Version: this.version, Client: this.client, }, }); await this.checkResponseForErrors(response); this.checkApiWarning(response.headers); return { data: (await response.json()), status: response.status, }; } async checkResponseForErrors(response) { var _a; if (response.status < 400) return; if ((_a = response.headers.get('content-type')) === null || _a === void 0 ? void 0 : _a.includes('application/json')) { const errorResponse = await response.json(); throw new Error(`${errorResponse === null || errorResponse === void 0 ? void 0 : errorResponse.error}: ${errorResponse === null || errorResponse === void 0 ? void 0 : errorResponse.message}`); } else { throw new Error(`HTTP Error: ${response.status}`); } } checkApiWarning(headers) { const warning = headers.get('warning'); if (warning) this.logger.warn(warning); } } exports.FetchHttpClient = FetchHttpClient; //# sourceMappingURL=FetchHttpClient.js.map