@@ -2,102 +2,100 @@ import { parse } from 'url'
2
2
import { request as nodeRequest } from 'https'
3
3
import { left , right , Either } from 'fp-ts/lib/Either'
4
4
5
- export const resolveFromAPI = ( {
6
- apiKey,
7
- endpoint,
8
- } : {
9
- apiKey : string
10
- endpoint : string
11
- } ) => async ( cell : {
12
- area : number
13
- mccmnc : number
14
- cell : number
15
- } ) : Promise < Either < Error , { lat : number ; lng : number ; accuracy : number } > > => {
16
- try {
17
- const { hostname, path } = parse ( endpoint )
5
+ export const resolveFromAPI =
6
+ ( { apiKey, endpoint } : { apiKey : string ; endpoint : string } ) =>
7
+ async ( cell : {
8
+ area : number
9
+ mccmnc : number
10
+ cell : number
11
+ } ) : Promise <
12
+ Either < Error , { lat : number ; lng : number ; accuracy : number } >
13
+ > => {
14
+ try {
15
+ const { hostname, path } = parse ( endpoint )
18
16
19
- // See https://eu1.unwiredlabs.com/docs-html/index.html#response
20
- const {
21
- status,
22
- lat,
23
- lon,
24
- accuracy,
25
- } : {
26
- status : 'ok' | 'error'
27
- message ?: string
28
- balance : number
29
- balance_slots ?: number
30
- lat : number
31
- lon : number
32
- accuracy : number
33
- aged ?: boolean
34
- fallback ?: 'ipf' | 'lacf' | 'scf'
35
- // address: string (not requested)
36
- // address_details?: string (not requested)
37
- } = await new Promise ( ( resolve , reject ) => {
38
- const options = {
39
- host : hostname ,
40
- path : `${ path ?. replace ( / \/ * $ / , '' ) ?? '' } /v2/process.php` ,
41
- method : 'POST' ,
42
- agent : false ,
43
- }
17
+ // See https://eu1.unwiredlabs.com/docs-html/index.html#response
18
+ const {
19
+ status,
20
+ lat,
21
+ lon,
22
+ accuracy,
23
+ } : {
24
+ status : 'ok' | 'error'
25
+ message ?: string
26
+ balance : number
27
+ balance_slots ?: number
28
+ lat : number
29
+ lon : number
30
+ accuracy : number
31
+ aged ?: boolean
32
+ fallback ?: 'ipf' | 'lacf' | 'scf'
33
+ // address: string (not requested)
34
+ // address_details?: string (not requested)
35
+ } = await new Promise ( ( resolve , reject ) => {
36
+ const options = {
37
+ host : hostname ,
38
+ path : `${ path ?. replace ( / \/ * $ / , '' ) ?? '' } /v2/process.php` ,
39
+ method : 'POST' ,
40
+ agent : false ,
41
+ }
44
42
45
- const req = nodeRequest ( options , ( res ) => {
46
- console . debug (
47
- JSON . stringify ( {
48
- response : {
49
- statusCode : res . statusCode ,
50
- headers : res . headers ,
51
- } ,
52
- } ) ,
53
- )
54
- res . on ( 'data' , ( d ) => {
55
- const responseBody = JSON . parse ( d . toString ( ) )
43
+ const req = nodeRequest ( options , ( res ) => {
56
44
console . debug (
57
45
JSON . stringify ( {
58
- responseBody,
46
+ response : {
47
+ statusCode : res . statusCode ,
48
+ headers : res . headers ,
49
+ } ,
59
50
} ) ,
60
51
)
61
- if ( res . statusCode === undefined ) {
62
- return reject ( new Error ( 'No response received!' ) )
63
- }
64
- if ( res . statusCode >= 400 ) {
65
- reject ( new Error ( responseBody . description ) )
66
- }
67
- resolve ( responseBody )
52
+ res . on ( 'data' , ( d ) => {
53
+ const responseBody = JSON . parse ( d . toString ( ) )
54
+ console . debug (
55
+ JSON . stringify ( {
56
+ responseBody,
57
+ } ) ,
58
+ )
59
+ if ( res . statusCode === undefined ) {
60
+ return reject ( new Error ( 'No response received!' ) )
61
+ }
62
+ if ( res . statusCode >= 400 ) {
63
+ reject ( new Error ( responseBody . description ) )
64
+ }
65
+ resolve ( responseBody )
66
+ } )
68
67
} )
69
- } )
70
68
71
- req . on ( 'error' , ( e ) => {
72
- reject ( new Error ( e . message ) )
73
- } )
69
+ req . on ( 'error' , ( e ) => {
70
+ reject ( new Error ( e . message ) )
71
+ } )
74
72
75
- const payload = JSON . stringify ( {
76
- token : apiKey ,
77
- radio : 'lte' ,
78
- mcc : Math . floor ( cell . mccmnc / 100 ) ,
79
- mnc : cell . mccmnc % 100 ,
80
- cells : [
81
- {
82
- lac : cell . area ,
83
- cid : cell . cell ,
84
- } ,
85
- ] ,
73
+ const payload = JSON . stringify ( {
74
+ token : apiKey ,
75
+ radio : 'lte' ,
76
+ mcc : Math . floor ( cell . mccmnc / 100 ) ,
77
+ mnc : cell . mccmnc % 100 ,
78
+ cells : [
79
+ {
80
+ lac : cell . area ,
81
+ cid : cell . cell ,
82
+ } ,
83
+ ] ,
84
+ } )
85
+ console . log ( payload . replace ( apiKey , '***' ) )
86
+ req . write ( payload )
87
+ req . end ( )
86
88
} )
87
- console . log ( payload . replace ( apiKey , '***' ) )
88
- req . write ( payload )
89
- req . end ( )
90
- } )
91
89
92
- if ( status === 'ok' && lat && lon ) {
93
- return right ( {
94
- lat,
95
- lng : lon ,
96
- accuracy,
97
- } )
90
+ if ( status === 'ok' && lat && lon ) {
91
+ return right ( {
92
+ lat,
93
+ lng : lon ,
94
+ accuracy,
95
+ } )
96
+ }
97
+ return left ( new Error ( `Failed to resolve.` ) )
98
+ } catch ( err ) {
99
+ return left ( err )
98
100
}
99
- return left ( new Error ( `Failed to resolve.` ) )
100
- } catch ( err ) {
101
- return left ( err )
102
101
}
103
- }
0 commit comments