@@ -3,24 +3,41 @@ import {
33 type ActionDispatcher ,
44 type ActionDispatcherHook ,
55 type Http ,
6+ type HttpResponse ,
67} from "../../interfaces/index.js" ;
8+ import { type Paginator } from "../../mastodon/paginator.js" ;
79import { PaginatorHttp } from "./paginator-http.js" ;
810
9- export type HttpActionType = "fetch" | "create" | "update" | "remove" | "list" ;
11+ export type HttpActionMap = {
12+ fetch : Promise < HttpResponse < unknown > > ;
13+ create : Promise < HttpResponse < unknown > > ;
14+ update : Promise < HttpResponse < unknown > > ;
15+ remove : Promise < HttpResponse < unknown > > ;
16+ list : Paginator < unknown > ;
17+ } ;
18+
19+ export type HttpActionType = keyof HttpActionMap ;
1020export type HttpAction = Action < HttpActionType > ;
1121
12- export class HttpActionDispatcher implements ActionDispatcher < HttpAction > {
22+ export class HttpActionDispatcher implements ActionDispatcher < HttpActionMap > {
1323 constructor (
1424 private readonly http : Http ,
1525 private readonly hook : ActionDispatcherHook < HttpAction > ,
1626 ) { }
1727
18- dispatch < T > ( action : HttpAction ) : T | Promise < T > {
28+ dispatch (
29+ action : Action < "fetch" | "create" | "update" | "remove" > ,
30+ ) : Promise < HttpResponse < unknown > > ;
31+ dispatch ( action : Action < "list" > ) : Paginator < unknown > ;
32+ dispatch ( action : Action < HttpActionType > ) : unknown {
1933 if ( this . hook ) {
2034 action = this . hook . beforeDispatch ( action ) ;
2135 }
2236
23- let result = this . hook . dispatch ( action ) as T | Promise < T > | false ;
37+ let result = this . hook . dispatch ( action ) as
38+ | Promise < HttpResponse < unknown > >
39+ | Paginator < unknown >
40+ | false ;
2441 if ( result !== false ) {
2542 return result ;
2643 }
@@ -43,16 +60,16 @@ export class HttpActionDispatcher implements ActionDispatcher<HttpAction> {
4360 break ;
4461 }
4562 case "list" : {
46- result = new PaginatorHttp ( this . http , action . path , action . data ) as T ;
63+ result = new PaginatorHttp ( this . http , action . path , action . data ) ;
4764 break ;
4865 }
4966 }
5067
5168 /* eslint-disable unicorn/prefer-ternary, prettier/prettier */
5269 if ( result instanceof Promise ) {
53- return result . then ( ( result ) => this . hook ?. afterDispatch ( action , result ) ) as Promise < T > ;
70+ return result . then ( ( result ) => this . hook ?. afterDispatch ( action , result ) ) as Promise < HttpResponse < unknown > > ;
5471 } else {
55- return this . hook . afterDispatch ( action , result ) as T ;
72+ return this . hook . afterDispatch ( action , result ) as Paginator < unknown > ;
5673 }
5774 /* eslint-enable unicorn/prefer-ternary, prettier/prettier */
5875 }
0 commit comments