ip-geolocation-api-sdk-typescript
Advanced tools
Comparing version
export class GeolocationParams { | ||
ip: string; | ||
fields: string; | ||
ips: any; | ||
ipAddress: string; | ||
fields: string; | ||
excludes: string; | ||
lang: string; | ||
ipAddresses: string[]; | ||
constructor() { | ||
this.ip = ""; | ||
this.fields = ""; | ||
this.ips = ""; | ||
this.ipAddress = ""; | ||
this.fields = ""; | ||
this.excludes = ""; | ||
this.lang = "en"; | ||
this.ipAddresses = []; | ||
} | ||
setIP(ip = "") { | ||
this.ip = ip; | ||
setIPAddress(ipAddress: string = ""): void { | ||
this.ipAddress = ipAddress; | ||
} | ||
getIP() { | ||
return this.ip; | ||
getIPAddress(): string { | ||
return this.ipAddress; | ||
} | ||
setFields(fields = "") { | ||
setFields(fields: string = ""): void { | ||
this.fields = fields; | ||
} | ||
getFields() { | ||
getFields(): string { | ||
return this.fields; | ||
} | ||
setIPList(ips : any = null) { | ||
if(ips.length > 50) { | ||
setExcludes(excludes: string = ""): void { | ||
this.excludes = excludes; | ||
} | ||
getExcludes(): string { | ||
return this.excludes; | ||
} | ||
setLang(lang: string = "en"): void { | ||
this.lang = lang; | ||
} | ||
getLang(): string { | ||
return this.lang; | ||
} | ||
setIPAddresses(ipAddresses : string[] = []): void { | ||
if(ipAddresses.length > 50) { | ||
console.log("Max. number of IP addresses cannot be more than 50."); | ||
} else { | ||
this.ips = ips; | ||
this.ipAddresses = ipAddresses; | ||
} | ||
} | ||
getIPList() { | ||
return this.ips; | ||
getIPAddresses(): string[] { | ||
return this.ipAddresses; | ||
} | ||
} |
@@ -1,10 +0,12 @@ | ||
import {GeolocationParams} from './GeolocationParams'; | ||
import {TimezoneParams} from './TimezoneParams'; | ||
import {XMLHttpRequest} from 'xmlhttprequest'; | ||
import { GeolocationParams } from './GeolocationParams'; | ||
import { TimezoneParams } from './TimezoneParams'; | ||
import { XMLHttpRequest } from 'xmlhttprequest'; | ||
export class IPGeolocationAPI { | ||
apiKey: string; | ||
async: boolean; | ||
constructor(apiKey: string = null) { | ||
constructor(apiKey: string = null, async: boolean = true) { | ||
this.apiKey = apiKey; | ||
this.async = async; | ||
} | ||
@@ -16,120 +18,156 @@ | ||
public getGeolocation(params : GeolocationParams = null, callback) { | ||
if(params && params.getIPList()) | ||
{ return this.postRequest("ipgeo-bulk", params, this.apiKey, callback); | ||
}else { | ||
return this.getRequest("ipgeo", this.buildGeolocationUrlParams(params, this.apiKey), callback); | ||
} | ||
public isAsync() { | ||
return this.async; | ||
} | ||
public getTimezone(params : TimezoneParams = null, callback) { | ||
return this.getRequest("timezone", this.buildTimezoneUrlParams(params, this.apiKey), callback); | ||
} | ||
private buildTimezoneUrlParams(params=null, apiKey="") { | ||
var urlParams = "apiKey=" + apiKey; | ||
public getGeolocation(callback, geolocationParams: GeolocationParams = null): void { | ||
if(geolocationParams && geolocationParams.getIPAddresses().length === 0) { | ||
this.getRequest("ipgeo", this.buildGeolocationUrlParams(this.apiKey, geolocationParams), callback); | ||
} else { | ||
const jsonData = JSON.stringify({ | ||
"ips": geolocationParams.getIPAddresses() | ||
}); | ||
if(params != null) { | ||
var param = params.getIP(); | ||
if(param && param != "") { | ||
urlParams = urlParams + "&ip=" + param; | ||
this.postRequest("ipgeo-bulk", this.buildGeolocationUrlParams(this.apiKey, geolocationParams), jsonData, callback); | ||
} | ||
} | ||
param = params.getTimezone(); | ||
if(param && param != "") { | ||
urlParams = urlParams + "&tz=" + param; | ||
private buildGeolocationUrlParams(apiKey: string = null, geolocationParams: GeolocationParams = null): string { | ||
let urlParams = ""; | ||
if(apiKey) { | ||
urlParams = urlParams.concat("apiKey=", apiKey) | ||
} | ||
var latitude = params.getLatitude(); | ||
var longitude = params.getLongitude(); | ||
if(latitude && latitude != 1000.0 && longitude && longitude != 1000.0) { | ||
urlParams = urlParams + "&lat=" + latitude + "&long=" + longitude; | ||
if(geolocationParams) { | ||
if(geolocationParams.getIPAddress()) { | ||
if(urlParams) { | ||
urlParams = urlParams.concat("&"); | ||
} | ||
urlParams = urlParams.concat("ip=", geolocationParams.getIPAddress()); | ||
} | ||
if(geolocationParams.getFields()) { | ||
if(urlParams) { | ||
urlParams = urlParams.concat("&"); | ||
} | ||
urlParams = urlParams.concat("fields=", geolocationParams.getFields()); | ||
} | ||
if(geolocationParams.getExcludes()) { | ||
if(urlParams) { | ||
urlParams = urlParams.concat("&"); | ||
} | ||
urlParams = urlParams.concat("excludes=", geolocationParams.getExcludes()); | ||
} | ||
if(geolocationParams.getLang()) { | ||
if(urlParams) { | ||
urlParams = urlParams.concat("&"); | ||
} | ||
urlParams = urlParams.concat("lang=", geolocationParams.getLang()); | ||
} | ||
} | ||
return urlParams; | ||
} | ||
return urlParams; | ||
public getTimezone(callback, timezoneParams: TimezoneParams = null): void { | ||
this.getRequest("timezone", this.buildTimezoneUrlParams(this.apiKey, timezoneParams), callback); | ||
} | ||
private buildGeolocationUrlParams(params=null, apiKey="") { | ||
var urlParams = "apiKey=" + apiKey; | ||
if(params != null) { | ||
var param = params.getIP(); | ||
if(param && param != "") { | ||
urlParams = urlParams + "&ip=" + param; | ||
private buildTimezoneUrlParams(apiKey: string = null, timezoneParams: TimezoneParams = null) { | ||
let urlParams = ""; | ||
if(apiKey) { | ||
urlParams = urlParams.concat("apiKey=", apiKey); | ||
} | ||
if(timezoneParams) { | ||
if(timezoneParams.getIPAddress()) { | ||
if(urlParams) { | ||
urlParams = urlParams.concat("&"); | ||
} | ||
urlParams = urlParams.concat("ip=", timezoneParams.getIPAddress()); | ||
} | ||
param = params.getFields(); | ||
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) { | ||
urlParams = urlParams.concat("&"); | ||
} | ||
urlParams = urlParams.concat("lat=", timezoneParams.getLatitude(), "&long=", timezoneParams.getLongitude()); | ||
} | ||
if(param && param != "") { | ||
urlParams = urlParams + "&fields=" + param; | ||
if(timezoneParams.getLang()) { | ||
if(urlParams) { | ||
urlParams = urlParams.concat("&"); | ||
} | ||
urlParams = urlParams.concat("lang=", timezoneParams.getLang()); | ||
} | ||
} | ||
return urlParams; | ||
} | ||
private getRequest(subUrl = "", params = "", callback){ | ||
private getRequest(subUrl: string, urlParams: string = "", callback): void { | ||
let jsonData = {}; | ||
let xhr = new XMLHttpRequest(); | ||
var jsonData = null; | ||
var data = null; | ||
var xhr = new XMLHttpRequest(); | ||
xhr.withCredentials = true; | ||
xhr.addEventListener("readystatechange", function () { | ||
if (this.readyState === 4) { | ||
xhr.addEventListener("readystatechange", function () { | ||
if (this.readyState === 4) { | ||
if (this.status == 0){ | ||
jsonData = { | ||
"message": "Internet is not connected!" | ||
}; | ||
} else { | ||
jsonData = JSON.parse(this.responseText); | ||
} | ||
if(this.status == 0){ | ||
jsonData = { | ||
"message": "Internet is not connected!" | ||
}; | ||
}else{ | ||
jsonData = JSON.parse(this.responseText); | ||
} | ||
if (callback && typeof(callback) === typeof(Function)) { | ||
callback(jsonData); | ||
} else { | ||
console.error(`Passed callback '${callback}' is not a valid Function.`) | ||
} | ||
} | ||
}); | ||
if (callback && typeof(callback) === typeof(Function)) { | ||
callback(jsonData); | ||
} | ||
xhr.withCredentials = true; | ||
xhr.open("GET", "https://api.ipgeolocation.io/".concat(subUrl, "?", urlParams, ""), this.async, null, null); | ||
xhr.setRequestHeader("Accept", "application/json"); | ||
xhr.send(null); | ||
} | ||
}); | ||
xhr.open("GET", "https://api.ipgeolocation.io/"+subUrl+"?"+params+"", true); | ||
xhr.send(data); | ||
return jsonData; | ||
} | ||
private postRequest(subUrl = "", params : GeolocationParams = null, apiKey="", callback){ | ||
private postRequest(subUrl: string, urlParams: string = "", requestData: {} = {}, callback): void { | ||
let jsonData = {}; | ||
let xhr = new XMLHttpRequest(); | ||
var jsonData = null; | ||
var data = JSON.stringify({ | ||
"ips": params.getIPList() | ||
}); | ||
var xhr = new XMLHttpRequest(); | ||
xhr.withCredentials = true; | ||
xhr.addEventListener("readystatechange", function () { | ||
if (this.readyState === 4) { | ||
if(this.status == 0){ | ||
if (this.status == 0){ | ||
jsonData = { | ||
"message": "Internet is not connected!" | ||
"message": "Internet is not connected!" | ||
}; | ||
}else{ | ||
} else { | ||
jsonData = JSON.parse(this.responseText); | ||
} | ||
if (callback && typeof(callback) === typeof(Function)) { | ||
callback(jsonData); | ||
} | ||
if (callback && typeof(callback) === typeof(Function)) { | ||
callback(jsonData); | ||
} else { | ||
console.error(`Passed callback '${callback}' is not a valid Function.`) | ||
} | ||
} | ||
}); | ||
xhr.open("POST", "https://api.ipgeolocation.io/"+subUrl+"?apiKey="+apiKey+"", true); | ||
xhr.withCredentials = true; | ||
xhr.open("POST", "https://api.ipgeolocation.io/".concat(subUrl, "?", urlParams, ""), this.async, null, null); | ||
xhr.setRequestHeader("Accept", "application/json"); | ||
xhr.setRequestHeader("Content-Type", "application/json"); | ||
xhr.send(data); | ||
return jsonData; | ||
xhr.send(requestData); | ||
} | ||
} | ||
@@ -136,0 +174,0 @@ |
{ | ||
"_from": "ip-geolocation-api-sdk-typescript", | ||
"_id": "ip-geolocation-api-sdk-typescript@1.0.4", | ||
"_inBundle": false, | ||
"_integrity": "sha512-7u4ECCg5vxxYghaf4eCmyE9M0R5B+xL/VzjRRtPNCt/aYtxtQQQc+OlOlz35Ep8Hih7mUUo10Gjfv6gpT5lvjg==", | ||
"_integrity": "sha512-3H57yHooCwH3glQLZskv8fw1PYFB9+95PZzsFO89acrzIeGcUsbUzd91f4uM8OJIODPXYP+UQEBajjIJWIOWeg==", | ||
"_location": "/ip-geolocation-api-sdk-typescript", | ||
@@ -22,6 +21,6 @@ "_phantomChildren": {}, | ||
], | ||
"_resolved": "https://registry.npmjs.org/ip-geolocation-api-sdk-typescript/-/ip-geolocation-api-sdk-typescript-1.0.4.tgz", | ||
"_shasum": "92626ab85adb5f11188ad24643744863c5830170", | ||
"_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-ts", | ||
"_where": "/home/developer/test-ts2", | ||
"author": "", | ||
@@ -32,7 +31,4 @@ "bugs": { | ||
"bundleDependencies": false, | ||
"dependencies": { | ||
"xmlhttprequest": "^1.8.0" | ||
}, | ||
"deprecated": false, | ||
"description": "## Installation ```cli npm i ip-geolocation-api-sdk-typescript ```", | ||
"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.", | ||
"homepage": "https://gitlab.com/ahsanNawaz111/ipGeoLocation-TypeScript#README", | ||
@@ -61,3 +57,7 @@ "keywords": [ | ||
}, | ||
"version": "1.0.4" | ||
"version": "1.0.5", | ||
"dependencies": { | ||
"xmlhttprequest": "^1.8.0" | ||
}, | ||
"devDependencies": {} | ||
} |
163
README.md
@@ -1,77 +0,144 @@ | ||
# IPGeolocation API Typescript SDK | ||
# IP Geolocation API Typescript SDK | ||
## Introduction | ||
[IPGeolocation API](https://ipgeolocation.io) is the solution to identify country code (ISO2 and ISO3 standard), country name, continent code, continent name, country capital, state/province, district, city, zip code, latitude and longitude of city, is country belongs to Europian Union, calling code, top level domain (TLD), languages, country flag, internet service provider (ISP), connection type, organization, geoname ID, currency code, currency name, time zone ID, time zone offset, current time in the time zone, is time zone in daylight saving time, and total daylight savings. This document provides important information to help you get up to speed with IPGeolocation API using IP Geolocation API Typescript SDK. | ||
Developers can use this Typescript SDK for software and web projects related to, but not limited to: | ||
1. Display native language and currency | ||
2. Redirect based on the country | ||
3. Digital rights management | ||
4. Web log stats and analysis | ||
5. Auto-selection of country, state/province and city on forms | ||
6. Filter access from countries you do not do business with | ||
7. Geo-targeting for increased sales and click-through | ||
## Quick Start Guide | ||
You need a valid 'IPGeolocation API key' to use this SDK. [Sign up](https://ipgeolocation.io/signup) here and get your free API key if you don't have one. | ||
**Note:** Complete documentation to use this SDK is also available at [IP Geolocation API Typescript SDK Documentation](https://ipgeolocation.io/documentation/ip-geolocation-api-typescript-sdk-201809051239). | ||
## System Requirements | ||
Internet connection is required to run this component. | ||
## Installation | ||
### NPM | ||
```cli | ||
npm i ip-geolocation-api-sdk-typescript | ||
$ npm install ip-geolocation-api-sdk-typescript | ||
``` | ||
## Usage | ||
```ts | ||
import {IPGeolocationAPI} from './node_modules/ip-geolocation-api-sdk-typescript/IPGeolocationAPI'; | ||
import {GeolocationParams} from './node_modules/ip-geolocation-api-sdk-typescript/GeolocationParams'; | ||
import {TimezoneParams} from './node_modules/ip-geolocation-api-sdk-typescript/TimezoneParams'; | ||
``` | ||
## Basic Usage | ||
### Setup API | ||
```ts | ||
var api = new IPGeolocationAPI("YOUR_API_KEY"); | ||
or | ||
var api = new IPGeolocationAPI(); | ||
import { IPGeolocationAPI } from './node_modules/ip-geolocation-api-sdk-typescript/IPGeolocationAPI'; | ||
// Create IPGeolocationAPI object. Constructor takes two parameters. | ||
// 1) API key (Optional: To authenticate your requests through "Request Origin", you can skip it.) | ||
// 2) Async (Optional: It is used to toggle "async" mode in the requests. By default, it is true.) | ||
let ipgeolocationApi = new IPGeolocationAPI("YOUR_API_KEY", false); | ||
``` | ||
### Geolocation Lookup | ||
```ts | ||
// Query geolocation for IP address (1.1.1.1) and fields (geo, time_zone and currency) | ||
var geolocationParams = new GeolocationParams(); | ||
geolocationParams.setIp("1.1.1.1"); | ||
geolocationParams.setFields("geo,time_zone,currency"); | ||
import { GeolocationParams } from './node_modules/ip-geolocation-api-sdk-typescript/GeolocationParams'; | ||
api.getGeolocation(geolocationParams, geoResponse); | ||
function geoResponse(json) { | ||
// Function to handle API response | ||
function handleResponse(json) { | ||
console.log(json); | ||
} | ||
// Query geolocation for IP address (1.1.1.1) and all fields | ||
GeolocationParams geoParams = new GeolocationParams(); | ||
geoParams.SetIp("1.1.1.1"); | ||
// Get complete geolocation for the calling machine's IP address | ||
ipgeolocationApi.getGeolocation(handleResponse); | ||
api.getGeolocation(geolocationParams, geoResponse); | ||
function geoResponse(json) { | ||
console.log(json); | ||
} | ||
// Get complete geolocation in Russian** for IP address (1.1.1.1) | ||
let geolocationParams = new GeolocationParams(); | ||
geolocationParams.setIPAddress('1.1.1.1'); | ||
geolocationParams.setLang('ru'); | ||
ipgeolocationApi.getGeolocation(handleResponse, geolocationParams); | ||
// Query geolocation for the calling machine's IP address for all fields | ||
api.getGeolocation(null, geoResponse); | ||
function geoResponse(json) { | ||
console.log(json); | ||
} | ||
// Get custom geolocation (only "geo, time_zone and currency" fields/objects) for an IP address (1.1.1.1) | ||
let geolocationParams = new GeolocationParams(); | ||
geolocationParams.setIPAddress('1.1.1.1'); | ||
geolocationParams.setFields('geo,time_zone,currency'); | ||
ipgeolocationApi.getGeolocation(handleResponse, geolocationParams); | ||
// Exclude fields/obejects from complete geolocation in Italian language | ||
let geolocationParams = new GeolocationParams(); | ||
geolocationParams.setExcludes('continent_name,country_code3,time_zone'); | ||
geolocationParams.setLang('it'); | ||
ipgeolocationApi.getGeolocation(handleResponse, geolocationParams); | ||
``` | ||
### Bulk Geolocations Lookup | ||
```ts | ||
// Query geolocations for multiple IP addresses and all fields | ||
var geolocationParams = new GeolocationParams(); | ||
geolocationParams.setIps(['1.1.1.1','2.2.22.2','34.1.1.3']); | ||
// Query geolocation in German** for multiple IP addresses and all fields | ||
let geolocationParams = new GeolocationParams(); | ||
geolocationParams.setLang('de'); | ||
geolocationParams.setIPAddresses(['1.1.1.1', '2.2.2.2', '3.3.3.3']); | ||
api.getGeolocation(geolocationParams, geoResponse); | ||
function geoResponse(json) { | ||
console.log(json); | ||
} | ||
ipgeolocationApi.getGeolocation(handleResponse, geolocationParams); | ||
// Specify the required fields/objects for multiple IP addresses | ||
let geolocationParams = new GeolocationParams(); | ||
geolocationParams.setIPAddresses(['1.1.1.1', '2.2.2.2', '3.3.3.3']); | ||
geolocationParams.setFields('geo'); | ||
ipgeolocationApi.getGeolocation(geolocationParams, geoResponse); | ||
``` | ||
### Time Zone API | ||
### Timezone API | ||
```ts | ||
// Query time zone information by time zone ID | ||
var tzParams = new TimezoneParams(); | ||
import { TimezoneParams } from './node_modules/ip-geolocation-api-sdk-typescript/TimezoneParams'; | ||
tzParams.setIp("1.1.1.1"); | ||
obj.getTimezone(null, geoResponse); | ||
function geoResponse(json) { | ||
console.log(json); | ||
} | ||
// Get time zone information by time zone ID | ||
let timezoneParams = new TimezoneParams(); | ||
timezoneParams.setTimezone('America/Los_Angeles'); | ||
ipgeolocationApi.getTimezone(handleResponse, timezoneParams); | ||
// Get time zone information by latitude and longitude of the location | ||
let timezoneParams = new TimezoneParams(); | ||
timezoneParams.setLocation('37.1838139', '-123.8105225'); | ||
ipgeolocationApi.getTimezone(handleResponse, timezoneParams); | ||
// Get time zone information for IP address (1.1.1.1) and geolocation information Japanese** | ||
let timezoneParams = new TimezoneParams(); | ||
timezoneParams.setIPAddress('1.1.1.1'); | ||
ipgeolocationApi.getTimezone(handleResponse, timezoneParams); | ||
// Query time zone information for calling machine's IP address | ||
ipgeolocationApi.getTimezone(handleResponse); | ||
``` | ||
## Commands To Run | ||
** IPGeolocation provides geolocation information in the following languages: | ||
* English (en) | ||
* German (de) | ||
* Russian (ru) | ||
* Japanese (ja) | ||
* French (fr) | ||
* Chinese Simplified (cn) | ||
* Spanish (es) | ||
* Czech (cs) | ||
* Italian (it) | ||
By default, geolocation information is returned in English. Response in a language other than English is available to paid users only. | ||
### Commands To Run Typescript Application | ||
1. tsc *.ts | ||
2. node *.js | ||
export class TimezoneParams { | ||
timezone :string; | ||
ip :string; | ||
latitude :number; | ||
longitude :number; | ||
timezone: string; | ||
latitude: string; | ||
longitude: string; | ||
ipAddress: string; | ||
lang: string; | ||
constructor() { | ||
this.timezone = ""; | ||
this.ip = ""; | ||
this.latitude = 1000.0; | ||
this.longitude = 1000.0; | ||
this.latitude = "1000.0"; | ||
this.longitude = "1000.0"; | ||
this.ipAddress = ""; | ||
this.lang = "en"; | ||
} | ||
setTimezone(timezone = "") { | ||
setTimezone(timezone: string = ""): void { | ||
this.timezone = timezone; | ||
} | ||
getTimezone() { | ||
getTimezone(): string { | ||
return this.timezone; | ||
} | ||
setIP(ip = "") { | ||
this.ip = ip; | ||
setLocation(latitude: string = "1000.0", longitude: string = "1000.0"): void { | ||
this.latitude = latitude; | ||
this.longitude = longitude; | ||
} | ||
getIP() { | ||
return this.ip; | ||
setIPAddress(ipAddress: string = ""): void { | ||
this.ipAddress = ipAddress; | ||
} | ||
setLocation(latitude = 0, longitude = 0) { | ||
this.latitude = latitude; | ||
this.longitude = longitude; | ||
getIPAddress(): string { | ||
return this.ipAddress; | ||
} | ||
getLatitude() { | ||
setLang(lang: string = "en"): void { | ||
this.lang = lang; | ||
} | ||
getLang(): string { | ||
return this.lang; | ||
} | ||
getLatitude(): string { | ||
return this.latitude; | ||
} | ||
getLongitude() { | ||
getLongitude(): string { | ||
return this.longitude; | ||
} | ||
} | ||
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
15908
77.23%235
38.24%145
85.9%1
Infinity%