Skip to content

Commit

Permalink
feat(TS): add types for composed dispatchers (nodejs#2967)
Browse files Browse the repository at this point in the history
* feat: add types for composed dispatchers

* Update types/dispatcher.d.ts

* refactor: adjust

---------

Co-authored-by: Matteo Collina <[email protected]>
  • Loading branch information
2 people authored and Mert Can Altin committed Mar 26, 2024
1 parent fd3ce8e commit 07647db
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
6 changes: 6 additions & 0 deletions test/types/dispatcher.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ expectAssignable<Dispatcher>(new Dispatcher())

declare const { body }: Dispatcher.ResponseData;

// compose
{
expectAssignable<Dispatcher.ComposedDispatcher>(new Dispatcher().compose(new Dispatcher().dispatch, new Dispatcher().dispatch))
expectAssignable<Dispatcher.ComposedDispatcher>(new Dispatcher().compose([new Dispatcher().dispatch, new Dispatcher().dispatch]))
}

{
// body mixin tests
expectType<never | undefined>(body.body)
Expand Down
5 changes: 5 additions & 0 deletions types/dispatcher.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ declare class Dispatcher extends EventEmitter {
/** Starts two-way communications with the requested resource. */
connect(options: Dispatcher.ConnectOptions): Promise<Dispatcher.ConnectData>;
connect(options: Dispatcher.ConnectOptions, callback: (err: Error | null, data: Dispatcher.ConnectData) => void): void;
/** Compose a chain of dispatchers */
compose(dispatchers: Dispatcher['dispatch'][]): Dispatcher.ComposedDispatcher;
compose(...dispatchers: Dispatcher['dispatch'][]): Dispatcher.ComposedDispatcher;
/** Performs an HTTP request. */
request(options: Dispatcher.RequestOptions): Promise<Dispatcher.ResponseData>;
request(options: Dispatcher.RequestOptions, callback: (err: Error | null, data: Dispatcher.ResponseData) => void): void;
Expand Down Expand Up @@ -93,6 +96,8 @@ declare class Dispatcher extends EventEmitter {
}

declare namespace Dispatcher {
export interface ComposedDispatcher extends Dispatcher {}
export type DispatcherInterceptor = (dispatch: Dispatcher['dispatch']) => Dispatcher['dispatch'];
export interface DispatchOptions {
origin?: string | URL;
path: string;
Expand Down

0 comments on commit 07647db

Please sign in to comment.