microsoft / kiota-typescript

TypeScript libraries for Kiota-generated API clients.
https://aka.ms/kiota/docs
MIT License
37 stars 31 forks source link

How do I create a request interceptor/middleware? #1492

Open illunix opened 1 week ago

illunix commented 1 week ago

How do I create a request interceptor/middleware?

baywet commented 6 days ago

Here is an example of one, there are more in the parent directory.

https://github.com/microsoft/kiota-typescript/blob/ecebb900ea828df6851a137f474a7202c474d94f/packages/http/fetch/src/middlewares/retryHandler.ts#L25

Let us know if you have any additional comments or questions.

illunix commented 6 days ago

Any idea why it does not include this header in actual request but it shows in console? @baywet


  public async execute(
    url: string,
    reqInit: RequestInit,
    reqOptions?: Record<string, RequestOption>
  ): Promise<FetchResponse> {
    return this.executeInternal(url, reqInit as FetchRequestInit);
  }

  private async executeInternal(
    url: string,
    reqInit: FetchRequestInit
  ): Promise<Response> {
    setRequestHeader(reqInit as FetchRequestInit, 'Test', `Test`);

    console.log(reqInit as RequestInit);

    const res = await this.next?.execute(url, reqInit as RequestInit);
    if (!res) {
      throw new Error('Response is undefined');
    }

    return res;
  }

Image Image

baywet commented 5 days ago

Can you check what versions of the packages are actually installed and upgrade to the latest please? There used to be a a similar bug we fixed in #1358

microsoft-github-policy-service[bot] commented 11 hours ago

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.