kohin-js
Version:
The Kohin JS is a comprehensive developer toolkit designed to integrate Kohin's decentralized insurance system seamlessly into your applications. It enables efficient interaction with Kohin smart contracts and backend APIs, facilitating management and ana
124 lines (123 loc) • 4.94 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApiCall = void 0;
const BaseApi_1 = require("./BaseApi"); // Import the base class for API calls
// Class to handle API requests and responses, extending the BaseApi
class ApiCall extends BaseApi_1.BaseApi {
constructor() {
super(...arguments);
this.responseFunction = (success, data, message, detailMessage, code) => {
const response = {
success,
data,
};
if (message)
response.message = message;
if (detailMessage)
response.detailMessage = detailMessage;
if (code)
response.code = code;
return response;
};
}
/**
* GET method to fetch data from an API endpoint.
* @param endpoint - API endpoint to make the request.
* @param params - Query parameters to include in the request (optional).
* @returns A promise resolving to the API response.
*/
async getData(endpoint, params = {}) {
try {
const response = await this.axiosInstance.get(endpoint, { params });
return this.responseFunction(true, response.data.data, response.data.message, response.data.detailMessage, response.data.code);
}
catch (error) {
throw this.handleError(error);
}
}
/**
* POST method to send data to an API endpoint.
* @param endpoint - API endpoint to make the request.
* @param data - Payload to send in the request body (optional).
* @param params - Query parameters to include in the request (optional).
* @returns A promise resolving to the API response.
*/
async postData(endpoint, data = {}, params = {}) {
try {
const response = await this.axiosInstance.post(endpoint, data, {
params,
});
return this.responseFunction(true, response.data.data, response.data.message, response.data.detailMessage, response.data.code);
}
catch (error) {
throw this.handleError(error);
}
}
/**
* PUT method to update data on an API endpoint.
* @param endpoint - API endpoint to make the request.
* @param data - Payload to send in the request body (optional).
* @returns A promise resolving to the API response.
*/
async putData(endpoint, data = {}) {
try {
const response = await this.axiosInstance.put(endpoint, data);
return this.responseFunction(true, response.data.data, response.data.message, response.data.detailMessage, response.data.code);
}
catch (error) {
throw this.handleError(error);
}
}
/**
* DELETE method to remove data from an API endpoint.
* @param endpoint - API endpoint to make the request.
* @returns A promise resolving to the API response.
*/
async deleteData(endpoint) {
try {
const response = await this.axiosInstance.delete(endpoint);
return this.responseFunction(true, response.data.data, response.data.message, response.data.detailMessage, response.data.code);
}
catch (error) {
throw this.handleError(error);
}
}
/**
* Custom error handling method to format errors.
* @param error - Axios error object containing details of the error.
* @returns A formatted error object.
*/
handleError(error) {
if (error.response) {
// Server-side error
return {
success: error.response?.data?.success || false, // Success flag from response, default to false
code: error.response?.data?.code || null, // Response code or null if not provided
data: error.response?.data?.data || null, // Response data or null
message: error.response?.data?.message || "Unknown server error", // Response message or default
error: error.response?.data?.error || "Unknown error", // Error details or default
};
}
else if (error.request) {
// Network error (no response received)
return {
success: false,
code: null,
data: null,
message: "Network error, no response received", // Network-specific error message
error: error.message || "Network error", // Error message from the Axios request
};
}
else {
// Other types of errors
return {
success: false,
code: null,
data: null,
message: "An error occurred", // Generic error message
error: error.message || "Unknown error", // Error message details
};
}
}
}
exports.ApiCall = ApiCall;