ipgeolocation-te
Version:
new pkg
77 lines (59 loc) • 1.54 kB
JavaScript
exports.Ipgeo = function Ipgeo(apiKey){
this.apiKey = apiKey;
this.ipgeoByFieldsAndIp = function(fields="" ,ip="") {
request("ipgeo", this.apiKey, fields, ip, "");
};
this.ipgeoByFields = function(fields="") {
request("ipgeo", this.apiKey, fields, "", "");
};
this.ipgeoByIp = function(ip="") {
request("ipgeo", this.apiKey, "", ip, "");
};
this.ipgeoByApikey = function(){
request("ipgeo", this.apiKey, "", "", "")
};
}
exports.Timezone = function Timezone(apiKey){
this.apiKey = apiKey;
this.timezoneByIp = function(ip="") {
request("timezone", this.apiKey, "", ip, "");
}
this.timezoneByApikey = function() {
request("timezone", this.apiKey, "", "", "");
}
this.timezoneByTz = function(tz="") {
request("timezone", this.apiKey, "", "", tz);
}
}
function request(subUrl=null, apiKey="", fields="", ip="", tz=""){
var URL = "";
if(apiKey){
URL = subUrl;
URL = URL + ("?apiKey=" + apiKey);
if(fields){
URL = URL + "&fields=";
URL = URL + fields;
}
if(ip){
URL = URL + "&ip=";
URL = URL + ip;
}
if(tz){
URL = URL + "&tz=";
URL = URL + tz;
}
}
var data = null;
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener("readystatechange", function () {
if (this.readyState === 4) {
var data = JSON.parse(this.responseText);
console.log(data);
return data;
}
});
xhr.open("GET", "https://api.ipgeolocation.io/"+URL+"");
xhr.send(data);
}