Skip to content

Commit e926dde

Browse files
committed
fix issue with nullable types and initialization of the types
1 parent 0ab8449 commit e926dde

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

IPGeolocationAPI.ts

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,50 @@ import { TimezoneParams } from './TimezoneParams';
33
import { XMLHttpRequest } from 'xmlhttprequest';
44

55
export class IPGeolocationAPI {
6-
apiKey: string;
6+
apiKey: string | null;
77
async: boolean;
88

9-
constructor(apiKey: string = null, async: boolean = true) {
9+
constructor(apiKey: string | null = null, async: boolean = true) {
1010
this.apiKey = apiKey;
1111
this.async = async;
1212
}
1313

14-
public getGeolocation(callback: (json: any) => any, geolocationParams: GeolocationParams = null): void {
14+
public getGeolocation(callback: (json: any) => any, geolocationParams: GeolocationParams | null = null): void {
1515
if (geolocationParams && geolocationParams.getIPAddresses().length === 0) {
16-
this.getRequest("ipgeo", this.buildGeolocationUrlParams(this.apiKey, geolocationParams), callback);
16+
if (this.apiKey) {
17+
this.getRequest("ipgeo", this.buildGeolocationUrlParams(this.apiKey, geolocationParams), callback);
18+
} else {
19+
throw new Error('API Key invalid');
20+
}
1721
} else {
1822
const jsonData = JSON.stringify({
19-
"ips": geolocationParams.getIPAddresses()
23+
"ips": geolocationParams!.getIPAddresses()
2024
});
2125

22-
this.postRequest("ipgeo-bulk", this.buildGeolocationUrlParams(this.apiKey, geolocationParams), jsonData, callback);
26+
if (this.apiKey && geolocationParams) {
27+
this.postRequest("ipgeo-bulk", this.buildGeolocationUrlParams(this.apiKey, geolocationParams), jsonData, callback);
28+
}
2329
}
2430
}
2531

26-
public getTimezone(callback: (json: any) => any, timezoneParams: TimezoneParams = null): void {
27-
this.getRequest("timezone", this.buildTimezoneUrlParams(this.apiKey, timezoneParams), callback);
32+
public getTimezone(callback: (json: any) => any, timezoneParams: TimezoneParams | null = null): void {
33+
if (this.apiKey && timezoneParams) {
34+
this.getRequest("timezone", this.buildTimezoneUrlParams(this.apiKey, timezoneParams), callback);
35+
} else {
36+
throw new Error('You need to provide a API Key valid or your Timezone is invalid');
37+
}
2838
}
2939

30-
public getUserAgent(callback: (json: any) => any, uaString: string = null): void {
40+
public getUserAgent(callback: (json: any) => any, uaString: string | null = null): void {
3141
var jsonData: string = JSON.stringify({
3242
"uaString": uaString
3343
});
3444

35-
this.postRequest('user-agent', "apiKey=" + this.apiKey, jsonData, callback);
45+
if (this.apiKey) {
46+
this.postRequest('user-agent', "apiKey=" + this.apiKey, jsonData, callback);
47+
} else {
48+
throw new Error('You need to provide a API Key');
49+
}
3650
}
3751

3852
public getBulkUserAgent(callback: (json: any) => any, uaStrings = []): void {
@@ -51,7 +65,7 @@ export class IPGeolocationAPI {
5165
return this.async;
5266
}
5367

54-
private buildGeolocationUrlParams(apiKey: string = null, geolocationParams: GeolocationParams = null): string {
68+
private buildGeolocationUrlParams(apiKey: string | null = null, geolocationParams: GeolocationParams | null = null): string {
5569
let urlParams = "";
5670

5771
if (apiKey) {
@@ -133,7 +147,7 @@ export class IPGeolocationAPI {
133147
return urlParams;
134148
}
135149

136-
private buildTimezoneUrlParams(apiKey: string = null, timezoneParams: TimezoneParams = null) {
150+
private buildTimezoneUrlParams(apiKey: string | null = null, timezoneParams: TimezoneParams | null = null) {
137151
let urlParams = "";
138152

139153
if (apiKey) {
@@ -161,10 +175,10 @@ export class IPGeolocationAPI {
161175
if (urlParams) {
162176
urlParams = urlParams.concat('&');
163177
}
164-
178+
165179
urlParams = urlParams.concat('location=', timezoneParams.getLocation());
166180
}
167-
181+
168182
if (timezoneParams.getLatitude() >= -90 && timezoneParams.getLatitude() <= 90 && timezoneParams.getLongitude() >= -180 && timezoneParams.getLongitude() <= 180) {
169183
if (urlParams) {
170184
urlParams = urlParams.concat("&");

0 commit comments

Comments
 (0)