1
1
import type { Client } from '../clients/createClient.js'
2
+ import type { PublicActions } from '../clients/decorators/public.js'
3
+ import type { WalletActions } from '../clients/decorators/wallet.js'
4
+ import type { Transport } from '../clients/transports/createTransport.js'
5
+ import type { Account } from '../types/account.js'
6
+ import type { Chain } from '../types/chain.js'
7
+ import type { RpcSchema } from '../types/eip1193.js'
2
8
3
9
/**
4
10
* Retrieves and returns an action from the client (if exists), and falls
@@ -7,20 +13,30 @@ import type { Client } from '../clients/createClient.js'
7
13
* Useful for extracting overridden actions from a client (ie. if a consumer
8
14
* wants to override the `sendTransaction` implementation).
9
15
*/
10
- export function getAction < params extends { } , returnType extends { } > (
11
- client : Client ,
12
- action : ( _ : any , params : params ) => returnType ,
13
- // Some minifiers drop `Function.prototype.name` or can change function
14
- // names so that getting the name by reflection through `action.name` will
15
- // not work.
16
- name : string ,
17
- ) {
18
- type DecoratedClient = Client & {
19
- [ key : string ] : ( params : params ) => returnType
20
- }
16
+ export function getAction <
17
+ transport extends Transport ,
18
+ chain extends Chain | undefined ,
19
+ account extends Account | undefined ,
20
+ rpcSchema extends RpcSchema | undefined ,
21
+ extended extends { [ key : string ] : unknown } ,
22
+ client extends Client < transport , chain , account , rpcSchema , extended > ,
23
+ parameters ,
24
+ returnType ,
25
+ > (
26
+ client : client ,
27
+ actionFn : ( _ : any , parameters : parameters ) => returnType ,
28
+ // Some minifiers drop `Function.prototype.name`, or replace it with short letters,
29
+ // meaning that `actionFn.name` will not always work. For that case, the consumer
30
+ // needs to pass the name explicitly.
31
+ name : keyof PublicActions | keyof WalletActions | ( string & { } ) ,
32
+ ) : ( parameters : parameters ) => returnType {
33
+ const action_implicit = client [ actionFn . name ]
34
+ if ( typeof action_implicit === 'function' )
35
+ return action_implicit as ( params : parameters ) => returnType
21
36
22
- return ( params : params ) : returnType =>
23
- ( client as DecoratedClient ) [ action . name ] ?.( params ) ??
24
- ( client as DecoratedClient ) [ name ] ?.( params ) ??
25
- action ( client , params )
37
+ const action_explicit = client [ name ]
38
+ if ( typeof action_explicit === 'function' )
39
+ return action_explicit as ( params : parameters ) => returnType
40
+
41
+ return ( params ) => actionFn ( client , params )
26
42
}
0 commit comments