UNPKG

veend-sdk

Version:

a nodejs package for veend products

116 lines (115 loc) 4.67 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 }); exports.APIClient = void 0; const axios_1 = __importDefault(require("axios")); const helper_1 = require("../utils/helper"); class APIClient { constructor(isLive = false, developerToken, clientId, // 'userId_accountId' format xTag = "VeendHQ SDK", xJwt = "VeendHQ SDK") { this.xTag = null; const [userId, accountId] = clientId.split("_"); if (!userId || !accountId) { throw new Error("Invalid clientId format. Expected 'userId_accountId'."); } this.isLive = isLive || false; this.userId = userId; this.accountId = accountId; this.xTag = xTag; this.giroDevBaseUrl = "https://test.veendhq.com"; this.giroProdBaseUrl = "https://tx.veendhq.com"; this.lendingDevBaseUrl = "https://api2.veendhq.com"; this.lendingProdBaseUrl = "https://api.veendhq.com"; this.giroClient = axios_1.default.create({ baseURL: isLive ? this.giroProdBaseUrl : this.giroDevBaseUrl, headers: { Authorization: `Bearer ${developerToken}`, "Content-Type": "application/json", "x-tag": this.xTag, }, }); this.lendingClient = axios_1.default.create({ baseURL: isLive ? this.lendingProdBaseUrl : this.lendingDevBaseUrl, headers: { // Authorization: `Bearer ${developerToken}`, "x-jwt": xJwt, "Content-Type": "application/json", "x-tag": this.xTag, }, }); } // Generic GET method get(service, url, config) { return __awaiter(this, void 0, void 0, function* () { try { let isGiro = service === "giro"; const response = isGiro ? yield this.giroClient.get(url, config) : yield this.lendingClient.get(url, config); return response.data; } catch (error) { this.handleError(error); } }); } // Generic POST method post() { return __awaiter(this, arguments, void 0, function* (service = "giro", url, data, config) { try { let isGiro = service === "giro"; const response = isGiro ? yield this.giroClient.post(url, data, config) : yield this.lendingClient.post(url, data, config); return response.data; } catch (error) { this.handleError(error); } }); } // Generic PATCH method patch(service, url, data, config) { return __awaiter(this, void 0, void 0, function* () { try { let isGiro = service === "giro"; const response = isGiro ? yield this.giroClient.patch(url, data, config) : yield this.lendingClient.patch(url, data, config); return response.data; } catch (error) { this.handleError(error); } }); } // Error handling handleError(error) { if (error.response) { throw new Error(`API Error: ${error.response.status} - ${error.response.data.message || error.message}`); } throw new Error(`Network Error: ${error.message}`); } buildQuery(json) { let query = "?"; if ((0, helper_1.isEmptyObject)(json)) { return ""; } for (const i in (0, helper_1.removeNullKey)(json)) { query += encodeURIComponent(i) + "=" + encodeURIComponent(json[i]) + "&"; } return query.replace(/&$/g, ""); } } exports.APIClient = APIClient;