outers
Version:
outers - a all in one package for your day to day use
88 lines • 4.7 kB
JavaScript
;
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 });
const Console_log_1 = require("../Logs/Console.log"); // Import Color Console Log
const Code_1 = require("../StatusCode/Code"); // Import Status Code
const IP_Type_Checker_function_1 = __importDefault(require("./IP Type Checker.function")); // Import IP Type Checker Function
// Import Storage
const ShortStorage_storage_1 = __importDefault(require("../Storage Management/ShortStorage.storage")); // Import Short Storage Module
// Import Constant Values
const Functions_constant_1 = require("../Config/Constant/Functions.constant"); // Import APiCall from IP Details.utils.constant
// Main Function
/**
* Retrieves details for a given IP address using an API.
*
* @param {string} IP_INFO_API_KEY The API key for accessing IP information.
* @readonly login to https://https://ipinfo.io and get your API key.
* @param {string} ClientIP The IP address for which to retrieve details.
* @returns {Promise<IGetIPDetails>} A Promise that resolves to an object containing the IP details.
*/
function getIPDetails(IP_INFO_API_KEY, ClientIP) {
return __awaiter(this, void 0, void 0, function* () {
try {
// Register IP Details Short Storage for IP Details Cache
const IPDetailsShortStorage = new ShortStorage_storage_1.default("cache-ip-details-for-nodejs", 99000000, // Default Storage is 99TB
"cache-ip-details-for-nodejs"); // Create New Short Storage for IP Details
// Check if user provided IP_INFO_API_KEY
if (IP_INFO_API_KEY === undefined ||
IP_INFO_API_KEY === null ||
IP_INFO_API_KEY === "") {
throw new Error("IP_INFO_API_KEY is not provided");
}
// Check if user provided ClientIP
if (ClientIP === undefined || ClientIP === null || ClientIP === "") {
throw new Error("ClientIP is not provided");
}
// Check if IP_INFO_API_KEY is valid
if (IP_INFO_API_KEY.length < 1) {
throw new Error("IP_INFO_API_KEY is not valid");
}
// Check if ClientIP is valid
if (ClientIP.length < 1) {
throw new Error("ClientIP is not valid");
}
// Check if IP_INFO_API_KEY is valid
// Search IP Details in Short Storage
const IPDetails = yield IPDetailsShortStorage.Get(ClientIP); // Get IP Details from Short Storage
// Check if IP Details are available in Short Storage
if (IPDetails.status == Code_1.StatusCode.OK) {
return {
status: 200,
message: "Success",
details: IPDetails.Data[0].Data,
};
}
const ClientIPData = yield Functions_constant_1.APiCall.Get(`/${ClientIP}/json?token=${IP_INFO_API_KEY}`, true);
const IPType = (0, IP_Type_Checker_function_1.default)(ClientIP); // Check if IP is IPv4 or IPv6
// Save to Short Storage
yield IPDetailsShortStorage.Save(ClientIP, Object.assign(Object.assign({}, ClientIPData), { Type: IPType, origin: "outers Cache Server" })); // Save IP Details to Short Storage
// Send Return data
return {
status: 200,
message: "Success",
details: Object.assign(Object.assign({}, ClientIPData), { Type: IPType, origin: "IP Address Lookup API Server" }),
};
}
catch (error) {
(0, Console_log_1.red)(error); // Log Error
return {
status: 500,
message: "Internal Server Error",
details: null,
};
}
});
}
exports.default = getIPDetails;
//# sourceMappingURL=Get%20IP%20Details.function.js.map