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
Provenance
- Commit
0f2f00a- Tree
dc3f9be- Author
Sindre Sorhus <sindresorhus@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
b7572d0
2 files changed+45 added−4 removed
modified:source/core/Ky.ts+14 added-4 removed
43 unmodified lines44 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 );51 52 if (modifiedResponse instanceof globalThis.Response) {3 unmodified lines56 57 ky._decorateResponse(response);58 59 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());61 62 for (const hook of ky._options.hooks.beforeError) {63 // eslint-disable-next-line no-await-in-loop64 error = await hook(error);91 unmodified lines156 protected _retryCount = 0;157 protected _input: Input;158 protected _options: InternalOptions;159 protected _originalRequest?: Request;160+ #cachedNormalizedOptions?: NormalizedOptions;161 162 // eslint-disable-next-line complexity163 constructor(input: Input, options: Options = {}) {164 this._input = input;155 unmodified lines320 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 lines340 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 );347 348 if (result instanceof Request) {17 unmodified lines366 }367 368 return timeout(this._originalRequest, nonRequestOptions, this.abortController, this._options as TimeoutOptions);369 }370+371+ #getNormalizedOptions(): NormalizedOptions {372+ if (!this.#cachedNormalizedOptions) {373+ const {hooks, ...normalizedOptions} = this._options;374+ this.#cachedNormalizedOptions = Object.freeze(normalizedOptions) as NormalizedOptions;375+ }376+377+ return this.#cachedNormalizedOptions;378+ }379 }modified:test/hooks.ts+31 added
718 unmodified lines719 t.is(result, 'token refreshed-token');720 721 await server.close();722 });723+724+test('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+ });729+730+ await ky.get(server.url, {731+ hooks: {732+ beforeRequest: [733+ (_request, options) => {734+ // Verify hooks field is not present735+ t.false('hooks' in options);736+737+ // Verify options object is frozen (can't add/modify properties)738+ t.throws(() => {739+ // @ts-expect-error - Testing freeze behavior740+ options.newProperty = 'test';741+ });742+743+ // Verify nested objects like headers are still mutable744+ t.notThrows(() => {745+ options.headers.set('X-Test', 'value');746+ });747+ },748+ ],749+ },750+ });751+752+ await server.close();753+});