1
- import nodeFetch from 'node-fetch'
2
1
import tough from 'tough-cookie'
3
2
import timeoutSignal from 'timeout-signal'
4
3
import HttpsProxyAgent from 'https-proxy-agent'
@@ -13,7 +12,7 @@ const ECODE = {
13
12
TIMEOUT : 'TIMEOUT'
14
13
}
15
14
16
- async function fetch ( url , opts = { } ) {
15
+ async function request ( url , opts = { } ) {
17
16
const cookieJar = opts . cookie
18
17
const existCookie = cookieJar ?. getCookieStringSync ?. ( url )
19
18
const optCookie = opts . headers ?. cookie || ''
@@ -66,7 +65,7 @@ async function fetch(url, opts = {}) {
66
65
}
67
66
68
67
try {
69
- res = await nodeFetch ( urlObj , opts )
68
+ res = await fetch ( urlObj , opts )
70
69
} catch ( e ) {
71
70
if ( opts . signal ?. aborted ) {
72
71
throw { code : ECODE . TIMEOUT , req : urlObj , msg : e }
@@ -75,7 +74,7 @@ async function fetch(url, opts = {}) {
75
74
throw { code : ECODE . FETCH , req : urlObj , msg : res . statusText }
76
75
}
77
76
78
- const setCookies = res . headers . raw ( ) [ 'set-cookie' ]
77
+ const setCookies = res . headers [ 'set-cookie' ]
79
78
80
79
if ( setCookies ) {
81
80
setCookies . map ( ( cookie ) =>
@@ -87,7 +86,7 @@ async function fetch(url, opts = {}) {
87
86
}
88
87
89
88
async function doGet ( url , opts = { } ) {
90
- const res = await fetch ( url , {
89
+ const res = await request ( url , {
91
90
...opts ,
92
91
timeout : opts . timeout ?? 10000
93
92
} )
@@ -111,7 +110,7 @@ async function doPost(url, data, opts = {}) {
111
110
body = data ? JSON . stringify ( data ) : ''
112
111
}
113
112
114
- const res = await fetch ( url , {
113
+ const res = await request ( url , {
115
114
...opts ,
116
115
method : 'POST' ,
117
116
body : body ,
@@ -126,11 +125,11 @@ async function doPost(url, data, opts = {}) {
126
125
throw { code : ECODE . FETCH , url : url , msg : res . statusText }
127
126
}
128
127
129
- fetch . ECODE = ECODE
130
- fetch . get = doGet
131
- fetch . post = doPost
132
- fetch . setProxyAgent = ( url ) => {
133
- fetch . _proxyAgent = new HttpsProxyAgent ( url )
128
+ request . ECODE = ECODE
129
+ request . get = doGet
130
+ request . post = doPost
131
+ request . setProxyAgent = ( url ) => {
132
+ request . _proxyAgent = new HttpsProxyAgent ( url )
134
133
}
135
134
136
135
export function createCookieJar ( id ) {
@@ -141,4 +140,4 @@ export function createCookieJar(id) {
141
140
return cookieJar
142
141
}
143
142
144
- export default fetch
143
+ export default request
0 commit comments