ipgeo_sdk
Version:
A TypeScript SDK for interacting with the IPGeo API, providing fast and reliable geolocation services.
33 lines (32 loc) • 1.24 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.IPGeoSdk = void 0;
const axios_1 = __importDefault(require("axios"));
const zod_1 = require("zod");
class IPGeoSdk {
constructor(options) {
this.apiKey = options.apiKey;
this.baseUrl = process.env.NODE_ENV === 'test' ? 'http://localhost:8000' : 'https://api.ipgeo.dev';
this.axiosInstance = axios_1.default.create({
baseURL: this.baseUrl,
headers: {
'X-API-Key': `${this.apiKey}`,
},
});
}
async getGeoData(ip) {
// Zod schema for IP address (IPv4 or IPv6)
const ipSchema = zod_1.z.string().refine(val => {
const ipv4 = /^(?:\d{1,3}\.){3}\d{1,3}$/;
const ipv6 = /^([\da-fA-F]{1,4}:){7}[\da-fA-F]{1,4}$/;
return ipv4.test(val) || ipv6.test(val);
}, { message: 'Invalid IP address format' });
ipSchema.parse(ip);
const response = await this.axiosInstance.get(`/ipgeo/${ip}`);
return response.data;
}
}
exports.IPGeoSdk = IPGeoSdk;
;