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
98 lines (97 loc) • 3.01 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
default: () => FraudChecker,
fraudChecker: () => fraudChecker
});
module.exports = __toCommonJS(src_exports);
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);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
fraudChecker
});
//# sourceMappingURL=index.js.map
;