2
2
* @Author : renxia
3
3
* @Date : 2024-01-15 11:26:52
4
4
* @LastEditors : renxia
5
- * @LastEditTime : 2024-02-02 11:08:37
5
+ * @LastEditTime : 2024-03-14 09:46:01
6
6
* @Description :
7
7
*/
8
8
import type { OutgoingHttpHeaders } from 'node:http' ;
@@ -32,13 +32,15 @@ export class ReqBase {
32
32
accept : 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8' ,
33
33
'user-agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36' ,
34
34
} ;
35
+ protected isBrowser = typeof document !== 'undefined' && typeof window !== 'undefined' ;
35
36
protected config : ReqConfig = { } ;
36
37
constructor ( config ?: string | ReqConfig , headers ?: OutgoingHttpHeaders ) {
37
38
if ( config ) {
38
39
if ( typeof config === 'string' ) config = { cookie : config } ;
39
40
config = assign ( this . config , config ) ;
40
41
}
41
42
43
+ if ( this . isBrowser && ! this . config . prefixUrl ) this . config . prefixUrl = location . origin ;
42
44
if ( this . config . cookie ) this . setCookie ( this . config . cookie ) ;
43
45
if ( this . config . headers ) this . setHeaders ( this . config . headers ) ;
44
46
if ( headers ) this . setHeaders ( headers ) ;
@@ -49,7 +51,7 @@ export class ReqBase {
49
51
...toLowcaseKeyObject ( headers ) ,
50
52
} ;
51
53
52
- if ( urlObject ) {
54
+ if ( ! this . isBrowser && urlObject ) {
53
55
if ( ! headers . host ) headers . host = urlObject . host ;
54
56
if ( ! headers . origin ) headers . origin = urlObject . origin || `${ urlObject . protocol } ://${ urlObject . hostname } ` ;
55
57
}
0 commit comments