bc-payments-sdk
Version:
BetterCommerce's Payments NodeJS SDK is a complete solution for storefront clients that integrate payments. `bc-payments-sdk` is a single point interface for storefront clients for interacting with payment gateways.
49 lines (48 loc) • 2.77 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Api = void 0;
const fetcher_1 = __importDefault(require("./util/fetcher"));
const RequestMethod_1 = require("../../../constants/enums/RequestMethod");
const BCEnvironment_1 = require("../../../base/config/BCEnvironment");
/**
* The {Api} class provides methods to make HTTP requests using specified parameters.
* It integrates with the fetcher utility to handle HTTP requests and responses,
* offering a consistent interface for interacting with external APIs.
*
* @remark
* This class assumes that the fetcher utility handles exceptions and errors,
* and it is designed to work seamlessly with the BCEnvironment configuration.
*/
class Api {
/**
* Makes an HTTP request using the specified parameters and returns the response data or an error object.
*
* @param {string} url - The endpoint URL for the request.
* @param {string} method - The HTTP method to use (e.g., 'post', 'get').
* @param {object} params - The data to send in the request body (for POST/PUT requests) or as query parameters (for GET requests).
* @param {object} headers - The headers to include in the request.
* @param {object} cookies - Cookies to use for setting additional headers like Currency, Language, etc.
*
* @returns {Promise<any>} The response data if the request is successful, or an error object if it fails.
*
* @throws {InvalidRequestException} If the response status is 400 or 404.
* @throws {AuthenticationException} If the response status is 401.
* @throws {APIException} For any other non-2xx response status.
*/
static async call(url, method, params, headers, cookies, logRequest = false, baseUrl = null) {
let options = { url, method, headers, cookies, baseUrl: baseUrl || BCEnvironment_1.BCEnvironment.baseApiUrl, };
if (params) {
if ((method === null || method === void 0 ? void 0 : method.toUpperCase()) === RequestMethod_1.RequestMethod.GET) {
options = Object.assign(Object.assign({}, options), { params: params });
}
else if ((method === null || method === void 0 ? void 0 : method.toUpperCase()) === RequestMethod_1.RequestMethod.POST || (method === null || method === void 0 ? void 0 : method.toUpperCase()) === RequestMethod_1.RequestMethod.PUT) {
options = Object.assign(Object.assign({}, options), { data: params });
}
}
return await (0, fetcher_1.default)(Object.assign(Object.assign({}, options), { logRequest }));
}
}
exports.Api = Api;