@@ -5,12 +5,14 @@ import http, {
5
5
IncomingHttpHeaders
6
6
} from 'node:http'
7
7
import https from 'node:https'
8
- import Url , { URL } from 'node:url'
8
+ import Url from 'node:url'
9
+ import querystring from 'node:querystring'
10
+
9
11
import HttpsProxyAgent from 'https-proxy-agent'
10
12
11
13
import { isUndefined } from './utils'
12
14
13
- import { AnyObject , MapTypeEmptyObject } from './types/common'
15
+ import { AnyObject } from './types/common'
14
16
import { LoaderCrawlDataDetail , LoaderCrawlFileDetail } from './api'
15
17
16
18
/* Type */
@@ -20,24 +22,9 @@ export interface Request {
20
22
data : Buffer
21
23
}
22
24
23
- function parseParams ( urlSearch : string , params ?: AnyObject ) : string {
24
- let res = urlSearch ? `${ urlSearch } ` : '?'
25
-
26
- if ( params ) {
27
- for ( const key in params ) {
28
- const value = params [ key ]
29
- res += `&${ key } =${ value } `
30
- }
31
- } else {
32
- res = urlSearch
33
- }
34
-
35
- return res
36
- }
37
-
38
25
function parseHeaders (
39
26
rawConfig : LoaderCrawlDataDetail & LoaderCrawlFileDetail ,
40
- config : RequestOptions & MapTypeEmptyObject < URL >
27
+ config : RequestOptions
41
28
) {
42
29
const rawHeaders = rawConfig . headers ?? { }
43
30
const headers : AnyObject = {
@@ -56,13 +43,24 @@ function parseHeaders(
56
43
57
44
function handleRequestConfig (
58
45
rawConfig : LoaderCrawlDataDetail & LoaderCrawlFileDetail
59
- ) : RequestOptions & MapTypeEmptyObject < URL > {
46
+ ) : RequestOptions {
60
47
const { protocol, hostname, port, pathname, search } = new Url . URL (
61
48
rawConfig . url
62
49
)
63
50
const isHttp = protocol === 'http:'
64
51
65
- const config : RequestOptions & MapTypeEmptyObject < URL > = {
52
+ let path = pathname
53
+ if ( search || rawConfig . params ) {
54
+ if ( search ) {
55
+ path += `${ search } ${
56
+ rawConfig . params ? '&' + querystring . stringify ( rawConfig . params ) : ''
57
+ } `
58
+ } else {
59
+ path += `?${ querystring . stringify ( rawConfig . params ) } `
60
+ }
61
+ }
62
+
63
+ const config : RequestOptions = {
66
64
agent : rawConfig . proxyUrl
67
65
? HttpsProxyAgent ( rawConfig . proxyUrl )
68
66
: isHttp
@@ -72,8 +70,7 @@ function handleRequestConfig(
72
70
protocol,
73
71
hostname,
74
72
port,
75
- path : pathname ,
76
- search : parseParams ( search , rawConfig . params ) ,
73
+ path,
77
74
78
75
method : rawConfig . method ?. toLocaleUpperCase ( ) ?? 'GET' ,
79
76
headers : { } ,
0 commit comments