ci-validation
Version:
🇺🇾 Complete TypeScript/JavaScript library for validating Uruguayan CI (Cédula de Identidad) with official algorithm and government service integration
51 lines • 1.88 kB
JavaScript
;
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"));
class PuntosMas {
constructor(ci) {
this.ci = ci;
this.endpoint = "https://gduavailability.herokuapp.com/get-points";
}
/**
* Extracts JSON object from a string that contains JSON data
* @param str - String containing JSON data like: ({"Total":2,"HIPERCARD":2})
* @returns Parsed JSON object or null if extraction fails
*/
extractJsonFromString(str) {
try {
// Remove parentheses and any leading/trailing whitespace
const cleanedStr = str.replace(/^\(|\)$/g, "").trim();
// Parse the JSON string
return JSON.parse(cleanedStr);
}
catch (error) {
console.error("Error extracting JSON from string:", error);
return null;
}
}
async getPoints() {
try {
const response = await axios_1.default.get(`${this.endpoint}/${this.ci}`).catch((e) => {
console.error("Error fetching points:", e);
return { data: { error: e.message } };
});
if (typeof response.data === "string") {
// Use the extractJsonFromString method to parse string responses
const extractedData = this.extractJsonFromString(response.data);
if (extractedData) {
return extractedData;
}
}
return response.data;
}
catch (error) {
console.error("Error fetching points:", error);
throw error;
}
}
}
exports.default = PuntosMas;
//# sourceMappingURL=PuntosMas.js.map