1
- import { FetchStream , fetchUrl } from 'fetch' ;
1
+ import { FetchStream , fetchUrl , FetchOptions } from 'fetch' ;
2
2
import { Promise } from "./Promise" ;
3
3
import { IXHROptions , IXHRApi , IXHRProgress } from "./Interfaces" ;
4
4
5
- /** @internal */
6
- export class XHRDefaults implements IXHRApi {
5
+ /**
6
+ * Default implementation of XHRApi using fetch
7
+ */
8
+ export class XHRDefault implements IXHRApi {
7
9
static FetchStream : typeof FetchStream = FetchStream ;
8
10
static fetchUrl : typeof fetchUrl = null ;
11
+ static defaultOptions : FetchOptions = { } ;
9
12
13
+ fetchOptions : FetchOptions = { } ;
10
14
private stream : FetchStream ;
11
15
12
16
xhr ( xhroptions : IXHROptions , progressDelegate ?: ( progressData : IXHRProgress ) => void ) : Promise < XMLHttpRequest > {
13
- if ( XHRDefaults . fetchUrl === null ) {
17
+ if ( XHRDefault . fetchUrl === null ) {
14
18
throw new Error ( "xhrApi - stub method, must be bootstrapped" ) ;
15
19
}
16
20
//setup xhr for github.com/andris9/fetch options
@@ -26,7 +30,7 @@ export class XHRDefaults implements IXHRApi {
26
30
// delete xhroptions["type"];
27
31
28
32
return new Promise < XMLHttpRequest > ( ( resolve , reject ) => {
29
- XHRDefaults . fetchUrl ( xhroptions . url , options , ( error , meta , body ) => {
33
+ XHRDefault . fetchUrl ( xhroptions . url , this . getOptions ( options ) , ( error , meta , body ) => {
30
34
if ( error ) {
31
35
if ( typeof ( < any > error ) . status === 'undefined' ) {
32
36
( < any > error ) . status = 0 ;
@@ -56,7 +60,7 @@ export class XHRDefaults implements IXHRApi {
56
60
}
57
61
58
62
xhrStream ( xhroptions : IXHROptions , progressDelegate : ( progressData : IXHRProgress ) => void ) : Promise < XMLHttpRequest > {
59
- if ( XHRDefaults . FetchStream === null ) {
63
+ if ( XHRDefault . FetchStream === null ) {
60
64
throw new Error ( "xhrApi - stub method, must be bootstrapped" ) ;
61
65
}
62
66
@@ -68,7 +72,7 @@ export class XHRDefaults implements IXHRApi {
68
72
}
69
73
70
74
return new Promise ( ( resolve , reject ) => {
71
- this . stream = new XHRDefaults . FetchStream ( xhroptions . url , options ) ;
75
+ this . stream = new XHRDefault . FetchStream ( xhroptions . url , this . getOptions ( options ) ) ;
72
76
73
77
this . stream . on ( "data" , ( chunk ) => {
74
78
//console.log(chunk.toString());
@@ -105,14 +109,20 @@ export class XHRDefaults implements IXHRApi {
105
109
return "default" ;
106
110
}
107
111
108
- constructor ( ) {
112
+ constructor ( fetchOptions : FetchOptions = { } ) {
113
+ this . fetchOptions = fetchOptions ;
109
114
try {
110
115
let fetch = require ( "fetch" ) ;
111
- XHRDefaults . FetchStream = fetch . FetchStream ;
112
- XHRDefaults . fetchUrl = fetch . fetchUrl ;
116
+ XHRDefault . FetchStream = fetch . FetchStream ;
117
+ XHRDefault . fetchUrl = fetch . fetchUrl ;
113
118
}
114
119
catch ( e ) { }
115
120
}
121
+
122
+ private getOptions ( opts : FetchOptions ) {
123
+ let headers = Object . assign ( { } , ( XHRDefault . defaultOptions || { } ) . headers , ( this . fetchOptions || { } ) . headers , ( opts || { } ) . headers )
124
+ return Object . assign ( { } , XHRDefault . defaultOptions , this . fetchOptions , opts , { headers } ) ;
125
+ }
116
126
}
117
127
118
128
/** @internal */
@@ -144,4 +154,47 @@ function setupXhrResponse(xhrResponse: XMLHttpRequest): XMLHttpRequest {
144
154
}
145
155
146
156
return xhrResponse ;
147
- }
157
+ }
158
+
159
+ export interface xFetchOptions {
160
+ /** how many redirects allowed, defaults to 10 */
161
+ maxRedirects : number ;
162
+ /** set to true if redirects are not allowed, defaults to false */
163
+ disableRedirects : boolean ;
164
+ /** optional header fields, in the form of {'Header-Field':'value'} */
165
+ headers : { [ key : string ] : ( any ) } ;
166
+ /** maximum allowd length for the file, the remainder is cut off. Defaults to Infinity */
167
+ maxResponseLength : number ;
168
+ /** defaults to GET */
169
+ method : string ;
170
+ /** request body */
171
+ payload : string ;
172
+ /** set to false, to disable content gzipping, needed for Node v0.5.9 which has buggy zlib */
173
+ disableGzip : boolean ;
174
+ /** an array of cookie definitions in the form of ['name=val'] */
175
+ cookies : any ;
176
+ /** for sharing cookies between requests, see below */
177
+ cookieJar : any ;
178
+ /** valid for fetchUrl */
179
+ outputEncoding : string ;
180
+ /** valid for fetchUrl, set to true to disable automatic charset decoding to utf-8 */
181
+ disableDecoding : boolean ;
182
+ /** valid for fetchUrl, set input encoding */
183
+ overrideCharset : string ;
184
+ /** use high performance asyncronous DNS resolution based on c-ares instead of a thread pool calling getaddrinfo(3) */
185
+ asyncDnsLoookup : boolean ;
186
+ /** set a timeout in ms */
187
+ timeout : number ;
188
+ /** pass-through http.request agent parameter for https */
189
+ agentHttps : any ;
190
+ /** pass-through http.request agent parameter for http */
191
+ agentHttp : any ;
192
+ /** pass-through http.request agent parameter as fallback, if agentHttps or agentHttp are not specified */
193
+ agent : any ;
194
+ /** whether to reject self-signed certificates (true, default behavior), or ignore and allow them (false) */
195
+ rejectUnauthorized : boolean ;
196
+ /** is the username for Basic auth */
197
+ user : string ;
198
+ /** is the password for Basic auth */
199
+ pass : string ;
200
+ }
0 commit comments