fraud-check-ts
Version:
This module integrates the IP-API service to analyze IP addresses and determine their potential for fraudulent activity based on a predefined list of high-risk countries. It is designed to enhance security measures for online platforms by identifying and
73 lines • 2 kB
JavaScript
// src/index.ts
var FraudChecker = class _FraudChecker {
key;
constructor(payload) {
if (typeof payload.key === "string") {
this.key = payload.key;
}
}
async runApi(ip) {
const isPro = Boolean(this.key);
const apiLink = isPro ? "https://pro.ip-api.com/json/" : "http://ip-api.com/json/";
const params = new URLSearchParams({
key: isPro && this.key ? this.key : "",
fields: "status,countryCode,proxy,hosting,org"
}).toString();
const url = `${apiLink}${ip}?${params}`;
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error) {
console.error("Error fetching data: ", error);
throw error;
}
}
async verify(ip, opts) {
try {
const result = await this.runApi(ip);
if (result.status === "fail") {
if (opts.allowFailStatus) {
return {
success: true
};
}
throw "status_fail";
}
if (result.proxy && !opts.allowProxy && !(opts.byPassIcloudRelay && result.org === "iCloud Private Relay")) {
throw "is_proxy";
}
if (result.hosting && !opts.allowHosting) {
throw "is_hosting";
}
if ("bannedCountries" in opts && opts.bannedCountries.includes(result.countryCode)) {
throw "banned_country";
}
if ("validCountries" in opts && !opts.validCountries.includes(result.countryCode)) {
throw "not_valid_country";
}
return {
success: true
};
} catch (error) {
return {
success: false,
reason: String(error)
};
}
}
static fraudChecker(payload) {
return new _FraudChecker(payload);
}
};
function fraudChecker(payload) {
return new FraudChecker(payload);
}
export {
FraudChecker as default,
fraudChecker
};
//# sourceMappingURL=index.mjs.map