diff --git a/source/core/Ky.ts b/source/core/Ky.ts index b8f43949..a14ea1ba 100644 --- a/source/core/Ky.ts +++ b/source/core/Ky.ts @@ -169,13 +169,10 @@ export class Ky { if (supportsAbortController) { this.abortController = new globalThis.AbortController(); - if (this._options.signal) { - const originalSignal = this._options.signal; - - this._options.signal.addEventListener('abort', () => { - this.abortController!.abort(originalSignal.reason); - }); - } + const originalSignal = this._options.signal ?? (this._input as Request).signal; + originalSignal?.addEventListener('abort', () => { + this.abortController!.abort(originalSignal.reason); + }); this._options.signal = this.abortController.signal; } diff --git a/test/main.ts b/test/main.ts index a830ae41..4ea3a231 100644 --- a/test/main.ts +++ b/test/main.ts @@ -655,6 +655,23 @@ test('throws DOMException/Error with name AbortError when aborted by user', asyn t.is(error.name, 'AbortError', `Expected AbortError, got ${error.name}`); }); +test('throws AbortError when aborted via Request', async t => { + const server = await createHttpTestServer(); + // eslint-disable-next-line @typescript-eslint/no-empty-function + server.get('/', () => {}); + + const abortController = new AbortController(); + const {signal} = abortController; + const request = new Request(server.url, {signal}); + const response = ky(request); + abortController.abort(); + + const error = (await t.throwsAsync(response))!; + + t.true(['DOMException', 'Error'].includes(error.constructor.name), `Expected DOMException or Error, got ${error.constructor.name}`); + t.is(error.name, 'AbortError', `Expected AbortError, got ${error.name}`); +}); + test('supports Request instance as input', async t => { const server = await createHttpTestServer(); const inputRequest = new Request(server.url, {method: 'POST'});