diff --git a/source/core/Ky.ts b/source/core/Ky.ts index 63e778ac..92f05f37 100644 --- a/source/core/Ky.ts +++ b/source/core/Ky.ts @@ -168,13 +168,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 013b4bdf..f7c1ad56 100644 --- a/test/main.ts +++ b/test/main.ts @@ -711,6 +711,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'});