From 07647db4ef2a4f7ed515ad134d092320c51b31b3 Mon Sep 17 00:00:00 2001 From: Carlos Fuentes Date: Wed, 20 Mar 2024 14:44:44 +0100 Subject: [PATCH] feat(TS): add types for composed dispatchers (#2967) * feat: add types for composed dispatchers * Update types/dispatcher.d.ts * refactor: adjust --------- Co-authored-by: Matteo Collina --- test/types/dispatcher.test-d.ts | 6 ++++++ types/dispatcher.d.ts | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/test/types/dispatcher.test-d.ts b/test/types/dispatcher.test-d.ts index 275d88c4af9..245ee218d59 100644 --- a/test/types/dispatcher.test-d.ts +++ b/test/types/dispatcher.test-d.ts @@ -127,6 +127,12 @@ expectAssignable(new Dispatcher()) declare const { body }: Dispatcher.ResponseData; +// compose +{ + expectAssignable(new Dispatcher().compose(new Dispatcher().dispatch, new Dispatcher().dispatch)) + expectAssignable(new Dispatcher().compose([new Dispatcher().dispatch, new Dispatcher().dispatch])) +} + { // body mixin tests expectType(body.body) diff --git a/types/dispatcher.d.ts b/types/dispatcher.d.ts index 08c59ca0718..e632e0e921a 100644 --- a/types/dispatcher.d.ts +++ b/types/dispatcher.d.ts @@ -18,6 +18,9 @@ declare class Dispatcher extends EventEmitter { /** Starts two-way communications with the requested resource. */ connect(options: Dispatcher.ConnectOptions): Promise; 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; request(options: Dispatcher.RequestOptions, callback: (err: Error | null, data: Dispatcher.ResponseData) => void): void; @@ -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;