UNPKG

ts-midtrans-client

Version:

This library is an UNOFFICIAL TypeScript version of the Midtrans Client - Node.js.

81 lines (80 loc) 3.55 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 snapBiConfig_1 = __importDefault(require("./snapBiConfig")); const https_1 = __importDefault(require("https")); // Set up axios interceptors axios_1.default.interceptors.request.use((config) => { if (snapBiConfig_1.default.enableLogging) { console.log(`Request URL: ${config.url}`); console.log(`Request Headers: \n${JSON.stringify(config.headers, null, 2)}`); if (config.data) { console.log(`Request Body: \n${JSON.stringify(config.data, null, 2)}`); } } return config; }, (error) => { if (snapBiConfig_1.default.enableLogging) { console.error(`Request Error: ${error.message}`); } return Promise.reject(error); }); axios_1.default.interceptors.response.use((response) => { if (snapBiConfig_1.default.enableLogging) { console.log(`Response Status: ${response.status}`); console.log(`Response Body: \n${JSON.stringify(response.data, null, 2)}`); } return response; }, (error) => { if (snapBiConfig_1.default.enableLogging) { console.error(`Response Error: ${error.message}`); } return Promise.reject(error); }); class SnapBiApiRequestor { /** * Make a remote API call with the specified URL, headers, and request body. * @param {string} url - The API endpoint URL. * @param {object} header - The headers for the request. * @param {object} body - The JSON payload for the request. * @returns {Promise<object>} - The JSON response from the API. */ static remoteCall(url_1, header_1, body_1) { return __awaiter(this, arguments, void 0, function* (url, header, body, timeout = 10000) { const axiosHeaders = Object.assign({}, header); try { const axiosOptions = { headers: axiosHeaders, validateStatus: function (status) { return status >= 200 && status < 300; }, httpsAgent: new https_1.default.Agent({ rejectUnauthorized: false }), timeout: timeout !== null ? timeout : 10000, }; const response = yield axios_1.default.post(url, body, axiosOptions); return response.data; } catch (error) { if (error.response) { return error.response.data; } else { return { message: error.message, status: error.code || 500 }; } } }); } } exports.default = SnapBiApiRequestor;