Skip to content

Commit

Permalink
fix: getAction for cases where minifier changes function names (#2132)
Browse files Browse the repository at this point in the history
* fix getAction for cases where minifier changes function names

* Create six-bears-carry.md

* Update six-bears-carry.md

---------

Co-authored-by: jxom <jakemoxey@gmail.com>
  • Loading branch information
acedward and jxom committed Apr 15, 2024
1 parent 0d87d4b commit 8c3eb6a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/six-bears-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": patch
---

Fixed `getAction` for cases where the bundler could change function names.
17 changes: 10 additions & 7 deletions src/utils/getAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ import type { Client } from '../clients/createClient.js'
export function getAction<params extends {}, returnType extends {}>(
client: Client,
action: (_: any, params: params) => returnType,
// Some minifiers drop `Function.prototype.name`, meaning that `action.name`
// will not work. For that case, the consumer needs to pass the name explicitly.
// Some minifiers drop `Function.prototype.name` or can change function
// names so that getting the name by reflection through `action.name` will
// not work.
name: string,
) {
return (params: params): returnType =>
(
client as Client & {
[key: string]: (params: params) => returnType
}
)[action.name || name]?.(params) ?? action(client, params)
(client as Client & { [key: string]: (params: params) => returnType })[
action.name
]?.(params) ??
(client as Client & { [key: string]: (params: params) => returnType })[
name
]?.(params) ??
action(client, params)
}

0 comments on commit 8c3eb6a

Please sign in to comment.