From e926dde34d4a15724493bffb8d1a369e2e115174 Mon Sep 17 00:00:00 2001 From: delciocapolo Date: Sun, 31 Mar 2024 17:58:33 +0100 Subject: [PATCH 1/2] fix issue with nullable types and initialization of the types --- IPGeolocationAPI.ts | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/IPGeolocationAPI.ts b/IPGeolocationAPI.ts index b1dd8de..2b5db73 100644 --- a/IPGeolocationAPI.ts +++ b/IPGeolocationAPI.ts @@ -3,36 +3,50 @@ import { TimezoneParams } from './TimezoneParams'; import { XMLHttpRequest } from 'xmlhttprequest'; export class IPGeolocationAPI { - apiKey: string; + apiKey: string | null; async: boolean; - constructor(apiKey: string = null, async: boolean = true) { + constructor(apiKey: string | null = null, async: boolean = true) { this.apiKey = apiKey; this.async = async; } - public getGeolocation(callback: (json: any) => any, geolocationParams: GeolocationParams = null): void { + public getGeolocation(callback: (json: any) => any, geolocationParams: GeolocationParams | null = null): void { if (geolocationParams && geolocationParams.getIPAddresses().length === 0) { - this.getRequest("ipgeo", this.buildGeolocationUrlParams(this.apiKey, geolocationParams), callback); + if (this.apiKey) { + this.getRequest("ipgeo", this.buildGeolocationUrlParams(this.apiKey, geolocationParams), callback); + } else { + throw new Error('API Key invalid'); + } } else { const jsonData = JSON.stringify({ - "ips": geolocationParams.getIPAddresses() + "ips": geolocationParams!.getIPAddresses() }); - this.postRequest("ipgeo-bulk", this.buildGeolocationUrlParams(this.apiKey, geolocationParams), jsonData, callback); + if (this.apiKey && geolocationParams) { + this.postRequest("ipgeo-bulk", this.buildGeolocationUrlParams(this.apiKey, geolocationParams), jsonData, callback); + } } } - public getTimezone(callback: (json: any) => any, timezoneParams: TimezoneParams = null): void { - this.getRequest("timezone", this.buildTimezoneUrlParams(this.apiKey, timezoneParams), callback); + public getTimezone(callback: (json: any) => any, timezoneParams: TimezoneParams | null = null): void { + if (this.apiKey && timezoneParams) { + this.getRequest("timezone", this.buildTimezoneUrlParams(this.apiKey, timezoneParams), callback); + } else { + throw new Error('You need to provide a API Key valid or your Timezone is invalid'); + } } - public getUserAgent(callback: (json: any) => any, uaString: string = null): void { + public getUserAgent(callback: (json: any) => any, uaString: string | null = null): void { var jsonData: string = JSON.stringify({ "uaString": uaString }); - this.postRequest('user-agent', "apiKey=" + this.apiKey, jsonData, callback); + if (this.apiKey) { + this.postRequest('user-agent', "apiKey=" + this.apiKey, jsonData, callback); + } else { + throw new Error('You need to provide a API Key'); + } } public getBulkUserAgent(callback: (json: any) => any, uaStrings = []): void { @@ -51,7 +65,7 @@ export class IPGeolocationAPI { return this.async; } - private buildGeolocationUrlParams(apiKey: string = null, geolocationParams: GeolocationParams = null): string { + private buildGeolocationUrlParams(apiKey: string | null = null, geolocationParams: GeolocationParams | null = null): string { let urlParams = ""; if (apiKey) { @@ -133,7 +147,7 @@ export class IPGeolocationAPI { return urlParams; } - private buildTimezoneUrlParams(apiKey: string = null, timezoneParams: TimezoneParams = null) { + private buildTimezoneUrlParams(apiKey: string | null = null, timezoneParams: TimezoneParams | null = null) { let urlParams = ""; if (apiKey) { @@ -161,10 +175,10 @@ export class IPGeolocationAPI { 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("&"); From 93fd767abe50806442353181a43a7d0028cf6397 Mon Sep 17 00:00:00 2001 From: delciocapolo Date: Sun, 31 Mar 2024 18:07:06 +0100 Subject: [PATCH 2/2] types for the xmlhttprequest were requested in the package[json] and treat the arguments for xhr function --- IPGeolocationAPI.ts | 2 +- package.json | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/IPGeolocationAPI.ts b/IPGeolocationAPI.ts index 2b5db73..fa4bb6b 100644 --- a/IPGeolocationAPI.ts +++ b/IPGeolocationAPI.ts @@ -253,7 +253,7 @@ export class IPGeolocationAPI { 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); + xhr.send(requestData as XMLHttpRequestBodyInit); } } diff --git a/package.json b/package.json index 8d37616..17f5091 100644 --- a/package.json +++ b/package.json @@ -32,5 +32,7 @@ "dependencies": { "xmlhttprequest": "^1.8.0" }, - "devDependencies": {} + "devDependencies": { + "@types/xmlhttprequest": "^1.8.2" + } }