@@ -7,25 +7,27 @@ import * as fetch from 'node-fetch';
7
7
import type * as http from 'http' ;
8
8
import { Import } from '@rushstack/node-core-library' ;
9
9
10
- // ===================================================================================================================
11
- // AS A TEMPORARY WORKAROUND, THIS FILE WAS COPY+PASTED INTO THE "rush-amazon-s3-build-cache-plugin" PROJECT.
12
- // See that copy for notes.
13
- // ===================================================================================================================
14
-
15
10
const createHttpsProxyAgent : typeof import ( 'https-proxy-agent' ) = Import . lazy ( 'https-proxy-agent' , require ) ;
16
11
17
12
/**
18
13
* For use with {@link WebClient}.
19
14
*/
20
15
export type WebClientResponse = fetch . Response ;
21
16
17
+ /**
18
+ * For use with {@link WebClient}.
19
+ */
20
+ export type WebClientHeaders = fetch . Headers ;
21
+ // eslint-disable-next-line @typescript-eslint/no-redeclare
22
+ export const WebClientHeaders : typeof fetch . Headers = fetch . Headers ;
23
+
22
24
/**
23
25
* For use with {@link WebClient}.
24
26
*/
25
27
export interface IWebFetchOptionsBase {
26
28
timeoutMs ?: number ;
27
- verb ?: 'GET' | 'PUT' ;
28
- headers ?: fetch . Headers ;
29
+ headers ?: WebClientHeaders | Record < string , string > ;
30
+ redirect ?: fetch . RequestInit [ 'redirect' ] ;
29
31
}
30
32
31
33
/**
@@ -38,8 +40,8 @@ export interface IGetFetchOptions extends IWebFetchOptionsBase {
38
40
/**
39
41
* For use with {@link WebClient}.
40
42
*/
41
- export interface IPutFetchOptions extends IWebFetchOptionsBase {
42
- verb : 'PUT' ;
43
+ export interface IFetchOptionsWithBody extends IWebFetchOptionsBase {
44
+ verb : 'PUT' | 'POST' | 'PATCH' ;
43
45
body ?: Buffer ;
44
46
}
45
47
@@ -56,19 +58,30 @@ export enum WebClientProxy {
56
58
* A helper for issuing HTTP requests.
57
59
*/
58
60
export class WebClient {
61
+ private static _requestFn : typeof fetch . default = fetch . default ;
62
+
59
63
public readonly standardHeaders : fetch . Headers = new fetch . Headers ( ) ;
60
64
61
65
public accept : string | undefined = '*/*' ;
62
66
public userAgent : string | undefined = `rush node/${ process . version } ${ os . platform ( ) } ${ os . arch ( ) } ` ;
63
67
64
68
public proxy : WebClientProxy = WebClientProxy . Detect ;
65
69
66
- public constructor ( ) { }
70
+ public static mockRequestFn ( fn : typeof fetch . default ) : void {
71
+ WebClient . _requestFn = fn ;
72
+ }
73
+
74
+ public static resetMockRequestFn ( ) : void {
75
+ WebClient . _requestFn = fetch . default ;
76
+ }
67
77
68
- public static mergeHeaders ( target : fetch . Headers , source : fetch . Headers ) : void {
69
- source . forEach ( ( value , name ) => {
78
+ public static mergeHeaders ( target : fetch . Headers , source : fetch . Headers | Record < string , string > ) : void {
79
+ const iterator : Iterable < [ string , string ] > =
80
+ 'entries' in source && typeof source . entries === 'function' ? source . entries ( ) : Object . entries ( source ) ;
81
+
82
+ for ( const [ name , value ] of iterator ) {
70
83
target . set ( name , value ) ;
71
- } ) ;
84
+ }
72
85
}
73
86
74
87
public addBasicAuthHeader ( userName : string , password : string ) : void {
@@ -80,7 +93,7 @@ export class WebClient {
80
93
81
94
public async fetchAsync (
82
95
url : string ,
83
- options ?: IGetFetchOptions | IPutFetchOptions
96
+ options ?: IGetFetchOptions | IFetchOptionsWithBody
84
97
) : Promise < WebClientResponse > {
85
98
const headers : fetch . Headers = new fetch . Headers ( ) ;
86
99
@@ -93,6 +106,7 @@ export class WebClient {
93
106
if ( this . userAgent ) {
94
107
headers . set ( 'user-agent' , this . userAgent ) ;
95
108
}
109
+
96
110
if ( this . accept ) {
97
111
headers . set ( 'accept' , this . accept ) ;
98
112
}
@@ -126,13 +140,14 @@ export class WebClient {
126
140
method : options ?. verb ,
127
141
headers : headers ,
128
142
agent : agent ,
129
- timeout : timeoutMs
143
+ timeout : timeoutMs ,
144
+ redirect : options ?. redirect
130
145
} ;
131
- const putOptions : IPutFetchOptions | undefined = options as IPutFetchOptions | undefined ;
132
- if ( putOptions ?. body ) {
133
- requestInit . body = putOptions . body ;
146
+ const optionsWithBody : IFetchOptionsWithBody | undefined = options as IFetchOptionsWithBody | undefined ;
147
+ if ( optionsWithBody ?. body ) {
148
+ requestInit . body = optionsWithBody . body ;
134
149
}
135
150
136
- return await fetch . default ( url , requestInit ) ;
151
+ return await WebClient . _requestFn ( url , requestInit ) ;
137
152
}
138
153
}
0 commit comments