Skip to content

Commit bc3afe9

Browse files
committed
First commit
1 parent a5542ef commit bc3afe9

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed

index.js

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
exports.Ipgeo = function Ipgeo(apiKey){
2+
3+
this.apiKey = apiKey;
4+
5+
this.ipgeoByFieldsAndIp = function(fields="" ,ip="") {
6+
getRequest("ipgeo", this.apiKey, fields, ip, "");
7+
};
8+
9+
this.ipgeoByFields = function(fields="") {
10+
getRequest("ipgeo", this.apiKey, fields, "", "");
11+
};
12+
13+
this.ipgeoByIp = function(ip="") {
14+
getRequest("ipgeo", this.apiKey, "", ip, "");
15+
};
16+
17+
this.ipgeoByApikey = function(){
18+
getRequest("ipgeo", this.apiKey, "", "", "")
19+
};
20+
21+
this.ipgeoByIps = function(ips=""){
22+
postRequest("ipgeo-bulk", this.apiKey, ips);
23+
};
24+
25+
}
26+
27+
28+
29+
exports.Timezone = function Timezone(apiKey){
30+
31+
this.apiKey = apiKey;
32+
this.timezoneByIp = function(ip="") {
33+
getRequest("timezone", this.apiKey, "", ip, "");
34+
}
35+
36+
this.timezoneByApikey = function() {
37+
getRequest("timezone", this.apiKey, "", "", "");
38+
}
39+
40+
this.timezoneByTz = function(tz="") {
41+
getRequest("timezone", this.apiKey, "", "", tz);
42+
}
43+
44+
}
45+
46+
47+
function getRequest(subUrl=null, apiKey="", fields="", ip="", tz=""){
48+
49+
var URL = "";
50+
if(apiKey){
51+
URL = subUrl;
52+
URL = URL + ("?apiKey=" + apiKey);
53+
if(fields){
54+
URL = URL + "&fields=";
55+
URL = URL + fields;
56+
}
57+
if(ip){
58+
URL = URL + "&ip=";
59+
URL = URL + ip;
60+
}
61+
if(tz){
62+
URL = URL + "&tz=";
63+
URL = URL + tz;
64+
}
65+
66+
}
67+
var data = null;
68+
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
69+
var xhr = new XMLHttpRequest();
70+
xhr.withCredentials = true;
71+
xhr.addEventListener("readystatechange", function () {
72+
if (this.readyState === 4) {
73+
var data = JSON.parse(this.responseText);
74+
console.log(data);
75+
return data;
76+
}
77+
});
78+
xhr.open("GET", "https://api.ipgeolocation.io/"+URL+"");
79+
xhr.send(data);
80+
}
81+
82+
function postRequest(subUrl=null, apiKey="", ips=""){
83+
84+
var data = JSON.stringify({
85+
"ips": ips
86+
});
87+
88+
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
89+
var xhr = new XMLHttpRequest();
90+
xhr.withCredentials = true;
91+
92+
xhr.addEventListener("readystatechange", function () {
93+
if (this.readyState === 4) {
94+
var data = JSON.parse(this.responseText);
95+
console.log(data);
96+
return data;
97+
}
98+
});
99+
100+
xhr.open("POST", "https://api.ipgeolocation.io/"+subUrl+"?apiKey="+apiKey+"");
101+
xhr.setRequestHeader("Content-Type", "application/json");
102+
103+
xhr.send(data);
104+
105+
}
106+
107+
108+

0 commit comments

Comments
 (0)