trade360-nodejs-sdk
Version:
LSports Trade360 SDK for Node.js
85 lines • 3.4 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AxiosService = void 0;
const axios_1 = __importDefault(require("axios"));
const lossless_json_1 = require("lossless-json");
/**
* Custom number parser for lossless-json that converts large positive integers to BigInt
* and other numeric values to regular numbers. Since ID values are always positive,
* we only need to handle large positive integers to prevent precision loss.
*/
function customNumberParser(value) {
if ((0, lossless_json_1.isInteger)(value)) {
// For positive integers only (since IDs are always positive)
if (!value.startsWith('-')) {
// For very long numbers (17+ digits), definitely use BigInt
if (value.length > 16) {
return BigInt(value);
}
// For 16-digit numbers, compare against MAX_SAFE_INTEGER as string
if (value.length === 16) {
const maxSafeIntegerStr = '9007199254740991';
if (value > maxSafeIntegerStr) {
return BigInt(value);
}
}
}
// Safe to convert to number (includes all negative numbers and safe positive numbers)
return parseInt(value, 10);
}
return parseFloat(value);
}
/**
* Axios service instance for different API endpoints with
* varying request and response types. class with a generic
* type TRequest for the request body:
* a generic type TRequest which represents the type of the
* request body. and a response type TResponse and return
* a promise of that response type TResponse.
* @param TRequest Type of the request body
* @param TResponse Type of the response body
* @returns Promise with object of TResponse structure
*/
class AxiosService {
constructor(baseURL) {
this.configRequest = {
validateStatus: function (status) {
return status >= 200 && status < 300; // Resolve only if the status
// code is above then 200 and less then 300
},
// Override the default JSON parsing to use lossless-json
transformResponse: [
function (data) {
if (typeof data === 'string') {
try {
// Use lossless-json to parse with BigInt support for large integers
return (0, lossless_json_1.parse)(data, undefined, customNumberParser);
}
catch (error) {
// If lossless-json fails, return the original data
return data;
}
}
return data;
},
],
};
this.axiosInstance = axios_1.default.create({
baseURL,
headers: {
'Content-Type': 'application/json',
},
});
}
async get(url) {
return this.axiosInstance.get(url, this.configRequest);
}
async post(url, body) {
return this.axiosInstance.post(url, body, this.configRequest);
}
}
exports.AxiosService = AxiosService;
//# sourceMappingURL=axios.service.js.map