cngn-typescript-library
Version:
A lightweight Typescript library to give you the best experience with managing your cNGN merchant account
86 lines (85 loc) • 3.21 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.cNGNManager = void 0;
const axios_1 = __importDefault(require("axios"));
const aes_standard_1 = require("../utils/aes.standard");
const Ed25519_standard_1 = require("../utils/Ed25519.standard");
const API_CURRENT_VERSION = 'v1';
class cNGNManager {
constructor(secrets) {
this.secrets = secrets;
this.axiosInstance = axios_1.default.create({
baseURL: `https://api.cngn.co/${API_CURRENT_VERSION}/api`,
headers: {
'Authorization': `Bearer ${this.secrets.apiKey}`,
'Content-Type': 'application/json'
},
});
this.axiosInstance.interceptors.response.use(response => response, error => this.handleApiError(error));
}
async handleApiError(error) {
if (error.response) {
throw new Error(`API Error: ${error.response.status} - ${error.response.data.message || 'Unknown error'}`);
}
else if (error.request) {
throw new Error('No response received from API');
}
else {
throw new Error(`Error setting up request: ${error.message}`);
}
}
async makeCalls(method, endpoint, data) {
try {
const response = await this.axiosInstance.request({
method,
url: endpoint,
data: data ? aes_standard_1.AESCrypto.encrypt(JSON.stringify(data), this.secrets.encryptionKey) : undefined
});
if (typeof response.data.data === 'string') {
const decryptedData = await Ed25519_standard_1.Ed25519Crypto.decryptWithPrivateKey(this.secrets.privateKey, response.data.data);
return {
...response.data,
data: JSON.parse(decryptedData)
};
}
return response.data;
}
catch (error) {
throw await this.handleApiError(error);
}
}
async getBalance() {
return this.makeCalls('GET', '/balance');
}
async getTransactionHistory(page = 1, limit = 10) {
return this.makeCalls('GET', `/transactions?page=${page}&limit=${limit}`);
}
async withdraw(data) {
return this.makeCalls('POST', '/withdraw', data);
}
async verifyWithdrawal(tnxRef) {
return this.makeCalls('GET', `/withdraw/verify/${tnxRef}`);
}
async redeemAsset(data) {
return this.makeCalls('POST', '/redeemAsset', data);
}
async createVirtualAccount(data) {
return this.makeCalls('POST', '/createVirtualAccount', data);
}
async updateExternalAccounts(data) {
return this.makeCalls('POST', '/updateBusiness', data);
}
async getBanks() {
return this.makeCalls('GET', '/banks');
}
async swapAsset(data) {
return this.makeCalls('POST', '/swap', data);
}
async getSwapQuote(data) {
return this.makeCalls('POST', '/swap-quote', data);
}
}
exports.cNGNManager = cNGNManager;