Skip to content

Commit e58c3ed

Browse files
Expose types and support using the new URL class
1 parent 46a202c commit e58c3ed

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"@types/node": "^7.0.31",
2525
"caseless": "~0.12.0",
2626
"concat-stream": "^1.4.6",
27-
"http-response-object": "^3.0.0",
27+
"http-response-object": "^3.0.1",
2828
"parse-cache-control": "^1.0.1"
2929
},
3030
"devDependencies": {

src/Headers.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
export type Headers = {
2-
readonly [name: string]: string | string[],
3-
};
1+
import { IncomingHttpHeaders } from 'http';
2+
export type Headers = IncomingHttpHeaders;

src/index.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as cacheUtils from './cache-utils';
22
import FileCache from './FileCache';
33
import MemoryCache from './MemoryCache';
44
import { Callback } from './Callback';
5+
import {CachedResponse} from './CachedResponse';
56
import {
67
ClientRequest,
78
IncomingMessage,
@@ -16,6 +17,7 @@ import { parse as parseUrl, resolve as resolveUrl } from 'url';
1617
import { PassThrough } from 'stream';
1718
import { request as requestHttps } from 'https';
1819
import Response = require('http-response-object');
20+
import {URL} from 'url';
1921

2022
const caseless = require('caseless');
2123
const fileCache = new FileCache(__dirname + '/cache');
@@ -30,9 +32,9 @@ function requestProtocol(protocol: string, options: RequestOptions, callback?: (
3032
throw new Error('Unsupported protocol ' + protocol);
3133
}
3234

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 {
3638
if (typeof options === 'function') {
3739
callback = options;
3840
options = null;
@@ -46,15 +48,17 @@ function request(method: HttpVerb, url: string, options?: Options | Callback | n
4648
if (typeof callback !== 'function') {
4749
throw new TypeError('callback must be a function');
4850
}
49-
return _request(method, url, options, callback);
51+
return _request(method, (
52+
(url && typeof url === 'object') ? url.href : url
53+
), options, callback);
5054
}
5155
function _request(method: HttpVerb, url: string, options: Options, callback: Callback): void | NodeJS.WritableStream {
5256
const start = Date.now();
5357
if (typeof method !== 'string') {
5458
throw new TypeError('The method must be a string.');
5559
}
5660
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.');
5862
}
5963

6064
method = (method.toUpperCase() as any);
@@ -294,7 +298,7 @@ function _request(method: HttpVerb, url: string, options: Options, callback: Cal
294298
if (timeout !== null) clearTimeout(timeout);
295299
var result = new Response(res.statusCode || 0, res.headers, res, url);
296300
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) => {
298302
if (err && !ignoreFailedInvalidation) {
299303
callback(new Error('Error invalidating the cache for' + url + ': ' + err.message), result);
300304
} else {
@@ -341,4 +345,14 @@ function isRedirect(statusCode: number): boolean {
341345
return statusCode === 301 || statusCode === 302 || statusCode === 303 || statusCode === 307 || statusCode === 308;
342346
}
343347

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

Comments
 (0)