@@ -2,6 +2,7 @@ import * as cacheUtils from './cache-utils';
2
2
import FileCache from './FileCache' ;
3
3
import MemoryCache from './MemoryCache' ;
4
4
import { Callback } from './Callback' ;
5
+ import { CachedResponse } from './CachedResponse' ;
5
6
import {
6
7
ClientRequest ,
7
8
IncomingMessage ,
@@ -16,6 +17,7 @@ import { parse as parseUrl, resolve as resolveUrl } from 'url';
16
17
import { PassThrough } from 'stream' ;
17
18
import { request as requestHttps } from 'https' ;
18
19
import Response = require( 'http-response-object' ) ;
20
+ import { URL } from 'url' ;
19
21
20
22
const caseless = require ( 'caseless' ) ;
21
23
const fileCache = new FileCache ( __dirname + '/cache' ) ;
@@ -30,9 +32,9 @@ function requestProtocol(protocol: string, options: RequestOptions, callback?: (
30
32
throw new Error ( 'Unsupported protocol ' + protocol ) ;
31
33
}
32
34
33
- function request ( method : HttpVerb , url : string , options : Options | null | void , callback : Callback ) : void | NodeJS . WritableStream ;
34
- function request ( method : HttpVerb , url : string , callback : Callback ) : void | NodeJS . WritableStream ;
35
- function request ( method : HttpVerb , url : string , options ?: Options | Callback | null , callback ?: Callback | null ) : void | NodeJS . WritableStream {
35
+ function request ( method : HttpVerb , url : string | URL , options : Options | null | void , callback : Callback ) : void | NodeJS . WritableStream ;
36
+ function request ( method : HttpVerb , url : string | URL , callback : Callback ) : void | NodeJS . WritableStream ;
37
+ function request ( method : HttpVerb , url : string | URL , options ?: Options | Callback | null | void , callback ?: Callback | null ) : void | NodeJS . WritableStream {
36
38
if ( typeof options === 'function' ) {
37
39
callback = options ;
38
40
options = null ;
@@ -46,15 +48,17 @@ function request(method: HttpVerb, url: string, options?: Options | Callback | n
46
48
if ( typeof callback !== 'function' ) {
47
49
throw new TypeError ( 'callback must be a function' ) ;
48
50
}
49
- return _request ( method , url , options , callback ) ;
51
+ return _request ( method , (
52
+ ( url && typeof url === 'object' ) ? url . href : url
53
+ ) , options , callback ) ;
50
54
}
51
55
function _request ( method : HttpVerb , url : string , options : Options , callback : Callback ) : void | NodeJS . WritableStream {
52
56
const start = Date . now ( ) ;
53
57
if ( typeof method !== 'string' ) {
54
58
throw new TypeError ( 'The method must be a string.' ) ;
55
59
}
56
60
if ( typeof url !== 'string' ) {
57
- throw new TypeError ( 'The URL/path must be a string.' ) ;
61
+ throw new TypeError ( 'The URL/path must be a string or a URL object .' ) ;
58
62
}
59
63
60
64
method = ( method . toUpperCase ( ) as any ) ;
@@ -294,7 +298,7 @@ function _request(method: HttpVerb, url: string, options: Options, callback: Cal
294
298
if ( timeout !== null ) clearTimeout ( timeout ) ;
295
299
var result = new Response ( res . statusCode || 0 , res . headers , res , url ) ;
296
300
if ( cache && unsafe && res . statusCode && res . statusCode < 400 ) {
297
- ( cache as ICache ) . invalidateResponse ( url , ( err : Error ) => {
301
+ ( cache as ICache ) . invalidateResponse ( url , ( err : Error | null ) => {
298
302
if ( err && ! ignoreFailedInvalidation ) {
299
303
callback ( new Error ( 'Error invalidating the cache for' + url + ': ' + err . message ) , result ) ;
300
304
} else {
@@ -341,4 +345,14 @@ function isRedirect(statusCode: number): boolean {
341
345
return statusCode === 301 || statusCode === 302 || statusCode === 303 || statusCode === 307 || statusCode === 308 ;
342
346
}
343
347
344
- export = request ;
348
+ export default request ;
349
+ export { HttpVerb } ;
350
+ export { Options } ;
351
+ export { Callback } ;
352
+ export { Response } ;
353
+ export { CachedResponse } ;
354
+ export { ICache } ;
355
+
356
+ module . exports = request ;
357
+ module . exports . default = request ;
358
+ module . exports . Response = Response ;
0 commit comments