Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type Definition for Interceptors incorrect #2268

Closed
ComradeCow opened this issue Sep 18, 2023 · 1 comment
Closed

Type Definition for Interceptors incorrect #2268

ComradeCow opened this issue Sep 18, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@ComradeCow
Copy link
Contributor

Bug Description

The type definition for defining Client interceptors doesn't match the documentation. The Client constructor is expecting an array of Dispatchers, not the interceptor functions.

Reproducible By

Install TypeScript 5.2.2, undici 5.24.0.

import { Client, Dispatcher } from "undici";

const insertHeaderInterceptor = dispatch => {
  return function InterceptedDispatch(opts, handler){
    opts.headers.push('Authorization', 'Bearer [Some token]')
    return dispatch(opts, handler)
  }
}

const client = new Client('https://localhost:3000', {
  interceptors: { Client: [insertHeaderInterceptor] } // error here
})

The error returned by TypeScript:

src/lib/quote/try.ts:13:28 - error TS2322: Type '(dispatch: (options: DispatchOptions, handler: DispatchHandlers) => boolean) => (opts: DispatchOptions, handler: DispatchHandlers) => boolean' is not assignable to type 'Dispatcher'.

13   interceptors: { Client: [insertHeaderInterceptor] },
                              ~~~~~~~~~~~~~~~~~~~~~~~

I also tried:

import { Client, Dispatcher } from "undici";

// the change from above is explicitly defining the type for the interceptor to match
// the intent
const insertHeaderInterceptor: Dispatcher.DispatchInterceptor = (
  dispatch: Dispatcher["dispatch"]
) => {
  return function InterceptedDispatch(
    opts: Dispatcher.DispatchOptions,
    handler: Dispatcher.DispatchHandlers
  ) {
    return dispatch(opts, handler);
  };
};

const client = new Client("https://localhost:3000", {
  interceptors: { Client: [insertHeaderInterceptor] },
});

But the error is the same:

src/lib/quote/try.ts:15:28 - error TS2322: Type 'DispatchInterceptor' is not assignable to type 'Dispatcher'.

15   interceptors: { Client: [insertHeaderInterceptor] },
                              ~~~~~~~~~~~~~~~~~~~~~~~

Expected Behavior

The code sample should successfully type check.

Environment

Windows 11 / WSL - Ubuntu 20.04 / Node v20.6.1 / TypeScript 5.2.2

Additional context

It looks like this bug comes from using the default import multiple times in types/client.d.ts - that imports the Dispatcher class twice, not the DispatchInterceptor interface.

@ComradeCow ComradeCow added the bug Something isn't working label Sep 18, 2023
@KhafraDev
Copy link
Member

thank you for the PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants