@@ -3,36 +3,50 @@ import { TimezoneParams } from './TimezoneParams';
3
3
import { XMLHttpRequest } from 'xmlhttprequest' ;
4
4
5
5
export class IPGeolocationAPI {
6
- apiKey : string ;
6
+ apiKey : string | null ;
7
7
async : boolean ;
8
8
9
- constructor ( apiKey : string = null , async : boolean = true ) {
9
+ constructor ( apiKey : string | null = null , async : boolean = true ) {
10
10
this . apiKey = apiKey ;
11
11
this . async = async ;
12
12
}
13
13
14
- public getGeolocation ( callback : ( json : any ) => any , geolocationParams : GeolocationParams = null ) : void {
14
+ public getGeolocation ( callback : ( json : any ) => any , geolocationParams : GeolocationParams | null = null ) : void {
15
15
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
+ }
17
21
} else {
18
22
const jsonData = JSON . stringify ( {
19
- "ips" : geolocationParams . getIPAddresses ( )
23
+ "ips" : geolocationParams ! . getIPAddresses ( )
20
24
} ) ;
21
25
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
+ }
23
29
}
24
30
}
25
31
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
+ }
28
38
}
29
39
30
- public getUserAgent ( callback : ( json : any ) => any , uaString : string = null ) : void {
40
+ public getUserAgent ( callback : ( json : any ) => any , uaString : string | null = null ) : void {
31
41
var jsonData : string = JSON . stringify ( {
32
42
"uaString" : uaString
33
43
} ) ;
34
44
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
+ }
36
50
}
37
51
38
52
public getBulkUserAgent ( callback : ( json : any ) => any , uaStrings = [ ] ) : void {
@@ -51,7 +65,7 @@ export class IPGeolocationAPI {
51
65
return this . async ;
52
66
}
53
67
54
- private buildGeolocationUrlParams ( apiKey : string = null , geolocationParams : GeolocationParams = null ) : string {
68
+ private buildGeolocationUrlParams ( apiKey : string | null = null , geolocationParams : GeolocationParams | null = null ) : string {
55
69
let urlParams = "" ;
56
70
57
71
if ( apiKey ) {
@@ -133,7 +147,7 @@ export class IPGeolocationAPI {
133
147
return urlParams ;
134
148
}
135
149
136
- private buildTimezoneUrlParams ( apiKey : string = null , timezoneParams : TimezoneParams = null ) {
150
+ private buildTimezoneUrlParams ( apiKey : string | null = null , timezoneParams : TimezoneParams | null = null ) {
137
151
let urlParams = "" ;
138
152
139
153
if ( apiKey ) {
@@ -161,10 +175,10 @@ export class IPGeolocationAPI {
161
175
if ( urlParams ) {
162
176
urlParams = urlParams . concat ( '&' ) ;
163
177
}
164
-
178
+
165
179
urlParams = urlParams . concat ( 'location=' , timezoneParams . getLocation ( ) ) ;
166
180
}
167
-
181
+
168
182
if ( timezoneParams . getLatitude ( ) >= - 90 && timezoneParams . getLatitude ( ) <= 90 && timezoneParams . getLongitude ( ) >= - 180 && timezoneParams . getLongitude ( ) <= 180 ) {
169
183
if ( urlParams ) {
170
184
urlParams = urlParams . concat ( "&" ) ;
0 commit comments