Skip to content

NormalizedOptions in afterResponse hook has hooks #726

@LuiGeeDev

Description

@LuiGeeDev
let accessToken = undefined;

const apiClient = ky.extend({
...
  beforeRequest: [async (request) => {
    if (accessToken) request.headers.set('Authorization', `Bearer ${accessToken}`);
  }], 
  afterResponse: [async (request, options) => {
  ... 
    refreshPromise = ky.post('/refresh').json().then(data => { accessToken = data.accessToken });
    await refreshPromise;
    return ky(request, options); // This runs beforeRequest hooks.
  }]
})

I wondered why the access token is set when I use ky instance, not the instance I made, and I found that options in afterResponse hook has hooks from the original request. Not sure this is intended, since it is declared as NormarlizedOptions and it doesn't have hooks in it. I think the type declaration or the options passed to hooks should be changed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions