UNPKG

@fairmint/canton-node-sdk

Version:
114 lines 4.78 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.HttpClient = void 0; const axios_1 = __importDefault(require("axios")); const errors_1 = require("../errors"); /** Handles HTTP requests with authentication, logging, and error handling */ class HttpClient { constructor(logger) { this.axiosInstance = axios_1.default.create(); this.logger = logger; } async makeGetRequest(url, config = {}) { try { const headers = await this.buildHeaders(config); const response = await this.axiosInstance.get(url, { headers }); await this.logRequestResponse(url, { method: 'GET', headers }, response.data); return response.data; } catch (error) { // Log the error response before throwing if (axios_1.default.isAxiosError(error)) { await this.logRequestResponse(url, { method: 'GET' }, error.response?.data || error.message); } throw this.handleRequestError(error); } } async makePostRequest(url, data, config = {}) { try { const headers = await this.buildHeaders(config); const response = await this.axiosInstance.post(url, data, { headers }); await this.logRequestResponse(url, { method: 'POST', headers, data }, response.data); return response.data; } catch (error) { // Log the error response before throwing if (axios_1.default.isAxiosError(error)) { await this.logRequestResponse(url, { method: 'POST', data }, error.response?.data || error.message); } throw this.handleRequestError(error); } } async makeDeleteRequest(url, config = {}) { try { const headers = await this.buildHeaders(config); const response = await this.axiosInstance.delete(url, { headers }); await this.logRequestResponse(url, { method: 'DELETE', headers }, response.data); return response.data; } catch (error) { // Log the error response before throwing if (axios_1.default.isAxiosError(error)) { await this.logRequestResponse(url, { method: 'DELETE' }, error.response?.data || error.message); } throw this.handleRequestError(error); } } async makePatchRequest(url, data, config = {}) { try { const headers = await this.buildHeaders(config); const response = await this.axiosInstance.patch(url, data, { headers }); await this.logRequestResponse(url, { method: 'PATCH', headers, data }, response.data); return response.data; } catch (error) { // Log the error response before throwing if (axios_1.default.isAxiosError(error)) { await this.logRequestResponse(url, { method: 'PATCH', data }, error.response?.data || error.message); } throw this.handleRequestError(error); } } async buildHeaders(config) { const headers = {}; if (config.contentType) { headers['Content-Type'] = config.contentType; } else { headers['Content-Type'] = 'application/json'; } if (config.includeBearerToken) { // This will be set by the client that uses this HTTP client // The bearer token should be passed in the config or set separately } return headers; } setBearerToken(token) { this.axiosInstance.defaults.headers.common['Authorization'] = `Bearer ${token}`; } clearBearerToken() { delete this.axiosInstance.defaults.headers.common['Authorization']; } async logRequestResponse(url, request, response) { if (this.logger) { await this.logger.logRequestResponse(url, request, response); } } handleRequestError(error) { if (axios_1.default.isAxiosError(error)) { const status = error.response?.status; const data = error.response?.data || {}; const code = data.code; const msg = code ? `HTTP ${status}: ${code}` : `HTTP ${status}`; const err = new errors_1.ApiError(msg, status, error.response?.statusText); err.response = data; return err; } return new errors_1.NetworkError(`Request failed: ${error instanceof Error ? error.message : String(error)}`); } } exports.HttpClient = HttpClient; //# sourceMappingURL=HttpClient.js.map