@crediblex.io/fineract-api-client
Version:
TypeScript client for Fineract APIs
93 lines • 3.84 kB
JavaScript
;
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"));
/**
* HttpClient is a wrapper around the Axios library that provides a more
* user-friendly interface for making HTTP requests.
*
* It handles the configuration of headers, authentication, and request
*
* @remarks
* This class is not intended to be used directly. It is used internally by the Fineract API client.
*/
class HttpClient {
constructor(config) {
this.tenantId = config.tenantId || "default";
const headers = {
"Content-Type": "application/json",
Accept: "application/json",
};
if (this.tenantId) {
headers["Fineract-Platform-TenantId"] = this.tenantId; // Corrected header name
}
const auth = config.username && config.password
? { username: config.username, password: config.password }
: undefined;
this.axiosInstance = axios_1.default.create({
baseURL: config.baseURL,
timeout: config.timeout || 30000,
headers,
auth,
});
this.axiosInstance.interceptors.response.use((response) => response, (error) => {
const fineractError = this.transformError(error);
return Promise.reject(fineractError);
});
}
transformError(error) {
if (error.response) {
const { status, data } = error.response;
const errorBody = data; // Fineract error body can be complex
return {
error: errorBody?.developerMessage || `HTTP Error: ${status}`,
message: errorBody?.defaultUserMessage || error.message,
details: errorBody,
statusCode: status,
};
}
else if (error.request) {
// Avoid circular JSON issues by not directly including error.request
return {
error: "Network Error",
message: "No response received from server. Check network connectivity or server status.",
details: {
code: error.code, // e.g., ECONNREFUSED, ENOTFOUND
message: "The request was made, but no response was received. This usually indicates a network issue or that the server is not reachable at the specified address.",
targetAddress: `${error.config?.baseURL || ""}${error.config?.url || ""}`,
},
statusCode: 0, // Or another appropriate code for network errors
};
}
else {
return {
error: "Request Setup Error",
message: error.message ||
"An unexpected error occurred while setting up the request.",
statusCode: -1, // Or another appropriate code for client-side errors
};
}
}
async request(method, url, data, params, headers) {
const config = { method, url, data, params, headers };
const response = await this.axiosInstance.request(config);
return response;
}
async get(url, params, headers) {
return this.request("GET", url, undefined, params, headers);
}
async post(url, data, headers) {
return this.request("POST", url, data, undefined, headers);
}
async put(url, data, headers) {
return this.request("PUT", url, data, undefined, headers);
}
async delete(url, headers) {
return this.request("DELETE", url, undefined, undefined, headers);
}
}
exports.HttpClient = HttpClient;
//# sourceMappingURL=http-client.js.map