From 65ca773f138d4bb534df0834902a8200462e093c Mon Sep 17 00:00:00 2001 From: Sergey Shpak Date: Thu, 13 Jun 2019 11:25:00 +0300 Subject: [PATCH 1/6] * Added tslint * Added prettier * Fixed code style * Fixed type validation errors --- GeolocationParams.ts | 118 ++++++------- IPGeolocationAPI.ts | 385 ++++++++++++++++++++++++------------------- TimezoneParams.ts | 105 ++++++------ package.json | 9 +- tsconfig.json | 63 +++++++ tslint.json | 11 ++ 6 files changed, 410 insertions(+), 281 deletions(-) create mode 100644 tsconfig.json create mode 100644 tslint.json diff --git a/GeolocationParams.ts b/GeolocationParams.ts index 6b466c9..7a5effc 100644 --- a/GeolocationParams.ts +++ b/GeolocationParams.ts @@ -1,60 +1,60 @@ -export class GeolocationParams { - - ipAddress: string; - fields: string; - excludes: string; - lang: string; - ipAddresses: string[]; - - constructor() { - this.ipAddress = ""; - this.fields = ""; - this.excludes = ""; - this.lang = "en"; - this.ipAddresses = []; - } - - setIPAddress(ipAddress: string = ""): void { - this.ipAddress = ipAddress; - } - - getIPAddress(): string { - return this.ipAddress; - } - - setFields(fields: string = ""): void { - this.fields = fields; - } - - getFields(): string { - return this.fields; - } - - 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.ipAddresses = ipAddresses; - } - } - - getIPAddresses(): string[] { - return this.ipAddresses; - } +export class GeolocationParams { + public ipAddress: string; + public fields: string; + public excludes: string; + public lang: string; + public ipAddresses: string[]; + + constructor() { + this.ipAddress = ""; + this.fields = ""; + this.excludes = ""; + this.lang = "en"; + this.ipAddresses = []; + } + + public setIPAddress(ipAddress: string = ""): void { + this.ipAddress = ipAddress; + } + + public getIPAddress(): string { + return this.ipAddress; + } + + public setFields(fields: string = ""): void { + this.fields = fields; + } + + public getFields(): string { + return this.fields; + } + + public setExcludes(excludes: string = ""): void { + this.excludes = excludes; + } + + public getExcludes(): string { + return this.excludes; + } + + public setLang(lang: string = "en"): void { + this.lang = lang; + } + + public getLang(): string { + return this.lang; + } + + public setIPAddresses(ipAddresses: string[] = []): void { + if (ipAddresses.length > 50) { + // tslint:disable-next-line:no-console + console.log("Max. number of IP addresses cannot be more than 50."); + } else { + this.ipAddresses = ipAddresses; + } + } + + public getIPAddresses(): string[] { + return this.ipAddresses; + } } diff --git a/IPGeolocationAPI.ts b/IPGeolocationAPI.ts index d593705..abf8f56 100644 --- a/IPGeolocationAPI.ts +++ b/IPGeolocationAPI.ts @@ -1,187 +1,236 @@ -import { GeolocationParams } from './GeolocationParams'; -import { TimezoneParams } from './TimezoneParams'; -import { XMLHttpRequest } from 'xmlhttprequest'; +import { XMLHttpRequest } from "xmlhttprequest"; +import { GeolocationParams } from "./GeolocationParams"; +import { TimezoneParams } from "./TimezoneParams"; export class IPGeolocationAPI { - - apiKey: string; - async: boolean; - - constructor(apiKey: string = null, async: boolean = true) { - this.apiKey = apiKey; - this.async = async; + private static buildGeolocationUrlParams( + apiKey: string = null, + geolocationParams: GeolocationParams | null = null + ): string { + let urlParams = ""; + + if (apiKey) { + urlParams = urlParams.concat("apiKey=", apiKey); } - public getApiKey() { - return this.apiKey; - } - - public isAsync() { - return this.async; - } + if (geolocationParams) { + if (geolocationParams.getIPAddress()) { + if (urlParams) { + urlParams = urlParams.concat("&"); + } + urlParams = urlParams.concat("ip=", geolocationParams.getIPAddress()); + } - 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 (geolocationParams.getFields()) { + if (urlParams) { + urlParams = urlParams.concat("&"); + } + urlParams = urlParams.concat("fields=", geolocationParams.getFields()); + } - this.postRequest("ipgeo-bulk", this.buildGeolocationUrlParams(this.apiKey, geolocationParams), jsonData, callback); + 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; + } + + private static buildTimezoneUrlParams( + apiKey: string = null, + timezoneParams: TimezoneParams | null = null + ) { + let urlParams = ""; - private buildGeolocationUrlParams(apiKey: string = null, geolocationParams: GeolocationParams = null): string { - let urlParams = ""; + if (apiKey) { + urlParams = urlParams.concat("apiKey=", apiKey); + } - if(apiKey) { - urlParams = urlParams.concat("apiKey=", apiKey) + if (timezoneParams) { + if (timezoneParams.getIPAddress()) { + if (urlParams) { + urlParams = urlParams.concat("&"); } + urlParams = urlParams.concat("ip=", timezoneParams.getIPAddress()); + } - 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()); - } + 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 (timezoneParams.getLang()) { + if (urlParams) { + urlParams = urlParams.concat("&"); } - return urlParams; + urlParams = urlParams.concat("lang=", timezoneParams.getLang()); + } } - - public getTimezone(callback, timezoneParams: TimezoneParams = null): void { - this.getRequest("timezone", this.buildTimezoneUrlParams(this.apiKey, timezoneParams), callback); + return urlParams; + } + + public apiKey: string; + public async: boolean; + + constructor(apiKey: string = null, async: boolean = true) { + this.apiKey = apiKey; + this.async = async; + } + + public getApiKey() { + return this.apiKey; + } + + public isAsync() { + return this.async; + } + + public getGeolocation( + callback, + geolocationParams: GeolocationParams | null = null + ): void { + if (geolocationParams && geolocationParams.getIPAddresses().length === 0) { + this.getRequest( + "ipgeo", + IPGeolocationAPI.buildGeolocationUrlParams( + this.apiKey, + geolocationParams + ), + callback + ); + } else { + const jsonData = JSON.stringify({ + ips: geolocationParams.getIPAddresses() + }); + + this.postRequest( + "ipgeo-bulk", + IPGeolocationAPI.buildGeolocationUrlParams( + this.apiKey, + geolocationParams + ), + jsonData, + callback + ); } - - private buildTimezoneUrlParams(apiKey: string = null, timezoneParams: TimezoneParams = null) { - let urlParams = ""; - - if(apiKey) { - urlParams = urlParams.concat("apiKey=", apiKey); + } + + public getTimezone( + callback, + timezoneParams: TimezoneParams | null = null + ): void { + this.getRequest( + "timezone", + IPGeolocationAPI.buildTimezoneUrlParams(this.apiKey, timezoneParams), + callback + ); + } + + private getRequest(subUrl: string, urlParams: string = "", callback): void { + let jsonData = {}; + const xhr = new XMLHttpRequest(); + + 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(timezoneParams) { - if(timezoneParams.getIPAddress()) { - if(urlParams) { - urlParams = urlParams.concat("&"); - } - urlParams = urlParams.concat("ip=", timezoneParams.getIPAddress()); - } - - 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(timezoneParams.getLang()) { - if(urlParams) { - urlParams = urlParams.concat("&"); - } - urlParams = urlParams.concat("lang=", timezoneParams.getLang()); - } + if (callback && typeof callback === typeof Function) { + callback(jsonData); + } else { + // tslint:disable-next-line:no-console + console.error( + `Passed callback '${callback}' is not a valid Function.` + ); + } + } + }); + + 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); + } + + private postRequest( + subUrl: string, + urlParams: string = "", + requestData: {} = {}, + callback + ): void { + let jsonData = {}; + const xhr = new XMLHttpRequest(); + + xhr.addEventListener("readystatechange", function() { + if (this.readyState === 4) { + if (this.status === 0) { + jsonData = { + message: "Internet is not connected!" + }; + } else { + jsonData = JSON.parse(this.responseText); } - return urlParams; - } - - private getRequest(subUrl: string, urlParams: string = "", callback): void { - let jsonData = {}; - let xhr = new XMLHttpRequest(); - - 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 (callback && typeof(callback) === typeof(Function)) { - callback(jsonData); - } else { - console.error(`Passed callback '${callback}' is not a valid Function.`) - } - } - }); - - 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); - } - private postRequest(subUrl: string, urlParams: string = "", requestData: {} = {}, callback): void { - let jsonData = {}; - let xhr = new XMLHttpRequest(); - - 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 (callback && typeof(callback) === typeof(Function)) { - callback(jsonData); - } else { - console.error(`Passed callback '${callback}' is not a valid Function.`) - } - } - }); - - 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(requestData); - } + if (callback && typeof callback === typeof Function) { + callback(jsonData); + } else { + // tslint:disable-next-line:no-console + console.error( + `Passed callback '${callback}' is not a valid Function.` + ); + } + } + }); + + 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(requestData); + } } - - - - - - - - - - - - - - - diff --git a/TimezoneParams.ts b/TimezoneParams.ts index 009d1ce..34fd94e 100644 --- a/TimezoneParams.ts +++ b/TimezoneParams.ts @@ -1,54 +1,55 @@ export class TimezoneParams { - - timezone: string; - latitude: string; - longitude: string; - ipAddress: string; - lang: string; - - constructor() { - this.timezone = ""; - this.latitude = "1000.0"; - this.longitude = "1000.0"; - this.ipAddress = ""; - this.lang = "en"; - } - - setTimezone(timezone: string = ""): void { - this.timezone = timezone; - } - - getTimezone(): string { - return this.timezone; - } - - setLocation(latitude: string = "1000.0", longitude: string = "1000.0"): void { - this.latitude = latitude; - this.longitude = longitude; - } - - setIPAddress(ipAddress: string = ""): void { - this.ipAddress = ipAddress; - } - - getIPAddress(): string { - return this.ipAddress; - } - - setLang(lang: string = "en"): void { - this.lang = lang; - } - - getLang(): string { - return this.lang; - } - - getLatitude(): string { - return this.latitude; - } - - getLongitude(): string { - return this.longitude; - } + public timezone: string; + public latitude: string; + public longitude: string; + public ipAddress: string; + public lang: string; + + constructor() { + this.timezone = ""; + this.latitude = "1000.0"; + this.longitude = "1000.0"; + this.ipAddress = ""; + this.lang = "en"; + } + + public setTimezone(timezone: string = ""): void { + this.timezone = timezone; + } + + public getTimezone(): string { + return this.timezone; + } + + public setLocation( + latitude: string = "1000.0", + longitude: string = "1000.0" + ): void { + this.latitude = latitude; + this.longitude = longitude; + } + + public setIPAddress(ipAddress: string = ""): void { + this.ipAddress = ipAddress; + } + + public getIPAddress(): string { + return this.ipAddress; + } + + public setLang(lang: string = "en"): void { + this.lang = lang; + } + + public getLang(): string { + return this.lang; + } + + public getLatitude(): string { + return this.latitude; + } + + public getLongitude(): string { + return this.longitude; + } } - diff --git a/package.json b/package.json index 24a762e..42d9b18 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "bugs": { "url": "https://gitlab.com/ahsanNawaz111/ipGeoLocation-TypeScript/issues" }, - "bundleDependencies": false, + "bundleDependencies": [], "deprecated": false, "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", @@ -56,5 +56,10 @@ "dependencies": { "xmlhttprequest": "^1.8.0" }, - "devDependencies": {} + "devDependencies": { + "prettier": "^1.18.2", + "tslint": "^5.17.0", + "tslint-plugin-prettier": "^2.0.1", + "typescript": "^3.5.1" + } } diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..237b745 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,63 @@ +{ + "compilerOptions": { + /* Basic Options */ + "target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */ + "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */ + // "lib": [], /* Specify library files to be included in the compilation. */ + // "allowJs": true, /* Allow javascript files to be compiled. */ + // "checkJs": true, /* Report errors in .js files. */ + // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */ + // "declaration": true, /* Generates corresponding '.d.ts' file. */ + // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ + // "sourceMap": true, /* Generates corresponding '.map' file. */ + // "outFile": "./", /* Concatenate and emit output to single file. */ + // "outDir": "./", /* Redirect output structure to the directory. */ + "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ + // "composite": true, /* Enable project compilation */ + // "removeComments": true, /* Do not emit comments to output. */ + // "noEmit": true, /* Do not emit outputs. */ + // "importHelpers": true, /* Import emit helpers from 'tslib'. */ + // "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */ + // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */ + + /* Strict Type-Checking Options */ + "strict": true, /* Enable all strict type-checking options. */ + // "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */ + // "strictNullChecks": true, /* Enable strict null checks. */ + // "strictFunctionTypes": true, /* Enable strict checking of function types. */ + // "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */ + // "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */ + // "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */ + // "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */ + + /* Additional Checks */ + // "noUnusedLocals": true, /* Report errors on unused locals. */ + // "noUnusedParameters": true, /* Report errors on unused parameters. */ + // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */ + // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */ + + /* Module Resolution Options */ + // "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ + // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */ + // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ + // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ + // "typeRoots": [], /* List of folders to include type definitions from. */ + // "types": [], /* Type declaration files to be included in compilation. */ + // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */ + "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */ + // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */ + + /* Source Map Options */ + // "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */ + // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ + // "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */ + // "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */ + + /* Experimental Options */ + // "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */ + // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */ + }, + "files": [ + "tslint" + ] +} diff --git a/tslint.json b/tslint.json new file mode 100644 index 0000000..9bf6f9f --- /dev/null +++ b/tslint.json @@ -0,0 +1,11 @@ +{ + "defaultSeverity": "error", + "extends": [ + "tslint-plugin-prettier" + ], + "jsRules": {}, + "rules": { + "prettier": true + }, + "rulesDirectory": [] +} From ba154f1b2581d9e622a097a132995194bcdf3384 Mon Sep 17 00:00:00 2001 From: Sergey Shpak Date: Thu, 13 Jun 2019 11:35:25 +0300 Subject: [PATCH 2/6] * Updated version * Fixed type errors --- IPGeolocationAPI.ts | 6 +++--- package.json | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/IPGeolocationAPI.ts b/IPGeolocationAPI.ts index abf8f56..531bded 100644 --- a/IPGeolocationAPI.ts +++ b/IPGeolocationAPI.ts @@ -3,7 +3,7 @@ import { GeolocationParams } from "./GeolocationParams"; import { TimezoneParams } from "./TimezoneParams"; export class IPGeolocationAPI { private static buildGeolocationUrlParams( - apiKey: string = null, + apiKey: string = "", geolocationParams: GeolocationParams | null = null ): string { let urlParams = ""; @@ -48,7 +48,7 @@ export class IPGeolocationAPI { } private static buildTimezoneUrlParams( - apiKey: string = null, + apiKey: string = "", timezoneParams: TimezoneParams | null = null ) { let urlParams = ""; @@ -100,7 +100,7 @@ export class IPGeolocationAPI { public apiKey: string; public async: boolean; - constructor(apiKey: string = null, async: boolean = true) { + constructor(apiKey: string = "", async: boolean = true) { this.apiKey = apiKey; this.async = async; } diff --git a/package.json b/package.json index 42d9b18..ff96b92 100644 --- a/package.json +++ b/package.json @@ -52,7 +52,7 @@ "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, - "version": "1.0.5", + "version": "1.0.6", "dependencies": { "xmlhttprequest": "^1.8.0" }, From d90258b96dbcce59b4cc40c06418fc8c3f51b5ef Mon Sep 17 00:00:00 2001 From: Sergey Shpak Date: Thu, 13 Jun 2019 11:45:22 +0300 Subject: [PATCH 3/6] * Updated version * Fixed object is possibly 'null' error from lint --- IPGeolocationAPI.ts | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/IPGeolocationAPI.ts b/IPGeolocationAPI.ts index 531bded..0bf6230 100644 --- a/IPGeolocationAPI.ts +++ b/IPGeolocationAPI.ts @@ -128,7 +128,7 @@ export class IPGeolocationAPI { ); } else { const jsonData = JSON.stringify({ - ips: geolocationParams.getIPAddresses() + ips: geolocationParams && geolocationParams.getIPAddresses() }); this.postRequest( diff --git a/package.json b/package.json index ff96b92..2b54d18 100644 --- a/package.json +++ b/package.json @@ -52,7 +52,7 @@ "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, - "version": "1.0.6", + "version": "1.0.7", "dependencies": { "xmlhttprequest": "^1.8.0" }, From 125baf060f7c330456fe5c3953e2b2ecd8881c1b Mon Sep 17 00:00:00 2001 From: Sergey Shpak Date: Tue, 30 Jul 2019 10:43:08 +0300 Subject: [PATCH 4/6] * Some code fixes * Added index file --- IPGeolocationAPI.ts | 34 +++++++++++++++++++++----------- index.ts | 5 +++++ package.json | 48 +++++++++++++++++++++++---------------------- 3 files changed, 53 insertions(+), 34 deletions(-) create mode 100644 index.ts diff --git a/IPGeolocationAPI.ts b/IPGeolocationAPI.ts index 0bf6230..6c08b14 100644 --- a/IPGeolocationAPI.ts +++ b/IPGeolocationAPI.ts @@ -1,6 +1,8 @@ -import { XMLHttpRequest } from "xmlhttprequest"; import { GeolocationParams } from "./GeolocationParams"; import { TimezoneParams } from "./TimezoneParams"; + +const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest; + export class IPGeolocationAPI { private static buildGeolocationUrlParams( apiKey: string = "", @@ -114,7 +116,7 @@ export class IPGeolocationAPI { } public getGeolocation( - callback, + callback: any, geolocationParams: GeolocationParams | null = null ): void { if (geolocationParams && geolocationParams.getIPAddresses().length === 0) { @@ -144,7 +146,7 @@ export class IPGeolocationAPI { } public getTimezone( - callback, + callback: any, timezoneParams: TimezoneParams | null = null ): void { this.getRequest( @@ -154,18 +156,25 @@ export class IPGeolocationAPI { ); } - private getRequest(subUrl: string, urlParams: string = "", callback): void { + private getRequest( + subUrl: string, + urlParams: string = "", + callback: any + ): void { let jsonData = {}; const xhr = new XMLHttpRequest(); xhr.addEventListener("readystatechange", function() { - if (this.readyState === 4) { - if (this.status == 0) { + // @ts-ignore + const { responseText, status, readyState } = this; + + if (readyState === 4) { + if (status == 0) { jsonData = { message: "Internet is not connected!" }; } else { - jsonData = JSON.parse(this.responseText); + jsonData = JSON.parse(responseText); } if (callback && typeof callback === typeof Function) { @@ -195,19 +204,22 @@ export class IPGeolocationAPI { subUrl: string, urlParams: string = "", requestData: {} = {}, - callback + callback: any ): void { let jsonData = {}; const xhr = new XMLHttpRequest(); xhr.addEventListener("readystatechange", function() { - if (this.readyState === 4) { - if (this.status === 0) { + // @ts-ignore + const { responseText, status, readyState } = this; + + if (readyState === 4) { + if (status === 0) { jsonData = { message: "Internet is not connected!" }; } else { - jsonData = JSON.parse(this.responseText); + jsonData = JSON.parse(responseText); } if (callback && typeof callback === typeof Function) { diff --git a/index.ts b/index.ts new file mode 100644 index 0000000..3d2d9a1 --- /dev/null +++ b/index.ts @@ -0,0 +1,5 @@ +import { GeolocationParams } from "./GeolocationParams"; +import { IPGeolocationAPI } from "./IPGeolocationAPI"; +import { TimezoneParams } from "./TimezoneParams"; + +export default { GeolocationParams, IPGeolocationAPI, TimezoneParams }; diff --git a/package.json b/package.json index 2b54d18..b005936 100644 --- a/package.json +++ b/package.json @@ -1,34 +1,45 @@ { - "_from": "ip-geolocation-api-sdk-typescript", + "_from": "github:sergak01/ip-geolocation-api-typescript-sdk", + "_id": "ip-geolocation-api-sdk-typescript@1.0.7", "_inBundle": false, - "_integrity": "sha512-3H57yHooCwH3glQLZskv8fw1PYFB9+95PZzsFO89acrzIeGcUsbUzd91f4uM8OJIODPXYP+UQEBajjIJWIOWeg==", + "_integrity": "", "_location": "/ip-geolocation-api-sdk-typescript", "_phantomChildren": {}, "_requested": { - "type": "tag", - "registry": true, - "raw": "ip-geolocation-api-sdk-typescript", + "type": "git", + "raw": "ip-geolocation-api-sdk-typescript@github:sergak01/ip-geolocation-api-typescript-sdk", "name": "ip-geolocation-api-sdk-typescript", "escapedName": "ip-geolocation-api-sdk-typescript", - "rawSpec": "", - "saveSpec": null, - "fetchSpec": "latest" + "rawSpec": "github:sergak01/ip-geolocation-api-typescript-sdk", + "saveSpec": "github:sergak01/ip-geolocation-api-typescript-sdk", + "fetchSpec": null, + "gitCommittish": null }, "_requiredBy": [ - "#USER", - "/" + "/", + "/ip-geolocation" ], - "_resolved": "https://registry.npmjs.org/ip-geolocation-api-sdk-typescript/-/ip-geolocation-api-sdk-typescript-1.0.5.tgz", + "_resolved": "github:sergak01/ip-geolocation-api-typescript-sdk#d90258b96dbcce59b4cc40c06418fc8c3f51b5ef", "_shasum": "247858c28360a16ba09569b2e8501385c9d05356", - "_spec": "ip-geolocation-api-sdk-typescript", - "_where": "/home/developer/test-ts2", + "_spec": "ip-geolocation-api-sdk-typescript@github:sergak01/ip-geolocation-api-typescript-sdk", + "_where": "/home/sergey/SoftRize/softrize-frontend", "author": "", "bugs": { "url": "https://gitlab.com/ahsanNawaz111/ipGeoLocation-TypeScript/issues" }, "bundleDependencies": [], + "dependencies": { + "@types/node": "^12.6.8", + "xmlhttprequest": "^1.8.0" + }, "deprecated": false, "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.", + "devDependencies": { + "prettier": "^1.18.2", + "tslint": "^5.17.0", + "tslint-plugin-prettier": "^2.0.1", + "typescript": "^3.5.1" + }, "homepage": "https://gitlab.com/ahsanNawaz111/ipGeoLocation-TypeScript#README", "keywords": [ "ip", @@ -52,14 +63,5 @@ "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, - "version": "1.0.7", - "dependencies": { - "xmlhttprequest": "^1.8.0" - }, - "devDependencies": { - "prettier": "^1.18.2", - "tslint": "^5.17.0", - "tslint-plugin-prettier": "^2.0.1", - "typescript": "^3.5.1" - } + "version": "1.0.7" } From 48e1c51cfb9f598f7cc7f9635541ee0cdf716d2f Mon Sep 17 00:00:00 2001 From: Sergey Shpak Date: Tue, 30 Jul 2019 10:58:03 +0300 Subject: [PATCH 5/6] * Updated index file --- index.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/index.ts b/index.ts index 3d2d9a1..a2df753 100644 --- a/index.ts +++ b/index.ts @@ -1,5 +1,3 @@ -import { GeolocationParams } from "./GeolocationParams"; -import { IPGeolocationAPI } from "./IPGeolocationAPI"; -import { TimezoneParams } from "./TimezoneParams"; - -export default { GeolocationParams, IPGeolocationAPI, TimezoneParams }; +export { GeolocationParams } from "./GeolocationParams"; +export { IPGeolocationAPI } from "./IPGeolocationAPI"; +export { TimezoneParams } from "./TimezoneParams"; From 8fc5db318c4cd383ec33ca0eb3dc849d0a6dc615 Mon Sep 17 00:00:00 2001 From: Sergey Shpak Date: Tue, 30 Jul 2019 11:03:29 +0300 Subject: [PATCH 6/6] * Updated main file in package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index b005936..50c61fc 100644 --- a/package.json +++ b/package.json @@ -54,7 +54,7 @@ "ip location api" ], "license": "ISC", - "main": "IPGeolocationAPI.ts", + "main": "index.ts", "name": "ip-geolocation-api-sdk-typescript", "repository": { "type": "git",