Skip to content

Commit c6c4389

Browse files
committed
Strip Ky-specific properties from normalized options passed to hooks
They were already stripped in types, but not in JS. See #762
1 parent ecdd45e commit c6c4389

File tree

3 files changed

+31
-46
lines changed

3 files changed

+31
-46
lines changed

source/core/Ky.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,19 @@ export class Ky {
623623

624624
#getNormalizedOptions(): NormalizedOptions {
625625
if (!this.#cachedNormalizedOptions) {
626-
const {hooks, ...normalizedOptions} = this.#options;
626+
// Exclude Ky-specific options that are not part of `RequestInit`.
627+
const {
628+
hooks,
629+
json,
630+
parseJson,
631+
stringifyJson,
632+
searchParams,
633+
timeout,
634+
throwHttpErrors,
635+
fetch,
636+
...normalizedOptions
637+
} = this.#options;
638+
627639
this.#cachedNormalizedOptions = Object.freeze(normalizedOptions) as NormalizedOptions;
628640
}
629641

test/hooks.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {setTimeout as delay} from 'node:timers/promises';
22
import test from 'ava';
33
import ky, {HTTPError, isHTTPError, isForceRetryError} from '../source/index.js';
4-
import {type Options} from '../source/types/options.js';
54
import {createHttpTestServer} from './helpers/create-http-test-server.js';
65

76
const createStreamBody = (text: string) => new ReadableStream<Uint8Array>({
@@ -430,7 +429,7 @@ test('`afterResponse` hook is called with request, normalized options, and respo
430429
return ky(request, {
431430
...options,
432431
json: {
433-
...(options as Options).json as Record<string, unknown>,
432+
...json,
434433
token: 'valid:token',
435434
},
436435
});
@@ -932,18 +931,31 @@ test('beforeRequest hook receives retryCount parameter', async t => {
932931
t.is(result, 'token refreshed-token');
933932
});
934933

935-
test('hooks are not included in normalized options passed to hooks', async t => {
934+
test('Ky-specific options are not included in normalized options passed to hooks', async t => {
936935
const server = await createHttpTestServer(t);
937-
server.get('/', (_request, response) => {
936+
server.post('/', (_request, response) => {
938937
response.end('ok');
939938
});
940939

941-
await ky.get(server.url, {
940+
await ky.post(server.url, {
941+
json: {key: 'value'},
942+
searchParams: {foo: 'bar'},
943+
parseJson: JSON.parse,
944+
stringifyJson: JSON.stringify,
945+
timeout: 5000,
946+
throwHttpErrors: false,
942947
hooks: {
943948
beforeRequest: [
944949
({options}) => {
945-
// Verify hooks field is not present
950+
// Verify Ky-specific properties are not present
946951
t.false('hooks' in options);
952+
t.false('json' in options);
953+
t.false('parseJson' in options);
954+
t.false('stringifyJson' in options);
955+
t.false('searchParams' in options);
956+
t.false('timeout' in options);
957+
t.false('throwHttpErrors' in options);
958+
t.false('fetch' in options);
947959

948960
// Verify options object is frozen (can't add/modify properties)
949961
t.throws(() => {

test/main.ts

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -525,45 +525,6 @@ test('throwHttpErrors function - selective error handling', async t => {
525525
);
526526
});
527527

528-
test('throwHttpErrors preserves original type in hooks', async t => {
529-
const server = await createHttpTestServer(t);
530-
531-
server.get('/', (_request, response) => {
532-
response.sendStatus(200);
533-
});
534-
535-
// Test that boolean is preserved
536-
let booleanTypeInHook: unknown;
537-
await ky.get(server.url, {
538-
throwHttpErrors: false,
539-
hooks: {
540-
beforeRequest: [
541-
({options}) => {
542-
booleanTypeInHook = options.throwHttpErrors;
543-
},
544-
],
545-
},
546-
});
547-
t.is(typeof booleanTypeInHook, 'boolean');
548-
t.is(booleanTypeInHook, false);
549-
550-
// Test that function is preserved
551-
let functionTypeInHook: unknown;
552-
const throwFunction = (status: number) => status >= 500;
553-
await ky.get(server.url, {
554-
throwHttpErrors: throwFunction,
555-
hooks: {
556-
beforeRequest: [
557-
({options}) => {
558-
functionTypeInHook = options.throwHttpErrors;
559-
},
560-
],
561-
},
562-
});
563-
t.is(typeof functionTypeInHook, 'function');
564-
t.is(functionTypeInHook, throwFunction);
565-
});
566-
567528
test('ky.create()', async t => {
568529
const server = await createHttpTestServer(t);
569530
server.get('/', (request, response) => {

0 commit comments

Comments
 (0)