Skip to content
Myr

Fix `hooks` field incorrectly included in `NormalizedOptions` (#726) (#756)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Sindre Sorhus · committed by GitHub · parent b7572d0 · Browse files · 2 files+45 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+45 added−4 removed

  • tests:+31 added−0 removed
  • other:+14 added−4 removed

2 files+45 added−4 removed

modified:source/core/Ky.ts+14 added-4 removed
43 unmodified lines
44 for (const hook of ky._options.hooks.afterResponse) {45 // eslint-disable-next-line no-await-in-loop46 const modifiedResponse = await hook(47 ky.request,48 ky._options as NormalizedOptions,48 ky.#getNormalizedOptions(),49 ky._decorateResponse(response.clone()),50 );5152 if (modifiedResponse instanceof globalThis.Response) {
3 unmodified lines
5657 ky._decorateResponse(response);5859 if (!response.ok && ky._options.throwHttpErrors) {60 let error = new HTTPError(response, ky.request, ky._options as NormalizedOptions);60 let error = new HTTPError(response, ky.request, ky.#getNormalizedOptions());6162 for (const hook of ky._options.hooks.beforeError) {63 // eslint-disable-next-line no-await-in-loop64 error = await hook(error);
91 unmodified lines
156 protected _retryCount = 0;157 protected _input: Input;158 protected _options: InternalOptions;159 protected _originalRequest?: Request;160 #cachedNormalizedOptions?: NormalizedOptions;161162 // eslint-disable-next-line complexity163 constructor(input: Input, options: Options = {}) {164 this._input = input;
155 unmodified lines
320 for (const hook of this._options.hooks.beforeRetry) {321 // eslint-disable-next-line no-await-in-loop322 const hookResult = await hook({323 request: this.request,323 options: (this._options as unknown) as NormalizedOptions,324 options: this.#getNormalizedOptions(),325 error: error as Error,326 retryCount: this._retryCount,327 });328
11 unmodified lines
340 for (const hook of this._options.hooks.beforeRequest) {341 // eslint-disable-next-line no-await-in-loop342 const result = await hook(343 this.request,343 (this._options as unknown) as NormalizedOptions,344 this.#getNormalizedOptions(),345 {retryCount: this._retryCount},346 );347348 if (result instanceof Request) {
17 unmodified lines
366 }367368 return timeout(this._originalRequest, nonRequestOptions, this.abortController, this._options as TimeoutOptions);369 }370371 #getNormalizedOptions(): NormalizedOptions {372 if (!this.#cachedNormalizedOptions) {373 const {hooks, ...normalizedOptions} = this._options;374 this.#cachedNormalizedOptions = Object.freeze(normalizedOptions) as NormalizedOptions;375 }376377 return this.#cachedNormalizedOptions;378 }379}
modified:test/hooks.ts+31 added
718 unmodified lines
719 t.is(result, 'token refreshed-token');720721 await server.close();722});723724test('hooks are not included in normalized options passed to hooks', async t => {725 const server = await createHttpTestServer();726 server.get('/', (_request, response) => {727 response.end('ok');728 });729730 await ky.get(server.url, {731 hooks: {732 beforeRequest: [733 (_request, options) => {734 // Verify hooks field is not present735 t.false('hooks' in options);736737 // Verify options object is frozen (can't add/modify properties)738 t.throws(() => {739 // @ts-expect-error - Testing freeze behavior740 options.newProperty = 'test';741 });742743 // Verify nested objects like headers are still mutable744 t.notThrows(() => {745 options.headers.set('X-Test', 'value');746 });747 },748 ],749 },750 });751752 await server.close();753});