@@ -16,12 +16,15 @@ import process from "node:process";
16
16
*/
17
17
export const _internals = {
18
18
execute : execute ,
19
- getBaseUrl : getBaseUrl ,
19
+ getHostnameAndPort : getHostnameAndPort ,
20
20
} ;
21
21
22
22
/** Facilitates stubbing in tests, e.g. localhost as the base url */
23
- function getBaseUrl ( ) {
24
- return "https://serpapi.com" ;
23
+ function getHostnameAndPort ( ) {
24
+ return {
25
+ hostname : "serpapi.com" ,
26
+ port : 443 ,
27
+ } ;
25
28
}
26
29
27
30
export function getSource ( ) {
@@ -40,25 +43,35 @@ export function getSource() {
40
43
return `nodejs,${ moduleSource } ` ;
41
44
}
42
45
43
- export function buildUrl (
46
+ export function buildRequestOptions (
44
47
path : string ,
45
48
parameters : qs . ParsedUrlQueryInput ,
46
- ) : string {
49
+ ) : http . RequestOptions {
47
50
const clonedParams = { ...parameters } ;
48
51
for ( const k in clonedParams ) {
49
52
if ( clonedParams [ k ] === undefined ) {
50
53
delete clonedParams [ k ] ;
51
54
}
52
55
}
53
- return `${ _internals . getBaseUrl ( ) } ${ path } ?${ qs . stringify ( clonedParams ) } ` ;
56
+ const base = {
57
+ ..._internals . getHostnameAndPort ( ) ,
58
+ path : `${ path } ?${ qs . stringify ( clonedParams ) } ` ,
59
+ method : "GET" ,
60
+ } ;
61
+
62
+ return {
63
+ ...base ,
64
+ ...( parameters . requestOptions as http . RequestOptions ) ,
65
+ ...config . requestOptions ,
66
+ } ;
54
67
}
55
68
56
69
export function execute (
57
70
path : string ,
58
71
parameters : qs . ParsedUrlQueryInput ,
59
72
timeout : number ,
60
73
) : Promise < string > {
61
- const url = buildUrl ( path , {
74
+ const options = buildRequestOptions ( path , {
62
75
...parameters ,
63
76
source : getSource ( ) ,
64
77
} ) ;
@@ -96,13 +109,7 @@ export function execute(
96
109
if ( timer ) clearTimeout ( timer ) ;
97
110
} ;
98
111
99
- const options = ( parameters . requestOptions as http . RequestOptions ) ||
100
- config . requestOptions ||
101
- { } ;
102
-
103
- const req = https
104
- . get ( url , options , handleResponse )
105
- . on ( "error" , handleError ) ;
112
+ const req = https . get ( options , handleResponse ) . on ( "error" , handleError ) ;
106
113
107
114
if ( timeout > 0 ) {
108
115
timer = setTimeout ( ( ) => {
0 commit comments