From 9589701d46acdcdc7e2ccf9214145c60f5581330 Mon Sep 17 00:00:00 2001 From: fent <933490+fent@users.noreply.github.com> Date: Sun, 23 Feb 2020 17:54:53 -0500 Subject: [PATCH] feat: add promise mode you can now do `let [res, body] = await miniget.promise(url)` --- README.md | 6 +++++- src/index.ts | 13 +++++++++++++ test/request-test.ts | 25 +++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c681d0d..0763b7b 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,11 @@ Makes a GET request. `options` can have any properties from the [`http.request() ``` Given encodings will be added to the `Accept-Encoding` header, and the response will be decoded if the server responds with encoded content. -If `callback` is given, will concatenate the response, and call `callback` with a possible error, the response, and the response body. +If `callback` is given, will concatenate the response, and call `callback` with a possible error, the response, and the response body. If you'd like a concatenated response, but want to use `await` instead, you can call `miniget.promise()`. + +```js +let [res, body] = await miniget.promise('http://yourwebsite.com'); +``` Miniget returns a readable stream if `callback` is not given, errors will then be emitted on the stream. Returned stream also contains an `.abort()` method, and can emit the following events. diff --git a/src/index.ts b/src/index.ts index 65eba55..44108df 100644 --- a/src/index.ts +++ b/src/index.ts @@ -232,4 +232,17 @@ function Miniget(url: string, options?: Miniget.Options | Callback, callback?: C return callback ? null : stream; } +// istanbul ignore next +// https://github.com/istanbuljs/nyc/issues/1209 +namespace Miniget { + export const promise = (url: string, options?: Options): Promise<[IncomingMessage, string]> => { + return new Promise((resolve, reject) => { + Miniget(url, options, (err, res, body) => { + if (err) return reject(err); + resolve([res, body]); + }); + }); + }; +} + export = Miniget; diff --git a/test/request-test.ts b/test/request-test.ts index df1fec3..d27e198 100644 --- a/test/request-test.ts +++ b/test/request-test.ts @@ -79,6 +79,31 @@ describe('Make a request', () => { }); }); + describe('with a promise', () => { + it('Gives contents of page', async () => { + const scope = nock('http://webby.com') + .get('/pathos') + .replyWithFile(200, __filename); + let [res, body] = await miniget.promise('http://webby.com/pathos'); + scope.done(); + assert.equal(res.statusCode, 200); + assert.ok(body.length > 100); + }); + + describe('that errors', () => { + it('error is caught', async () => { + const scope = nock('http://something.com') + .get('/one/two/three') + .replyWithError('NONONO'); + await assert.rejects( + miniget.promise('http://something.com/one/two/three', { maxRetries: 0 }), + null, 'NONONO' + ); + scope.done(); + }); + }); + }); + describe('that errors', () => { it('Emits error event', (done) => { const scope = nock('https://mysite.com')