From d93457fba4367c4b8dff6d307845b31cefb2f0dd Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Thu, 29 Aug 2019 17:54:59 +0100 Subject: [PATCH 1/4] feat: add support to return Response from beforeRequest --- index.js | 5 ++++- readme.md | 2 ++ test/hooks.js | 12 ++++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index b1c447f3..2604f129 100644 --- a/index.js +++ b/index.js @@ -383,7 +383,10 @@ class Ky { async _fetch() { for (const hook of this._hooks.beforeRequest) { // eslint-disable-next-line no-await-in-loop - await hook(this._input, this._options); + const hookOutput = await hook(this._input, this._options); + if (hookOutput instanceof Response) { + return hookOutput; + } } if (this._timeout === false) { diff --git a/readme.md b/readme.md index 81b13af0..93ced67f 100644 --- a/readme.md +++ b/readme.md @@ -226,6 +226,8 @@ Default: `[]` This hook enables you to modify the request right before it is sent. Ky will make no further changes to the request after this. The hook function receives normalized input and options as arguments. You could, for example, modify `options.headers` here. +A [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response) can be returned from a beforeRequest hook and completely avoid making an HTTP request. This can be used to mock a request, check an internal cache, etc. An **IMPORTANT** consideration when returning a Response from this hook is that, all the following hooks will be skipped, so be **sure to only return a Response from the last one**. + Note that the argument order has changed in non-backward compatible way since [#163](https://github.com/sindresorhus/ky/pull/163). ###### hooks.afterResponse diff --git a/test/hooks.js b/test/hooks.js index 421f54d4..b70d0998 100644 --- a/test/hooks.js +++ b/test/hooks.js @@ -291,3 +291,15 @@ test.failing('`afterResponse` hook is called with input, normalized options, and await server.close(); }); + +test('hooks beforeRequest returning Response skips HTTP Request', async t => { + const expectedResponse = 'empty hook'; + + const response = await ky.get('server.url', {hooks: { + beforeRequest: [ + () => new Response(expectedResponse, {status: 200, statusText: 'OK'}) + ] + }}).text(); + + t.is(response, expectedResponse); +}); From 616315cd21fc2a2f7aa8b3f4e9a19714b646a50a Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Mon, 9 Sep 2019 15:12:06 +0100 Subject: [PATCH 2/4] fix: tweak the description Co-Authored-By: Szymon Marczak <36894700+szmarczak@users.noreply.github.com> --- readme.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 93ced67f..08dd543b 100644 --- a/readme.md +++ b/readme.md @@ -226,7 +226,9 @@ Default: `[]` This hook enables you to modify the request right before it is sent. Ky will make no further changes to the request after this. The hook function receives normalized input and options as arguments. You could, for example, modify `options.headers` here. -A [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response) can be returned from a beforeRequest hook and completely avoid making an HTTP request. This can be used to mock a request, check an internal cache, etc. An **IMPORTANT** consideration when returning a Response from this hook is that, all the following hooks will be skipped, so be **sure to only return a Response from the last one**. +A [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response) instance can be returned from a `beforeRequest` hook to completely avoid making an HTTP request. This is very helpful when mocking a request or using an internal cache. + +**Note:** all the following hooks will be skipped. Note that the argument order has changed in non-backward compatible way since [#163](https://github.com/sindresorhus/ky/pull/163). From 5c19881c2b915d87eee4acabd03bedc09de2e0eb Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Mon, 9 Sep 2019 15:12:18 +0100 Subject: [PATCH 3/4] Update index.js Co-Authored-By: Szymon Marczak <36894700+szmarczak@users.noreply.github.com> --- index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/index.js b/index.js index 2604f129..ffaab0c1 100644 --- a/index.js +++ b/index.js @@ -384,6 +384,7 @@ class Ky { for (const hook of this._hooks.beforeRequest) { // eslint-disable-next-line no-await-in-loop const hookOutput = await hook(this._input, this._options); + if (hookOutput instanceof Response) { return hookOutput; } From 1d5c268fe5d8f68df4d093a00d8a85f14e3a710d Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Mon, 9 Sep 2019 15:13:23 +0100 Subject: [PATCH 4/4] fix: change const name --- index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index ffaab0c1..2c246534 100644 --- a/index.js +++ b/index.js @@ -383,10 +383,10 @@ class Ky { async _fetch() { for (const hook of this._hooks.beforeRequest) { // eslint-disable-next-line no-await-in-loop - const hookOutput = await hook(this._input, this._options); + const result = await hook(this._input, this._options); - if (hookOutput instanceof Response) { - return hookOutput; + if (result instanceof Response) { + return result; } }