ip-geolocation-api-sdk-typescript
Advanced tools
Comparing version
@@ -1,15 +0,24 @@ | ||
export class GeolocationParams { | ||
export class GeolocationParams { | ||
ipAddress: string; | ||
ipAddresses: string[]; | ||
lang: string; | ||
fields: string; | ||
excludes: string; | ||
lang: string; | ||
ipAddresses: string[]; | ||
includeHostname: boolean; | ||
includeHostnameFallbackLive: boolean; | ||
includeLiveHostname: boolean; | ||
includeSecurity: boolean; | ||
includeUserAgent: boolean; | ||
constructor() { | ||
this.ipAddress = ""; | ||
this.ipAddress = ""; | ||
this.ipAddresses = []; | ||
this.lang = "en"; | ||
this.fields = ""; | ||
this.excludes = ""; | ||
this.lang = "en"; | ||
this.ipAddresses = []; | ||
this.includeHostname = false; | ||
this.includeHostnameFallbackLive = false; | ||
this.includeLiveHostname = false; | ||
this.includeSecurity = false; | ||
this.includeUserAgent = false; | ||
} | ||
@@ -25,2 +34,22 @@ | ||
setIPAddresses(ipAddresses: string[] = []): void { | ||
if (ipAddresses.length > 50) { | ||
console.log("Max. number of IP addresses cannot be more than 50."); | ||
} else { | ||
this.ipAddresses = ipAddresses; | ||
} | ||
} | ||
getIPAddresses(): string[] { | ||
return this.ipAddresses; | ||
} | ||
setLang(lang: string = "en"): void { | ||
this.lang = lang; | ||
} | ||
getLang(): string { | ||
return this.lang; | ||
} | ||
setFields(fields: string = ""): void { | ||
@@ -42,21 +71,43 @@ this.fields = fields; | ||
setLang(lang: string = "en"): void { | ||
this.lang = lang; | ||
setIncludeHostname(hostname: boolean = false): void { | ||
this.includeHostname = hostname; | ||
} | ||
getLang(): string { | ||
return this.lang; | ||
isIncludeHostname(): boolean { | ||
return this.includeHostname; | ||
} | ||
setIPAddresses(ipAddresses : string[] = []): void { | ||
if(ipAddresses.length > 50) { | ||
console.log("Max. number of IP addresses cannot be more than 50."); | ||
} else { | ||
this.ipAddresses = ipAddresses; | ||
} | ||
setIncludeHostnameFallbackLive(hostnameFallbackLive: boolean = false): void { | ||
this.includeHostnameFallbackLive = hostnameFallbackLive; | ||
} | ||
getIPAddresses(): string[] { | ||
return this.ipAddresses; | ||
isIncludeHostnameFallbackLive(): boolean { | ||
return this.includeHostnameFallbackLive; | ||
} | ||
setIncludeLiveHostname(liveHostname: boolean = false): void { | ||
this.includeLiveHostname = liveHostname; | ||
} | ||
isIncludeLiveHostname(): boolean { | ||
return this.includeLiveHostname; | ||
} | ||
setIncludeSecurity(security: boolean = false): void { | ||
this.includeSecurity = security; | ||
} | ||
isIncludeSecurity(): boolean { | ||
return this.includeSecurity; | ||
} | ||
setIncludeUserAgent(userAgent: boolean = false): void { | ||
this.includeUserAgent = userAgent; | ||
} | ||
isIncludeUserAgent(): boolean { | ||
return this.includeUserAgent; | ||
} | ||
} |
import { GeolocationParams } from './GeolocationParams'; | ||
import { TimezoneParams } from './TimezoneParams'; | ||
import { XMLHttpRequest } from 'xmlhttprequest'; | ||
export class IPGeolocationAPI { | ||
apiKey: string; | ||
@@ -14,12 +14,4 @@ async: boolean; | ||
public getApiKey() { | ||
return this.apiKey; | ||
} | ||
public isAsync() { | ||
return this.async; | ||
} | ||
public getGeolocation(callback, geolocationParams: GeolocationParams = null): void { | ||
if(geolocationParams && geolocationParams.getIPAddresses().length === 0) { | ||
public getGeolocation(callback: (json: any) => any, geolocationParams: GeolocationParams = null): void { | ||
if (geolocationParams && geolocationParams.getIPAddresses().length === 0) { | ||
this.getRequest("ipgeo", this.buildGeolocationUrlParams(this.apiKey, geolocationParams), callback); | ||
@@ -35,44 +27,111 @@ } else { | ||
public getTimezone(callback: (json: any) => any, timezoneParams: TimezoneParams = null): void { | ||
this.getRequest("timezone", this.buildTimezoneUrlParams(this.apiKey, timezoneParams), callback); | ||
} | ||
public getUserAgent(callback: (json: any) => any, uaString: string = null): void { | ||
var jsonData: string = JSON.stringify({ | ||
"uaString": uaString | ||
}); | ||
this.postRequest('user-agent', "apiKey=" + this.apiKey, jsonData, callback); | ||
} | ||
public getBulkUserAgent(callback: (json: any) => any, uaStrings = []): void { | ||
var jsonData: string = JSON.stringify({ | ||
"uaStrings": uaStrings | ||
}); | ||
this.postRequest('user-agent-bulk', "apiKey=" + this.apiKey, jsonData, callback); | ||
} | ||
public getApiKey() { | ||
return this.apiKey; | ||
} | ||
public isAsync() { | ||
return this.async; | ||
} | ||
private buildGeolocationUrlParams(apiKey: string = null, geolocationParams: GeolocationParams = null): string { | ||
let urlParams = ""; | ||
if(apiKey) { | ||
if (apiKey) { | ||
urlParams = urlParams.concat("apiKey=", apiKey) | ||
} | ||
if(geolocationParams) { | ||
if(geolocationParams.getIPAddress()) { | ||
if(urlParams) { | ||
if (geolocationParams) { | ||
if (geolocationParams.getIPAddress()) { | ||
if (urlParams) { | ||
urlParams = urlParams.concat("&"); | ||
} | ||
urlParams = urlParams.concat("ip=", geolocationParams.getIPAddress()); | ||
} | ||
if(geolocationParams.getFields()) { | ||
if(urlParams) { | ||
if (geolocationParams.getFields()) { | ||
if (urlParams) { | ||
urlParams = urlParams.concat("&"); | ||
} | ||
urlParams = urlParams.concat("fields=", geolocationParams.getFields()); | ||
} | ||
if(geolocationParams.getExcludes()) { | ||
if(urlParams) { | ||
if (geolocationParams.getExcludes()) { | ||
if (urlParams) { | ||
urlParams = urlParams.concat("&"); | ||
} | ||
urlParams = urlParams.concat("excludes=", geolocationParams.getExcludes()); | ||
} | ||
if(geolocationParams.getLang()) { | ||
if(urlParams) { | ||
if (geolocationParams.getLang()) { | ||
if (urlParams) { | ||
urlParams = urlParams.concat("&"); | ||
} | ||
urlParams = urlParams.concat("lang=", geolocationParams.getLang()); | ||
} | ||
if (geolocationParams.isIncludeHostname() || geolocationParams.isIncludeHostnameFallbackLive() || geolocationParams.isIncludeLiveHostname() || geolocationParams.isIncludeSecurity() || geolocationParams.isIncludeUserAgent()) { | ||
var val: string = ""; | ||
var includeHost = false; | ||
if (geolocationParams.isIncludeHostname()) { | ||
val = "hostname"; | ||
includeHost = true; | ||
} else if (geolocationParams.isIncludeHostnameFallbackLive()) { | ||
val = "hostnameFallbackLive"; | ||
includeHost = true; | ||
} else if (geolocationParams.isIncludeLiveHostname()) { | ||
val = "liveHostname"; | ||
includeHost = true; | ||
} | ||
if (geolocationParams.isIncludeSecurity()) { | ||
if (includeHost) { | ||
val = val + ",security"; | ||
} else { | ||
val = "security"; | ||
} | ||
} | ||
if (geolocationParams.isIncludeUserAgent()) { | ||
if (includeHost || geolocationParams.isIncludeSecurity()) { | ||
val = val + ",useragent"; | ||
} else { | ||
val = "useragent"; | ||
} | ||
} | ||
if (urlParams) { | ||
urlParams = urlParams.concat('&'); | ||
} | ||
urlParams = urlParams.concat('include=', val); | ||
} | ||
} | ||
return urlParams; | ||
} | ||
public getTimezone(callback, timezoneParams: TimezoneParams = null): void { | ||
this.getRequest("timezone", this.buildTimezoneUrlParams(this.apiKey, timezoneParams), callback); | ||
} | ||
@@ -82,39 +141,52 @@ private buildTimezoneUrlParams(apiKey: string = null, timezoneParams: TimezoneParams = null) { | ||
if(apiKey) { | ||
if (apiKey) { | ||
urlParams = urlParams.concat("apiKey=", apiKey); | ||
} | ||
if(timezoneParams) { | ||
if(timezoneParams.getIPAddress()) { | ||
if(urlParams) { | ||
if (timezoneParams) { | ||
if (timezoneParams.getIPAddress()) { | ||
if (urlParams) { | ||
urlParams = urlParams.concat("&"); | ||
} | ||
urlParams = urlParams.concat("ip=", timezoneParams.getIPAddress()); | ||
} | ||
if(timezoneParams.getTimezone()) { | ||
if(urlParams) { | ||
if (timezoneParams.getTimezone()) { | ||
if (urlParams) { | ||
urlParams = urlParams.concat("&"); | ||
} | ||
urlParams = urlParams.concat("tz=", timezoneParams.getTimezone()); | ||
} | ||
if(timezoneParams.getLatitude() !== "1000.0" && timezoneParams.getLongitude() !== "1000.0") { | ||
if(urlParams) { | ||
if (timezoneParams.getLocation()) { | ||
if (urlParams) { | ||
urlParams = urlParams.concat('&'); | ||
} | ||
urlParams = urlParams.concat('location=', timezoneParams.getLocation()); | ||
} | ||
if (timezoneParams.getLatitude() >= -90 && timezoneParams.getLatitude() <= 90 && timezoneParams.getLongitude() >= -180 && timezoneParams.getLongitude() <= 180) { | ||
if (urlParams) { | ||
urlParams = urlParams.concat("&"); | ||
} | ||
urlParams = urlParams.concat("lat=", timezoneParams.getLatitude(), "&long=", timezoneParams.getLongitude()); | ||
urlParams = urlParams.concat("lat=" + timezoneParams.getLatitude() + "&long=" + timezoneParams.getLongitude()); | ||
} | ||
if(timezoneParams.getLang()) { | ||
if(urlParams) { | ||
if (timezoneParams.getLang()) { | ||
if (urlParams) { | ||
urlParams = urlParams.concat("&"); | ||
} | ||
urlParams = urlParams.concat("lang=", timezoneParams.getLang()); | ||
} | ||
} | ||
return urlParams; | ||
} | ||
private getRequest(subUrl: string, urlParams: string = "", callback): void { | ||
private getRequest(subUrl: string, urlParams: string = "", callback: (json: any) => any): void { | ||
let jsonData = {}; | ||
@@ -125,3 +197,3 @@ let xhr = new XMLHttpRequest(); | ||
if (this.readyState === 4) { | ||
if (this.status == 0){ | ||
if (this.status == 0) { | ||
jsonData = { | ||
@@ -134,3 +206,3 @@ "message": "Internet is not connected!" | ||
if (callback && typeof(callback) === typeof(Function)) { | ||
if (callback) { | ||
callback(jsonData); | ||
@@ -149,4 +221,4 @@ } else { | ||
private postRequest(subUrl: string, urlParams: string = "", requestData: {} = {}, callback): void { | ||
let jsonData = {}; | ||
private postRequest(subUrl: string, urlParams: string = "", requestData: {} = {}, callback: (json: any) => any): void { | ||
let jsonData = {}; | ||
let xhr = new XMLHttpRequest(); | ||
@@ -156,3 +228,3 @@ | ||
if (this.readyState === 4) { | ||
if (this.status == 0){ | ||
if (this.status == 0) { | ||
jsonData = { | ||
@@ -162,8 +234,8 @@ "message": "Internet is not connected!" | ||
} else { | ||
jsonData = JSON.parse(this.responseText); | ||
jsonData = JSON.parse(this.responseText); | ||
} | ||
if (callback && typeof(callback) === typeof(Function)) { | ||
callback(jsonData); | ||
} else { | ||
if (callback) { | ||
callback(jsonData); | ||
} else { | ||
console.error(`Passed callback '${callback}' is not a valid Function.`) | ||
@@ -173,3 +245,3 @@ } | ||
}); | ||
xhr.withCredentials = true; | ||
@@ -179,3 +251,3 @@ xhr.open("POST", "https://api.ipgeolocation.io/".concat(subUrl, "?", urlParams, ""), this.async, null, null); | ||
xhr.setRequestHeader("Content-Type", "application/json"); | ||
xhr.send(requestData); | ||
xhr.send(requestData); | ||
} | ||
@@ -182,0 +254,0 @@ } |
{ | ||
"_from": "ip-geolocation-api-sdk-typescript", | ||
"_inBundle": false, | ||
"_integrity": "sha512-3H57yHooCwH3glQLZskv8fw1PYFB9+95PZzsFO89acrzIeGcUsbUzd91f4uM8OJIODPXYP+UQEBajjIJWIOWeg==", | ||
"_location": "/ip-geolocation-api-sdk-typescript", | ||
"_phantomChildren": {}, | ||
"_requested": { | ||
"type": "tag", | ||
"registry": true, | ||
"raw": "ip-geolocation-api-sdk-typescript", | ||
"name": "ip-geolocation-api-sdk-typescript", | ||
"escapedName": "ip-geolocation-api-sdk-typescript", | ||
"rawSpec": "", | ||
"saveSpec": null, | ||
"fetchSpec": "latest" | ||
}, | ||
"_requiredBy": [ | ||
"#USER", | ||
"/" | ||
], | ||
"_resolved": "https://registry.npmjs.org/ip-geolocation-api-sdk-typescript/-/ip-geolocation-api-sdk-typescript-1.0.5.tgz", | ||
"_shasum": "247858c28360a16ba09569b2e8501385c9d05356", | ||
"_spec": "ip-geolocation-api-sdk-typescript", | ||
"_where": "/home/developer/test-ts2", | ||
"author": "", | ||
@@ -29,3 +6,2 @@ "bugs": { | ||
}, | ||
"bundleDependencies": false, | ||
"deprecated": false, | ||
@@ -56,3 +32,3 @@ "description": "[Typescript SDK for IP Geolocation API](https://ipgeolocation.io/documentation/ip-geolocation-api-typescript-sdk-201809051239). You can use this SDK to Geolocate an IP address and get time zone information based on geolocation coordinates, or an IP address.", | ||
}, | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"dependencies": { | ||
@@ -59,0 +35,0 @@ "xmlhttprequest": "^1.8.0" |
@@ -34,2 +34,5 @@ # IP Geolocation API Typescript SDK | ||
``` | ||
## Documentation | ||
Use the following URL to visit documentation | ||
[https://ipgeolocation.io/documentation.html](https://ipgeolocation.io/documentation.html) | ||
@@ -128,2 +131,14 @@ ## Basic Usage | ||
``` | ||
### UserAgent API | ||
```ts | ||
// Get user agent information for single user agent string | ||
ipgeolocationApi.getUserAgent(handleResponse, "AppleTV6,2/11.1"); | ||
// Get user agents information in bulk by providing array of user agent strings | ||
var uaStrings:string[] = ["AppleTV6,2/11.1", "Roku4640X/DVP-7.70 (297.70E04154A)", "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:15.0) Gecko/20100101 Firefox/15.0.1"]; | ||
ipgeolocationApi.getBulkUserAgent(handleResponse, uaStrings); | ||
``` | ||
** IPGeolocation provides geolocation information in the following languages: | ||
@@ -146,2 +161,2 @@ | ||
1. tsc *.ts | ||
2. node *.js | ||
2. node Main.js |
export class TimezoneParams { | ||
ipAddress: string; | ||
timezone: string; | ||
latitude: string; | ||
longitude: string; | ||
ipAddress: string; | ||
lang: string; | ||
location: string; | ||
latitude: number; | ||
longitude: number; | ||
constructor() { | ||
this.ipAddress = ""; | ||
this.timezone = ""; | ||
this.latitude = "1000.0"; | ||
this.longitude = "1000.0"; | ||
this.ipAddress = ""; | ||
this.lang = "en"; | ||
this.location = ""; | ||
this.latitude = 1000.0; | ||
this.longitude = 1000.0; | ||
} | ||
setTimezone(timezone: string = ""): void { | ||
setIPAddress(ipAddress: string = ""): void { | ||
this.ipAddress = ipAddress; | ||
} | ||
getIPAddress(): string { | ||
return this.ipAddress; | ||
} | ||
setTimezone(timezone: string = ""): void { | ||
this.timezone = timezone; | ||
@@ -25,28 +34,32 @@ } | ||
setLocation(latitude: string = "1000.0", longitude: string = "1000.0"): void { | ||
this.latitude = latitude; | ||
this.longitude = longitude; | ||
setLang(lang: string = "en"): void { | ||
this.lang = lang; | ||
} | ||
setIPAddress(ipAddress: string = ""): void { | ||
this.ipAddress = ipAddress; | ||
getLang(): string { | ||
return this.lang; | ||
} | ||
getIPAddress(): string { | ||
return this.ipAddress; | ||
setLocation(location: string = ""): void { | ||
this.location = location; | ||
} | ||
setLang(lang: string = "en"): void { | ||
this.lang = lang; | ||
getLocation(): string { | ||
return this.location; | ||
} | ||
getLang(): string { | ||
return this.lang; | ||
setCoordinates(la: number = 1000.0, lo: number = 1000.0): void { | ||
if ((la >= -90 && la <= 90) && (lo >= -180 && lo <= 180)) { | ||
this.latitude = la; | ||
this.longitude = lo; | ||
} else { | ||
throw new Error("Provided latitude and longitude values are out of bound."); | ||
} | ||
} | ||
getLatitude(): string { | ||
getLatitude(): number { | ||
return this.latitude; | ||
} | ||
getLongitude(): string { | ||
getLongitude(): number { | ||
return this.longitude; | ||
@@ -53,0 +66,0 @@ } |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
19825
24.62%337
43.4%160
10.34%0
-100%