Skip to content

Commit c678c78

Browse files
authored
Merge pull request #31 from coder-hxl/fix/30
fix: The params option does not work
2 parents b815fe3 + 71bffa7 commit c678c78

File tree

1 file changed

+19
-22
lines changed

1 file changed

+19
-22
lines changed

src/request.ts

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import http, {
55
IncomingHttpHeaders
66
} from 'node:http'
77
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+
911
import HttpsProxyAgent from 'https-proxy-agent'
1012

1113
import { isUndefined } from './utils'
1214

13-
import { AnyObject, MapTypeEmptyObject } from './types/common'
15+
import { AnyObject } from './types/common'
1416
import { LoaderCrawlDataDetail, LoaderCrawlFileDetail } from './api'
1517

1618
/* Type */
@@ -20,24 +22,9 @@ export interface Request {
2022
data: Buffer
2123
}
2224

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-
3825
function parseHeaders(
3926
rawConfig: LoaderCrawlDataDetail & LoaderCrawlFileDetail,
40-
config: RequestOptions & MapTypeEmptyObject<URL>
27+
config: RequestOptions
4128
) {
4229
const rawHeaders = rawConfig.headers ?? {}
4330
const headers: AnyObject = {
@@ -56,13 +43,24 @@ function parseHeaders(
5643

5744
function handleRequestConfig(
5845
rawConfig: LoaderCrawlDataDetail & LoaderCrawlFileDetail
59-
): RequestOptions & MapTypeEmptyObject<URL> {
46+
): RequestOptions {
6047
const { protocol, hostname, port, pathname, search } = new Url.URL(
6148
rawConfig.url
6249
)
6350
const isHttp = protocol === 'http:'
6451

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 = {
6664
agent: rawConfig.proxyUrl
6765
? HttpsProxyAgent(rawConfig.proxyUrl)
6866
: isHttp
@@ -72,8 +70,7 @@ function handleRequestConfig(
7270
protocol,
7371
hostname,
7472
port,
75-
path: pathname,
76-
search: parseParams(search, rawConfig.params),
73+
path,
7774

7875
method: rawConfig.method?.toLocaleUpperCase() ?? 'GET',
7976
headers: {},

0 commit comments

Comments
 (0)