Skip to content

Commit

Permalink
feat: miniget now accepts a URL object as the first parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
fent committed Jan 24, 2021
1 parent c601b0e commit 73695a8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ miniget('http://api.mywebsite.com/v1/messages.json')

### miniget(url, [options])

Makes a GET request. `options` can have any properties from the [`http.request()` function](https://nodejs.org/api/http.html#http_http_request_options_callback), in addition to
Makes a GET request. `url` can be a string or a `URL` object. `options` can have any properties from the [`http.request()` function](https://nodejs.org/api/http.html#http_http_request_options_callback), in addition to

* `maxRedirects` - Default is `10`.
* `maxRetries` - Number of times to retry the request if there is a 500 or connection error. Default is `2`.
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Miniget.defaultOptions = {
backoff: { inc: 100, max: 10000 },
};

function Miniget(url: string, options: Miniget.Options = {}): Miniget.Stream {
function Miniget(url: string | URL, options: Miniget.Options = {}): Miniget.Stream {
const opts: Miniget.DefaultOptions = Object.assign({}, Miniget.defaultOptions, options);
const stream = new PassThrough({ highWaterMark: opts.highWaterMark }) as Miniget.Stream;
stream.destroyed = stream.aborted = false;
Expand Down Expand Up @@ -148,7 +148,7 @@ function Miniget(url: string, options: Miniget.Options = {}): Miniget.Stream {
const doDownload = () => {
let parsed: RequestOptions = {}, httpLib;
try {
let urlObj = new URL(url);
let urlObj = typeof url === 'string' ? new URL(url) : url;
parsed = Object.assign({}, {
host: urlObj.host,
hostname: urlObj.hostname,
Expand Down
12 changes: 12 additions & 0 deletions test/request-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import zlib from 'zlib';
import assert from 'assert';
import { Transform } from 'stream';
import { IncomingMessage, ClientRequest, RequestOptions } from 'http';
import { URL } from 'url';

import miniget from '../dist';

Expand Down Expand Up @@ -155,6 +156,17 @@ describe('Make a request', () => {
});
});

describe('with URL object passed', () => {
it('Creates request and gets correct response', async() => {
const scope = nock('http://webby.com')
.get('/pathos')
.replyWithFile(200, __filename);
let body = await miniget(new URL('http://webby.com/pathos')).text();
scope.done();
assert.ok(body.length > 100);
});
});

describe('with an incorrect URL', () => {
it('Emits error', done => {
miniget('file:///path/to/file/').on('error', err => {
Expand Down

0 comments on commit 73695a8

Please sign in to comment.